Skip to content

Commit b23f284

Browse files
committed
feature: add context menu for selected change in stashes page
1 parent d0ae24b commit b23f284

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/ViewModels/StashesPage.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Threading.Tasks;
45

56
using Avalonia.Controls;
@@ -150,6 +151,74 @@ public ContextMenu MakeContextMenu(Models.Stash stash)
150151
return menu;
151152
}
152153

154+
public ContextMenu MakeContextMenuForChange(Models.Change change)
155+
{
156+
if (change == null)
157+
return null;
158+
159+
var diffWithMerger = new MenuItem();
160+
diffWithMerger.Header = App.Text("DiffWithMerger");
161+
diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
162+
diffWithMerger.Click += (_, ev) =>
163+
{
164+
var toolType = Preference.Instance.ExternalMergeToolType;
165+
var toolPath = Preference.Instance.ExternalMergeToolPath;
166+
var opt = new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, change);
167+
168+
Task.Run(() => Commands.MergeTool.OpenForDiff(_repo.FullPath, toolType, toolPath, opt));
169+
ev.Handled = true;
170+
};
171+
172+
var fullPath = Path.Combine(_repo.FullPath, change.Path);
173+
var explore = new MenuItem();
174+
explore.Header = App.Text("RevealFile");
175+
explore.Icon = App.CreateMenuIcon("Icons.Explore");
176+
explore.IsEnabled = File.Exists(fullPath);
177+
explore.Click += (_, ev) =>
178+
{
179+
Native.OS.OpenInFileManager(fullPath, true);
180+
ev.Handled = true;
181+
};
182+
183+
var resetToThisRevision = new MenuItem();
184+
resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision");
185+
resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout");
186+
resetToThisRevision.Click += (_, ev) =>
187+
{
188+
new Commands.Checkout(_repo.FullPath).FileWithRevision(change.Path, $"{_selectedStash.SHA}");
189+
ev.Handled = true;
190+
};
191+
192+
var copyPath = new MenuItem();
193+
copyPath.Header = App.Text("CopyPath");
194+
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
195+
copyPath.Click += (_, ev) =>
196+
{
197+
App.CopyText(change.Path);
198+
ev.Handled = true;
199+
};
200+
201+
var copyFileName = new MenuItem();
202+
copyFileName.Header = App.Text("CopyFileName");
203+
copyFileName.Icon = App.CreateMenuIcon("Icons.Copy");
204+
copyFileName.Click += (_, e) =>
205+
{
206+
App.CopyText(Path.GetFileName(change.Path));
207+
e.Handled = true;
208+
};
209+
210+
var menu = new ContextMenu();
211+
menu.Items.Add(diffWithMerger);
212+
menu.Items.Add(explore);
213+
menu.Items.Add(new MenuItem { Header = "-" });
214+
menu.Items.Add(resetToThisRevision);
215+
menu.Items.Add(new MenuItem { Header = "-" });
216+
menu.Items.Add(copyPath);
217+
menu.Items.Add(copyFileName);
218+
219+
return menu;
220+
}
221+
153222
public void Clear()
154223
{
155224
if (PopupHost.CanCreatePopup())

src/Views/StashesPage.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136

137137
<ListBox.ItemTemplate>
138138
<DataTemplate DataType="m:Change">
139-
<Grid ColumnDefinitions="24,*">
139+
<Grid ColumnDefinitions="24,*" Background="Transparent" ContextRequested="OnChangeContextRequested">
140140
<v:ChangeStatusIcon Grid.Column="0" Width="14" Height="14" Change="{Binding}"/>
141141
<TextBlock Grid.Column="1" Classes="primary" Text="{Binding Path}" Margin="4,0,0,0"/>
142142
</Grid>

src/Views/StashesPage.axaml.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ private void OnStashContextRequested(object sender, ContextRequestedEventArgs e)
1818
}
1919
e.Handled = true;
2020
}
21+
22+
private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e)
23+
{
24+
if (DataContext is ViewModels.StashesPage vm && sender is Grid grid)
25+
{
26+
var menu = vm.MakeContextMenuForChange(grid.DataContext as Models.Change);
27+
grid.OpenContextMenu(menu);
28+
}
29+
e.Handled = true;
30+
}
2131
}
2232
}

0 commit comments

Comments
 (0)