[nautilus/gtk4-preparation-trunk: 16/50] files-view: Use popovers for context menus




commit 454aca726132c0c6d5ba9404b95060b19569c9b3
Author: António Fernandes <antoniof gnome org>
Date:   Fri Dec 17 11:43:35 2021 +0000

    files-view: Use popovers for context menus
    
    GtkMenu is gone in GTK4. Prepare for the transition using GtkPopover.
    
    This is going to need design review after the GTK 4 switch, but it's
    good enough for the purpose of simplifying the switch.
    
    Closes https://gitlab.gnome.org/GNOME/nautilus/-/issues/738

 src/nautilus-files-view.c                          | 75 ++++++++--------------
 src/nautilus-files-view.h                          |  6 +-
 src/nautilus-list-view.c                           | 11 ++--
 src/nautilus-view-icon-controller.c                |  8 +--
 .../ui/nautilus-files-view-context-menus.ui        |  3 +-
 5 files changed, 42 insertions(+), 61 deletions(-)
---
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 576a7bf84..5d4dd577f 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -8369,7 +8369,8 @@ nautilus_files_view_reveal_for_selection_context_menu (NautilusFilesView *view)
  **/
 void
 nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
-                                                    const GdkEvent    *event)
+                                                    gdouble            x,
+                                                    gdouble            y)
 {
     NautilusFilesViewPrivate *priv;
 
@@ -8384,37 +8385,28 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
 
     if (NULL == priv->selection_menu)
     {
-        priv->selection_menu = gtk_menu_new ();
-
-        gtk_menu_attach_to_widget (GTK_MENU (priv->selection_menu),
-                                   GTK_WIDGET (view),
-                                   NULL);
+        priv->selection_menu = gtk_popover_new (GTK_WIDGET (view));
     }
 
-    gtk_menu_shell_bind_model (GTK_MENU_SHELL (priv->selection_menu),
-                               G_MENU_MODEL (priv->selection_menu_model),
-                               NULL,
-                               TRUE);
-
-    if (event != NULL)
-    {
-        gtk_menu_popup_at_pointer (GTK_MENU (priv->selection_menu), event);
-    }
-    else
+    gtk_popover_bind_model (GTK_POPOVER (priv->selection_menu),
+                            G_MENU_MODEL (priv->selection_menu_model),
+                            NULL);
+    if (x == -1 && y == -1)
     {
         /* If triggered from the keyboard, popup at selection, not pointer */
         g_autofree GdkRectangle *rectangle = NULL;
 
         rectangle = nautilus_files_view_reveal_for_selection_context_menu (view);
         g_return_if_fail (rectangle != NULL);
-
-        gtk_menu_popup_at_rect (GTK_MENU (priv->selection_menu),
-                                gtk_widget_get_window (GTK_WIDGET (view)),
-                                rectangle,
-                                GDK_GRAVITY_SOUTH_WEST,
-                                GDK_GRAVITY_NORTH_WEST,
-                                NULL);
+        gtk_popover_set_pointing_to (GTK_POPOVER (priv->selection_menu),
+                                     rectangle);
+    }
+    else
+    {
+        gtk_popover_set_pointing_to (GTK_POPOVER (priv->selection_menu),
+                                     &(GdkRectangle){x, y, 0, 0});
     }
+    gtk_popover_popup (GTK_POPOVER (priv->selection_menu));
 }
 
 /**
@@ -8426,7 +8418,8 @@ nautilus_files_view_pop_up_selection_context_menu  (NautilusFilesView *view,
  **/
 void
 nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
-                                                    const GdkEvent    *event)
+                                                    gdouble            x,
+                                                    gdouble            y)
 {
     NautilusFilesViewPrivate *priv;
 
@@ -8441,30 +8434,14 @@ nautilus_files_view_pop_up_background_context_menu (NautilusFilesView *view,
 
     if (NULL == priv->background_menu)
     {
-        priv->background_menu = gtk_menu_new ();
-
-        gtk_menu_attach_to_widget (GTK_MENU (priv->background_menu),
-                                   GTK_WIDGET (view),
-                                   NULL);
-    }
-    gtk_menu_shell_bind_model (GTK_MENU_SHELL (priv->background_menu),
-                               G_MENU_MODEL (priv->background_menu_model),
-                               NULL,
-                               TRUE);
-    if (event != NULL)
-    {
-        gtk_menu_popup_at_pointer (GTK_MENU (priv->background_menu), event);
-    }
-    else
-    {
-        /* It was triggered from the keyboard, so pop up from the center of view.
-         */
-        gtk_menu_popup_at_widget (GTK_MENU (priv->background_menu),
-                                  GTK_WIDGET (view),
-                                  GDK_GRAVITY_CENTER,
-                                  GDK_GRAVITY_CENTER,
-                                  NULL);
+        priv->background_menu = gtk_popover_new (GTK_WIDGET (view));
     }
+    gtk_popover_bind_model (GTK_POPOVER (priv->background_menu),
+                            G_MENU_MODEL (priv->background_menu_model),
+                            NULL);
+    gtk_popover_set_pointing_to (GTK_POPOVER (priv->background_menu),
+                                 &(GdkRectangle){x, y, 0, 0});
+    gtk_popover_popup (GTK_POPOVER (priv->background_menu));
 }
 
 static gboolean
@@ -8476,11 +8453,11 @@ popup_menu_callback (NautilusFilesView *view)
 
     if (selection != NULL)
     {
-        nautilus_files_view_pop_up_selection_context_menu (view, NULL);
+        nautilus_files_view_pop_up_selection_context_menu (view, -1, -1);
     }
     else
     {
-        nautilus_files_view_pop_up_background_context_menu (view, NULL);
+        nautilus_files_view_pop_up_background_context_menu (view, 0, 0);
     }
 
     return TRUE;
diff --git a/src/nautilus-files-view.h b/src/nautilus-files-view.h
index 32ac9937c..9bbd61962 100644
--- a/src/nautilus-files-view.h
+++ b/src/nautilus-files-view.h
@@ -263,9 +263,11 @@ void                nautilus_files_view_notify_selection_changed         (Nautil
 NautilusDirectory  *nautilus_files_view_get_model                        (NautilusFilesView *view);
 NautilusFile       *nautilus_files_view_get_directory_as_file            (NautilusFilesView *view);
 void                nautilus_files_view_pop_up_background_context_menu   (NautilusFilesView *view,
-                                                                          const GdkEvent    *event);
+                                                                          gdouble            x,
+                                                                          gdouble            y);
 void                nautilus_files_view_pop_up_selection_context_menu    (NautilusFilesView *view,
-                                                                          const GdkEvent    *event);
+                                                                          gdouble            x,
+                                                                          gdouble            y);
 gboolean            nautilus_files_view_should_show_file                 (NautilusFilesView *view,
                                                                           NautilusFile      *file);
 gboolean            nautilus_files_view_should_sort_directories_first    (NautilusFilesView *view);
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 242a45490..9e5917853 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -531,7 +531,7 @@ on_tree_view_multi_press_gesture_pressed (GtkGestureMultiPress *gesture,
         if (button == GDK_BUTTON_SECONDARY)
         {
             nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (view),
-                                                                event);
+                                                                x, y);
         }
 
         return;
