[gnome-photos] Move selection-mode state to a GAction



commit 1d3e12350c3f6236400dfa8c2331be5a38120222
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri May 12 14:48:22 2017 +0200

    Move selection-mode state to a GAction
    
    This lets us use the GAction accelerators instead of a custom event
    listener and keeps the code in sync with gnome-documents.
    
    Original patch from Cosimo Cecchi for gnome-documents.

 src/photos-application.c          |   58 +++++++++++++++++++++++++++++-------
 src/photos-base-item.c            |   17 +++++------
 src/photos-main-toolbar.c         |   45 +++++++++-------------------
 src/photos-main-window.c          |   26 ++--------------
 src/photos-selection-controller.c |   33 ---------------------
 src/photos-selection-controller.h |    5 ---
 src/photos-selection-toolbar.c    |   36 +++++++++++++++-------
 src/photos-utils.c                |   21 +++++++++++++
 src/photos-utils.h                |    2 +
 src/photos-view-container.c       |   36 +++++++++++++++++------
 10 files changed, 146 insertions(+), 133 deletions(-)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index d7a3b78..35505c4 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -106,6 +106,7 @@ struct _PhotosApplication
   GSimpleAction *search_type_action;
   GSimpleAction *sel_all_action;
   GSimpleAction *sel_none_action;
+  GSimpleAction *selection_mode_action;
   GSimpleAction *set_bg_action;
   GSimpleAction *set_ss_action;
   GSimpleAction *share_action;
