Simplify and move wallet coins behavior
MODIFY
WalletWasabi.Fluent/Behaviors/ShowWalletCoinsOnKeyCombinationBehavior.cs
WalletWasabi.Fluent/Behaviors/ShowWalletCoinsOnKeyCombinationBehavior.cs
-using System.Diagnostics.CodeAnalysis;
using System.Reactive.Disposables;
-using System.Reactive.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
-using WalletWasabi.Fluent.ViewModels;
+using Avalonia.Interactivity;
+using Avalonia.VisualTree;
using WalletWasabi.Fluent.ViewModels.Wallets;
namespace WalletWasabi.Fluent.Behaviors;
-public class ShowWalletCoinsOnKeyCombinationBehavior : DisposingBehavior<Control>
+public class ShowWalletCoinsOnKeyCombinationBehavior : AttachedToVisualTreeBehavior<Control>
{
private bool _key1Active;
private bool _key2Active;
@@ -24,6 +23,9 @@ public class ShowWalletCoinsOnKeyCombinationBehavior : DisposingBehavior<Control
public static readonly StyledProperty<Key?> Key3Property =
AvaloniaProperty.Register<ShowWalletCoinsOnKeyCombinationBehavior, Key?>(nameof(Key3));
+ public static readonly StyledProperty<WalletViewModel?> WalletProperty =
+ AvaloniaProperty.Register<ShowWalletCoinsOnKeyCombinationBehavior, WalletViewModel?>(nameof(Wallet));
+
public Key? Key1
{
get => GetValue(Key1Property);
@@ -42,32 +44,36 @@ public class ShowWalletCoinsOnKeyCombinationBehavior : DisposingBehavior<Control
set => SetValue(Key3Property, value);
}
- protected override void OnAttached(CompositeDisposable disposables)
+ public WalletViewModel? Wallet
+ {
+ get => GetValue(WalletProperty);
+ set => SetValue(WalletProperty, value);
+ }
+
+ protected override void OnAttachedToVisualTree(CompositeDisposable disposable)
{
- if (AssociatedObject is null)
+ if (AssociatedObject?.GetVisualRoot() is not IInputElement inputRoot)
{
return;
}
- Observable
- .FromEventPattern<KeyEventArgs>(AssociatedObject, nameof(AssociatedObject.KeyDown))
- .Select(x => x.EventArgs)
- .Subscribe(e =>
+ inputRoot.AddDisposableHandler(InputElement.KeyDownEvent, OnKeyDown).DisposeWith(disposable);
+ inputRoot.AddDisposableHandler(InputElement.KeyUpEvent, OnKeyUp).DisposeWith(disposable);
+ }
+
+ private void OnKeyUp(object? sender, KeyEventArgs e)
+ {
+ EvaluateKeyPress(e.Key, isPressed: false);
+ }
+
+ private void OnKeyDown(object? sender, KeyEventArgs e)
{
EvaluateKeyPress(e.Key, isPressed: true);
- if (_key1Active && _key2Active && _key3Active && TryGetValidSelectedWallet(out var wallet))
+ if (_key1Active && _key2Active && _key3Active && Wallet is { IsActive: true })
{
- wallet.WalletCoinsCommand.Execute(default);
+ Wallet.WalletCoinsCommand.Execute(default);
}
- })
- .DisposeWith(disposables);
-
- Observable
- .FromEventPattern<KeyEventArgs>(AssociatedObject, nameof(AssociatedObject.KeyUp))
- .Select(x => x.EventArgs)
- .Subscribe(e => EvaluateKeyPress(e.Key, isPressed: false))
- .DisposeWith(disposables);
}
private void EvaluateKeyPress(Key key, bool isPressed)
@@ -87,17 +93,4 @@ public class ShowWalletCoinsOnKeyCombinationBehavior : DisposingBehavior<Control
_key3Active = isPressed;
}
}
-
- private bool TryGetValidSelectedWallet([NotNullWhen(true)] out WalletViewModel? wallet)
- {
- wallet = null;
-
- if (UiServices.WalletManager.SelectedWallet is WalletViewModel { IsLoggedIn: true, IsActive: true } walletViewModel &&
- walletViewModel.WalletCoinsCommand.CanExecute(default))
- {
- wallet = walletViewModel;
- }
-
- return wallet is { };
- }
}

MODIFY
WalletWasabi.Fluent/Views/MainWindow.axaml
WalletWasabi.Fluent/Views/MainWindow.axaml
</Window.Styles>
<i:Interaction.Behaviors>
<behaviors:RegisterNotificationHostBehavior />
- <behaviors:ShowWalletCoinsOnKeyCombinationBehavior Key1="LeftCtrl" Key2="D" Key3="C" />
</i:Interaction.Behaviors>
<Panel Margin="{Binding #MainWindow.OffScreenMargin}">

MODIFY
WalletWasabi.Fluent/Views/Wallets/WalletView.axaml
WalletWasabi.Fluent/Views/Wallets/WalletView.axaml
x:CompileBindings="True"
ClipToBounds="False"
x:Class="WalletWasabi.Fluent.Views.Wallets.WalletView">
+ <i:Interaction.Behaviors>
+ <behaviors:ShowWalletCoinsOnKeyCombinationBehavior Wallet="{Binding}" Key1="LeftCtrl" Key2="D" Key3="C" />
+ </i:Interaction.Behaviors>
<c:ContentArea ScrollViewer.VerticalScrollBarVisibility="Disabled" ClipToBounds="False">
<c:ContentArea.Title>
<StackPanel>

Simplify and move wallet coins behavior