@@ -691,7 +691,7 @@ on_tree_view_multi_press_gesture_pressed (GtkGestureMultiPress *gesture,
         if (button == GDK_BUTTON_SECONDARY)
         {
             nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (view),
-                                                               event);
+                                                               x, y);
         }
 
         /* Don't open a new tab if we are in single click mode (this would open 2 tabs),
@@ -824,7 +824,7 @@ on_event_controller_key_key_pressed (GtkEventControllerKey *controller,
     {
         if ((state & GDK_CONTROL_MASK) != 0)
         {
-            nautilus_files_view_pop_up_background_context_menu (view, NULL);
+            nautilus_files_view_pop_up_background_context_menu (view, 0, 0);
 
             return GDK_EVENT_STOP;
         }
@@ -1928,11 +1928,11 @@ on_longpress_gesture_pressed_event (GtkGestureLongPress *gesture,
     selection = nautilus_view_get_selection (NAUTILUS_VIEW (view));
     if (selection != NULL)
     {
-        nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (view), event);
+        nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (view), x, y);
     }
     else
     {
-        nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (view), event);
+        nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (view), x, y);
     }
 }
 
@@ -3858,6 +3858,7 @@ nautilus_list_view_reveal_for_selection_context_menu (NautilusFilesView *view)
     gtk_tree_view_scroll_to_cell (tree_view, path, NULL, FALSE, 0.0, 0.0);
 
     rect = get_rectangle_for_path (list_view, path);
+    rect->width = rect->height;
 
     gtk_tree_path_free (path);
 
diff --git a/src/nautilus-view-icon-controller.c b/src/nautilus-view-icon-controller.c
index dc5051b3e..668c28d1c 100644
--- a/src/nautilus-view-icon-controller.c
+++ b/src/nautilus-view-icon-controller.c
@@ -844,7 +844,7 @@ on_button_press_event (GtkGestureMultiPress *gesture,
         else if (button == GDK_BUTTON_SECONDARY)
         {
             nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (self),
-                                                               event);
+                                                               x, y);
         }
     }
     else
@@ -853,7 +853,7 @@ on_button_press_event (GtkGestureMultiPress *gesture,
         if (button == GDK_BUTTON_SECONDARY)
         {
             nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (self),
-                                                                event);
+                                                                x, y);
         }
     }
 }
@@ -909,13 +909,13 @@ on_longpress_gesture_pressed_callback (GtkGestureLongPress *gesture,
     if (child_at_pos != NULL)
     {
         nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (self),
-                                                           event);
+                                                           x, y);
     }
     else
     {
         nautilus_view_set_selection (NAUTILUS_VIEW (self), NULL);
         nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (self),
-                                                            event);
+                                                            x, y);
     }
 }
 
diff --git a/src/resources/ui/nautilus-files-view-context-menus.ui 
b/src/resources/ui/nautilus-files-view-context-menus.ui
index ac29451a7..a951549ab 100644
--- a/src/resources/ui/nautilus-files-view-context-menus.ui
+++ b/src/resources/ui/nautilus-files-view-context-menus.ui
@@ -121,6 +121,7 @@
       </item>
     </section>
     <section>
+      <attribute name="display-hint">horizontal-buttons</attribute>
       <item>
         <attribute name="label" translatable="yes">Cu_t</attribute>
         <attribute name="action">view.cut</attribute>
@@ -129,13 +130,13 @@
         <attribute name="label" translatable="yes">_Copy</attribute>
         <attribute name="action">view.copy</attribute>
       </item>
+    </section>
     <section>
       <item>
         <attribute name="label" translatable="yes">Create _Link</attribute>
         <attribute name="action">view.create-link-in-place</attribute>
         <attribute name="hidden-when">action-disabled</attribute>
       </item>
-    </section>
       <item>
         <attribute name="label" translatable="yes">_Paste Into Folder</attribute>
         <attribute name="action">view.paste-into</attribute>


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