[nautilus] pathbar: Align button menu with current mouse position



commit 7475e1e908cdf74ae3239ed51b0365a1497bb365
Author: Corey Berla <corey berla me>
Date:   Sat Jul 30 21:24:06 2022 +0000

    pathbar: Align button menu with current mouse position
    
    Right clicking parent folders in the path bar pops up the popover
    in the middle of the button regardless of where the mouse is.
    The popover becomes the child of the button creating the potential for
    becoming an orphan.  Use the mouse coordinates with
    gtk_popover_set_pointing_to() to set popover location.

 src/nautilus-pathbar.c | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)
---
diff --git a/src/nautilus-pathbar.c b/src/nautilus-pathbar.c
index d130d961e..9b53d4317 100644
--- a/src/nautilus-pathbar.c
+++ b/src/nautilus-pathbar.c
@@ -279,6 +279,8 @@ nautilus_path_bar_init (NautilusPathBar *self)
     self->button_menu = g_object_ref_sink (G_MENU (gtk_builder_get_object (builder, "button-menu")));
     self->button_menu_popover = gtk_popover_menu_new_from_model (G_MENU_MODEL (self->button_menu));
     gtk_widget_set_parent (self->button_menu_popover, GTK_WIDGET (self));
+    gtk_popover_set_has_arrow (GTK_POPOVER (self->button_menu_popover), FALSE);
+    gtk_widget_set_halign (self->button_menu_popover, GTK_ALIGN_START);
 
     /* Add current location menu, which matches the view's background context menu */
     gtk_builder_add_from_resource (builder,
@@ -392,15 +394,6 @@ get_dir_name (ButtonData *button_data)
 static void
 button_data_free (ButtonData *button_data)
 {
-    NautilusPathBar *self;
-    self = button_data->path_bar;
-    if (self != NULL && self->button_menu_popover != NULL)
-    {
-        g_object_ref (self->button_menu_popover);
-        gtk_widget_unparent (self->button_menu_popover);
-        gtk_widget_set_parent (self->button_menu_popover, GTK_WIDGET (self));
-        g_object_unref (self->button_menu_popover);
-    }
     g_object_unref (button_data->path);
     g_free (button_data->dir_name);
     if (button_data->file != NULL)
@@ -601,6 +594,7 @@ on_click_gesture_pressed (GtkGestureClick *gesture,
     NautilusPathBar *self;
     guint current_button;
     GdkModifierType state;
+    double x_in_pathbar, y_in_pathbar;
 
     if (n_press != 1)
     {
@@ -612,6 +606,11 @@ on_click_gesture_pressed (GtkGestureClick *gesture,
     current_button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
     state = gtk_event_controller_get_current_event_state (GTK_EVENT_CONTROLLER (gesture));
 
+    gtk_widget_translate_coordinates (GTK_WIDGET (button_data->button),
+                                      GTK_WIDGET (self),
+                                      x, y,
+                                      &x_in_pathbar, &y_in_pathbar);
+
     switch (current_button)
     {
         case GDK_BUTTON_MIDDLE:
@@ -633,15 +632,8 @@ on_click_gesture_pressed (GtkGestureClick *gesture,
             }
             else
             {
-                /* Hold a reference to keep the popover from destroying itself
-                 * when unparented. */
-                g_object_ref (self->button_menu_popover);
-                gtk_widget_unparent (self->button_menu_popover);
-                gtk_widget_set_parent (self->button_menu_popover,
-                                       button_data->button);
-                gtk_popover_present (GTK_POPOVER (self->button_menu_popover));
-                g_object_unref (self->button_menu_popover);
-
+                gtk_popover_set_pointing_to (GTK_POPOVER (self->button_menu_popover),
+                                             &(GdkRectangle){x_in_pathbar, y_in_pathbar, 0, 0});
                 pop_up_pathbar_context_menu (self, button_data->file);
             }
         }


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