enhance: block other hotkeys when there is a command palette (#2173)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2026-03-09 13:01:08 +08:00
parent 2f06b9eb7d
commit e78815efb1
5 changed files with 55 additions and 7 deletions

View File

@@ -127,7 +127,9 @@
<Border Grid.Row="0" Grid.RowSpan="2"
Background="Transparent"
IsVisible="{Binding CommandPalette, Converter={x:Static ObjectConverters.IsNotNull}}"
PointerPressed="OnCloseCommandPalette">
PointerPressed="OnCloseCommandPalette"
KeyDown="OnCommandPaletteKeyDown"
KeyUp="OnCommandPaletteKeyUp">
<Border Width="420" HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
<Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
<ContentControl Margin="16,10,16,12" Content="{Binding CommandPalette}">

View File

@@ -131,6 +131,11 @@ namespace SourceGit.Views
ViewModels.Preferences.Instance.Layout.LauncherWindowState = state;
}
else if (change.Property == IsActiveProperty)
{
if (!IsActive && DataContext is ViewModels.Launcher { CommandPalette: { } } vm)
vm.CommandPalette = null;
}
}
protected override void OnSizeChanged(SizeChangedEventArgs e)
@@ -203,6 +208,13 @@ namespace SourceGit.Views
return;
}
if (e.Key == Key.T)
{
vm.AddNewTab();
e.Handled = true;
return;
}
if ((OperatingSystem.IsMacOS() && e.KeyModifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Right) ||
(!OperatingSystem.IsMacOS() && !e.KeyModifiers.HasFlag(KeyModifiers.Shift) && e.Key == Key.Tab))
{
@@ -265,11 +277,7 @@ namespace SourceGit.Views
}
else if (e.Key == Key.Escape)
{
if (vm.CommandPalette != null)
vm.CommandPalette = null;
else
vm.ActivePage.CancelPopup();
vm.ActivePage.CancelPopup();
e.Handled = true;
return;
}
@@ -317,6 +325,9 @@ namespace SourceGit.Views
{
if (sender is Button btn && DataContext is ViewModels.Launcher launcher)
{
if (launcher.CommandPalette != null)
launcher.CommandPalette = null;
var pref = ViewModels.Preferences.Instance;
var menu = new ContextMenu();
menu.Placement = PlacementMode.BottomEdgeAlignedLeft;
@@ -384,6 +395,23 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnCommandPaletteKeyDown(object sender, KeyEventArgs e)
{
if (DataContext is ViewModels.Launcher { CommandPalette: { } } vm)
{
if (e.Key == Key.Escape)
vm.CommandPalette = null;
e.Route = RoutingStrategies.Direct;
e.Handled = true;
}
}
private void OnCommandPaletteKeyUp(object sender, KeyEventArgs e)
{
e.Handled = true;
}
private WindowState _lastWindowState = WindowState.Normal;
}
}

View File

@@ -153,7 +153,7 @@
<Path Width="8" Height="14" Stretch="Fill" Data="{StaticResource Icons.TriangleRight}"/>
</RepeatButton>
<Button Classes="icon_button" Width="16" Height="28" Margin="4,2,0,0" Command="{Binding AddNewTab}" HotKey="{OnPlatform Ctrl+T, macOS=⌘+T}">
<Button Classes="icon_button" Width="16" Height="28" Margin="4,2,0,0" Command="{Binding AddNewTab}">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="{DynamicResource Text.PageTabBar.New}" VerticalAlignment="Center"/>

View File

@@ -163,6 +163,9 @@ namespace SourceGit.Views
private async void FetchDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.Repository repo)
{
await repo.FetchAsync(true);
@@ -181,6 +184,9 @@ namespace SourceGit.Views
private async void PullDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.Repository repo)
{
await repo.PullAsync(true);
@@ -199,6 +205,9 @@ namespace SourceGit.Views
private async void PushDirectlyByHotKey(object sender, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.Repository repo)
{
await repo.PushAsync(true);

View File

@@ -231,6 +231,9 @@ namespace SourceGit.Views
private async void OnCommit(object _, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.WorkingCopy vm)
await vm.CommitAsync(false, false);
@@ -239,6 +242,9 @@ namespace SourceGit.Views
private async void OnCommitWithAutoStage(object _, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.WorkingCopy vm)
await vm.CommitAsync(true, false);
@@ -247,6 +253,9 @@ namespace SourceGit.Views
private async void OnCommitWithPush(object _, RoutedEventArgs e)
{
if (App.GetLauncher() is { CommandPalette: { } } launcher)
return;
if (DataContext is ViewModels.WorkingCopy vm)
await vm.CommitAsync(false, true);