Handle function insertion on ListBox row taps directly: add Tapped handler to function item Border in DataTemplate and remove global view-level tap handler. Ensures handler is tied to list rows, not the toggle button.

This commit is contained in:
Codex CLI 2025-08-28 04:12:33 -05:00
commit 781f7161e5
2 changed files with 6 additions and 18 deletions

View file

@ -156,6 +156,7 @@
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate x:DataType="m:FunctionDefinitionItem"> <DataTemplate x:DataType="m:FunctionDefinitionItem">
<Border Background="Transparent" Padding="8" MinHeight="36" <Border Background="Transparent" Padding="8" MinHeight="36"
Tapped="OnFunctionsItemTapped"
AutomationProperties.Name="Insert function"> AutomationProperties.Name="Insert function">
<Grid ColumnDefinitions="*,Auto"> <Grid ColumnDefinitions="*,Auto">
<StackPanel> <StackPanel>

View file

@ -21,7 +21,6 @@ public partial class MainView : UserControl
private MainViewModel? _vm; private MainViewModel? _vm;
private ListBox? _historyList; private ListBox? _historyList;
private bool _functionsTapHandlerAttached;
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e) private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
{ {
@ -39,13 +38,7 @@ public partial class MainView : UserControl
RoutingStrategies.Tunnel | RoutingStrategies.Bubble); RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
} }
// Handle taps anywhere in the functions flyout list items to insert function // Functions list item taps are handled per-row via XAML (OnFunctionsItemTapped)
if (!_functionsTapHandlerAttached)
{
this.AddHandler(InputElement.TappedEvent, OnFunctionsTapped,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
_functionsTapHandlerAttached = true;
}
} }
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e) private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
@ -59,11 +52,6 @@ public partial class MainView : UserControl
_historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped); _historyList.RemoveHandler(InputElement.TappedEvent, OnHistoryTapped);
_historyList = null; _historyList = null;
} }
if (_functionsTapHandlerAttached)
{
this.RemoveHandler(InputElement.TappedEvent, OnFunctionsTapped);
_functionsTapHandlerAttached = false;
}
_vm = null; _vm = null;
} }
@ -98,12 +86,11 @@ public partial class MainView : UserControl
} }
} }
private void OnFunctionsTapped(object? sender, TappedEventArgs e) private void OnFunctionsItemTapped(object? sender, TappedEventArgs e)
{ {
// Find the ListBoxItem the tap originated from, and ensure it's a function item if (sender is not IControl control)
var srcVisual = e.Source as IVisual; return;
var container = srcVisual?.FindAncestorOfType<ListBoxItem>(); if (control.DataContext is not FunctionDefinitionItem item)
if (container?.DataContext is not FunctionDefinitionItem item)
return; return;
var vm = DataContext as MainViewModel; var vm = DataContext as MainViewModel;