Skip to content

Commit dbedf4b

Browse files
committed
feature: support to open selected file with default editor in CHANGES tab (#1750)
Signed-off-by: leo <[email protected]>
1 parent 85c49b7 commit dbedf4b

13 files changed

+161
-64
lines changed

src/ViewModels/RevisionCompare.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ namespace SourceGit.ViewModels
88
{
99
public class RevisionCompare : ObservableObject, IDisposable
1010
{
11-
public string RepositoryPath
12-
{
13-
get => _repo;
14-
}
15-
1611
public bool IsLoading
1712
{
1813
get => _isLoading;
@@ -127,6 +122,11 @@ public void Swap()
127122
Refresh();
128123
}
129124

125+
public string GetAbsPath(string path)
126+
{
127+
return Native.OS.GetAbsPath(_repo, path);
128+
}
129+
130130
public void SaveAsPatch(string saveTo)
131131
{
132132
Task.Run(async () =>

src/ViewModels/StashesPage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ namespace SourceGit.ViewModels
99
{
1010
public class StashesPage : ObservableObject, IDisposable
1111
{
12-
public string RepositoryPath
13-
{
14-
get => _repo.FullPath;
15-
}
16-
1712
public List<Models.Stash> Stashes
1813
{
1914
get => _stashes;
@@ -146,6 +141,11 @@ public void ClearSearchFilter()
146141
SearchFilter = string.Empty;
147142
}
148143

144+
public string GetAbsPath(string path)
145+
{
146+
return Native.OS.GetAbsPath(_repo.FullPath, path);
147+
}
148+
149149
public void Apply(Models.Stash stash)
150150
{
151151
if (_repo.CanCreatePopup())

src/Views/BranchCompare.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@
128128
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
129129
Changes="{Binding VisibleChanges}"
130130
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
131-
ContextRequested="OnChangeContextRequested"/>
131+
ContextRequested="OnChangeContextRequested"
132+
KeyDown="OnChangeCollectionViewKeyDown"/>
132133
</Border>
133134

134135
<!-- Loading Status Icon -->

src/Views/BranchCompare.axaml.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,25 @@ private void OnPressedSHA(object sender, PointerPressedEventArgs e)
8383

8484
e.Handled = true;
8585
}
86+
87+
private async void OnChangeCollectionViewKeyDown(object sender, KeyEventArgs e)
88+
{
89+
if (DataContext is not ViewModels.BranchCompare vm)
90+
return;
91+
92+
if (sender is not ChangeCollectionView { SelectedChanges: { Count: 1 } selectedChanges })
93+
return;
94+
95+
var change = selectedChanges[0];
96+
if (e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) && e.Key == Key.C)
97+
{
98+
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
99+
await App.CopyTextAsync(vm.GetAbsPath(change.Path));
100+
else
101+
await App.CopyTextAsync(change.Path);
102+
103+
e.Handled = true;
104+
}
105+
}
86106
}
87107
}

src/Views/ChangeCollectionView.axaml.cs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ChangeCollectionContainer : ListBox
3333
{
3434
protected override Type StyleKeyOverride => typeof(ListBox);
3535

36-
protected override async void OnKeyDown(KeyEventArgs e)
36+
protected override void OnKeyDown(KeyEventArgs e)
3737
{
3838
if (SelectedItems is [ViewModels.ChangeTreeNode node])
3939
{
@@ -43,36 +43,6 @@ protected override async void OnKeyDown(KeyEventArgs e)
4343
this.FindAncestorOfType<ChangeCollectionView>()?.ToggleNodeIsExpanded(node);
4444
e.Handled = true;
4545
}
46-
else if (e.Key == Key.C &&
47-
e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
48-
{
49-
var path = node.FullPath;
50-
51-
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
52-
{
53-
do
54-
{
55-
var repoView = this.FindAncestorOfType<Repository>();
56-
if (repoView is { DataContext: ViewModels.Repository repo })
57-
{
58-
path = Native.OS.GetAbsPath(repo.FullPath, path);
59-
break;
60-
}
61-
62-
var branchCompareView = this.FindAncestorOfType<BranchCompare>();
63-
if (branchCompareView is { DataContext: ViewModels.BranchCompare branchCompare })
64-
{
65-
path = branchCompare.GetAbsPath(path);
66-
break;
67-
}
68-
69-
// NOTE: if there is another window uses ChangeCollectionView, add it here!
70-
} while (false);
71-
}
72-
73-
await App.CopyTextAsync(path);
74-
e.Handled = true;
75-
}
7646
}
7747

7848
if (!e.Handled && e.Key != Key.Space && e.Key != Key.Enter)

src/Views/CommitChanges.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
5151
Changes="{Binding VisibleChanges}"
5252
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
53-
ContextRequested="OnChangeContextRequested"/>
53+
ContextRequested="OnChangeContextRequested"
54+
KeyDown="OnChangeCollectionViewKeyDown"/>
5455
</Border>
5556

5657
<!-- Summary -->

src/Views/CommitChanges.axaml.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System;
2+
13
using Avalonia.Controls;
4+
using Avalonia.Input;
25
using Avalonia.VisualTree;
36

47
namespace SourceGit.Views
@@ -36,5 +39,26 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
3639
menu.Open(view);
3740
}
3841
}
42+
43+
private async void OnChangeCollectionViewKeyDown(object sender, KeyEventArgs e)
44+
{
45+
if (DataContext is not ViewModels.CommitDetail vm)
46+
return;
47+
48+
if (sender is not ChangeCollectionView { SelectedChanges: { Count: 1 } selectedChanges })
49+
return;
50+
51+
var change = selectedChanges[0];
52+
if (e.Key == Key.C &&
53+
e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
54+
{
55+
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
56+
await App.CopyTextAsync(vm.GetAbsPath(change.Path));
57+
else
58+
await App.CopyTextAsync(change.Path);
59+
60+
e.Handled = true;
61+
}
62+
}
3963
}
4064
}

src/Views/CommitDetail.axaml.cs

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
111111
ev.Handled = true;
112112
};
113113

114+
var openWith = new MenuItem();
115+
openWith.Header = App.Text("OpenWith");
116+
openWith.Icon = App.CreateMenuIcon("Icons.OpenWith");
117+
openWith.IsEnabled = change.Index != Models.ChangeState.Deleted;
118+
openWith.Click += async (_, ev) =>
119+
{
120+
await vm.OpenRevisionFileWithDefaultEditorAsync(change.Path);
121+
ev.Handled = true;
122+
};
123+
114124
var fullPath = Native.OS.GetAbsPath(repo.FullPath, change.Path);
115125
var explore = new MenuItem();
116126
explore.Header = App.Text("RevealFile");
@@ -167,6 +177,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
167177

168178
var menu = new ContextMenu();
169179
menu.Items.Add(openWithMerger);
180+
menu.Items.Add(openWith);
170181
menu.Items.Add(explore);
171182
menu.Items.Add(new MenuItem { Header = "-" });
172183
menu.Items.Add(history);
@@ -293,24 +304,30 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
293304

294305
private async void OnCommitListKeyDown(object sender, KeyEventArgs e)
295306
{
296-
if (DataContext is ViewModels.CommitDetail detail &&
297-
sender is ListBox { SelectedItem: Models.Change change } &&
307+
if (DataContext is not ViewModels.CommitDetail vm)
308+
return;
309+
310+
if (sender is not ListBox { SelectedItem: Models.Change change })
311+
return;
312+
313+
if (e.Key == Key.C &&
298314
e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
299315
{
300-
if (e.Key == Key.C)
301-
{
302-
var path = change.Path;
303-
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
304-
path = detail.GetAbsPath(path);
316+
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
317+
await App.CopyTextAsync(vm.GetAbsPath(change.Path));
318+
else
319+
await App.CopyTextAsync(change.Path);
305320

306-
await App.CopyTextAsync(path);
307-
e.Handled = true;
308-
}
309-
else if (e.Key == Key.D && e.KeyModifiers.HasFlag(KeyModifiers.Shift))
310-
{
311-
detail.OpenChangeInMergeTool(change);
312-
e.Handled = true;
313-
}
321+
e.Handled = true;
322+
return;
323+
}
324+
325+
if (e.Key == Key.D &&
326+
e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) &&
327+
e.KeyModifiers.HasFlag(KeyModifiers.Shift))
328+
{
329+
vm.OpenChangeInMergeTool(change);
330+
e.Handled = true;
314331
}
315332
}
316333

src/Views/RevisionCompare.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@
100100
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
101101
Changes="{Binding VisibleChanges}"
102102
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
103-
ContextRequested="OnChangeContextRequested"/>
103+
ContextRequested="OnChangeContextRequested"
104+
KeyDown="OnChangeCollectionViewKeyDown"/>
104105
</Border>
105106

106107
<!-- Loading Status Icon -->

src/Views/RevisionCompare.axaml.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
2020
if (DataContext is ViewModels.RevisionCompare { SelectedChanges: { Count: 1 } selected } vm &&
2121
sender is ChangeCollectionView view)
2222
{
23-
var repo = vm.RepositoryPath;
2423
var change = selected[0];
25-
var changeFullPath = Native.OS.GetAbsPath(repo, change.Path);
24+
var changeFullPath = vm.GetAbsPath(change.Path);
2625
var menu = new ContextMenu();
2726

2827
var openWithMerger = new MenuItem();
@@ -105,5 +104,25 @@ private async void OnSaveAsPatch(object sender, RoutedEventArgs e)
105104

106105
e.Handled = true;
107106
}
107+
108+
private async void OnChangeCollectionViewKeyDown(object sender, KeyEventArgs e)
109+
{
110+
if (DataContext is not ViewModels.RevisionCompare vm)
111+
return;
112+
113+
if (sender is not ChangeCollectionView { SelectedChanges: { Count: 1 } selectedChanges })
114+
return;
115+
116+
var change = selectedChanges[0];
117+
if (e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control) && e.Key == Key.C)
118+
{
119+
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
120+
await App.CopyTextAsync(vm.GetAbsPath(change.Path));
121+
else
122+
await App.CopyTextAsync(change.Path);
123+
124+
e.Handled = true;
125+
}
126+
}
108127
}
109128
}

0 commit comments

Comments
 (0)