Skip to content

Commit f2830e9

Browse files
committed
refactor: move context menu creation from ViewModels to Views (PART 4)
Signed-off-by: leo <longshuang@msn.cn>
1 parent f652882 commit f2830e9

File tree

2 files changed

+69
-68
lines changed

2 files changed

+69
-68
lines changed

src/ViewModels/RevisionCompare.cs

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Threading.Tasks;
5-
6-
using Avalonia.Controls;
74
using Avalonia.Threading;
8-
95
using CommunityToolkit.Mvvm.ComponentModel;
106

117
namespace SourceGit.ViewModels
128
{
139
public class RevisionCompare : ObservableObject, IDisposable
1410
{
11+
public string RepositoryPath
12+
{
13+
get => _repo;
14+
}
15+
1516
public bool IsLoading
1617
{
1718
get => _isLoading;
@@ -96,6 +97,14 @@ public void Dispose()
9697
_diffContext = null;
9798
}
9899

100+
public void OpenChangeWithExternalDiffTool(Models.Change change)
101+
{
102+
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);
103+
var toolType = Preferences.Instance.ExternalMergeToolType;
104+
var toolPath = Preferences.Instance.ExternalMergeToolPath;
105+
new Commands.DiffTool(_repo, toolType, toolPath, opt).Open();
106+
}
107+
99108
public void NavigateTo(string commitSHA)
100109
{
101110
var launcher = App.GetLauncher();
@@ -136,68 +145,6 @@ public void ClearSearchFilter()
136145
SearchFilter = string.Empty;
137146
}
138147

139-
public ContextMenu CreateChangeContextMenu()
140-
{
141-
if (_selectedChanges is not { Count: 1 })
142-
return null;
143-
144-
var change = _selectedChanges[0];
145-
var menu = new ContextMenu();
146-
147-
var openWithMerger = new MenuItem();
148-
openWithMerger.Header = App.Text("OpenInExternalMergeTool");
149-
openWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
150-
openWithMerger.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+D" : "Ctrl+Shift+D";
151-
openWithMerger.Click += (_, ev) =>
152-
{
153-
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);
154-
var toolType = Preferences.Instance.ExternalMergeToolType;
155-
var toolPath = Preferences.Instance.ExternalMergeToolPath;
156-
new Commands.DiffTool(_repo, toolType, toolPath, opt).Open();
157-
ev.Handled = true;
158-
};
159-
menu.Items.Add(openWithMerger);
160-
161-
if (change.Index != Models.ChangeState.Deleted)
162-
{
163-
var full = Path.GetFullPath(Path.Combine(_repo, change.Path));
164-
var explore = new MenuItem();
165-
explore.Header = App.Text("RevealFile");
166-
explore.Icon = App.CreateMenuIcon("Icons.Explore");
167-
explore.IsEnabled = File.Exists(full);
168-
explore.Click += (_, ev) =>
169-
{
170-
Native.OS.OpenInFileManager(full, true);
171-
ev.Handled = true;
172-
};
173-
menu.Items.Add(explore);
174-
}
175-
176-
var copyPath = new MenuItem();
177-
copyPath.Header = App.Text("CopyPath");
178-
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
179-
copyPath.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
180-
copyPath.Click += async (_, ev) =>
181-
{
182-
await App.CopyTextAsync(change.Path);
183-
ev.Handled = true;
184-
};
185-
menu.Items.Add(copyPath);
186-
187-
var copyFullPath = new MenuItem();
188-
copyFullPath.Header = App.Text("CopyFullPath");
189-
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
190-
copyFullPath.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+C" : "Ctrl+Shift+C";
191-
copyFullPath.Click += async (_, e) =>
192-
{
193-
await App.CopyTextAsync(Native.OS.GetAbsPath(_repo, change.Path));
194-
e.Handled = true;
195-
};
196-
menu.Items.Add(copyFullPath);
197-
198-
return menu;
199-
}
200-
201148
private void RefreshVisible()
202149
{
203150
if (_changes == null)

src/Views/RevisionCompare.axaml.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using System;
2+
using System.IO;
3+
14
using Avalonia.Controls;
25
using Avalonia.Input;
36
using Avalonia.Interactivity;
@@ -14,9 +17,60 @@ public RevisionCompare()
1417

1518
private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e)
1619
{
17-
if (DataContext is ViewModels.RevisionCompare vm && sender is ChangeCollectionView view)
20+
if (DataContext is ViewModels.RevisionCompare { SelectedChanges: { Count: 1 } selected } vm &&
21+
sender is ChangeCollectionView view)
1822
{
19-
var menu = vm.CreateChangeContextMenu();
23+
var repo = vm.RepositoryPath;
24+
var change = selected[0];
25+
var changeFullPath = Native.OS.GetAbsPath(repo, change.Path);
26+
var menu = new ContextMenu();
27+
28+
var openWithMerger = new MenuItem();
29+
openWithMerger.Header = App.Text("OpenInExternalMergeTool");
30+
openWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
31+
openWithMerger.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+D" : "Ctrl+Shift+D";
32+
openWithMerger.Click += (_, ev) =>
33+
{
34+
vm.OpenChangeWithExternalDiffTool(change);
35+
ev.Handled = true;
36+
};
37+
menu.Items.Add(openWithMerger);
38+
39+
if (change.Index != Models.ChangeState.Deleted)
40+
{
41+
var explore = new MenuItem();
42+
explore.Header = App.Text("RevealFile");
43+
explore.Icon = App.CreateMenuIcon("Icons.Explore");
44+
explore.IsEnabled = File.Exists(changeFullPath);
45+
explore.Click += (_, ev) =>
46+
{
47+
Native.OS.OpenInFileManager(changeFullPath, true);
48+
ev.Handled = true;
49+
};
50+
menu.Items.Add(explore);
51+
}
52+
53+
var copyPath = new MenuItem();
54+
copyPath.Header = App.Text("CopyPath");
55+
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
56+
copyPath.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
57+
copyPath.Click += async (_, ev) =>
58+
{
59+
await App.CopyTextAsync(change.Path);
60+
ev.Handled = true;
61+
};
62+
menu.Items.Add(copyPath);
63+
64+
var copyFullPath = new MenuItem();
65+
copyFullPath.Header = App.Text("CopyFullPath");
66+
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
67+
copyFullPath.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+C" : "Ctrl+Shift+C";
68+
copyFullPath.Click += async (_, e) =>
69+
{
70+
await App.CopyTextAsync(changeFullPath);
71+
e.Handled = true;
72+
};
73+
menu.Items.Add(copyFullPath);
2074
menu?.Open(view);
2175
}
2276

0 commit comments

Comments
 (0)