[gnome-builder] tree: Handle popup menu shortcuts (Menu key and Shift+F10)



commit c71e0661d43e2fd6e4c1ec606291841a423e1bbe
Author: vanadiae <vanadiae35 gmail com>
Date:   Wed May 12 21:28:30 2021 +0200

    tree: Handle popup menu shortcuts (Menu key and Shift+F10)
    
    Currently it isn't possible to open the context menu of the project tree
    so it's hard to do anything useful with the project tree with keyboard
    only.
    
    So this commit now makes the IdeTree handle those keys, by connecting
    to the GtkWidget's popup-menu signal.

 src/libide/tree/ide-tree.c | 45 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 32 insertions(+), 13 deletions(-)
---
diff --git a/src/libide/tree/ide-tree.c b/src/libide/tree/ide-tree.c
index 41750cfde..8c2670235 100644
--- a/src/libide/tree/ide-tree.c
+++ b/src/libide/tree/ide-tree.c
@@ -401,22 +401,10 @@ ide_tree_popup (IdeTree        *self,
 
   g_assert (IDE_IS_TREE (self));
   g_assert (IDE_IS_TREE_NODE (node));
-
-  if (priv->context_menu == NULL)
-    return;
+  g_assert (priv->popover != NULL);
 
   dir = gtk_widget_get_direction (GTK_WIDGET (self));
 
-  if (priv->popover == NULL)
-    {
-      priv->popover = GTK_POPOVER (gtk_popover_new_from_model (GTK_WIDGET (self),
-                                                               G_MENU_MODEL (priv->context_menu)));
-      g_signal_connect (priv->popover,
-                        "destroy",
-                        G_CALLBACK (gtk_widget_destroyed),
-                        &priv->popover);
-    }
-
   gtk_popover_set_pointing_to (priv->popover, &area);
   gtk_popover_set_position (priv->popover, dir == GTK_TEXT_DIR_LTR ? GTK_POS_RIGHT : GTK_POS_LEFT);
 
@@ -545,6 +533,25 @@ ide_tree_query_tooltip (GtkWidget  *widget,
   return FALSE;
 }
 
+static gboolean
+ide_tree_popup_menu_cb (IdeTree *tree,
+                        gpointer user_data)
+{
+  IdeTreePrivate *priv = ide_tree_get_instance_private (tree);
+  IdeTreeNode *selected_node = NULL;
+
+  g_assert (priv != NULL);
+
+  selected_node = ide_tree_get_selected_node (tree);
+
+  if (selected_node)
+    {
+      ide_tree_show_popover_at_node (tree, selected_node, priv->popover);
+      return TRUE;
+    }
+  return FALSE;
+}
+
 static void
 ide_tree_destroy (GtkWidget *widget)
 {
@@ -606,6 +613,11 @@ ide_tree_init (IdeTree *self)
                            G_CALLBACK (ide_tree_selection_changed_cb),
                            self,
                            G_CONNECT_SWAPPED);
+  g_signal_connect_object (self,
+                           "popup-menu",
+                           G_CALLBACK (ide_tree_popup_menu_cb),
+                           NULL,
+                           0);
 
   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (self), FALSE);
   gtk_tree_view_set_activate_on_single_click (GTK_TREE_VIEW (self), TRUE);
@@ -658,6 +670,13 @@ ide_tree_set_context_menu (IdeTree *self,
     {
       if (priv->popover != NULL)
         gtk_widget_destroy (GTK_WIDGET (priv->popover));
+
+      priv->popover = GTK_POPOVER (gtk_popover_new_from_model (GTK_WIDGET (self),
+                                                               G_MENU_MODEL (priv->context_menu)));
+      g_signal_connect (priv->popover,
+                        "destroy",
+                        G_CALLBACK (gtk_widget_destroyed),
+                        &priv->popover);
     }
 
   g_return_if_fail (priv->popover == NULL);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]