[nautilus/2101-tabs-are-missing-context-menu] window: Attach tab context menu to the tab



commit 3ac3b04ac11161770aa143b0ad1cb08d1a0ed282
Author: António Fernandes <antoniof gnome org>
Date:   Mon Jan 10 12:30:13 2022 +0000

    window: Attach tab context menu to the tab
    
    Due to GtkPopover now being a child of the window, instead of setting an
    attach widget we need to pass it the allocation of the tab, with the
    origin coordinates translated to the window coordinate space.
    
    Also use double instead of int when appropriate and replace GdkEvent
    usage with event controller method to get modifier status.
    
    Closes https://gitlab.gnome.org/GNOME/nautilus/-/issues/2101

 src/nautilus-notebook.c | 29 +++++++++++++++++------------
 src/nautilus-notebook.h |  6 +++---
 src/nautilus-window.c   | 26 +++++++++++++++-----------
 3 files changed, 35 insertions(+), 26 deletions(-)
---
diff --git a/src/nautilus-notebook.c b/src/nautilus-notebook.c
index 5767d22a9..504ea5caa 100644
--- a/src/nautilus-notebook.c
+++ b/src/nautilus-notebook.c
@@ -37,22 +37,25 @@
 
 #define AFTER_ALL_TABS -1
 
-static gint
-find_tab_num_at_pos (GtkNotebook *notebook,
-                     gint         abs_x,
-                     gint         abs_y)
+static GtkWidget *
+find_tab_at_pos (GtkNotebook *notebook,
+                 gdouble      abs_x,
+                 gdouble      abs_y,
+                 gint        *tab_num)
 {
     int page_num = 0;
     GtkWidget *page;
     GtkAllocation allocation;
 
+    *tab_num = AFTER_ALL_TABS;
+
     while ((page = gtk_notebook_get_nth_page (notebook, page_num)))
     {
         GtkWidget *tab;
         gdouble tab_x, tab_y;
 
         tab = gtk_notebook_get_tab_label (notebook, page);
-        g_return_val_if_fail (tab != NULL, -1);
+        g_return_val_if_fail (tab != NULL, NULL);
 
         if (!gtk_widget_get_mapped (GTK_WIDGET (tab)))
         {
@@ -67,12 +70,13 @@ find_tab_num_at_pos (GtkNotebook *notebook,
         if (tab_x >= allocation.x && tab_x <= allocation.x + allocation.width &&
             tab_y >= allocation.y && tab_y <= allocation.y + allocation.height)
         {
-            return page_num;
+            *tab_num = page_num;
+            return tab;
         }
 
         page_num++;
     }
-    return AFTER_ALL_TABS;
+    return NULL;
 }
 
 static void
@@ -132,21 +136,22 @@ nautilus_notebook_contains_slot (GtkNotebook        *notebook,
     return found;
 }
 
-gboolean
+GtkWidget *
 nautilus_notebook_get_tab_clicked (GtkNotebook *notebook,
-                                   gint         x,
-                                   gint         y,
+                                   gdouble      x,
+                                   gdouble      y,
                                    gint        *position)
 {
+    GtkWidget *tab;
     gint tab_num;
 
-    tab_num = find_tab_num_at_pos (notebook, x, y);
+    tab = find_tab_at_pos (notebook, x, y, &tab_num);
 
     if (position != NULL)
     {
         *position = tab_num;
     }
-    return tab_num != -1;
+    return tab;
 }
 
 void
diff --git a/src/nautilus-notebook.h b/src/nautilus-notebook.h
index bbc17b00d..7851ee7ae 100644
--- a/src/nautilus-notebook.h
+++ b/src/nautilus-notebook.h
@@ -51,9 +51,9 @@ void            nautilus_notebook_next_page (GtkNotebook *notebook);
 gboolean        nautilus_notebook_contains_slot (GtkNotebook   *notebook,
                                                  NautilusWindowSlot *slot);
 
-gboolean        nautilus_notebook_get_tab_clicked (GtkNotebook *notebook,
-                                                   gint         x,
-                                                   gint         y,
+GtkWidget *     nautilus_notebook_get_tab_clicked (GtkNotebook *notebook,
+                                                   gdouble      x,
+                                                   gdouble      y,
                                                    gint        *position);
 void            nautilus_notebook_setup           (GtkNotebook *notebook);
 G_END_DECLS
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index 7cc676ac5..9214a56ab 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -1756,12 +1756,18 @@ on_path_bar_open_location (NautilusWindow    *window,
 
 static void
 notebook_popup_menu_show (NautilusWindow *window,
-                          gdouble         x,
-                          gdouble         y)
+                          GtkWidget      *tab)
 {
     GtkPopover *popover = GTK_POPOVER (window->tab_menu);
-
-    gtk_popover_set_pointing_to (popover, &(GdkRectangle){x, y, 0, 0});
+    GtkAllocation allocation;
+    gdouble x, y;
+
+    gtk_widget_get_allocation (tab, &allocation);
+    gtk_widget_translate_coordinates (tab, GTK_WIDGET (window),
+                                      allocation.x, allocation.y, &x, &y);
+    allocation.x = x;
+    allocation.y = y;
+    gtk_popover_set_pointing_to (popover, (GdkRectangle *) &allocation);
     gtk_popover_popup (popover);
 }
 
@@ -1775,9 +1781,8 @@ notebook_button_press_cb (GtkGestureClick *gesture,
     NautilusWindow *window;
     GtkNotebook *notebook;
     gint tab_clicked;
+    GtkWidget *tab_widget;
     guint button;
-    GdkEventSequence *sequence;
-    GdkEvent *event;
     GdkModifierType state;
 
     if (n_press != 1)
@@ -1788,22 +1793,21 @@ notebook_button_press_cb (GtkGestureClick *gesture,
     window = NAUTILUS_WINDOW (user_data);
     notebook = GTK_NOTEBOOK (window->notebook);
 
-    if (!nautilus_notebook_get_tab_clicked (notebook, x, y, &tab_clicked))
+    tab_widget = nautilus_notebook_get_tab_clicked (notebook, x, y, &tab_clicked);
+    if (tab_widget == NULL)
     {
         return;
     }
 
     button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
-    sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
-    event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
-    state = gdk_event_get_modifier_state (event);
+    state = gtk_event_controller_get_current_event_state (GTK_EVENT_CONTROLLER (gesture));
 
     if (button == GDK_BUTTON_SECONDARY &&
         (state & gtk_accelerator_get_default_mod_mask ()) == 0)
     {
         /* switch to the page before opening the menu */
         gtk_notebook_set_current_page (notebook, tab_clicked);
-        notebook_popup_menu_show (window, x, y);
+        notebook_popup_menu_show (window, tab_widget);
     }
     else if (button == GDK_BUTTON_MIDDLE)
     {


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