diff --git a/src/AdvancedCalculator/ViewModels/MainViewModel.cs b/src/AdvancedCalculator/ViewModels/MainViewModel.cs index 76f2a35..222de45 100644 --- a/src/AdvancedCalculator/ViewModels/MainViewModel.cs +++ b/src/AdvancedCalculator/ViewModels/MainViewModel.cs @@ -35,10 +35,6 @@ public partial class MainViewModel : ViewModelBase [ObservableProperty] private int _selectedHistoryIndex = -1; - // Caret position in the input TextBox - [ObservableProperty] - private int _caretIndex; - public ObservableCollection History { get; } public ObservableCollection Variables { get; } @@ -65,11 +61,6 @@ public partial class MainViewModel : ViewModelBase if (string.IsNullOrWhiteSpace(functionDefintionItem.FunctionName)) return; InsertToken(functionDefintionItem.FunctionName + "()"); - // Place caret inside the parentheses - if (!string.IsNullOrEmpty(InputText)) - { - CaretIndex = Math.Max(0, InputText.Length - 1); - } // Close the functions flyout after insertion for smoother UX IsFunctionsPanelOpen = false; } diff --git a/src/AdvancedCalculator/Views/MainView.axaml b/src/AdvancedCalculator/Views/MainView.axaml index ecef799..e9b804d 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml +++ b/src/AdvancedCalculator/Views/MainView.axaml @@ -136,7 +136,9 @@ + Placement="BottomEdgeAlignedLeft" + Opened="OnFunctionsFlyoutOpened" + Closed="OnFunctionsFlyoutClosed"> @@ -160,7 +162,7 @@ + Tapped="OnFunctionsItemTapped"> @@ -183,9 +185,7 @@ - diff --git a/src/AdvancedCalculator/Views/MainView.axaml.cs b/src/AdvancedCalculator/Views/MainView.axaml.cs index d6c645e..5a3bbd9 100644 --- a/src/AdvancedCalculator/Views/MainView.axaml.cs +++ b/src/AdvancedCalculator/Views/MainView.axaml.cs @@ -21,7 +21,7 @@ public partial class MainView : UserControl private MainViewModel? _vm; private ListBox? _historyList; - // Functions flyout is handled via per-item Tapped in XAML + private Border? _functionsPanelRoot; private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) { @@ -53,6 +53,12 @@ public partial class MainView : UserControl _historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped); _historyList = null; } + // Detach functions panel tap handler if still attached + if (_functionsPanelRoot is not null) + { + _functionsPanelRoot.RemoveHandler(InputElement.TappedEvent, OnFunctionsItemTapped); + _functionsPanelRoot = null; + } _vm = null; } @@ -94,12 +100,27 @@ public partial class MainView : UserControl _vm?.InsertFunctionCommand.CanExecute(item) == true) { _vm.InsertFunctionCommand.Execute(item); - // Return focus to the input box so the caret is active - var input = this.FindControl("InputBox"); - input?.Focus(); e.Handled = true; } } - // Removed Opened/Closed dynamic wiring; handled directly in XAML. + + 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); + } +} + + private void OnFunctionsFlyoutClosed(object? sender, EventArgs e) + { + if (_functionsPanelRoot is not null) + { + _functionsPanelRoot.RemoveHandler(InputElement.TappedEvent, OnFunctionsItemTapped); + _functionsPanelRoot = null; + } + } }