Skip to content

Commit 7f838f1

Browse files
committed
feature: add hotkeys to open file with default editor
Signed-off-by: leo <[email protected]>
1 parent 7fa1912 commit 7f838f1

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

src/ViewModels/CommitDetail.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ public ContextMenu CreateRevisionFileContextMenu(Models.Object file)
528528
var openWith = new MenuItem();
529529
openWith.Header = App.Text("OpenWith");
530530
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
531+
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
531532
openWith.IsEnabled = file.Type == Models.ObjectType.Blob;
532533
openWith.Click += async (_, ev) =>
533534
{

src/ViewModels/WorkingCopy.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,13 @@ public void SetData(List<Models.Change> changes)
328328
});
329329
}
330330

331+
public void OpenWithDefaultEditor(Models.Change c)
332+
{
333+
var absPath = Native.OS.GetAbsPath(_repo.FullPath, c.Path);
334+
if (File.Exists(absPath))
335+
Native.OS.OpenWithDefaultEditor(absPath);
336+
}
337+
331338
public void StashAll(bool autoStart)
332339
{
333340
if (!_repo.CanCreatePopup())
@@ -609,10 +616,11 @@ public ContextMenu CreateContextMenuForUnstagedChanges(string selectedSingleFold
609616
var openWith = new MenuItem();
610617
openWith.Header = App.Text("OpenWith");
611618
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
619+
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
612620
openWith.IsEnabled = File.Exists(path);
613621
openWith.Click += (_, e) =>
614622
{
615-
Native.OS.OpenWithDefaultEditor(path);
623+
OpenWithDefaultEditor(change);
616624
e.Handled = true;
617625
};
618626
menu.Items.Add(openWith);
@@ -1293,10 +1301,11 @@ public ContextMenu CreateContextMenuForStagedChanges(string selectedSingleFolder
12931301
var openWith = new MenuItem();
12941302
openWith.Header = App.Text("OpenWith");
12951303
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
1304+
openWith.Tag = OperatingSystem.IsMacOS() ? "⌘+O" : "Ctrl+O";
12961305
openWith.IsEnabled = File.Exists(path);
12971306
openWith.Click += (_, e) =>
12981307
{
1299-
Native.OS.OpenWithDefaultEditor(path);
1308+
OpenWithDefaultEditor(change);
13001309
e.Handled = true;
13011310
};
13021311

src/Views/RevisionFiles.axaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,14 @@
149149
Background="Transparent"
150150
Click="OnOpenFileWithDefaultEditor"
151151
IsVisible="{Binding CanOpenRevisionFileWithDefaultEditor, Mode=OneWay}"
152-
ToolTip.Tip="{DynamicResource Text.OpenWith}">
152+
HotKey="{OnPlatform Ctrl+O, macOS=⌘+O}">
153+
<ToolTip.Tip>
154+
<TextBlock>
155+
<Run Text="{DynamicResource Text.OpenWith}"/>
156+
<Run Text=" "/>
157+
<Run Text="{OnPlatform Ctrl+O, macOS=⌘+O}" Foreground="{DynamicResource Brush.FG2}"/>
158+
</TextBlock>
159+
</ToolTip.Tip>
153160
<Path Width="12" Height="12" Data="{StaticResource Icons.OpenWith}"/>
154161
</Button>
155162
</Grid>

src/Views/WorkingCopy.axaml.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Avalonia.Controls;
23
using Avalonia.Input;
34
using Avalonia.Interactivity;
@@ -96,25 +97,40 @@ private void OnUnstagedKeyDown(object _, KeyEventArgs e)
9697
vm.StageSelected(next);
9798
UnstagedChangesView.TakeFocus();
9899
e.Handled = true;
99-
return;
100100
}
101-
102-
if (e.Key is Key.Delete or Key.Back && vm.SelectedUnstaged is { Count: > 0 } selected)
101+
else if (e.Key is Key.Delete or Key.Back && vm.SelectedUnstaged is { Count: > 0 })
102+
{
103+
vm.Discard(vm.SelectedUnstaged);
104+
e.Handled = true;
105+
}
106+
else if (e.Key is Key.O &&
107+
e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) &&
108+
vm.SelectedUnstaged is { Count: 1 })
103109
{
104-
vm.Discard(selected);
110+
vm.OpenWithDefaultEditor(vm.SelectedUnstaged[0]);
105111
e.Handled = true;
106112
}
107113
}
108114
}
109115

110116
private void OnStagedKeyDown(object _, KeyEventArgs e)
111117
{
112-
if (DataContext is ViewModels.WorkingCopy vm && e.Key is Key.Space or Key.Enter)
118+
if (DataContext is ViewModels.WorkingCopy vm)
113119
{
114-
var next = StagedChangesView.GetNextChangeWithoutSelection();
115-
vm.UnstageSelected(next);
116-
StagedChangesView.TakeFocus();
117-
e.Handled = true;
120+
if (e.Key is Key.Space or Key.Enter)
121+
{
122+
var next = StagedChangesView.GetNextChangeWithoutSelection();
123+
vm.UnstageSelected(next);
124+
StagedChangesView.TakeFocus();
125+
e.Handled = true;
126+
}
127+
else if (e.Key is Key.O &&
128+
e.KeyModifiers == (OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) &&
129+
vm.SelectedStaged is { Count: 1 })
130+
{
131+
vm.OpenWithDefaultEditor(vm.SelectedStaged[0]);
132+
e.Handled = true;
133+
}
118134
}
119135
}
120136

0 commit comments

Comments
 (0)