From b23f284e21123e139964a0b54eba65661b911b05 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 17 Oct 2024 15:37:14 +0800 Subject: [PATCH] feature: add context menu for selected change in stashes page --- src/ViewModels/StashesPage.cs | 69 ++++++++++++++++++++++++++++++++++ src/Views/StashesPage.axaml | 2 +- src/Views/StashesPage.axaml.cs | 10 +++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/ViewModels/StashesPage.cs b/src/ViewModels/StashesPage.cs index e07ac432..1e35d647 100644 --- a/src/ViewModels/StashesPage.cs +++ b/src/ViewModels/StashesPage.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Threading.Tasks; using Avalonia.Controls; @@ -150,6 +151,74 @@ namespace SourceGit.ViewModels return menu; } + public ContextMenu MakeContextMenuForChange(Models.Change change) + { + if (change == null) + return null; + + var diffWithMerger = new MenuItem(); + diffWithMerger.Header = App.Text("DiffWithMerger"); + diffWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith"); + diffWithMerger.Click += (_, ev) => + { + var toolType = Preference.Instance.ExternalMergeToolType; + var toolPath = Preference.Instance.ExternalMergeToolPath; + var opt = new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, change); + + Task.Run(() => Commands.MergeTool.OpenForDiff(_repo.FullPath, toolType, toolPath, opt)); + ev.Handled = true; + }; + + var fullPath = Path.Combine(_repo.FullPath, change.Path); + var explore = new MenuItem(); + explore.Header = App.Text("RevealFile"); + explore.Icon = App.CreateMenuIcon("Icons.Explore"); + explore.IsEnabled = File.Exists(fullPath); + explore.Click += (_, ev) => + { + Native.OS.OpenInFileManager(fullPath, true); + ev.Handled = true; + }; + + var resetToThisRevision = new MenuItem(); + resetToThisRevision.Header = App.Text("ChangeCM.CheckoutThisRevision"); + resetToThisRevision.Icon = App.CreateMenuIcon("Icons.File.Checkout"); + resetToThisRevision.Click += (_, ev) => + { + new Commands.Checkout(_repo.FullPath).FileWithRevision(change.Path, $"{_selectedStash.SHA}"); + ev.Handled = true; + }; + + var copyPath = new MenuItem(); + copyPath.Header = App.Text("CopyPath"); + copyPath.Icon = App.CreateMenuIcon("Icons.Copy"); + copyPath.Click += (_, ev) => + { + App.CopyText(change.Path); + ev.Handled = true; + }; + + var copyFileName = new MenuItem(); + copyFileName.Header = App.Text("CopyFileName"); + copyFileName.Icon = App.CreateMenuIcon("Icons.Copy"); + copyFileName.Click += (_, e) => + { + App.CopyText(Path.GetFileName(change.Path)); + e.Handled = true; + }; + + var menu = new ContextMenu(); + menu.Items.Add(diffWithMerger); + menu.Items.Add(explore); + menu.Items.Add(new MenuItem { Header = "-" }); + menu.Items.Add(resetToThisRevision); + menu.Items.Add(new MenuItem { Header = "-" }); + menu.Items.Add(copyPath); + menu.Items.Add(copyFileName); + + return menu; + } + public void Clear() { if (PopupHost.CanCreatePopup()) diff --git a/src/Views/StashesPage.axaml b/src/Views/StashesPage.axaml index 5bdb75f2..b3e52771 100644 --- a/src/Views/StashesPage.axaml +++ b/src/Views/StashesPage.axaml @@ -136,7 +136,7 @@ - + diff --git a/src/Views/StashesPage.axaml.cs b/src/Views/StashesPage.axaml.cs index 13b74ee2..c499f76a 100644 --- a/src/Views/StashesPage.axaml.cs +++ b/src/Views/StashesPage.axaml.cs @@ -18,5 +18,15 @@ namespace SourceGit.Views } e.Handled = true; } + + private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e) + { + if (DataContext is ViewModels.StashesPage vm && sender is Grid grid) + { + var menu = vm.MakeContextMenuForChange(grid.DataContext as Models.Change); + grid.OpenContextMenu(menu); + } + e.Handled = true; + } } }