From c2bb03044bb78619e1d76a723e5a3f5ab2b65100 Mon Sep 17 00:00:00 2001 From: Jordan Wages Date: Thu, 28 Aug 2025 04:54:41 -0500 Subject: [PATCH] Got function insertion working --- src/AdvancedCalculator/Views/MainView.axaml | 3 +- .../Views/MainView.axaml.cs | 42 +++++-------------- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/AdvancedCalculator/Views/MainView.axaml b/src/AdvancedCalculator/Views/MainView.axaml index 80e54ae..41507f3 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml +++ b/src/AdvancedCalculator/Views/MainView.axaml @@ -160,7 +160,8 @@ + AutomationProperties.Name="Insert function" + Tapped="OnFunctionsItemTapped"> diff --git a/src/AdvancedCalculator/Views/MainView.axaml.cs b/src/AdvancedCalculator/Views/MainView.axaml.cs index 7aa8e78..5a3bbd9 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml.cs +++ b/src/AdvancedCalculator/Views/MainView.axaml.cs @@ -95,45 +95,25 @@ public partial class MainView : UserControl private void OnFunctionsItemTapped(object? sender, TappedEventArgs e) { - if (sender is not Control control) - return; - if (control.DataContext is not FunctionDefinitionItem item) + var lbi = (e.Source as Control)?.FindAncestorOfType(); + if (lbi?.DataContext is FunctionDefinitionItem item && + _vm?.InsertFunctionCommand.CanExecute(item) == true) { - // If attached to panel root, walk up from the source to a ListBoxItem and use its DataContext - if (sender is ContentPresenter root && e.Source is ContentPresenter src) - { - var lbi = src.FindAncestorOfType(); - if (lbi?.DataContext is not FunctionDefinitionItem rowItem) - return; - item = rowItem; - } - else - { - return; - } - } - - var vm = DataContext as MainViewModel; - if (vm?.InsertFunctionCommand.CanExecute(item) == true) - { - vm.InsertFunctionCommand.Execute(item); + _vm.InsertFunctionCommand.Execute(item); e.Handled = true; } } - private void OnFunctionsFlyoutOpened(object? sender, EventArgs e) - { - // Flyout content is rendered in a popup; find by name at TopLevel - var top = TopLevel.GetTopLevel(this); - if (top is null) return; - var panel = top.FindControl("FunctionsPanelRoot"); - if (panel is null) return; - // Attach a single tap handler to the flyout content root - panel.AddHandler(InputElement.TappedEvent, OnFunctionsItemTapped, + private void OnFunctionsFlyoutOpened(object? sender, EventArgs e) +{ + if (sender is Flyout flyout && sender is ContentPresenter presenter) + { + _functionsPanelRoot = presenter.FindControl("FunctionsPanelRoot"); + _functionsPanelRoot?.AddHandler(InputElement.TappedEvent, OnFunctionsItemTapped, RoutingStrategies.Tunnel | RoutingStrategies.Bubble); - _functionsPanelRoot = panel; } +} private void OnFunctionsFlyoutClosed(object? sender, EventArgs e) {