From 8a5a50fcaa488db57b5c20bfa342419a2e144cf3 Mon Sep 17 00:00:00 2001 From: Codex CLI Date: Thu, 28 Aug 2025 03:57:41 -0500 Subject: [PATCH] Use ListBoxItem taps to insert functions: remove inner Button from functions list template, keep item DataContext as FunctionDefinitionItem, and handle tap in code-behind by executing InsertFunctionCommand with the tapped item. Mirror history list clipboard pattern. Name flyout root for clarity. --- src/AdvancedCalculator/Views/MainView.axaml | 9 ++---- .../Views/MainView.axaml.cs | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/AdvancedCalculator/Views/MainView.axaml b/src/AdvancedCalculator/Views/MainView.axaml index b4f9990..ad2b16e 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml +++ b/src/AdvancedCalculator/Views/MainView.axaml @@ -135,7 +135,7 @@ AutomationProperties.Name="Toggle functions panel"> - + @@ -155,10 +155,7 @@ - + diff --git a/src/AdvancedCalculator/Views/MainView.axaml.cs b/src/AdvancedCalculator/Views/MainView.axaml.cs index c21b69e..b7faa52 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml.cs +++ b/src/AdvancedCalculator/Views/MainView.axaml.cs @@ -21,6 +21,7 @@ public partial class MainView : UserControl private MainViewModel? _vm; private ListBox? _historyList; + private bool _functionsTapHandlerAttached; private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) { @@ -37,6 +38,14 @@ public partial class MainView : UserControl _historyList.AddHandler(InputElement.TappedEvent, OnHistoryTapped, RoutingStrategies.Tunnel | RoutingStrategies.Bubble); } + + // Handle taps anywhere in the functions flyout list items to insert function + if (!_functionsTapHandlerAttached) + { + this.AddHandler(InputElement.TappedEvent, OnFunctionsTapped, + RoutingStrategies.Tunnel | RoutingStrategies.Bubble); + _functionsTapHandlerAttached = true; + } } private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) @@ -50,6 +59,11 @@ public partial class MainView : UserControl _historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped); _historyList = null; } + if (_functionsTapHandlerAttached) + { + this.RemoveHandler(InputElement.TappedEvent, OnFunctionsTapped); + _functionsTapHandlerAttached = false; + } _vm = null; } @@ -83,4 +97,20 @@ public partial class MainView : UserControl e.Handled = true; } } + + private void OnFunctionsTapped(object? sender, TappedEventArgs e) + { + // Find the ListBoxItem the tap originated from, and ensure it's a function item + var srcVisual = e.Source as IVisual; + var container = srcVisual?.FindAncestorOfType(); + if (container?.DataContext is not FunctionDefinitionItem item) + return; + + var vm = DataContext as MainViewModel; + if (vm?.InsertFunctionCommand.CanExecute(item) == true) + { + vm.InsertFunctionCommand.Execute(item); + e.Handled = true; + } + } }