From 7a2722e928d35059b60b8f9da4416a7a02edf6e6 Mon Sep 17 00:00:00 2001 From: leo Date: Sun, 25 Aug 2024 15:11:31 +0800 Subject: [PATCH] feature: add a context menu to copy entire content of SelectableTextBlock (#394) --- src/App.Commands.cs | 65 +++++++++++++++++++++++++++++++ src/App.axaml.cs | 35 ++--------------- src/Resources/Locales/en_US.axaml | 1 + src/Resources/Locales/zh_CN.axaml | 1 + src/Resources/Locales/zh_TW.axaml | 1 + src/Resources/Styles.axaml | 9 +++++ 6 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 src/App.Commands.cs diff --git a/src/App.Commands.cs b/src/App.Commands.cs new file mode 100644 index 00000000..55241afc --- /dev/null +++ b/src/App.Commands.cs @@ -0,0 +1,65 @@ +using System; +using System.Windows.Input; +using Avalonia.Controls; + +namespace SourceGit +{ + public partial class App + { + public class SimpleCommand : ICommand + { + public event EventHandler CanExecuteChanged + { + add { } + remove { } + } + + public SimpleCommand(Action action) + { + _action = action; + } + + public bool CanExecute(object parameter) => _action != null; + public void Execute(object parameter) => _action?.Invoke(); + + private Action _action = null; + } + + public class ParameterCommand : ICommand + { + public event EventHandler CanExecuteChanged + { + add { } + remove { } + } + + public ParameterCommand(Action action) + { + _action = action; + } + + public bool CanExecute(object parameter) => _action != null; + public void Execute(object parameter) => _action?.Invoke(parameter); + + private Action _action = null; + } + + public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand(() => OpenDialog(new Views.Preference())); + public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand(() => OpenDialog(new Views.Hotkeys())); + public static readonly SimpleCommand OpenAppDataDirCommand = new SimpleCommand(() => Native.OS.OpenInFileManager(Native.OS.DataDir)); + public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand(() => OpenDialog(new Views.About())); + public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand(() => Check4Update(true)); + public static readonly SimpleCommand QuitCommand = new SimpleCommand(() => Quit(0)); + + public static readonly ParameterCommand CopyTextCommand = new ParameterCommand(param => + { + if (param is TextBlock textBlock) + { + if (textBlock.Inlines is { Count: > 0 } inlines) + CopyText(inlines.Text); + else + CopyText(textBlock.Text); + } + }); + } +} diff --git a/src/App.axaml.cs b/src/App.axaml.cs index 0ae1a4c4..5dab4836 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Text; using System.Text.Json; using System.Threading.Tasks; -using System.Windows.Input; using Avalonia; using Avalonia.Controls; @@ -21,34 +20,8 @@ using Avalonia.Threading; namespace SourceGit { - public class SimpleCommand : ICommand - { - public event EventHandler CanExecuteChanged - { - add { } - remove { } - } - - public SimpleCommand(Action action) - { - _action = action; - } - - public bool CanExecute(object parameter) => _action != null; - public void Execute(object parameter) => _action?.Invoke(); - - private Action _action = null; - } - public partial class App : Application { - public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand(() => OpenDialog(new Views.Preference())); - public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand(() => OpenDialog(new Views.Hotkeys())); - public static readonly SimpleCommand OpenAppDataDirCommand = new SimpleCommand(() => Native.OS.OpenInFileManager(Native.OS.DataDir)); - public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand(() => OpenDialog(new Views.About())); - public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand(() => Check4Update(true)); - public static readonly SimpleCommand QuitCommand = new SimpleCommand(() => Quit(0)); - [STAThread] public static void Main(string[] args) { @@ -125,8 +98,8 @@ namespace SourceGit public static void OpenDialog(Window window) { - if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - window.ShowDialog(desktop.MainWindow); + if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow: { } owner}) + window.ShowDialog(owner); } public static void RaiseException(string context, string message) @@ -260,7 +233,7 @@ namespace SourceGit if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { if (desktop.MainWindow?.Clipboard is { } clipboard) - await clipboard.SetTextAsync(data); + await clipboard.SetTextAsync(data ?? ""); } } @@ -305,7 +278,7 @@ namespace SourceGit public static IStorageProvider GetStorageProvider() { if (Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) - return desktop.MainWindow.StorageProvider; + return desktop.MainWindow?.StorageProvider; return null; } diff --git a/src/Resources/Locales/en_US.axaml b/src/Resources/Locales/en_US.axaml index c729451f..223d8955 100644 --- a/src/Resources/Locales/en_US.axaml +++ b/src/Resources/Locales/en_US.axaml @@ -145,6 +145,7 @@ User Name User name for this repository Copy + Copy All Text COPY MESSAGE Copy Path Copy File Name diff --git a/src/Resources/Locales/zh_CN.axaml b/src/Resources/Locales/zh_CN.axaml index e86b8e52..a521ca2b 100644 --- a/src/Resources/Locales/zh_CN.axaml +++ b/src/Resources/Locales/zh_CN.axaml @@ -148,6 +148,7 @@ 用户名 应用于本仓库的用户名 复制 + 复制全部文本 复制内容 复制路径 复制文件名 diff --git a/src/Resources/Locales/zh_TW.axaml b/src/Resources/Locales/zh_TW.axaml index 7fe6610e..8f73e7ca 100644 --- a/src/Resources/Locales/zh_TW.axaml +++ b/src/Resources/Locales/zh_TW.axaml @@ -148,6 +148,7 @@ 使用者名稱 應用於本倉庫的使用者名稱 複製 + 複製全部内容 複製內容 複製路徑 複製檔名 diff --git a/src/Resources/Styles.axaml b/src/Resources/Styles.axaml index 3323cee8..a267e144 100644 --- a/src/Resources/Styles.axaml +++ b/src/Resources/Styles.axaml @@ -1,5 +1,6 @@ + + + + + +