From 7c591833919aa84046a210ea364346ead2ab8967 Mon Sep 17 00:00:00 2001 From: leo Date: Wed, 9 Jul 2025 12:57:11 +0800 Subject: [PATCH] feature: add hotkeys to stashes page Signed-off-by: leo --- src/ViewModels/StashesPage.cs | 16 ++++++----- src/Views/StashesPage.axaml | 2 +- src/Views/StashesPage.axaml.cs | 51 ++++++++++++++++++++-------------- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/ViewModels/StashesPage.cs b/src/ViewModels/StashesPage.cs index b9732f15..414fb055 100644 --- a/src/ViewModels/StashesPage.cs +++ b/src/ViewModels/StashesPage.cs @@ -78,8 +78,11 @@ namespace SourceGit.ViewModels Dispatcher.UIThread.Post(() => { - _untracked = untracked; - Changes = changes; + if (value.SHA.Equals(_selectedStash?.SHA ?? string.Empty, StringComparison.Ordinal)) + { + _untracked = untracked; + Changes = changes; + } }); }); } @@ -139,9 +142,6 @@ namespace SourceGit.ViewModels public ContextMenu MakeContextMenu(Models.Stash stash) { - if (stash == null) - return null; - var apply = new MenuItem(); apply.Header = App.Text("StashCM.Apply"); apply.Icon = App.CreateMenuIcon("Icons.CheckCircled"); @@ -154,6 +154,7 @@ namespace SourceGit.ViewModels var drop = new MenuItem(); drop.Header = App.Text("StashCM.Drop"); drop.Icon = App.CreateMenuIcon("Icons.Clear"); + drop.Tag = "Back/Delete"; drop.Click += (_, ev) => { Drop(stash); @@ -197,6 +198,7 @@ namespace SourceGit.ViewModels var copy = new MenuItem(); copy.Header = App.Text("StashCM.CopyMessage"); copy.Icon = App.CreateMenuIcon("Icons.Copy"); + copy.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C"; copy.Click += async (_, ev) => { await App.CopyTextAsync(stash.Message); @@ -308,13 +310,13 @@ namespace SourceGit.ViewModels public void Apply(Models.Stash stash) { - if (stash != null && _repo.CanCreatePopup()) + if (_repo.CanCreatePopup()) _repo.ShowPopup(new ApplyStash(_repo, stash)); } public void Drop(Models.Stash stash) { - if (stash != null && _repo.CanCreatePopup()) + if (_repo.CanCreatePopup()) _repo.ShowPopup(new DropStash(_repo, stash)); } diff --git a/src/Views/StashesPage.axaml b/src/Views/StashesPage.axaml index 3bd95a12..36d3d088 100644 --- a/src/Views/StashesPage.axaml +++ b/src/Views/StashesPage.axaml @@ -65,7 +65,6 @@ ItemsSource="{Binding VisibleStashes}" SelectedItem="{Binding SelectedStash, Mode=TwoWay}" SelectionMode="Single" - DoubleTapped="OnStashListDoubleTapped" KeyDown="OnStashListKeyDown" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"> @@ -89,6 +88,7 @@ BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="0,0,0,1" Padding="4" ContextRequested="OnStashContextRequested" + DoubleTapped="OnStashDoubleTapped" ToolTip.Tip="{Binding Message}"> diff --git a/src/Views/StashesPage.axaml.cs b/src/Views/StashesPage.axaml.cs index 4bddd3a6..1ea76480 100644 --- a/src/Views/StashesPage.axaml.cs +++ b/src/Views/StashesPage.axaml.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.Controls; using Avalonia.Input; @@ -23,34 +24,41 @@ namespace SourceGit.Views layout.StashesLeftWidth = new GridLength(maxLeft, GridUnitType.Pixel); } - private void OnStashListDoubleTapped(object _, TappedEventArgs e) + private async void OnStashListKeyDown(object sender, KeyEventArgs e) { - if (DataContext is not ViewModels.StashesPage vm) - return; - - vm.Apply(vm.SelectedStash); - e.Handled = true; - } - - private void OnStashListKeyDown(object sender, KeyEventArgs e) - { - if (e.Key is not (Key.Delete or Key.Back)) - return; - - if (DataContext is not ViewModels.StashesPage vm) - return; - - vm.Drop(vm.SelectedStash); - e.Handled = true; + if (DataContext is ViewModels.StashesPage { SelectedStash: { } stash } vm) + { + if (e.Key is Key.Delete or Key.Back) + { + vm.Drop(stash); + e.Handled = true; + } + else if (e.Key is Key.C && e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control)) + { + await App.CopyTextAsync(stash.Message); + e.Handled = true; + } + } } private void OnStashContextRequested(object sender, ContextRequestedEventArgs e) { - if (DataContext is ViewModels.StashesPage vm && sender is Border border) + if (DataContext is ViewModels.StashesPage vm && + sender is Border { DataContext: Models.Stash stash } border) { - var menu = vm.MakeContextMenu(border.DataContext as Models.Stash); - menu?.Open(border); + var menu = vm.MakeContextMenu(stash); + menu.Open(border); } + + e.Handled = true; + } + + private void OnStashDoubleTapped(object sender, TappedEventArgs e) + { + if (DataContext is ViewModels.StashesPage vm && + sender is Border { DataContext: Models.Stash stash }) + vm.Apply(stash); + e.Handled = true; } @@ -61,6 +69,7 @@ namespace SourceGit.Views var menu = vm.MakeContextMenuForChange(); menu?.Open(view); } + e.Handled = true; } }