Browse Source

Open node picker with space

Krzysztof Krysiński 2 months ago
parent
commit
d6b5649825

+ 2 - 2
src/PixiEditor/Styles/Templates/NodeGraphView.axaml

@@ -6,13 +6,13 @@
         <Setter Property="ZoomMode" Value="Move" />
         <Setter Property="Template">
             <ControlTemplate>
-                <Grid Background="Transparent">
+                <Grid Focusable="True" Background="Transparent" Name="PART_RootPanel">
                     <Rectangle Name="PART_SelectionRectangle" HorizontalAlignment="Left"
                                VerticalAlignment="Top"
                                IsVisible="False" ZIndex="100"
                                Fill="{DynamicResource SelectionFillBrush}" Opacity="1" />
                     <Grid.ContextFlyout>
-                        <Flyout Placement="Pointer" IsOpen="{Binding IsMenuOpen, RelativeSource={RelativeSource FindAncestor, AncestorType=nodes:NodeGraphView}, Mode=TwoWay}">
+                        <Flyout Placement="Pointer">
                             <nodes:NodePicker
                                 AllNodeTypeInfos="{Binding AllNodeTypeInfos, RelativeSource={RelativeSource TemplatedParent}}"
                                 SearchQuery="{Binding SearchQuery, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"

+ 18 - 8
src/PixiEditor/Views/Nodes/NodeGraphView.cs

@@ -10,6 +10,7 @@ using Avalonia.Controls.Presenters;
 using Avalonia.Controls.Primitives;
 using Avalonia.Controls.Shapes;
 using Avalonia.Input;
+using Avalonia.Interactivity;
 using Avalonia.Media;
 using Avalonia.Threading;
 using Avalonia.VisualTree;
@@ -182,12 +183,6 @@ internal class NodeGraphView : Zoombox.Zoombox
         set { SetValue(ActiveFrameProperty, value); }
     }
 
-    public bool IsMenuOpen
-    {
-        get { return (bool)GetValue(IsMenuOpenProperty); }
-        set { SetValue(IsMenuOpenProperty, value); }
-    }
-
     private bool isDraggingNodes;
     private bool isDraggingConnection;
     private VecD clickPointOffset;
@@ -214,11 +209,12 @@ internal class NodeGraphView : Zoombox.Zoombox
     private List<Control> nodeViewsCache = new();
 
     private bool isSelecting;
-    public static readonly StyledProperty<bool> IsMenuOpenProperty = AvaloniaProperty.Register<NodeGraphView, bool>("IsMenuOpen");
 
     public static readonly StyledProperty<int> ActiveFrameProperty =
         AvaloniaProperty.Register<NodeGraphView, int>("ActiveFrame");
 
+    private Panel rootPanel;
+
     public NodeGraphView()
     {
         SelectNodeCommand = new RelayCommand<PointerPressedEventArgs>(SelectNode);
@@ -239,6 +235,8 @@ internal class NodeGraphView : Zoombox.Zoombox
         connectionItemsControl = e.NameScope.Find<ItemsControl>("PART_Connections");
         selectionRectangle = e.NameScope.Find<Rectangle>("PART_SelectionRectangle");
 
+        rootPanel = e.NameScope.Find<Panel>("PART_RootPanel");
+
         Dispatcher.UIThread.Post(() =>
         {
             nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged += NodeItems_CollectionChanged;
@@ -247,6 +245,17 @@ internal class NodeGraphView : Zoombox.Zoombox
         });
     }
 
+    protected override void OnLoaded(RoutedEventArgs e)
+    {
+        base.OnLoaded(e);
+
+        Dispatcher.UIThread.Post(
+            () =>
+            {
+                rootPanel.Focus(NavigationMethod.Pointer);
+            }, DispatcherPriority.Input);
+    }
+
     protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
     {
         nodeItemsControl.ItemsPanelRoot.Children.CollectionChanged -= NodeItems_CollectionChanged;
@@ -267,7 +276,8 @@ internal class NodeGraphView : Zoombox.Zoombox
         base.OnKeyDown(e);
         if (e.Key == Key.Space)
         {
-            IsMenuOpen = true;
+            rootPanel.ContextFlyout?.ShowAt(rootPanel);
+            e.Handled = true;
         }
     }