From 0646db36a4c15762fd838e2095527e7ed7adebfd Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 12 Aug 2024 16:14:39 +0800 Subject: [PATCH] fix: `KeyDown` event won't be triggered unless toolbar got focus (#351) --- src/Views/Launcher.axaml.cs | 18 ++++++++++++++++++ src/Views/RepositoryToolbar.axaml.cs | 27 +++++++++------------------ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Views/Launcher.axaml.cs b/src/Views/Launcher.axaml.cs index dc9f91d2..95fbee7e 100644 --- a/src/Views/Launcher.axaml.cs +++ b/src/Views/Launcher.axaml.cs @@ -20,6 +20,11 @@ namespace SourceGit.Views InitializeComponent(); } + public bool HasKeyModifier(KeyModifiers modifier) + { + return _unhandledModifiers.HasFlag(modifier); + } + protected override void OnOpened(EventArgs e) { base.OnOpened(e); @@ -147,6 +152,17 @@ namespace SourceGit.Views } base.OnKeyDown(e); + + if (!e.Handled) + { + _unhandledModifiers = e.KeyModifiers; + } + } + + protected override void OnKeyUp(KeyEventArgs e) + { + base.OnKeyUp(e); + _unhandledModifiers = KeyModifiers.None; } protected override void OnClosing(WindowClosingEventArgs e) @@ -178,5 +194,7 @@ namespace SourceGit.Views e.Handled = true; } + + private KeyModifiers _unhandledModifiers = KeyModifiers.None; } } diff --git a/src/Views/RepositoryToolbar.axaml.cs b/src/Views/RepositoryToolbar.axaml.cs index 6fad104d..27ac43cd 100644 --- a/src/Views/RepositoryToolbar.axaml.cs +++ b/src/Views/RepositoryToolbar.axaml.cs @@ -1,6 +1,7 @@ using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; +using Avalonia.VisualTree; namespace SourceGit.Views { @@ -11,18 +12,6 @@ namespace SourceGit.Views InitializeComponent(); } - protected override void OnKeyDown(KeyEventArgs e) - { - base.OnKeyDown(e); - _hasCtrl = e.KeyModifiers.HasFlag(KeyModifiers.Control); - } - - protected override void OnKeyUp(KeyEventArgs e) - { - base.OnKeyUp(e); - _hasCtrl = false; - } - private void OpenWithExternalTools(object sender, RoutedEventArgs e) { if (sender is Button button && DataContext is ViewModels.Repository repo) @@ -55,25 +44,29 @@ namespace SourceGit.Views private void Fetch(object _, RoutedEventArgs e) { - (DataContext as ViewModels.Repository)?.Fetch(_hasCtrl); + var launcher = this.FindAncestorOfType(); + (DataContext as ViewModels.Repository)?.Fetch(launcher?.HasKeyModifier(KeyModifiers.Control) ?? false); e.Handled = true; } private void Pull(object _, RoutedEventArgs e) { - (DataContext as ViewModels.Repository)?.Pull(_hasCtrl); + var launcher = this.FindAncestorOfType(); + (DataContext as ViewModels.Repository)?.Pull(launcher?.HasKeyModifier(KeyModifiers.Control) ?? false); e.Handled = true; } private void Push(object _, RoutedEventArgs e) { - (DataContext as ViewModels.Repository)?.Push(_hasCtrl); + var launcher = this.FindAncestorOfType(); + (DataContext as ViewModels.Repository)?.Push(launcher?.HasKeyModifier(KeyModifiers.Control) ?? false); e.Handled = true; } private void StashAll(object _, RoutedEventArgs e) { - (DataContext as ViewModels.Repository)?.StashAll(_hasCtrl); + var launcher = this.FindAncestorOfType(); + (DataContext as ViewModels.Repository)?.StashAll(launcher?.HasKeyModifier(KeyModifiers.Control) ?? false); e.Handled = true; } @@ -98,8 +91,6 @@ namespace SourceGit.Views e.Handled = true; } - - private bool _hasCtrl = false; } }