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:
Codex CLI 2025-08-27 23:44:29 -05:00
commit 2f803db1c8
3 changed files with 157 additions and 23 deletions

View file

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