Got function insertion working

This commit is contained in:
Jordan Wages 2025-08-28 04:54:41 -05:00
commit c2bb03044b
2 changed files with 13 additions and 32 deletions

View file

@ -160,7 +160,8 @@
<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"
AutomationProperties.Name="Insert function"> AutomationProperties.Name="Insert function"
Tapped="OnFunctionsItemTapped">
<Grid ColumnDefinitions="*,Auto"> <Grid ColumnDefinitions="*,Auto">
<StackPanel> <StackPanel>
<TextBlock Text="{Binding FunctionName}" FontWeight="Bold"/> <TextBlock Text="{Binding FunctionName}" FontWeight="Bold"/>

View file

@ -95,45 +95,25 @@ public partial class MainView : UserControl
private void OnFunctionsItemTapped(object? sender, TappedEventArgs e) private void OnFunctionsItemTapped(object? sender, TappedEventArgs e)
{ {
if (sender is not Control control) var lbi = (e.Source as Control)?.FindAncestorOfType<ListBoxItem>();
return; if (lbi?.DataContext is FunctionDefinitionItem item &&
if (control.DataContext is not FunctionDefinitionItem item) _vm?.InsertFunctionCommand.CanExecute(item) == true)
{ {
// If attached to panel root, walk up from the source to a ListBoxItem and use its DataContext _vm.InsertFunctionCommand.Execute(item);
if (sender is ContentPresenter root && e.Source is ContentPresenter 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)
{
vm.InsertFunctionCommand.Execute(item);
e.Handled = true; 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<Border>("FunctionsPanelRoot");
if (panel is null) return;
// Attach a single tap handler to the flyout content root private void OnFunctionsFlyoutOpened(object? sender, EventArgs e)
panel.AddHandler(InputElement.TappedEvent, OnFunctionsItemTapped, {
if (sender is Flyout flyout && sender is ContentPresenter presenter)
{
_functionsPanelRoot = presenter.FindControl<Border>("FunctionsPanelRoot");
_functionsPanelRoot?.AddHandler(InputElement.TappedEvent, OnFunctionsItemTapped,
RoutingStrategies.Tunnel | RoutingStrategies.Bubble); RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
_functionsPanelRoot = panel;
} }
}
private void OnFunctionsFlyoutClosed(object? sender, EventArgs e) private void OnFunctionsFlyoutClosed(object? sender, EventArgs e)
{ {