[gnome-notes/136-implement-the-list-row-pattern-in-main-view: 17/17] main-view: fix window closing



commit 2eab590209cda0a7c93b543e42ef5003955ac424
Author: Isaque Galdino <igaldino gmail com>
Date:   Tue Jul 7 01:33:33 2020 -0300

    main-view: fix window closing
    
    After libgd removal, when closing a window, all application was closed.
    I found the code to plug signals were split between BjbWindowBase and
    BjbMainView and some signal connections were double disconnected.
    
    This code change this behaviour making BjbMainView responsible to handle
    all signal connection using g_signal_connect_object function and not
    double disconnecting signals anymore.

 src/bjb-main-view.c   | 124 ++++++++++++++------------------------------------
 src/bjb-main-view.h   |   4 --
 src/bjb-window-base.c |   3 --
 3 files changed, 33 insertions(+), 98 deletions(-)
---
diff --git a/src/bjb-main-view.c b/src/bjb-main-view.c
index 84fe134..ac456d4 100644
--- a/src/bjb-main-view.c
+++ b/src/bjb-main-view.c
@@ -69,12 +69,6 @@ struct _BjbMainView
   BjbListView         *view;
   BjbController       *controller;
   GtkWidget           *load_more;
-
-  /* Signals */
-  gulong               key;
-  gulong               activated;
-  gulong               data;
-  gulong               view_selection_changed;
 };
 
 G_DEFINE_TYPE (BjbMainView, bjb_main_view, GTK_TYPE_GRID)
@@ -86,50 +80,6 @@ bjb_main_view_init (BjbMainView *self)
 {
 }
 
-void
-bjb_main_view_disconnect_scrolled_window (BjbMainView *self)
-{
-  GtkAdjustment *vadjustment;
-  GtkWidget     *vscrollbar;
-
-  if (self->view == NULL || !GTK_IS_SCROLLED_WINDOW (self->view))
-    return;
-
-  vadjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (self->view));
-  vscrollbar = gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (self->view));
-
-  g_signal_handlers_disconnect_by_func (vadjustment, bjb_main_view_view_changed, self);
-  g_signal_handlers_disconnect_by_func (vscrollbar, bjb_main_view_view_changed, self);
-}
-
-static void
-bjb_main_view_disconnect_handlers (BjbMainView *self)
-{
-  GtkListBox *list_box = bjb_list_view_get_list_box (self->view);
-
-  if (self->key)
-    g_signal_handler_disconnect (self->window, self->key);
-  if (self->activated)
-    g_signal_handler_disconnect (list_box, self->activated);
-  if (self->data)
-    g_signal_handler_disconnect (self->view, self->data);
-  if (self->view_selection_changed)
-    g_signal_handler_disconnect (list_box, self->view_selection_changed);
-
-  self->key = 0;
-  self->activated = 0;
-  self->data = 0;
-  self->view_selection_changed =0;
-}
-
-static void
-bjb_main_view_dispose (GObject *object)
-{
-  bjb_main_view_disconnect_handlers (BJB_MAIN_VIEW (object));
-  bjb_main_view_disconnect_scrolled_window (BJB_MAIN_VIEW (object));
-  G_OBJECT_CLASS (bjb_main_view_parent_class)->dispose (object);
-}
-
 static void
 bjb_main_view_set_controller (BjbMainView   *self,
                               BjbController *controller)
@@ -187,7 +137,6 @@ void
 switch_to_note_view (BjbMainView *self,
                      BijiNoteObj *note)
 {
-  bjb_main_view_disconnect_handlers (self);
   bjb_window_base_switch_to_item (BJB_WINDOW_BASE (self->window),
                                   BIJI_ITEM (note));
 }
@@ -273,8 +222,7 @@ bjb_main_view_get_selected_items (BjbMainView *self)
 }
 
 static void
