feature: add hotkeys to stashes page

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-09 12:57:11 +08:00
parent 23e578f355
commit 7c59183391
3 changed files with 40 additions and 29 deletions

View File

@@ -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));
}

View File

@@ -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}">
<Grid RowDefinitions="Auto,*" >
<Grid Grid.Row="0" ColumnDefinitions="*,Auto">

View File

@@ -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;
}
}