From fdff62e317eb3890fb423caca9bc0b61e0c0ddde Mon Sep 17 00:00:00 2001 From: walterlv Date: Mon, 8 Apr 2024 18:00:58 +0800 Subject: [PATCH] fix: extra space of MenuItem see: https://github.com/AvaloniaUI/Avalonia/issues/6905 --- src/ViewModels/Repository.cs | 90 +++++++++++++---------------------- src/Views/Repository.axaml | 11 ----- src/Views/Repository.axaml.cs | 22 --------- 3 files changed, 32 insertions(+), 91 deletions(-) diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 63b668d2..2a0e117e 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -95,15 +95,15 @@ namespace SourceGit.ViewModels set => SetProperty(ref _selectedView, value); } - public AvaloniaList ExternalTerminals + public AvaloniaList ExternalTerminals { get; - } = new AvaloniaList(); + } = []; - public AvaloniaList ExternalEditors + public AvaloniaList ExternalEditors { get; - } = new AvaloniaList(); + } = []; [JsonIgnore] public List Remotes @@ -330,52 +330,66 @@ namespace SourceGit.ViewModels } } - public ImmutableArray CreateContextMenuForExternalTerminals() + public ImmutableArray CreateContextMenuForExternalTerminals() { var terminals = Native.OS.ExternalTerminals; if (terminals.Count == 0) { App.RaiseException(_fullpath, "No available external terminals found!"); - return [new ExternalMenuItem("No terminal found") + return [new MenuItem { + Header = "No terminal found", IsEnabled = false, }]; } - var items = new List(terminals.Count); + var items = new List(terminals.Count); foreach (var terminal in terminals) { var dupTerminal = terminal; - var item = new ExternalMenuItem( - App.Text("Repository.OpenIn", dupTerminal.Name), - $"ExternalTerminalIcons/{dupTerminal.Icon}", - () => dupTerminal.Open(_fullpath)); + var icon = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalTerminalIcons/{dupTerminal.Icon}", UriKind.RelativeOrAbsolute)); + var item = new MenuItem + { + Header = App.Text("Repository.OpenIn", dupTerminal.Name), + Icon = new Image + { + Width = 16, Height = 16, Source = new Bitmap(icon), + }, + Command = new RelayCommand(() => dupTerminal.Open(_fullpath)), + }; items.Add(item); } return [..items]; } - public ImmutableArray CreateContextMenuForExternalEditors() + public ImmutableArray CreateContextMenuForExternalEditors() { var editors = Native.OS.ExternalEditors; if (editors.Count == 0) { App.RaiseException(_fullpath, "No available external editors found!"); - return [new ExternalMenuItem("No editor found") + return [new MenuItem { + Header = "No editor found", IsEnabled = false, }]; } - var items = new List(editors.Count); + var items = new List(editors.Count); foreach (var editor in editors) { var dupEditor = editor; - var item = new ExternalMenuItem( - App.Text("Repository.OpenIn", dupEditor.Name), - $"ExternalToolIcons/{dupEditor.Icon}", - () => dupEditor.Open(_fullpath)); + var icon = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/ExternalToolIcons/{dupEditor.Icon}", UriKind.RelativeOrAbsolute)); + var item = new MenuItem + { + Header = App.Text("Repository.OpenIn", dupEditor.Name), + Icon = new Image + { + Width = 16, Height = 16, Source = new Bitmap(icon), + }, + Command = new RelayCommand(() => dupEditor.Open(_fullpath)), + }; items.Add(item); } @@ -1419,44 +1433,4 @@ namespace SourceGit.ViewModels private InProgressContext _inProgressContext = null; private bool _hasUnsolvedConflicts = false; } - - /// - /// A menu item for external tools. - /// - public readonly record struct ExternalMenuItem - { - public ExternalMenuItem(string header) - { - Header = header; - IconKey = null; - Command = null; - } - - public ExternalMenuItem(string header, string iconKey, Action click) - { - Header = header; - IconKey = iconKey; - Command = new RelayCommand(click); - } - - /// - /// The external tool name. - /// - public string Header { get; } - - /// - /// The resource key of the icon. - /// - public string? IconKey { get; init; } - - /// - /// The command when the user click the menu item. - /// - public ICommand Command { get; } - - /// - /// if the menu item is enabled; otherwise, . - /// - public bool IsEnabled { get; init; } = true; - } } diff --git a/src/Views/Repository.axaml b/src/Views/Repository.axaml index 50b41e47..06099a13 100644 --- a/src/Views/Repository.axaml +++ b/src/Views/Repository.axaml @@ -11,23 +11,12 @@ x:DataType="vm:Repository"> - - - - - - - - diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index c186cd3d..10be822c 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -305,26 +305,4 @@ namespace SourceGit.Views } } } - - public sealed class ExternalIconKeyToImageConverter : IValueConverter - { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - if (value is string iconKey && !string.IsNullOrWhiteSpace(iconKey)) - { - var icon = AssetLoader.Open(new Uri($"avares://SourceGit/Resources/{iconKey}", UriKind.RelativeOrAbsolute)); - return new Image - { - Width = 16, Height = 16, Source = new Bitmap(icon), - }; - } - - return null; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotSupportedException(); - } - } }