Use single tap handler for functions flyout: add named Flyout with Opened/Closed handlers, remove per-row Tapped, and attach handler to flyout root at runtime. Handler finds tapped ListBoxItem and executes InsertFunctionCommand.

This commit is contained in:
Codex CLI 2025-08-28 04:25:36 -05:00
commit 3985395141
2 changed files with 50 additions and 4 deletions

View file

@ -134,7 +134,11 @@
<Button Grid.Column="1" Command="{Binding ToggleFunctionsCommand}" Margin="0,0,6,0" MinHeight="40" MinWidth="40"
AutomationProperties.Name="Toggle functions panel">
<Button.Flyout>
<Flyout IsOpen="{Binding IsFunctionsPanelOpen, Mode=TwoWay}" Placement="BottomEdgeAlignedLeft">
<Flyout x:Name="FunctionsFlyout"
IsOpen="{Binding IsFunctionsPanelOpen, Mode=TwoWay}"
Placement="BottomEdgeAlignedLeft"
Opened="OnFunctionsFlyoutOpened"
Closed="OnFunctionsFlyoutClosed">
<Border x:Name="FunctionsPanelRoot" Padding="8" MinWidth="360" MaxHeight="420">
<ScrollViewer>
<StackPanel>
@ -156,7 +160,6 @@
<ListBox.ItemTemplate>
<DataTemplate x:DataType="m:FunctionDefinitionItem">
<Border Background="Transparent" Padding="8" MinHeight="36"
Tapped="OnFunctionsItemTapped"
AutomationProperties.Name="Insert function">
<Grid ColumnDefinitions="*,Auto">
<StackPanel>

View file

@ -21,6 +21,7 @@ public partial class MainView : UserControl
private MainViewModel? _vm;
private ListBox? _historyList;
private Border? _functionsPanelRoot;
private void OnAttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
{
@ -38,7 +39,7 @@ public partial class MainView : UserControl
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
}
// Functions list item taps are handled per-row via XAML (OnFunctionsItemTapped)
// Functions flyout wiring is done via Opened/Closed events
}
private void OnDetachedFromVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
@ -52,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;
}
@ -91,7 +98,20 @@ public partial class MainView : UserControl
if (sender is not IControl control)
return;
if (control.DataContext is not FunctionDefinitionItem item)
return;
{
// If attached to panel root, walk up from the source to a ListBoxItem and use its DataContext
if (sender is IVisual root && e.Source is IVisual src)
{
var lbi = src.FindAncestorOfType<ListBoxItem>();
if (lbi?.DataContext is not FunctionDefinitionItem rowItem)
return;
item = rowItem;
}
else
{
return;
}
}
var vm = DataContext as MainViewModel;
if (vm?.InsertFunctionCommand.CanExecute(item) == true)
@ -100,4 +120,27 @@ public partial class MainView : UserControl
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.FindDescendantOfType<Border>(b => b.Name == "FunctionsPanelRoot");
if (panel is null) return;
// Attach a single tap handler to the flyout content root
panel.AddHandler(InputElement.TappedEvent, OnFunctionsItemTapped,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
_functionsPanelRoot = panel;
}
private void OnFunctionsFlyoutClosed(object? sender, EventArgs e)
{
if (_functionsPanelRoot is not null)
{
_functionsPanelRoot.RemoveHandler(InputElement.TappedEvent, OnFunctionsItemTapped);
_functionsPanelRoot = null;
}
}
}