[nautilus/wip/antoniof/gtk4-preparation-popovers: 2/6] window: Handle tab clicks here




commit 3627ec2a57097c2f345991729f79e49358800dec
Author: António Fernandes <antoniof gnome org>
Date:   Sun Dec 19 14:40:07 2021 +0000

    window: Handle tab clicks here
    
    We have 2 gestures for handling clicks on the notebook: one in the
    notebook.c file, the other in window.c file.
    
    The notebook one needs to emit a signal which the window listens to
    anyway. So, it's simpler to handle everything in a single gesture
    in the window class.

 src/nautilus-notebook.c | 82 ++++++++-----------------------------------------
 src/nautilus-notebook.h |  7 +++--
 src/nautilus-window.c   | 37 ++++++++++++++++++++--
 3 files changed, 51 insertions(+), 75 deletions(-)
---
diff --git a/src/nautilus-notebook.c b/src/nautilus-notebook.c
index 755c8c7e8..3e12d6544 100644
--- a/src/nautilus-notebook.c
+++ b/src/nautilus-notebook.c
@@ -54,8 +54,6 @@ static guint signals[LAST_SIGNAL];
 struct _NautilusNotebook
 {
     GtkNotebook parent_instance;
-
-    GtkGesture *multi_press_gesture;
 };
 
 G_DEFINE_TYPE (NautilusNotebook, nautilus_notebook, GTK_TYPE_NOTEBOOK);
@@ -63,12 +61,6 @@ G_DEFINE_TYPE (NautilusNotebook, nautilus_notebook, GTK_TYPE_NOTEBOOK);
 static void
 nautilus_notebook_dispose (GObject *object)
 {
-    NautilusNotebook *notebook;
-
-    notebook = NAUTILUS_NOTEBOOK (object);
-
-    g_clear_object (&notebook->multi_press_gesture);
-
     G_OBJECT_CLASS (nautilus_notebook_parent_class)->dispose (object);
 }
 
@@ -133,55 +125,6 @@ find_tab_num_at_pos (NautilusNotebook *notebook,
     return AFTER_ALL_TABS;
 }
 
-static void
-button_press_cb (GtkGestureMultiPress *gesture,
-                 gint                  n_press,
-                 gdouble               x,
-                 gdouble               y,
-                 gpointer              user_data)
-{
-    guint button;
-    GdkEventSequence *sequence;
-    const GdkEvent *event;
-    GtkWidget *widget;
-    NautilusNotebook *notebook;
-    int tab_clicked;
-    GdkModifierType state;
-
-    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);
-    widget = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (gesture));
-    notebook = NAUTILUS_NOTEBOOK (widget);
-    tab_clicked = find_tab_num_at_pos (notebook, x, y);
-
-    gdk_event_get_state (event, &state);
-
-    if (n_press != 1)
-    {
-        return;
-    }
-
-    if (tab_clicked == -1)
-    {
-        return;
-    }
-
-    if (button == GDK_BUTTON_SECONDARY &&
-        (state & gtk_accelerator_get_default_mod_mask ()) == 0)
-    {
-        /* switch to the page the mouse is over, but don't consume the event */
-        gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), tab_clicked);
-    }
-    else if (button == GDK_BUTTON_MIDDLE)
-    {
-        GtkWidget *slot;
-
-        slot = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), tab_clicked);
-        g_signal_emit (notebook, signals[TAB_CLOSE_REQUEST], 0, slot);
-    }
-}
-
 static void
 on_page_removed (GtkNotebook *notebook,
                  GtkWidget   *child,
@@ -200,14 +143,6 @@ nautilus_notebook_init (NautilusNotebook *notebook)
     gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE);
 
     g_signal_connect (notebook, "page-removed", G_CALLBACK (on_page_removed), NULL);
-
-    notebook->multi_press_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (notebook));
-
-    gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (notebook->multi_press_gesture),
-                                                GTK_PHASE_CAPTURE);
-    gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (notebook->multi_press_gesture), 0);
-
-    g_signal_connect (notebook->multi_press_gesture, "pressed", G_CALLBACK (button_press_cb), NULL);
 }
 
 gboolean
@@ -236,11 +171,20 @@ nautilus_notebook_contains_slot (NautilusNotebook   *notebook,
 }
 
 gboolean
-nautilus_notebook_content_area_hit (NautilusNotebook *notebook,
-                                    gint              x,
-                                    gint              y)
+nautilus_notebook_get_tab_clicked (NautilusNotebook *notebook,
+                                   gint              x,
+                                   gint              y,
+                                   gint             *position)
 {
-    return find_tab_num_at_pos (notebook, x, y) == -1;
+    gint tab_num;
+
+    tab_num = find_tab_num_at_pos (notebook, x, y);
+
+    if (position != NULL)
+    {
+        *position = tab_num;
+    }
+    return tab_num != -1;
 }
 
 void
diff --git a/src/nautilus-notebook.h b/src/nautilus-notebook.h
index 1590e111b..ad227d183 100644
--- a/src/nautilus-notebook.h
+++ b/src/nautilus-notebook.h
@@ -54,8 +54,9 @@ void            nautilus_notebook_next_page (NautilusNotebook *notebook);
 gboolean        nautilus_notebook_contains_slot (NautilusNotebook   *notebook,
                                                  NautilusWindowSlot *slot);
 
-gboolean        nautilus_notebook_content_area_hit (NautilusNotebook *notebook,
-                                                    gint              x,
-                                                    gint              y);
+gboolean        nautilus_notebook_get_tab_clicked (NautilusNotebook *notebook,
+                                                   gint              x,
+                                                   gint              y,
+                                                   gint             *position);
 
 G_END_DECLS
diff --git a/src/nautilus-window.c b/src/nautilus-window.c
index 4432f66f6..508a015be 100644
--- a/src/nautilus-window.c
+++ b/src/nautilus-window.c
@@ -1778,15 +1778,46 @@ notebook_button_press_cb (GtkGestureMultiPress *gesture,
                           gpointer              user_data)
 {
     NautilusWindow *window;
+    GtkNotebook *notebook;
+    gint tab_clicked;
+    guint button;
+    GdkEventSequence *sequence;
+    const GdkEvent *event;
+    GdkModifierType state;
+
+    if (n_press != 1)
+    {
+        return;
+    }
 
     window = NAUTILUS_WINDOW (user_data);
+    notebook = GTK_NOTEBOOK (window->notebook);
 
-    if (nautilus_notebook_content_area_hit (NAUTILUS_NOTEBOOK (window->notebook), x, y))
+    if (n_press != 1 ||
+        !nautilus_notebook_get_tab_clicked (NAUTILUS_NOTEBOOK (window->notebook), x, y, &tab_clicked))
     {
         return;
     }
 
-    notebook_popup_menu_show (window, x, y);
+    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);
+    gdk_event_get_state (event, &state);
+
+    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);
+    }
+    else if (button == GDK_BUTTON_MIDDLE)
+    {
+        GtkWidget *slot;
+
+        slot = gtk_notebook_get_nth_page (notebook, tab_clicked);
+        nautilus_window_slot_close (window, NAUTILUS_WINDOW_SLOT (slot));
+    }
 }
 
 static gboolean
@@ -2668,7 +2699,7 @@ nautilus_window_init (NautilusWindow *window)
     gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (window->notebook_multi_press_gesture),
                                                 GTK_PHASE_CAPTURE);
     gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (window->notebook_multi_press_gesture),
-                                   GDK_BUTTON_SECONDARY);
+                                   0);
 }
 
 static void


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