[gnome-photos] Consolidate code to update MainToolbar on ItemManager::items-changed



commit 93fe82752cef4e26607c65d3c9d646273944ed07
Author: Debarshi Ray <debarshir gnome org>
Date:   Sat Aug 26 00:17:47 2017 +0200

    Consolidate code to update MainToolbar on ItemManager::items-changed
    
    Most GActions are enabled/disabled in photos_application_actions_update
    when there is a state change. Now that the selection mode is backed by
    a GAction, there is no reason to directly enable/disable the GtkButton
    when the number of BaseItems change.
    
    This also disables all the other app.search-* and app.selection-*
    GActions when there are no BaseItems in the current mode.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786936

 src/photos-application.c  |   35 ++++++++++++++++++++++++++---
 src/photos-main-toolbar.c |   53 +++-----------------------------------------
 2 files changed, 35 insertions(+), 53 deletions(-)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index 7464a66..cbedea0 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -356,10 +356,20 @@ photos_application_actions_update (PhotosApplication *self)
   g_simple_action_set_enabled (self->saturation_action, enable);
   g_simple_action_set_enabled (self->sharpen_action, enable);
 
-  enable = (mode == PHOTOS_WINDOW_MODE_COLLECTIONS
-            || mode == PHOTOS_WINDOW_MODE_FAVORITES
-            || mode == PHOTOS_WINDOW_MODE_OVERVIEW
-            || mode == PHOTOS_WINDOW_MODE_SEARCH);
+  enable = FALSE;
+  if (mode == PHOTOS_WINDOW_MODE_COLLECTIONS
+      || mode == PHOTOS_WINDOW_MODE_FAVORITES
+      || mode == PHOTOS_WINDOW_MODE_OVERVIEW
+      || mode == PHOTOS_WINDOW_MODE_SEARCH)
+    {
+      PhotosBaseManager *item_mngr_chld;
+      guint n_items;
+
+      item_mngr_chld = photos_item_manager_get_for_mode (PHOTOS_ITEM_MANAGER (self->state->item_mngr), mode);
+      n_items = g_list_model_get_n_items (G_LIST_MODEL (item_mngr_chld));
+      enable = n_items > 0;
+    }
+
   g_simple_action_set_enabled (self->search_action, enable);
   g_simple_action_set_enabled (self->search_match_action, enable);
   g_simple_action_set_enabled (self->search_source_action, enable);
@@ -399,7 +409,13 @@ photos_application_actions_update (PhotosApplication *self)
       PhotosBaseItem *selected_item;
       const gchar *urn = (gchar *) l->data;
 
+      /* When PhotosItemManager::items-changed is emitted, a selected
+       * item can potentially be removed from the item manager.
+       */
       selected_item = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr, urn));
+      if (selected_item == NULL)
+        continue;
+
       can_trash = can_trash && photos_base_item_can_trash (selected_item);
 
       if (photos_base_item_get_default_app_name (selected_item) != NULL)
@@ -916,6 +932,13 @@ photos_application_get_state (PhotosSearchContext *context)
 
 
 static void
