diff --git a/src/AdvancedCalculator/Views/MainView.axaml b/src/AdvancedCalculator/Views/MainView.axaml index dbdde55..d44cf41 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml +++ b/src/AdvancedCalculator/Views/MainView.axaml @@ -71,53 +71,43 @@ - - - + + + + + + + + + diff --git a/src/AdvancedCalculator/Views/MainView.axaml.cs b/src/AdvancedCalculator/Views/MainView.axaml.cs index 0f4853e..062a041 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml.cs +++ b/src/AdvancedCalculator/Views/MainView.axaml.cs @@ -1,7 +1,11 @@ using System; using Avalonia; using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.VisualTree; using AdvancedCalculator.ViewModels; +using AdvancedCalculator.Models; namespace AdvancedCalculator.Views; @@ -15,6 +19,7 @@ public partial class MainView : UserControl } private MainViewModel? _vm; + private ListBox? _historyList; private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) { @@ -23,6 +28,14 @@ public partial class MainView : UserControl { _vm.CopyRequested += OnCopyRequested; } + + _historyList = this.FindControl("HistoryList"); + if (_historyList is not null) + { + // Handle taps anywhere in a history row to copy output text + _historyList.AddHandler(InputElement.TappedEvent, OnHistoryTapped, + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); + } } private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) @@ -31,6 +44,11 @@ public partial class MainView : UserControl { _vm.CopyRequested -= OnCopyRequested; } + if (_historyList is not null) + { + _historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped); + _historyList = null; + } _vm = null; } @@ -47,4 +65,21 @@ public partial class MainView : UserControl // Ignore clipboard errors; e.g., browser permission or missing gesture context } } + + private void OnHistoryTapped(object? sender, TappedEventArgs e) + { + // Find the ListBoxItem the tap originated from + var source = e.Source as IVisual; + var container = source?.FindAncestorOfType(); + if (container?.DataContext is not HistoryItem item) + return; + + var vm = DataContext as MainViewModel; + if (vm?.CopyHistoryOutputCommand.CanExecute(item) == true) + { + vm.CopyHistoryOutputCommand.Execute(item); + // Mark handled to avoid unintended selection change on tap + e.Handled = true; + } + } }