feature: add a context menu to copy entire content of SelectableTextBlock (#394)

This commit is contained in:
leo 2024-08-25 15:11:31 +08:00
parent 5dc4ed6f54
commit 7a2722e928
No known key found for this signature in database
6 changed files with 81 additions and 31 deletions

65
src/App.Commands.cs Normal file
View file

@ -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<object> action)
{
_action = action;
}
public bool CanExecute(object parameter) => _action != null;
public void Execute(object parameter) => _action?.Invoke(parameter);
private Action<object> _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);
}
});
}
}

View file

@ -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;
}

View file

@ -145,6 +145,7 @@
<x:String x:Key="Text.Configure.User" xml:space="preserve">User Name</x:String>
<x:String x:Key="Text.Configure.User.Placeholder" xml:space="preserve">User name for this repository</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">Copy</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">Copy All Text</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">COPY MESSAGE</x:String>
<x:String x:Key="Text.CopyPath" xml:space="preserve">Copy Path</x:String>
<x:String x:Key="Text.CopyFileName" xml:space="preserve">Copy File Name</x:String>

View file

@ -148,6 +148,7 @@
<x:String x:Key="Text.Configure.User" xml:space="preserve">用户名</x:String>
<x:String x:Key="Text.Configure.User.Placeholder" xml:space="preserve">应用于本仓库的用户名</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">复制</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">复制全部文本</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">复制内容</x:String>
<x:String x:Key="Text.CopyPath" xml:space="preserve">复制路径</x:String>
<x:String x:Key="Text.CopyFileName" xml:space="preserve">复制文件名</x:String>

View file

@ -148,6 +148,7 @@
<x:String x:Key="Text.Configure.User" xml:space="preserve">使用者名稱</x:String>
<x:String x:Key="Text.Configure.User.Placeholder" xml:space="preserve">應用於本倉庫的使用者名稱</x:String>
<x:String x:Key="Text.Copy" xml:space="preserve">複製</x:String>
<x:String x:Key="Text.CopyAllText" xml:space="preserve">複製全部内容</x:String>
<x:String x:Key="Text.CopyMessage" xml:space="preserve">複製內容</x:String>
<x:String x:Key="Text.CopyPath" xml:space="preserve">複製路徑</x:String>
<x:String x:Key="Text.CopyFileName" xml:space="preserve">複製檔名</x:String>

View file

@ -1,5 +1,6 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="using:SourceGit"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:c="using:SourceGit.Converters"
xmlns:ae="using:AvaloniaEdit"
@ -304,6 +305,14 @@
<Path Width="11" Height="11" Data="{StaticResource Icons.Copy}" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="{DynamicResource Text.CopyAllText}"
Command="{x:Static s:App.CopyTextCommand}"
CommandParameter="{Binding $parent[SelectableTextBlock]}">
<MenuItem.Icon>
<Path Width="11" Height="11" Data="{StaticResource Icons.Copy}" VerticalAlignment="Center"/>
</MenuItem.Icon>
</MenuItem>
</MenuFlyout>
</Setter.Value>
</Setter>