[nautilus/wip/antoniof/gtk4-preparation-popovers: 2/14] files-view: Use popovers for context menus




commit b1f976724b4e365e3d6b61a330252a687f673900
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-canvas-view.c                         |  8 +--
 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 +-
 6 files changed, 46 insertions(+), 65 deletions(-)
---
diff --git a/src/nautilus-canvas-view.c b/src/nautilus-canvas-view.c
index ce162d414..2cd356ad4 100644
--- a/src/nautilus-canvas-view.c
+++ b/src/nautilus-canvas-view.c
@@ -1179,7 +1179,7 @@ canvas_container_context_click_selection_callback (NautilusCanvasContainer *cont
     g_assert (NAUTILUS_IS_CANVAS_VIEW (canvas_view));
 
     nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (canvas_view),
-                                                       event);
+                                                       (&event->button)->x, (&event->button)->y);
 }
 
 static void
@@ -1191,7 +1191,7 @@ canvas_container_context_click_background_callback (NautilusCanvasContainer *con
     g_assert (NAUTILUS_IS_CANVAS_VIEW (canvas_view));
 
     nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (canvas_view),
-                                                        event);
+                                                        (&event->button)->x, (&event->button)->y);
 }
 
 static char *
@@ -1354,12 +1354,12 @@ canvas_container_longpress_gesture_pressed_callback (GtkGestureLongPress *gestur
     if (nautilus_view_get_selection (NAUTILUS_VIEW (view)))
     {
         nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (view),
-                                                           event);
+                                                           x, y);
     }
     else
     {
         nautilus_files_view_pop_up_background_context_menu (NAUTILUS_FILES_VIEW (view),
-                                                            event);
+                                                            x, y);
     }
 }
 
diff --git a/src/nautilus-files-view.c b/src/nautilus-files-view.c
index 5a0aa7fb0..1e160dfda 100644
--- a/src/nautilus-files-view.c
+++ b/src/nautilus-files-view.c
@@ -8373,7 +8373,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;
 
@@ -8388,37 +8389,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));
 }
 
 /**
@@ -8430,7 +8422,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;
 
@@ -8445,30 +8438,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
@@ -8480,11 +8457,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 f1b31d464..2443f7e12 100644
--- a/src/nautilus-files-view.h
+++ b/src/nautilus-files-view.h
@@ -275,9 +275,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 1df7223b0..491d38cc4 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -601,7 +601,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;
@@ -831,7 +831,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),
@@ -915,7 +915,7 @@ key_press_callback (GtkWidget *widget,
     {
         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;
         }
@@ -2071,11 +2071,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);
     }
 }
 
@@ -3986,6 +3986,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 5e528e032..88700641b 100644
--- a/src/nautilus-view-icon-controller.c
+++ b/src/nautilus-view-icon-controller.c
@@ -736,7 +736,7 @@ on_button_press_event (GtkGestureMultiPress *gesture,
         if (button == GDK_BUTTON_SECONDARY)
         {
             nautilus_files_view_pop_up_selection_context_menu (NAUTILUS_FILES_VIEW (self),
-                                                               event);
+                                                               x, y);
         }
     }
     else
@@ -745,7 +745,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);
         }
     }
 }
@@ -793,13 +793,13 @@ on_longpress_gesture_pressed_callback (GtkGestureLongPress *gesture,
         nautilus_view_set_selection (NAUTILUS_VIEW (self), selection);
 
         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);
     }
 
     g_list_foreach (selection, (GFunc) g_object_unref, NULL);
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]