Refactor FlyoutController
MODIFY
WalletWasabi.Fluent/Behaviors/ShowAttachedFlyoutWhenFocusedBehavior.cs
WalletWasabi.Fluent/Behaviors/ShowAttachedFlyoutWhenFocusedBehavior.cs
@@ -52,7 +52,7 @@ public class ShowAttachedFlyoutWhenFocusedBehavior : AttachedToVisualTreeBehavio
.Do(
isAncestor =>
{
- _flyoutController.IsOpen = isAncestor;
+ _flyoutController.SetIsOpen(isAncestor);
IsFlyoutOpen = isAncestor;
})
.Subscribe()
@@ -62,7 +62,7 @@ public class ShowAttachedFlyoutWhenFocusedBehavior : AttachedToVisualTreeBehavio
.Do(
_ =>
{
- _flyoutController.IsOpen = true;
+ _flyoutController.SetIsOpen(true);
IsFlyoutOpen = true;
})
.Subscribe()
@@ -74,7 +74,7 @@ public class ShowAttachedFlyoutWhenFocusedBehavior : AttachedToVisualTreeBehavio
{
if (AssociatedObject.IsFocused)
{
- _flyoutController.IsOpen = true;
+ _flyoutController.SetIsOpen(true);
}
})
.Subscribe()
@@ -85,17 +85,20 @@ public class ShowAttachedFlyoutWhenFocusedBehavior : AttachedToVisualTreeBehavio
.Do(
_ =>
{
- _flyoutController.IsOpen = false;
+ _flyoutController.SetIsOpen(false);
IsFlyoutOpen = false;
})
.Subscribe()
.DisposeWith(disposable);
- this.GetObservable(IsFlyoutOpenProperty).Subscribe(b => _flyoutController.IsOpen = b);
+ this.GetObservable(IsFlyoutOpenProperty)
+ .Do(b => _flyoutController.SetIsOpen(b))
+ .Subscribe()
+ .DisposeWith(disposable);
if (IsFlyoutOpen)
{
- _flyoutController.IsOpen = false;
+ _flyoutController.SetIsOpen(false);
}
}

MODIFY
WalletWasabi.Fluent/Helpers/FlyoutController.cs
WalletWasabi.Fluent/Helpers/FlyoutController.cs
+using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
@@ -5,59 +6,63 @@ namespace WalletWasabi.Fluent.Helpers;
public class FlyoutController : IDisposable
{
- private readonly FlyoutCloseEnforcer _enforcer;
private readonly FlyoutBase _flyout;
private readonly Control _parent;
+ private bool _isForcedOpen;
private bool _isOpen;
public FlyoutController(FlyoutBase flyout, Control parent)
{
_flyout = flyout;
_parent = parent;
- _enforcer = new FlyoutCloseEnforcer(flyout);
}
- public bool IsOpen
+ public void Dispose()
{
- get => _isOpen;
- set
+ _flyout.Closing -= RejectClose;
+ }
+
+ public void SetIsOpen(bool value)
{
if (_isOpen == value)
{
return;
}
- ToggleFlyout(value);
+ Toggle(value);
_isOpen = value;
}
- }
- public void Dispose()
+ private static void RejectClose(object? sender, CancelEventArgs e)
{
- _enforcer.Dispose();
+ e.Cancel = true;
}
- private void ToggleFlyout(bool isVisible)
+ private void Toggle(bool value)
{
- if (isVisible)
+ if (_isForcedOpen == value)
{
- ShowFlyout();
+ return;
}
- else
+
+ _isForcedOpen = value;
+
+ if (_isForcedOpen)
{
- HideFlyout();
+ _flyout.Closing += RejectClose;
}
- }
-
- private void HideFlyout()
+ else
{
- _enforcer.IsForcedOpen = false;
- _flyout.Hide();
+ _flyout.Closing -= RejectClose;
}
- private void ShowFlyout()
+ if (value)
{
- _enforcer.IsForcedOpen = true;
_flyout.ShowAt(_parent);
}
+ else
+ {
+ _flyout.Hide();
+ }
+ }
}

Refactor FlyoutController