refactor: move context menu creation from ViewModels to Views (PART 5)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-07-23 13:41:36 +08:00
parent f2830e9573
commit dd00d0e7a8
2 changed files with 193 additions and 172 deletions

View File

@@ -1,18 +1,19 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class StashesPage : ObservableObject, IDisposable
{
public string RepositoryPath
{
get => _repo.FullPath;
}
public List<Models.Stash> Stashes
{
get => _stashes;
@@ -140,169 +141,6 @@ namespace SourceGit.ViewModels
_diffContext = null;
}
public ContextMenu MakeContextMenu(Models.Stash stash)
{
var apply = new MenuItem();
apply.Header = App.Text("StashCM.Apply");
apply.Icon = App.CreateMenuIcon("Icons.CheckCircled");
apply.Click += (_, ev) =>
{
Apply(stash);
ev.Handled = true;
};
var drop = new MenuItem();
drop.Header = App.Text("StashCM.Drop");
drop.Icon = App.CreateMenuIcon("Icons.Clear");
drop.Tag = "Back/Delete";
drop.Click += (_, ev) =>
{
Drop(stash);
ev.Handled = true;
};
var patch = new MenuItem();
patch.Header = App.Text("StashCM.SaveAsPatch");
patch.Icon = App.CreateMenuIcon("Icons.Diff");
patch.Click += async (_, e) =>
{
var storageProvider = App.GetStorageProvider();
if (storageProvider == null)
return;
var options = new FilePickerSaveOptions();
options.Title = App.Text("StashCM.SaveAsPatch");
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
{
var opts = new List<Models.DiffOption>();
foreach (var c in _changes)
{
if (_untracked.Contains(c))
opts.Add(new Models.DiffOption(Models.Commit.EmptyTreeSHA1, _selectedStash.Parents[2], c));
else
opts.Add(new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, c));
}
var succ = await Commands.SaveChangesAsPatch.ProcessStashChangesAsync(_repo.FullPath, opts, storageFile.Path.LocalPath);
if (succ)
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
}
e.Handled = true;
};
var copy = new MenuItem();
copy.Header = App.Text("StashCM.CopyMessage");
copy.Icon = App.CreateMenuIcon("Icons.Copy");
copy.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
copy.Click += async (_, ev) =>
{
await App.CopyTextAsync(stash.Message);
ev.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(apply);
menu.Items.Add(drop);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(patch);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(copy);
return menu;
}
public ContextMenu MakeContextMenuForChange()
{
if (_selectedChanges is not { Count: 1 })
return null;
var change = _selectedChanges[0];
var openWithMerger = new MenuItem();
openWithMerger.Header = App.Text("OpenInExternalMergeTool");
openWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
openWithMerger.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+D" : "Ctrl+Shift+D";
openWithMerger.Click += (_, ev) =>
{
var toolType = Preferences.Instance.ExternalMergeToolType;
var toolPath = Preferences.Instance.ExternalMergeToolPath;
var opt = new Models.DiffOption($"{_selectedStash.SHA}^", _selectedStash.SHA, change);
new Commands.DiffTool(_repo.FullPath, toolType, toolPath, opt).Open();
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 += async (_, ev) =>
{
var log = _repo.CreateLog($"Reset File to '{_selectedStash.SHA}'");
if (_untracked.Contains(change))
{
await Commands.SaveRevisionFile.RunAsync(_repo.FullPath, _selectedStash.Parents[2], change.Path, fullPath);
}
else if (change.Index == Models.ChangeState.Added)
{
await Commands.SaveRevisionFile.RunAsync(_repo.FullPath, _selectedStash.SHA, change.Path, fullPath);
}
else
{
await new Commands.Checkout(_repo.FullPath)
.Use(log)
.FileWithRevisionAsync(change.Path, $"{_selectedStash.SHA}");
}
log.Complete();
ev.Handled = true;
};
var copyPath = new MenuItem();
copyPath.Header = App.Text("CopyPath");
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyPath.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
copyPath.Click += async (_, ev) =>
{
await App.CopyTextAsync(change.Path);
ev.Handled = true;
};
var copyFullPath = new MenuItem();
copyFullPath.Header = App.Text("CopyFullPath");
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyFullPath.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+C" : "Ctrl+Shift+C";
copyFullPath.Click += async (_, e) =>
{
await App.CopyTextAsync(Native.OS.GetAbsPath(_repo.FullPath, change.Path));
e.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(openWithMerger);
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(copyFullPath);
return menu;
}
public void ClearSearchFilter()
{
SearchFilter = string.Empty;
@@ -320,6 +158,69 @@ namespace SourceGit.ViewModels
_repo.ShowPopup(new DropStash(_repo, stash));
}
public async Task SaveStashAsPathAsync(Models.Stash stash, string saveTo)
{
var opts = new List<Models.DiffOption>();
var changes = await new Commands.CompareRevisions(_repo.FullPath, $"{stash.SHA}^", stash.SHA)
.ReadAsync()
.ConfigureAwait(false);
foreach (var c in changes)
opts.Add(new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, c));
if (stash.Parents.Count == 3)
{
var untracked = await new Commands.CompareRevisions(_repo.FullPath, Models.Commit.EmptyTreeSHA1, stash.Parents[2])
.ReadAsync()
.ConfigureAwait(false);
foreach (var c in untracked)
opts.Add(new Models.DiffOption(Models.Commit.EmptyTreeSHA1, _selectedStash.Parents[2], c));
changes.AddRange(untracked);
}
var succ = await Commands.SaveChangesAsPatch.ProcessStashChangesAsync(_repo.FullPath, opts, saveTo);
if (succ)
App.SendNotification(_repo.FullPath, App.Text("SaveAsPatchSuccess"));
}
public void OpenChangeWithExternalDiffTool(Models.Change change)
{
Models.DiffOption opt;
if (_untracked.Contains(change))
opt = new Models.DiffOption(Models.Commit.EmptyTreeSHA1, _selectedStash.Parents[2], change);
else
opt = new Models.DiffOption(_selectedStash.Parents[0], _selectedStash.SHA, change);
var toolType = Preferences.Instance.ExternalMergeToolType;
var toolPath = Preferences.Instance.ExternalMergeToolPath;
new Commands.DiffTool(_repo.FullPath, toolType, toolPath, opt).Open();
}
public async Task CheckoutSingleFileAsync(Models.Change change)
{
var fullPath = Native.OS.GetAbsPath(_repo.FullPath, change.Path);
var log = _repo.CreateLog($"Reset File to '{_selectedStash.SHA}'");
if (_untracked.Contains(change))
{
await Commands.SaveRevisionFile.RunAsync(_repo.FullPath, _selectedStash.Parents[2], change.Path, fullPath);
}
else if (change.Index == Models.ChangeState.Added)
{
await Commands.SaveRevisionFile.RunAsync(_repo.FullPath, _selectedStash.SHA, change.Path, fullPath);
}
else
{
await new Commands.Checkout(_repo.FullPath)
.Use(log)
.FileWithRevisionAsync(change.Path, $"{_selectedStash.SHA}");
}
log.Complete();
}
private void RefreshVisible()
{
if (string.IsNullOrEmpty(_searchFilter))

View File

@@ -1,6 +1,9 @@
using System;
using System;
using System.IO;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Platform.Storage;
namespace SourceGit.Views
{
@@ -46,7 +49,63 @@ namespace SourceGit.Views
if (DataContext is ViewModels.StashesPage vm &&
sender is Border { DataContext: Models.Stash stash } border)
{
var menu = vm.MakeContextMenu(stash);
var apply = new MenuItem();
apply.Header = App.Text("StashCM.Apply");
apply.Icon = App.CreateMenuIcon("Icons.CheckCircled");
apply.Click += (_, ev) =>
{
vm.Apply(stash);
ev.Handled = true;
};
var drop = new MenuItem();
drop.Header = App.Text("StashCM.Drop");
drop.Icon = App.CreateMenuIcon("Icons.Clear");
drop.Tag = "Back/Delete";
drop.Click += (_, ev) =>
{
vm.Drop(stash);
ev.Handled = true;
};
var patch = new MenuItem();
patch.Header = App.Text("StashCM.SaveAsPatch");
patch.Icon = App.CreateMenuIcon("Icons.Diff");
patch.Click += async (_, e) =>
{
var storageProvider = TopLevel.GetTopLevel(this)?.StorageProvider;
if (storageProvider == null)
return;
var options = new FilePickerSaveOptions();
options.Title = App.Text("StashCM.SaveAsPatch");
options.DefaultExtension = ".patch";
options.FileTypeChoices = [new FilePickerFileType("Patch File") { Patterns = ["*.patch"] }];
var storageFile = await storageProvider.SaveFilePickerAsync(options);
if (storageFile != null)
await vm.SaveStashAsPathAsync(stash, storageFile.Path.LocalPath);
e.Handled = true;
};
var copy = new MenuItem();
copy.Header = App.Text("StashCM.CopyMessage");
copy.Icon = App.CreateMenuIcon("Icons.Copy");
copy.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
copy.Click += async (_, ev) =>
{
await App.CopyTextAsync(stash.Message);
ev.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(apply);
menu.Items.Add(drop);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(patch);
menu.Items.Add(new MenuItem { Header = "-" });
menu.Items.Add(copy);
menu.Open(border);
}
@@ -64,10 +123,71 @@ namespace SourceGit.Views
private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e)
{
if (DataContext is ViewModels.StashesPage vm && sender is ChangeCollectionView view)
if (DataContext is ViewModels.StashesPage { SelectedChanges: { Count: 1 } selected } vm &&
sender is ChangeCollectionView view)
{
var menu = vm.MakeContextMenuForChange();
menu?.Open(view);
var repo = vm.RepositoryPath;
var change = selected[0];
var openWithMerger = new MenuItem();
openWithMerger.Header = App.Text("OpenInExternalMergeTool");
openWithMerger.Icon = App.CreateMenuIcon("Icons.OpenWith");
openWithMerger.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+D" : "Ctrl+Shift+D";
openWithMerger.Click += (_, ev) =>
{
vm.OpenChangeWithExternalDiffTool(change);
ev.Handled = true;
};
var fullPath = Native.OS.GetAbsPath(repo, 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 += async (_, ev) =>
{
await vm.CheckoutSingleFileAsync(change);
ev.Handled = true;
};
var copyPath = new MenuItem();
copyPath.Header = App.Text("CopyPath");
copyPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyPath.Tag = OperatingSystem.IsMacOS() ? "⌘+C" : "Ctrl+C";
copyPath.Click += async (_, ev) =>
{
await App.CopyTextAsync(change.Path);
ev.Handled = true;
};
var copyFullPath = new MenuItem();
copyFullPath.Header = App.Text("CopyFullPath");
copyFullPath.Icon = App.CreateMenuIcon("Icons.Copy");
copyFullPath.Tag = OperatingSystem.IsMacOS() ? "⌘+⇧+C" : "Ctrl+Shift+C";
copyFullPath.Click += async (_, e) =>
{
await App.CopyTextAsync(fullPath);
e.Handled = true;
};
var menu = new ContextMenu();
menu.Items.Add(openWithMerger);
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(copyFullPath);
menu.Open(view);
}
e.Handled = true;