-on_selected_rows_changed_cb (GtkListBox  *box,
-                             BjbMainView *self)
+on_selected_rows_changed_cb (BjbMainView *self)
 {
   /* Workaround if items are selected
    * but selection mode not really active (?) */
@@ -293,12 +241,9 @@ on_selected_rows_changed_cb (GtkListBox  *box,
 
 /* Select all, escape */
 static gboolean
-on_key_press_event_cb (GtkWidget *widget,
-                       GdkEvent  *event,
-                       gpointer   user_data)
+on_key_press_event_cb (BjbMainView *self,
+                       GdkEvent    *event)
 {
-  BjbMainView *self = BJB_MAIN_VIEW (user_data);
-
   switch (event->key.keyval)
     {
       case GDK_KEY_a:
@@ -325,9 +270,8 @@ on_key_press_event_cb (GtkWidget *widget,
 }
 
 static void
-on_row_activated (GtkListBox     *view,
-                  BjbListViewRow *row,
-                  BjbMainView    *self)
+on_row_activated (BjbMainView    *self,
+                  BjbListViewRow *row)
 {
   BijiManager *manager;
   BijiItem    *to_open;
@@ -347,14 +291,13 @@ static GtkTargetEntry target_list[] = {
 };
 
 static void
-on_drag_data_received (GtkWidget        *widget,
+on_drag_data_received (BjbMainView      *self,
                        GdkDragContext   *context,
                        gint              x,
                        gint              y,
                        GtkSelectionData *data,
                        guint             info,
-                       guint             time,
-                       gpointer          user_data)
+                       guint             time)
 {
   gint length = gtk_selection_data_get_length (data);
 
@@ -366,7 +309,6 @@ on_drag_data_received (GtkWidget        *widget,
         {
           BijiManager *manager;
           BijiNoteObj *ret;
-          BjbMainView *self = BJB_MAIN_VIEW (user_data);
           BjbSettings *settings;
 
           /* FIXME Text is guchar utf 8, conversion to perform */
@@ -386,30 +328,34 @@ on_drag_data_received (GtkWidget        *widget,
   gtk_drag_finish (context, FALSE, FALSE, time);
 }
 
-void
+static void
 bjb_main_view_connect_signals (BjbMainView *self)
 {
   GtkListBox *list_box = bjb_list_view_get_list_box (self->view);
 
-  if (self->view_selection_changed == 0)
-    self->view_selection_changed = g_signal_connect (list_box,
-                                                     "selected-rows-changed",
-                                                     G_CALLBACK (on_selected_rows_changed_cb),
-                                                     self);
-
-  if (self->key == 0)
-    self->key = g_signal_connect (self->window, "key-press-event",
-                                  G_CALLBACK (on_key_press_event_cb), self);
-
-  if (self->activated == 0)
-    self->activated = g_signal_connect (list_box,
-                                        "row-activated",
-                                        G_CALLBACK (on_row_activated),
-                                        self);
-
-  if (self->data == 0)
-    self->data = g_signal_connect (self->view, "drag-data-received",
-                                   G_CALLBACK (on_drag_data_received), self);
+  g_signal_connect_object (list_box,
+                           "selected-rows-changed",
+                           G_CALLBACK (on_selected_rows_changed_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (list_box,
+                           "row-activated",
+                           G_CALLBACK (on_row_activated),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->view,
+                           "drag-data-received",
+                           G_CALLBACK (on_drag_data_received),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (self->window,
+                           "key-press-event",
+                           G_CALLBACK (on_key_press_event_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
 }
 
 static void
@@ -457,11 +403,10 @@ bjb_main_view_constructed (GObject *o)
   BjbMainView   *self;
   GtkAdjustment *vadjustment;
   GtkWidget     *vscrollbar;
-  GtkWidget     *button;
 
   G_OBJECT_CLASS (bjb_main_view_parent_class)->constructed (G_OBJECT (o));
 
-  self = BJB_MAIN_VIEW(o);
+  self = BJB_MAIN_VIEW (o);
 
   gtk_orientable_set_orientation (GTK_ORIENTABLE (self), GTK_ORIENTATION_VERTICAL);
   self->view = bjb_list_view_new ();
@@ -498,11 +443,9 @@ bjb_main_view_constructed (GObject *o)
 
   /* Load more */
   self->load_more = bjb_load_more_button_new (self->controller);
-  button = bjb_load_more_button_get_revealer (BJB_LOAD_MORE_BUTTON (self->load_more));
-  gtk_container_add (GTK_CONTAINER (self), button);
+  gtk_container_add (GTK_CONTAINER (self), self->load_more);
   bjb_main_view_view_changed (self);
 
-
   /* Selection Panel */
   self->select_bar = bjb_selection_toolbar_new (self->view, self);
   gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->select_bar));
@@ -520,7 +463,6 @@ bjb_main_view_class_init (BjbMainViewClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  object_class->dispose = bjb_main_view_dispose;
   object_class->get_property = bjb_main_view_get_property;
   object_class->set_property = bjb_main_view_set_property;
   object_class->constructed = bjb_main_view_constructed;
diff --git a/src/bjb-main-view.h b/src/bjb-main-view.h
index 00650b1..9c0283c 100644
--- a/src/bjb-main-view.h
+++ b/src/bjb-main-view.h
@@ -32,8 +32,6 @@ G_DECLARE_FINAL_TYPE (BjbMainView, bjb_main_view, BJB, MAIN_VIEW, GtkGrid)
 BjbMainView *bjb_main_view_new                        (GtkWidget     *win,
                                                        BjbController *controller);
 
-void         bjb_main_view_connect_signals            (BjbMainView   *self);
-
 GtkWidget   *bjb_main_view_get_window                 (BjbMainView   *view);
 
 void         action_new_window_callback               (GtkAction     *action,
@@ -61,7 +59,5 @@ gboolean     bjb_main_view_get_selection_mode         (BjbMainView   *view);
 void         bjb_main_view_set_selection_mode         (BjbMainView   *view,
                                                        gboolean       mode);
 
-void         bjb_main_view_disconnect_scrolled_window (BjbMainView   *self);
-
 G_END_DECLS
 
diff --git a/src/bjb-window-base.c b/src/bjb-window-base.c
index 3ad1682..e66a284 100644
--- a/src/bjb-window-base.c
+++ b/src/bjb-window-base.c
@@ -339,7 +339,6 @@ bjb_window_base_load_geometry (BjbWindowBase *self)
 static void
 bjb_window_base_destroy (gpointer a, BjbWindowBase * self)
 {
-  bjb_main_view_disconnect_scrolled_window (self->view);
   bjb_controller_disconnect (self->controller);
   bjb_window_base_save_geometry (self);
 }
@@ -584,14 +583,12 @@ bjb_window_base_switch_to (BjbWindowBase *self, BjbWindowViewType type)
 
     case BJB_WINDOW_BASE_MAIN_VIEW:
       bjb_search_toolbar_connect (self->search_bar);
-      bjb_main_view_connect_signals (self->view);
       gtk_widget_show (GTK_WIDGET (self->search_bar));
       gtk_stack_set_visible_child_name (self->stack, "main-view");
       break;
 
    case BJB_WINDOW_BASE_ARCHIVE_VIEW:
       bjb_search_toolbar_connect (self->search_bar);
-      bjb_main_view_connect_signals (self->view);
       gtk_widget_show (GTK_WIDGET (self->search_bar));
       gtk_stack_set_visible_child_name (self->stack, "main-view");
       break;


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