[nautilus/gtk4-preparation-trunk: 33/64] dnd: Stop using GtkMenu




commit 66f89a9f7d1afabfdabdad98dec2978ce7bad365
Author: António Fernandes <antoniof gnome org>
Date:   Wed Dec 22 09:53:17 2021 +0000

    dnd: Stop using GtkMenu
    
    This is the last usage of GtkMenu remaining in our own codebase.
    
    DND is due a reimplementation in GTK 4 and this particular menu (the
    one which asks the drop action if you hold Alt while dropping) uses
    a nested main loop and grabs.
    
    So, there is no need to aim for perfect here. Just reimplement it
    the lazy way as a GtkPopover with buttons and call it a day.

 src/nautilus-dnd.c | 49 +++++++++++++++++++++++++++++++++----------------
 1 file changed, 33 insertions(+), 16 deletions(-)
---
diff --git a/src/nautilus-dnd.c b/src/nautilus-dnd.c
index abef9b002..dac788f5c 100644
--- a/src/nautilus-dnd.c
+++ b/src/nautilus-dnd.c
@@ -25,6 +25,7 @@
 #include "nautilus-dnd.h"
 
 #include "nautilus-program-choosing.h"
+#include "nautilus-gtk4-helpers.h"
 #include <eel/eel-glib-extensions.h>
 #include <eel/eel-gtk-extensions.h>
 #include <eel/eel-string.h>
@@ -757,15 +758,17 @@ append_drop_action_menu_item (GtkWidget          *menu,
 {
     GtkWidget *menu_item;
 
-    menu_item = gtk_menu_item_new_with_mnemonic (text);
+    menu_item = gtk_button_new_with_mnemonic (text);
     gtk_widget_set_sensitive (menu_item, sensitive);
-    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+    gtk_box_append (GTK_BOX (menu), menu_item);
+
+    gtk_style_context_add_class (gtk_widget_get_style_context (menu_item), "flat");
 
     g_object_set_data (G_OBJECT (menu_item),
                        "action",
                        GINT_TO_POINTER (action));
 
-    g_signal_connect (menu_item, "activate",
+    g_signal_connect (menu_item, "clicked",
                       G_CALLBACK (drop_action_activated_callback),
                       damd);
 
@@ -777,6 +780,7 @@ GdkDragAction
 nautilus_drag_drop_action_ask (GtkWidget     *widget,
                                GdkDragAction  actions)
 {
+    GtkWidget *popover;
     GtkWidget *menu;
     GtkWidget *menu_item;
     DropActionMenuData damd;
@@ -784,8 +788,16 @@ nautilus_drag_drop_action_ask (GtkWidget     *widget,
     /* Create the menu and set the sensitivity of the items based on the
      * allowed actions.
      */
-    menu = gtk_menu_new ();
-    gtk_menu_set_screen (GTK_MENU (menu), gtk_widget_get_screen (widget));
+    popover = gtk_popover_new (widget);
+    gtk_popover_set_position (GTK_POPOVER (popover), GTK_POS_TOP);
+
+    menu = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+    gtk_widget_set_margin_top (menu, 6);
+    gtk_widget_set_margin_bottom (menu, 6);
+    gtk_widget_set_margin_start (menu, 6);
+    gtk_widget_set_margin_end (menu, 6);
+    gtk_popover_set_child (GTK_POPOVER (popover), menu);
+    gtk_widget_show (menu);
 
     append_drop_action_menu_item (menu, _("_Move Here"),
                                   GDK_ACTION_MOVE,
@@ -802,32 +814,37 @@ nautilus_drag_drop_action_ask (GtkWidget     *widget,
                                   (actions & GDK_ACTION_LINK) != 0,
                                   &damd);
 
-    eel_gtk_menu_append_separator (GTK_MENU (menu));
-
-    menu_item = gtk_menu_item_new_with_mnemonic (_("Cancel"));
-    gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+    menu_item = gtk_separator_new (GTK_ORIENTATION_VERTICAL);
+    gtk_box_append (GTK_BOX (menu), menu_item);
     gtk_widget_show (menu_item);
 
+    append_drop_action_menu_item (menu, _("Cancel"), 0, TRUE, &damd);
+
     damd.chosen = 0;
     damd.loop = g_main_loop_new (NULL, FALSE);
 
-    g_signal_connect (menu, "deactivate",
+    g_signal_connect (popover, "closed",
                       G_CALLBACK (menu_deactivate_callback),
                       &damd);
 
-    gtk_grab_add (menu);
+    gtk_grab_add (popover);
+
+    /* We don't have pointer coords here. Just pick the center of the widget. */
+    gtk_popover_set_pointing_to (GTK_POPOVER (popover),
+                                 &(GdkRectangle){ .x = 0.5 * gtk_widget_get_allocated_width (widget),
+                                                  .y = 0.5 * gtk_widget_get_allocated_height (widget),
+                                                  .width = 0, .height = 0 });
 
-    gtk_menu_popup_at_pointer (GTK_MENU (menu),
-                               NULL);
+    gtk_popover_popup (GTK_POPOVER (popover));
 
     g_main_loop_run (damd.loop);
 
-    gtk_grab_remove (menu);
+    gtk_grab_remove (popover);
 
     g_main_loop_unref (damd.loop);
 
-    g_object_ref_sink (menu);
-    g_object_unref (menu);
+    g_object_ref_sink (popover);
+    g_object_unref (popover);
 
     return damd.chosen;
 }


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