simplify `BindableFlyoutOpenBehavior`
MODIFY
WalletWasabi.Fluent/Behaviors/BindableFlyoutOpenBehavior.cs
WalletWasabi.Fluent/Behaviors/BindableFlyoutOpenBehavior.cs
@@ -3,59 +3,43 @@ using System.Reactive.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
-using Avalonia.Threading;
using ReactiveUI;
namespace WalletWasabi.Fluent.Behaviors;
-public class BindableFlyoutOpenBehavior : ShowFlyoutOnPointerOverBehavior
+public class BindableFlyoutOpenBehavior : DisposingBehavior<Control>
{
public static readonly StyledProperty<bool> IsOpenProperty =
AvaloniaProperty.Register<BindableFlyoutOpenBehavior, bool>(nameof(IsOpen));
- private IDisposable? _pointerEnterDisposable;
-
public bool IsOpen
{
get => GetValue(IsOpenProperty);
set => SetValue(IsOpenProperty, value);
}
- private IDisposable SubscribePointerOver()
+ protected override void OnAttached(CompositeDisposable disposable)
{
- _pointerEnterDisposable?.Dispose();
-
- return _pointerEnterDisposable = Observable
- .FromEventPattern(AssociatedObject, nameof(AssociatedObject.PointerEnter))
- .Subscribe(_ =>
+ if (AssociatedObject is null)
{
- _pointerEnterDisposable?.Dispose();
- _pointerEnterDisposable = null;
- FlyoutBase.ShowAttachedFlyout(AssociatedObject);
- });
+ return;
}
- protected override void OnAttached(CompositeDisposable disposable)
- {
- SubscribePointerOver()
+ Observable
+ .FromEventPattern(AssociatedObject, nameof(AssociatedObject.PointerEnter))
+ .Subscribe(_ => IsOpen = true)
.DisposeWith(disposable);
this.WhenAnyValue(x => x.IsOpen)
- .Subscribe(x =>
+ .Subscribe(isOpen =>
{
- if (x)
+ if (isOpen)
{
- FlyoutBase.ShowAttachedFlyout(AssociatedObject!);
+ FlyoutBase.ShowAttachedFlyout(AssociatedObject);
}
- else if (Flyout.GetAttachedFlyout(AssociatedObject) is FlyoutBase flyout)
+ else
{
- Flyout.GetAttachedFlyout(AssociatedObject!)?.Hide();
-
- Dispatcher.UIThread.Post(() =>
- {
- SubscribePointerOver()
- .DisposeWith(disposable);
- });
+ FlyoutBase.GetAttachedFlyout(AssociatedObject)?.Hide();
}
});
}

simplify `BindableFlyoutOpenBehavior`