@@ -295,7 +296,7 @@ photos_application_get_selection_or_active_item (PhotosApplication *self)
 {
   PhotosBaseItem *item = NULL;
 
-  if (photos_selection_controller_get_selection_mode (self->sel_cntrlr))
+  if (photos_utils_get_selection_mode ())
     {
       GList *selection;
       const gchar *urn;
@@ -334,7 +335,7 @@ photos_application_actions_update (PhotosApplication *self)
   load_state = photos_item_manager_get_load_state (self->state->item_mngr);
   mode = photos_mode_controller_get_window_mode (self->state->mode_cntrlr);
   selection = photos_selection_controller_get_selection (self->sel_cntrlr);
-  selection_mode = photos_selection_controller_get_selection_mode (self->sel_cntrlr);
+  selection_mode = photos_utils_get_selection_mode ();
 
   enable = (mode == PHOTOS_WINDOW_MODE_EDIT);
   g_simple_action_set_enabled (self->blacks_exposure_action, enable);
@@ -356,6 +357,7 @@ photos_application_actions_update (PhotosApplication *self)
   g_simple_action_set_enabled (self->search_type_action, enable);
   g_simple_action_set_enabled (self->sel_all_action, enable);
   g_simple_action_set_enabled (self->sel_none_action, enable);
+  g_simple_action_set_enabled (self->selection_mode_action, enable);
 
   enable = (mode == PHOTOS_WINDOW_MODE_PREVIEW);
   g_simple_action_set_enabled (self->load_next_action, enable);
@@ -927,11 +929,12 @@ static void
 photos_application_open_current (PhotosApplication *self)
 {
   PhotosBaseItem *item;
+  GVariant *new_state;
   guint32 time;
 
   time = gtk_get_current_event_time ();
 
-  if (photos_selection_controller_get_selection_mode (self->sel_cntrlr))
+  if (photos_utils_get_selection_mode ())
     {
       GList *l;
       GList *selection;
@@ -953,7 +956,8 @@ photos_application_open_current (PhotosApplication *self)
       photos_base_item_open (item, GTK_WINDOW (self->main_window), time);
     }
 
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (G_ACTION (self->selection_mode_action), new_state);
 }
 
 
@@ -1029,9 +1033,12 @@ static void
 photos_application_properties_response (GtkDialog *dialog, gint response_id, gpointer user_data)
 {
   PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+  GVariant *new_state;
 
   gtk_widget_destroy (GTK_WIDGET (dialog));
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (G_ACTION (self->selection_mode_action), new_state);
 }
 
 
@@ -1233,6 +1240,7 @@ photos_application_save_response (GtkDialog *dialog, gint response_id, gpointer
   GError *error;
   GFile *export = NULL;
   GFile *tmp;
+  GVariant *new_state;
   PhotosBaseItem *item;
   const gchar *export_dir_name;
   const gchar *pictures_path;
@@ -1245,7 +1253,8 @@ photos_application_save_response (GtkDialog *dialog, gint response_id, gpointer
   item = photos_application_get_selection_or_active_item (self);
   g_return_if_fail (item != NULL);
 
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (G_ACTION (self->selection_mode_action), new_state);
 
   pictures_path = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
   export_path = g_build_filename (pictures_path, PHOTOS_EXPORT_SUBPATH, NULL);
@@ -1385,6 +1394,7 @@ static void
 photos_application_share_response (GtkDialog *dialog, gint response_id, gpointer user_data)
 {
   PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+  GVariant *new_state;
   PhotosBaseItem *item;
   PhotosSharePoint *share_point;
 
@@ -1397,7 +1407,8 @@ photos_application_share_response (GtkDialog *dialog, gint response_id, gpointer
   item = photos_application_get_selection_or_active_item (self);
   g_return_if_fail (item != NULL);
 
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (G_ACTION (self->selection_mode_action), new_state);
 
   g_application_hold (G_APPLICATION (self));
   g_application_mark_busy (G_APPLICATION (self));
@@ -1531,6 +1542,24 @@ photos_application_selection_changed (PhotosApplication *self)
 
 
 static void
+photos_application_selection_mode_notify_state (PhotosApplication *self)
+{
+  if (photos_utils_get_selection_mode ())
+    {
+      const gchar *selection_mode_accels[2] = {"Escape", NULL};
+      gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.selection-mode", 
selection_mode_accels);
+    }
+  else
+    {
+      const gchar *selection_mode_accels[1] = {NULL};
+      gtk_application_set_accels_for_action (GTK_APPLICATION (self), "app.selection-mode", 
selection_mode_accels);
+    }
+
+  photos_application_actions_update (self);
+}
+
+
+static void
 photos_application_activate (GApplication *application)
 {
   PhotosApplication *self = PHOTOS_APPLICATION (application);
@@ -1717,11 +1746,6 @@ photos_application_startup (GApplication *application)
                             G_CALLBACK (photos_application_selection_changed),
                             self);
 
-  g_signal_connect_swapped (self->sel_cntrlr,
-                            "selection-mode-changed",
-                            G_CALLBACK (photos_application_selection_changed),
-                            self);
-
   action = g_simple_action_new ("about", NULL);
   g_signal_connect_swapped (action, "activate", G_CALLBACK (photos_application_about), self);
   g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (action));
@@ -1845,6 +1869,15 @@ photos_application_startup (GApplication *application)
   self->sel_none_action = g_simple_action_new ("select-none", NULL);
   g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (self->sel_none_action));
 
+  state = g_variant_new ("b", FALSE);
+  self->selection_mode_action = g_simple_action_new_stateful ("selection-mode", NULL, state);
+  g_signal_connect (self->selection_mode_action, "activate", G_CALLBACK (photos_application_action_toggle), 
self);
+  g_signal_connect_swapped (self->selection_mode_action,
+                            "notify::state",
+                            G_CALLBACK (photos_application_selection_mode_notify_state),
+                            self);
+  g_action_map_add_action (G_ACTION_MAP (self), G_ACTION (self->selection_mode_action));
+
   self->set_bg_action = g_simple_action_new ("set-background", NULL);
   g_object_set_data_full (G_OBJECT (self->set_bg_action),
                           "settings",
@@ -1957,6 +1990,7 @@ photos_application_dispose (GObject *object)
   g_clear_object (&self->search_type_action);
   g_clear_object (&self->sel_all_action);
   g_clear_object (&self->sel_none_action);
+  g_clear_object (&self->selection_mode_action);
   g_clear_object (&self->set_bg_action);
   g_clear_object (&self->set_ss_action);
   g_clear_object (&self->share_action);
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index a6b59f8..263f6b4 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -57,7 +57,6 @@
 #include "photos-print-operation.h"
 #include "photos-query.h"
 #include "photos-search-context.h"
-#include "photos-selection-controller.h"
 #include "photos-single-item-job.h"
 #include "photos-utils.h"
 
@@ -78,7 +77,6 @@ struct _PhotosBaseItemPrivate
   GQuark flash;
   GQuark orientation;
   PhotosCollectionIconWatcher *watcher;
-  PhotosSelectionController *sel_cntrlr;
   TrackerSparqlCursor *cursor;
   gboolean collection;
   gboolean failed_thumbnailing;
@@ -2579,15 +2577,12 @@ static void
 photos_base_item_print_load (GObject *source_object, GAsyncResult *res, gpointer user_data)
 {
   PhotosBaseItem *self = PHOTOS_BASE_ITEM (source_object);
-  PhotosBaseItemPrivate *priv;
   GError *error;
   GtkWindow *toplevel = GTK_WINDOW (user_data);
   GeglNode *node;
   GtkPrintOperation *print_op = NULL;
   GtkPrintOperationResult print_res;
 
-  priv = photos_base_item_get_instance_private (self);
-
   error = NULL;
   node = photos_base_item_load_finish (self, res, &error);
   if (error != NULL)
@@ -2606,7 +2601,14 @@ photos_base_item_print_load (GObject *source_object, GAsyncResult *res, gpointer
   print_res = gtk_print_operation_run (print_op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, toplevel, &error);
   if (print_res == GTK_PRINT_OPERATION_RESULT_APPLY)
     {
-      photos_selection_controller_set_selection_mode (priv->sel_cntrlr, FALSE);
+      GAction *action;
+      GApplication *app;
+      GVariant *new_state;
+
+      app = g_application_get_default ();
+      action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");
+      new_state = g_variant_new ("b", FALSE);
+      g_action_change_state (action, new_state);
     }
   else if (print_res == GTK_PRINT_OPERATION_RESULT_ERROR)
     {
@@ -2656,7 +2658,6 @@ photos_base_item_dispose (GObject *object)
   g_clear_object (&priv->default_app);
   g_clear_object (&priv->original_icon);
   g_clear_object (&priv->watcher);
-  g_clear_object (&priv->sel_cntrlr);
   g_clear_object (&priv->cursor);
 
   G_OBJECT_CLASS (photos_base_item_parent_class)->dispose (object);
@@ -2778,8 +2779,6 @@ photos_base_item_init (PhotosBaseItem *self)
 
   g_mutex_init (&priv->mutex_download);
   g_mutex_init (&priv->mutex_save_metadata);
-
-  priv->sel_cntrlr = photos_selection_controller_dup_singleton ();
 }
 
 
diff --git a/src/photos-main-toolbar.c b/src/photos-main-toolbar.c
index e555c9e..8aba9bb 100644
--- a/src/photos-main-toolbar.c
+++ b/src/photos-main-toolbar.c
@@ -40,6 +40,7 @@
 #include "photos-remote-display-manager.h"
 #include "photos-search-context.h"
 #include "photos-selection-controller.h"
+#include "photos-utils.h"
 
 
 struct _PhotosMainToolbar
@@ -83,7 +84,7 @@ photos_main_toolbar_set_toolbar_title (PhotosMainToolbar *self)
   gchar *primary = NULL;
 
   active_collection = photos_item_manager_get_active_collection (PHOTOS_ITEM_MANAGER (self->item_mngr));
-  selection_mode = photos_selection_controller_get_selection_mode (self->sel_cntrlr);
+  selection_mode = photos_utils_get_selection_mode ();
   window_mode = photos_mode_controller_get_window_mode (self->mode_cntrlr);
 
   if (window_mode == PHOTOS_WINDOW_MODE_OVERVIEW
@@ -207,7 +208,7 @@ photos_main_toolbar_update_remote_display_button (PhotosMainToolbar *self)
   PhotosWindowMode window_mode;
   gboolean selection_mode, active;
 
-  selection_mode = photos_selection_controller_get_selection_mode (self->sel_cntrlr);
+  selection_mode = photos_utils_get_selection_mode ();
   window_mode = photos_mode_controller_get_window_mode (self->mode_cntrlr);
   active = photos_remote_display_manager_is_active (self->remote_mngr);
 
@@ -311,13 +312,6 @@ photos_main_toolbar_items_changed (PhotosMainToolbar *self)
 }
 
 
-static void
-photos_main_toolbar_select_button_clicked (PhotosMainToolbar *self)
-{
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, TRUE);
-}
-
-
 static GtkWidget *
 photos_main_toolbar_add_search_button (PhotosMainToolbar *self)
 {
@@ -342,11 +336,8 @@ photos_main_toolbar_add_selection_button (PhotosMainToolbar *self)
 
   selection_button = gtk_button_new_from_icon_name (PHOTOS_ICON_OBJECT_SELECT_SYMBOLIC, 
GTK_ICON_SIZE_BUTTON);
   gtk_widget_set_tooltip_text (selection_button, _("Select Items"));
+  gtk_actionable_set_action_name (GTK_ACTIONABLE (selection_button), "app.selection-mode");
   gtk_header_bar_pack_end (GTK_HEADER_BAR (self->header_bar), selection_button);
-  g_signal_connect_swapped (selection_button,
-                            "clicked",
-                            G_CALLBACK (photos_main_toolbar_select_button_clicked),
-                            self);
 
   g_signal_connect_object (self->item_mngr,
                            "active-collection-changed",
@@ -440,13 +431,6 @@ photos_main_toolbar_create_preview_menu (PhotosMainToolbar *self)
 
 
 static void
-photos_main_toolbar_done_button_clicked (PhotosMainToolbar *self)
-{
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
-}
-
-
-static void
 photos_main_toolbar_favorite_button_clicked (PhotosMainToolbar *self)
 {
   PhotosBaseItem *item;
@@ -649,11 +633,8 @@ photos_main_toolbar_populate_for_selection_mode (PhotosMainToolbar *self)
   photos_header_bar_set_mode (PHOTOS_HEADER_BAR (self->header_bar), PHOTOS_HEADER_BAR_MODE_SELECTION);
 
   selection_button = gtk_button_new_with_label (_("Cancel"));
+  gtk_actionable_set_action_name (GTK_ACTIONABLE (selection_button), "app.selection-mode");
   gtk_header_bar_pack_end (GTK_HEADER_BAR (self->header_bar), selection_button);
-  g_signal_connect_swapped (selection_button,
-                            "clicked",
-                            G_CALLBACK (photos_main_toolbar_done_button_clicked),
-                            self);
 
   g_signal_connect_object (self->sel_cntrlr,
                            "selection-changed",
@@ -744,6 +725,7 @@ static void
 photos_main_toolbar_init (PhotosMainToolbar *self)
 {
   GMenu *selection_menu;
+  GAction *action;
   GApplication *app;
   GtkBuilder *builder;
   PhotosSearchContextState *state;
@@ -756,6 +738,13 @@ photos_main_toolbar_init (PhotosMainToolbar *self)
   self->gear_menu = G_SIMPLE_ACTION (g_action_map_lookup_action (G_ACTION_MAP (app), "gear-menu"));
   self->search = g_action_map_lookup_action (G_ACTION_MAP (app), "search");
 
+  action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");
+  g_signal_connect_object (action,
+                           "notify::state",
+                           G_CALLBACK (photos_main_toolbar_reset_toolbar_mode),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   builder = gtk_builder_new_from_resource ("/org/gnome/Photos/selection-menu.ui");
 
   selection_menu = G_MENU (gtk_builder_get_object (builder, "selection-menu"));
@@ -773,13 +762,7 @@ photos_main_toolbar_init (PhotosMainToolbar *self)
                            G_CONNECT_SWAPPED);
 
   self->mode_cntrlr = g_object_ref (state->mode_cntrlr);
-
   self->sel_cntrlr = photos_selection_controller_dup_singleton ();
-  g_signal_connect_object (self->sel_cntrlr,
-                           "selection-mode-changed",
-                           G_CALLBACK (photos_main_toolbar_reset_toolbar_mode),
-                           self,
-                           G_CONNECT_SWAPPED);
 
   self->remote_mngr = photos_remote_display_manager_dup_singleton ();
   g_signal_connect_object (self->remote_mngr,
@@ -861,7 +844,7 @@ photos_main_toolbar_reset_toolbar_mode (PhotosMainToolbar *self)
   gboolean selection_mode;
 
   photos_main_toolbar_clear_toolbar (self);
-  selection_mode = photos_selection_controller_get_selection_mode (self->sel_cntrlr);
+  selection_mode = photos_utils_get_selection_mode ();
   window_mode = photos_mode_controller_get_window_mode (self->mode_cntrlr);
 
   if (selection_mode)
diff --git a/src/photos-main-window.c b/src/photos-main-window.c
index 40f2b8d..c789763 100644
--- a/src/photos-main-window.c
+++ b/src/photos-main-window.c
@@ -226,21 +226,6 @@ photos_main_window_handle_key_edit (PhotosMainWindow *self, GdkEventKey *event)
 
 
 static gboolean
-photos_main_window_handle_key_overview (PhotosMainWindow *self, GdkEventKey *event)
-{
-  gboolean handled = FALSE;
-
-  if (photos_selection_controller_get_selection_mode (self->sel_cntrlr) && event->keyval == GDK_KEY_Escape)
-    {
-      photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
-      handled = TRUE;
-    }
-
-  return handled;
-}
-
-
-static gboolean
 photos_main_window_handle_key_preview (PhotosMainWindow *self, GdkEventKey *event)
 {
   gboolean fullscreen;
@@ -295,6 +280,10 @@ photos_main_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
   switch (mode)
     {
     case PHOTOS_WINDOW_MODE_NONE:
+    case PHOTOS_WINDOW_MODE_COLLECTIONS:
+    case PHOTOS_WINDOW_MODE_FAVORITES:
+    case PHOTOS_WINDOW_MODE_OVERVIEW:
+    case PHOTOS_WINDOW_MODE_SEARCH:
       handled = GDK_EVENT_PROPAGATE;
       break;
 
@@ -306,13 +295,6 @@ photos_main_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
       handled = photos_main_window_handle_key_preview (self, event);
       break;
 
-    case PHOTOS_WINDOW_MODE_COLLECTIONS:
-    case PHOTOS_WINDOW_MODE_FAVORITES:
-    case PHOTOS_WINDOW_MODE_OVERVIEW:
-    case PHOTOS_WINDOW_MODE_SEARCH:
-      handled = photos_main_window_handle_key_overview (self, event);
-      break;
-
     default:
       g_assert_not_reached ();
       break;
diff --git a/src/photos-selection-controller.c b/src/photos-selection-controller.c
index 3091d36..56a88cb 100644
--- a/src/photos-selection-controller.c
+++ b/src/photos-selection-controller.c
@@ -41,7 +41,6 @@ struct _PhotosSelectionController
   GList *selection;
   PhotosBaseManager *item_mngr;
   gboolean is_frozen;
-  gboolean selection_mode;
 };
 
 struct _PhotosSelectionControllerClass
@@ -49,13 +48,11 @@ struct _PhotosSelectionControllerClass
   GObjectClass parent_class;
 
   void (*selection_changed)      (PhotosSelectionController *self);
-  void (*selection_mode_changed) (PhotosSelectionController *self);
 };
 
 enum
 {
   SELECTION_CHANGED,
-  SELECTION_MODE_CHANGED,
   LAST_SIGNAL
 };
 
@@ -168,18 +165,6 @@ photos_selection_controller_class_init (PhotosSelectionControllerClass *class)
                                              g_cclosure_marshal_VOID__VOID,
                                              G_TYPE_NONE,
                                              0);
-
-  signals[SELECTION_MODE_CHANGED] = g_signal_new ("selection-mode-changed",
-                                                  G_TYPE_FROM_CLASS (class),
-                                                  G_SIGNAL_RUN_LAST,
-                                                  G_STRUCT_OFFSET (PhotosSelectionControllerClass,
-                                                                   selection_mode_changed),
-                                                  NULL, /*accumulator */
-                                                  NULL, /*accu_data */
-                                                  g_cclosure_marshal_VOID__BOOLEAN,
-                                                  G_TYPE_NONE,
-                                                  1,
-                                                  G_TYPE_BOOLEAN);
 }
 
 
@@ -204,13 +189,6 @@ photos_selection_controller_get_selection (PhotosSelectionController *self)
 }
 
 
-gboolean
-photos_selection_controller_get_selection_mode (PhotosSelectionController *self)
-{
-  return self->selection_mode;
-}
-
-
 void
 photos_selection_controller_set_selection (PhotosSelectionController *self, GList *selection)
 {
@@ -226,14 +204,3 @@ photos_selection_controller_set_selection (PhotosSelectionController *self, GLis
   self->selection = g_list_copy_deep (selection, (GCopyFunc) g_strdup, NULL);
   g_signal_emit (self, signals[SELECTION_CHANGED], 0);
 }
-
-
-void
-photos_selection_controller_set_selection_mode (PhotosSelectionController *self, gboolean mode)
-{
-  if (self->selection_mode == mode)
-    return;
-
-  self->selection_mode = mode;
-  g_signal_emit (self, signals[SELECTION_MODE_CHANGED], 0, self->selection_mode);
-}
diff --git a/src/photos-selection-controller.h b/src/photos-selection-controller.h
index f73a0a4..1a05452 100644
--- a/src/photos-selection-controller.h
+++ b/src/photos-selection-controller.h
@@ -51,14 +51,9 @@ void                        photos_selection_controller_freeze_selection   (Phot
 
 GList                      *photos_selection_controller_get_selection      (PhotosSelectionController *self);
 
-gboolean                    photos_selection_controller_get_selection_mode (PhotosSelectionController *self);
-
 void                        photos_selection_controller_set_selection      (PhotosSelectionController *self,
                                                                             GList                     
*selection);
 
-void                        photos_selection_controller_set_selection_mode (PhotosSelectionController *self,
-                                                                            gboolean mode);
-
 G_END_DECLS
 
 #endif /* PHOTOS_SELECTION_CONTROLLER_H */
diff --git a/src/photos-selection-toolbar.c b/src/photos-selection-toolbar.c
index 6730049..d657afd 100644
--- a/src/photos-selection-toolbar.c
+++ b/src/photos-selection-toolbar.c
@@ -44,6 +44,7 @@
 struct _PhotosSelectionToolbar
 {
   GtkActionBar parent_instance;
+  GAction *selection_mode_action;
   GHashTable *item_listeners;
   GtkWidget *toolbar_collection;
   GtkWidget *toolbar_favorite;
@@ -72,12 +73,15 @@ static void
 photos_selection_toolbar_dialog_response (GtkDialog *dialog, gint response_id, gpointer user_data)
 {
   PhotosSelectionToolbar *self = PHOTOS_SELECTION_TOOLBAR (user_data);
+  GVariant *new_state;
 
   if (response_id != GTK_RESPONSE_CLOSE)
     return;
 
   gtk_widget_destroy (GTK_WIDGET (dialog));
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (self->selection_mode_action, new_state);
 }
 
 
@@ -104,8 +108,9 @@ photos_selection_toolbar_delete (PhotosSelectionToolbar *self)
   GList *items = NULL;
   GList *selection;
   GList *l;
+  GVariant *new_state;
 
-  if (!photos_selection_controller_get_selection_mode (self->sel_cntrlr))
+  if (!photos_utils_get_selection_mode ())
     return;
 
   selection = photos_selection_controller_get_selection (self->sel_cntrlr);
@@ -128,7 +133,9 @@ photos_selection_toolbar_delete (PhotosSelectionToolbar *self)
     }
 
   photos_delete_notification_new (items);
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (self->selection_mode_action, new_state);
 
   g_list_free_full (items, g_object_unref);
 }
@@ -149,6 +156,7 @@ photos_selection_toolbar_favorite_clicked (GtkButton *button, gpointer user_data
   GList *items = NULL;
   GList *selection;
   GList *l;
+  GVariant *new_state;
 
   if (self->inside_refresh)
     return;
@@ -177,7 +185,9 @@ photos_selection_toolbar_favorite_clicked (GtkButton *button, gpointer user_data
       photos_base_item_set_favorite (item, !favorite);
     }
 
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, FALSE);
+  new_state = g_variant_new ("b", FALSE);
+  g_action_change_state (G_ACTION (self->selection_mode_action), new_state);
+
   g_list_free_full (items, g_object_unref);
 }
 
@@ -291,7 +301,7 @@ photos_selection_toolbar_selection_changed (PhotosSelectionToolbar *self)
 {
   GList *selection;
 
-  if (!photos_selection_controller_get_selection_mode (self->sel_cntrlr))
+  if (!photos_utils_get_selection_mode ())
     return;
 
   selection = photos_selection_controller_get_selection (self->sel_cntrlr);
@@ -303,9 +313,9 @@ photos_selection_toolbar_selection_changed (PhotosSelectionToolbar *self)
 
 
 static void
-photos_selection_toolbar_selection_mode_changed (PhotosSelectionToolbar *self, gboolean mode)
+photos_selection_toolbar_selection_mode_notify_state (PhotosSelectionToolbar *self)
 {
-  if (mode)
+  if (photos_utils_get_selection_mode ())
     photos_selection_toolbar_selection_changed (self);
   else
     gtk_widget_hide (GTK_WIDGET (self));
@@ -335,6 +345,13 @@ photos_selection_toolbar_init (PhotosSelectionToolbar *self)
   app = g_application_get_default ();
   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
 
+  self->selection_mode_action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");
+  g_signal_connect_object (self->selection_mode_action,
+                           "notify::state",
+                           G_CALLBACK (photos_selection_toolbar_selection_mode_notify_state),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   self->item_listeners = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);
 
   gtk_widget_init_template (GTK_WIDGET (self));
@@ -347,11 +364,6 @@ photos_selection_toolbar_init (PhotosSelectionToolbar *self)
                            G_CALLBACK (photos_selection_toolbar_selection_changed),
                            self,
                            G_CONNECT_SWAPPED);
-  g_signal_connect_object (self->sel_cntrlr,
-                           "selection-mode-changed",
-                           G_CALLBACK (photos_selection_toolbar_selection_mode_changed),
-                           self,
-                           G_CONNECT_SWAPPED);
 
   action = g_action_map_lookup_action (G_ACTION_MAP (app), "delete");
   g_signal_connect_object (action,
diff --git a/src/photos-utils.c b/src/photos-utils.c
index 17d6a8d..e12c336 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -1075,6 +1075,27 @@ photos_utils_get_provider_name (PhotosBaseManager *src_mngr, PhotosBaseItem *ite
 }
 
 
+gboolean
+photos_utils_get_selection_mode (void)
+{
+  GAction *action;
+  GApplication *app;
+  GVariant *state;
+  gboolean selection_mode;
+
+  app = g_application_get_default ();
+  action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");
+
+  state = g_action_get_state (action);
+  g_return_val_if_fail (state != NULL, FALSE);
+
+  selection_mode = g_variant_get_boolean (state);
+
+  g_variant_unref (state);
+  return selection_mode;
+}
+
+
 gchar *
 photos_utils_get_thumbnail_path_for_file (GFile *file)
 {
diff --git a/src/photos-utils.h b/src/photos-utils.h
index f1da60a..5ec8bd3 100644
--- a/src/photos-utils.h
+++ b/src/photos-utils.h
@@ -141,6 +141,8 @@ char*            photos_utils_get_pixbuf_common_suffix    (GdkPixbufFormat *form
 
 const gchar     *photos_utils_get_provider_name           (PhotosBaseManager *src_mngr, PhotosBaseItem 
*item);
 
+gboolean         photos_utils_get_selection_mode          (void);
+
 gchar           *photos_utils_get_thumbnail_path_for_file (GFile *file);
 
 gchar           *photos_utils_get_thumbnail_path_for_uri  (const gchar *uri);
diff --git a/src/photos-view-container.c b/src/photos-view-container.c
index 1dcf819..fc8c121 100644
--- a/src/photos-view-container.c
+++ b/src/photos-view-container.c
@@ -43,6 +43,7 @@
 struct _PhotosViewContainer
 {
   GtkStack parent_instance;
+  GAction *selection_mode_action;
   GtkWidget *error_box;
   GtkWidget *no_results;
   GtkWidget *sw;
@@ -158,12 +159,15 @@ static void
 photos_view_container_select_all (PhotosViewContainer *self)
 {
   PhotosWindowMode mode;
+  GVariant *new_state;
 
   mode = photos_mode_controller_get_window_mode (self->mode_cntrlr);
   if (self->mode != mode)
     return;
 
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, TRUE);
+  new_state = g_variant_new ("b", TRUE);
+  g_action_change_state (self->selection_mode_action, new_state);
+
   gd_main_box_select_all (GD_MAIN_BOX (self->view));
 }
 
@@ -184,7 +188,10 @@ photos_view_container_select_none (PhotosViewContainer *self)
 static void
 photos_view_container_selection_mode_request (PhotosViewContainer *self)
 {
-  photos_selection_controller_set_selection_mode (self->sel_cntrlr, TRUE);
+  GVariant *new_state;
+
+  new_state = g_variant_new ("b", TRUE);
+  g_action_change_state (self->selection_mode_action, new_state);
 }
 
 
@@ -217,6 +224,16 @@ photos_view_container_set_selection_mode (PhotosViewContainer *self, gboolean se
 
 
 static void
+photos_view_container_selection_mode_notify_state (PhotosViewContainer *self)
+{
+  gboolean selection_mode;
+
+  selection_mode = photos_utils_get_selection_mode ();
+  photos_view_container_set_selection_mode (self, selection_mode);
+}
+
+
+static void
 photos_view_container_window_mode_changed (PhotosViewContainer *self,
                                            PhotosWindowMode mode,
                                            PhotosWindowMode old_mode)
@@ -249,6 +266,13 @@ photos_view_container_constructed (GObject *object)
   app = g_application_get_default ();
   state = photos_search_context_get_state (PHOTOS_SEARCH_CONTEXT (app));
 
+  self->selection_mode_action = g_action_map_lookup_action (G_ACTION_MAP (app), "selection-mode");
+  g_signal_connect_object (self->selection_mode_action,
+                           "notify::state",
+                           G_CALLBACK (photos_view_container_selection_mode_notify_state),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   accessible = gtk_widget_get_accessible (GTK_WIDGET (self));
   if (accessible != NULL)
     atk_object_set_name (accessible, self->name);
@@ -290,13 +314,7 @@ photos_view_container_constructed (GObject *object)
                             self);
 
   self->item_mngr = g_object_ref (state->item_mngr);
-
   self->sel_cntrlr = photos_selection_controller_dup_singleton ();
-  g_signal_connect_object (self->sel_cntrlr,
-                           "selection-mode-changed",
-                           G_CALLBACK (photos_view_container_set_selection_mode),
-                           self,
-                           G_CONNECT_SWAPPED);
 
   self->mode_cntrlr = g_object_ref (state->mode_cntrlr);
   g_signal_connect_object (self->mode_cntrlr,
@@ -339,7 +357,7 @@ photos_view_container_constructed (GObject *object)
                            self,
                            G_CONNECT_SWAPPED);
 
-  selection_mode = photos_selection_controller_get_selection_mode (self->sel_cntrlr);
+  selection_mode = photos_utils_get_selection_mode ();
   photos_view_container_set_selection_mode (self, selection_mode);
 
   photos_tracker_controller_start (self->trk_cntrlr);


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