+photos_application_items_changed (PhotosApplication *self)
+{
+  photos_application_actions_update (self);
+}
+
+
+static void
 photos_application_launch_search (PhotosApplication *self, const gchar* const *terms, guint timestamp)
 {
   GVariant *state;
@@ -1998,6 +2021,10 @@ photos_application_startup (GApplication *application)
   gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.zoom-out(-1.0)", zoom_out_accels);
 
   g_signal_connect_swapped (self->state->item_mngr,
+                            "items-changed",
+                            G_CALLBACK (photos_application_items_changed),
+                            self);
+  g_signal_connect_swapped (self->state->item_mngr,
                             "load-finished",
                             G_CALLBACK (photos_application_load_changed),
                             self);
diff --git a/src/photos-main-toolbar.c b/src/photos-main-toolbar.c
index 3d92408..a1135db 100644
--- a/src/photos-main-toolbar.c
+++ b/src/photos-main-toolbar.c
@@ -54,7 +54,6 @@ struct _PhotosMainToolbar
   GtkWidget *overlay;
   GtkWidget *remote_display_button;
   GtkWidget *searchbar;
-  GtkWidget *selection_button;
   GtkWidget *selection_menu;
   PhotosBaseManager *item_mngr;
   PhotosModeController *mode_cntrlr;
@@ -295,41 +294,6 @@ photos_main_toolbar_item_active_changed (PhotosMainToolbar *self, GObject *objec
 }
 
 
-static void
-photos_main_toolbar_items_changed (PhotosMainToolbar *self)
-{
-  PhotosBaseManager *item_mngr_chld;
-  PhotosWindowMode window_mode;
-  gboolean is_empty;
-  guint n_items;
-
-  window_mode = photos_mode_controller_get_window_mode (self->mode_cntrlr);
-  switch (window_mode)
-    {
-    case PHOTOS_WINDOW_MODE_NONE:
-    case PHOTOS_WINDOW_MODE_EDIT:
-    case PHOTOS_WINDOW_MODE_PREVIEW:
-      return;
-      break;
-
-    case PHOTOS_WINDOW_MODE_COLLECTIONS:
-    case PHOTOS_WINDOW_MODE_FAVORITES:
-    case PHOTOS_WINDOW_MODE_OVERVIEW:
-    case PHOTOS_WINDOW_MODE_SEARCH:
-    default:
-      break;
-    }
-
-  item_mngr_chld = photos_item_manager_get_for_mode (PHOTOS_ITEM_MANAGER (self->item_mngr), window_mode);
-  n_items = g_list_model_get_n_items (G_LIST_MODEL (item_mngr_chld));
-
-  is_empty = n_items == 0;
-  g_simple_action_set_enabled (G_SIMPLE_ACTION (self->search), !is_empty);
-  if (self->selection_button != NULL)
-    gtk_widget_set_sensitive (self->selection_button, !is_empty);
-}
-
-
 static GtkWidget *
 photos_main_toolbar_add_search_button (PhotosMainToolbar *self)
 {
@@ -379,7 +343,6 @@ photos_main_toolbar_clear_state_data (PhotosMainToolbar *self)
 {
   g_clear_pointer (&self->coll_back_button, (GDestroyNotify) gtk_widget_destroy);
   g_clear_pointer (&self->remote_display_button, (GDestroyNotify) gtk_widget_destroy);
-  g_clear_pointer (&self->selection_button, (GDestroyNotify) gtk_widget_destroy);
 
   if (self->item_mngr != NULL)
     {
@@ -481,7 +444,7 @@ photos_main_toolbar_populate_for_collections (PhotosMainToolbar *self)
 
   gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->header_bar), TRUE);
   photos_header_bar_set_mode (PHOTOS_HEADER_BAR (self->header_bar), PHOTOS_HEADER_BAR_MODE_NORMAL);
-  self->selection_button = photos_main_toolbar_add_selection_button (self);
+  photos_main_toolbar_add_selection_button (self);
   photos_main_toolbar_add_search_button (self);
 
   collection = photos_item_manager_get_active_collection (PHOTOS_ITEM_MANAGER (self->item_mngr));
@@ -517,7 +480,7 @@ photos_main_toolbar_populate_for_favorites (PhotosMainToolbar *self)
 
   gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->header_bar), TRUE);
   photos_header_bar_set_mode (PHOTOS_HEADER_BAR (self->header_bar), PHOTOS_HEADER_BAR_MODE_NORMAL);
-  self->selection_button = photos_main_toolbar_add_selection_button (self);
+  photos_main_toolbar_add_selection_button (self);
   photos_main_toolbar_add_search_button (self);
 
   collection = photos_item_manager_get_active_collection (PHOTOS_ITEM_MANAGER (self->item_mngr));
@@ -532,7 +495,7 @@ photos_main_toolbar_populate_for_overview (PhotosMainToolbar *self)
 
   gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->header_bar), TRUE);
   photos_header_bar_set_mode (PHOTOS_HEADER_BAR (self->header_bar), PHOTOS_HEADER_BAR_MODE_NORMAL);
-  self->selection_button = photos_main_toolbar_add_selection_button (self);
+  photos_main_toolbar_add_selection_button (self);
   photos_main_toolbar_add_search_button (self);
 
   collection = photos_item_manager_get_active_collection (PHOTOS_ITEM_MANAGER (self->item_mngr));
@@ -612,7 +575,7 @@ photos_main_toolbar_populate_for_search (PhotosMainToolbar *self)
 
   gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (self->header_bar), TRUE);
   photos_header_bar_set_mode (PHOTOS_HEADER_BAR (self->header_bar), PHOTOS_HEADER_BAR_MODE_NORMAL);
-  self->selection_button = photos_main_toolbar_add_selection_button (self);
+  photos_main_toolbar_add_selection_button (self);
   photos_main_toolbar_add_search_button (self);
 
   collection = photos_item_manager_get_active_collection (PHOTOS_ITEM_MANAGER (self->item_mngr));
@@ -743,12 +706,6 @@ photos_main_toolbar_init (PhotosMainToolbar *self)
   photos_header_bar_set_selection_menu (PHOTOS_HEADER_BAR (self->header_bar), GTK_BUTTON 
(self->selection_menu));
 
   self->item_mngr = g_object_ref (state->item_mngr);
-  g_signal_connect_object (self->item_mngr,
-                           "items-changed",
-                           G_CALLBACK (photos_main_toolbar_items_changed),
-                           self,
-                           G_CONNECT_SWAPPED);
-
   self->mode_cntrlr = g_object_ref (state->mode_cntrlr);
   self->sel_cntrlr = photos_selection_controller_dup_singleton ();
 
@@ -879,9 +836,7 @@ photos_main_toolbar_reset_toolbar_mode (PhotosMainToolbar *self)
         }
     }
 
-  photos_main_toolbar_items_changed (self);
   photos_main_toolbar_update_remote_display_button (self);
-
   photos_main_toolbar_set_toolbar_title (self);
   gtk_widget_show_all (self->header_bar);
 }


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