Add MVVM-pure clipboard copy for history: hover-only copy button with flyout on Desktop/Web and long-press/right-click context menu; View writes to clipboard via CopyRequested event
This commit is contained in:
parent
b9de2cae2d
commit
2f803db1c8
3 changed files with 157 additions and 23 deletions
|
@ -1,4 +1,7 @@
|
|||
using Avalonia.Controls;
|
||||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using AdvancedCalculator.ViewModels;
|
||||
|
||||
namespace AdvancedCalculator.Views;
|
||||
|
||||
|
@ -7,5 +10,41 @@ public partial class MainView : UserControl
|
|||
public MainView()
|
||||
{
|
||||
InitializeComponent();
|
||||
this.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||
this.DetachedFromVisualTree += OnDetachedFromVisualTree;
|
||||
}
|
||||
|
||||
private MainViewModel? _vm;
|
||||
|
||||
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
_vm = DataContext as MainViewModel;
|
||||
if (_vm is not null)
|
||||
{
|
||||
_vm.CopyRequested += OnCopyRequested;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
if (_vm is not null)
|
||||
{
|
||||
_vm.CopyRequested -= OnCopyRequested;
|
||||
}
|
||||
_vm = null;
|
||||
}
|
||||
|
||||
private async void OnCopyRequested(object? sender, string text)
|
||||
{
|
||||
var top = TopLevel.GetTopLevel(this);
|
||||
if (top?.Clipboard is null) return;
|
||||
try
|
||||
{
|
||||
await top.Clipboard.SetTextAsync(text);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore clipboard errors; e.g., browser permission or missing gesture context
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue