feature: add hotkeys to open file with default editor

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-08 20:05:37 +08:00
parent 7fa1912721
commit 7f838f1f88
4 changed files with 45 additions and 12 deletions

View File

@@ -528,6 +528,7 @@ namespace SourceGit.ViewModels
var openWith = new MenuItem();
openWith.Header = App.Text("OpenWith");
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
openWith.IsEnabled = file.Type == Models.ObjectType.Blob;
openWith.Click += async (_, ev) =>
{

View File

@@ -328,6 +328,13 @@ namespace SourceGit.ViewModels
});
}
public void OpenWithDefaultEditor(Models.Change c)
{
var absPath = Native.OS.GetAbsPath(_repo.FullPath, c.Path);
if (File.Exists(absPath))
Native.OS.OpenWithDefaultEditor(absPath);
}
public void StashAll(bool autoStart)
{
if (!_repo.CanCreatePopup())
@@ -609,10 +616,11 @@ namespace SourceGit.ViewModels
var openWith = new MenuItem();
openWith.Header = App.Text("OpenWith");
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
openWith.IsEnabled = File.Exists(path);
openWith.Click += (_, e) =>
{
Native.OS.OpenWithDefaultEditor(path);
OpenWithDefaultEditor(change);
e.Handled = true;
};
menu.Items.Add(openWith);
@@ -1293,10 +1301,11 @@ namespace SourceGit.ViewModels
var openWith = new MenuItem();
openWith.Header = App.Text("OpenWith");
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
openWith.IsEnabled = File.Exists(path);
openWith.Click += (_, e) =>
{
Native.OS.OpenWithDefaultEditor(path);
OpenWithDefaultEditor(change);
e.Handled = true;
};

View File

@@ -149,7 +149,14 @@
Background="Transparent"
Click="OnOpenFileWithDefaultEditor"
IsVisible="{Binding CanOpenRevisionFileWithDefaultEditor, Mode=OneWay}"
ToolTip.Tip="{DynamicResource Text.OpenWith}">
HotKey="{OnPlatform Ctrl+O, macOS=⌘+O}">
<ToolTip.Tip>
<TextBlock>
<Run Text="{DynamicResource Text.OpenWith}"/>
<Run Text=" "/>
<Run Text="{OnPlatform Ctrl+O, macOS=⌘+O}" Foreground="{DynamicResource Brush.FG2}"/>
</TextBlock>
</ToolTip.Tip>
<Path Width="12" Height="12" Data="{StaticResource Icons.OpenWith}"/>
</Button>
</Grid>

View File

@@ -1,3 +1,4 @@
using System;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
@@ -96,12 +97,17 @@ namespace SourceGit.Views
vm.StageSelected(next);
UnstagedChangesView.TakeFocus();
e.Handled = true;
return;
}
if (e.Key is Key.Delete or Key.Back && vm.SelectedUnstaged is { Count: > 0 } selected)
else if (e.Key is Key.Delete or Key.Back && vm.SelectedUnstaged is { Count: > 0 })
{
vm.Discard(selected);
vm.Discard(vm.SelectedUnstaged);
e.Handled = true;
}
else if (e.Key is Key.O &&
e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) &&
vm.SelectedUnstaged is { Count: 1 })
{
vm.OpenWithDefaultEditor(vm.SelectedUnstaged[0]);
e.Handled = true;
}
}
@@ -109,12 +115,22 @@ namespace SourceGit.Views
private void OnStagedKeyDown(object _, KeyEventArgs e)
{
if (DataContext is ViewModels.WorkingCopy vm && e.Key is Key.Space or Key.Enter)
if (DataContext is ViewModels.WorkingCopy vm)
{
var next = StagedChangesView.GetNextChangeWithoutSelection();
vm.UnstageSelected(next);
StagedChangesView.TakeFocus();
e.Handled = true;
if (e.Key is Key.Space or Key.Enter)
{
var next = StagedChangesView.GetNextChangeWithoutSelection();
vm.UnstageSelected(next);
StagedChangesView.TakeFocus();
e.Handled = true;
}
else if (e.Key is Key.O &&
e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) &&
vm.SelectedStaged is { Count: 1 })
{
vm.OpenWithDefaultEditor(vm.SelectedStaged[0]);
e.Handled = true;
}
}
}