[gnome-photos] mode-controller: Add photos_mode_controller_go_back



commit c9ea9c86ad06fe312494aaeefabeed8d35a20ae9
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Feb 27 14:31:15 2014 +0100

    mode-controller: Add photos_mode_controller_go_back
    
    We have to track the previous or old mode in a number of places. Either
    to implement the back button or escape key or to figure out which view
    to switch to when the search constraints are removed. This helps with
    that.
    
    Secondly, we need to use a stack instead of a single variable for this.
    A single variable is not enough to track transitions like:
     - launch application in overview
     - search for something (ie. overview -> search)
     - preview a search result (ie. search -> preview)
     - go back to search results (ie. preview -> search)
     - remove search constraints (ie. search -> ???)
    
    At this point, with a single variable, preview is the "old mode", not
    the overview. This is where having the whole history in a stack is
    useful.
    
    Fixes: https://bugzilla.gnome.org/725329

 src/photos-embed.c           |   13 +--------
 src/photos-main-toolbar.c    |   14 +---------
 src/photos-main-window.c     |   18 +------------
 src/photos-mode-controller.c |   56 ++++++++++++++++++++++++++++++++++++------
 src/photos-mode-controller.h |    2 +
 5 files changed, 56 insertions(+), 47 deletions(-)
---
diff --git a/src/photos-embed.c b/src/photos-embed.c
index c2c4a9d..d2844d0 100644
--- a/src/photos-embed.c
+++ b/src/photos-embed.c
@@ -87,7 +87,6 @@ struct _PhotosEmbedPrivate
   PhotosModeController *mode_cntrlr;
   PhotosSearchController *srch_cntrlr;
   PhotosTrackerController *trk_ovrvw_cntrlr;
-  PhotosWindowMode old_mode;
   guint load_show_id;
 };
 
@@ -563,9 +562,6 @@ photos_embed_search_changed (PhotosEmbed *self)
   if (mode == PHOTOS_WINDOW_MODE_PREVIEW)
     return;
 
-  if (priv->old_mode == PHOTOS_WINDOW_MODE_PREVIEW)
-    return;
-
   object = photos_base_manager_get_active_object (priv->src_mngr);
   source_id = photos_filterable_get_id (PHOTOS_FILTERABLE (object));
 
@@ -577,11 +573,9 @@ photos_embed_search_changed (PhotosEmbed *self)
   if (g_strcmp0 (search_type_id, PHOTOS_SEARCH_TYPE_STOCK_ALL) == 0
       && g_strcmp0 (source_id, PHOTOS_SOURCE_STOCK_ALL) == 0
       && (str == NULL || str [0] == '\0'))
-    mode = priv->old_mode;
+    photos_mode_controller_go_back (priv->mode_cntrlr);
   else
-    mode = PHOTOS_WINDOW_MODE_SEARCH;
-
-  photos_mode_controller_set_window_mode (priv->mode_cntrlr, mode);
+    photos_mode_controller_set_window_mode (priv->mode_cntrlr, PHOTOS_WINDOW_MODE_SEARCH);
 }
 
 
@@ -593,8 +587,6 @@ photos_embed_window_mode_changed (PhotosModeController *mode_cntrlr,
 {
   PhotosEmbed *self = PHOTOS_EMBED (user_data);
 
-  self->priv->old_mode = old_mode;
-
   if (mode == PHOTOS_WINDOW_MODE_COLLECTIONS)
     photos_embed_prepare_for_collections (self);
   else if (mode == PHOTOS_WINDOW_MODE_FAVORITES)
@@ -719,7 +711,6 @@ photos_embed_init (PhotosEmbed *self)
                            self, G_CONNECT_SWAPPED);
 
   priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
-  priv->old_mode = PHOTOS_WINDOW_MODE_NONE;
   g_signal_connect (priv->mode_cntrlr,
                     "window-mode-changed",
                     G_CALLBACK (photos_embed_window_mode_changed),
diff --git a/src/photos-main-toolbar.c b/src/photos-main-toolbar.c
index e7cd50a..061aa17 100644
--- a/src/photos-main-toolbar.c
+++ b/src/photos-main-toolbar.c
@@ -60,7 +60,6 @@ struct _PhotosMainToolbarPrivate
   PhotosModeController *mode_cntrlr;
   PhotosRemoteDisplayManager *remote_mngr;
   PhotosSelectionController *sel_cntrlr;
-  PhotosWindowMode old_mode;
 };
 
 enum
@@ -315,7 +314,7 @@ photos_main_toolbar_back_button_clicked (GtkButton *button, gpointer user_data)
   PhotosMainToolbarPrivate *priv = self->priv;
 
   photos_base_manager_set_active_object (priv->item_mngr, NULL);
-  photos_mode_controller_set_window_mode (priv->mode_cntrlr, priv->old_mode);
+  photos_mode_controller_go_back (priv->mode_cntrlr);
 }
 
 
@@ -614,14 +613,6 @@ photos_main_toolbar_reset_toolbar_mode (PhotosMainToolbar *self)
 
 
 static void
-photos_main_toolbar_window_mode_changed (PhotosMainToolbar *self, PhotosWindowMode mode, PhotosWindowMode 
old_mode)
-{
-  self->priv->old_mode = old_mode;
-  photos_main_toolbar_reset_toolbar_mode (self);
-}
-
-
-static void
 photos_main_toolbar_constructed (GObject *object)
 {
   PhotosMainToolbar *self = PHOTOS_MAIN_TOOLBAR (object);
@@ -745,10 +736,9 @@ photos_main_toolbar_init (PhotosMainToolbar *self)
                            G_CONNECT_SWAPPED);
 
   priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
-  priv->old_mode = PHOTOS_WINDOW_MODE_NONE;
   g_signal_connect_object (priv->mode_cntrlr,
                            "window-mode-changed",
-                           G_CALLBACK (photos_main_toolbar_window_mode_changed),
+                           G_CALLBACK (photos_main_toolbar_reset_toolbar_mode),
                            self,
                            G_CONNECT_SWAPPED);
 
diff --git a/src/photos-main-window.c b/src/photos-main-window.c
index 26ab833..21bcaa9 100644
--- a/src/photos-main-window.c
+++ b/src/photos-main-window.c
@@ -46,7 +46,6 @@ struct _PhotosMainWindowPrivate
   PhotosBaseManager *item_mngr;
   PhotosModeController *mode_cntrlr;
   PhotosSelectionController *sel_cntrlr;
-  PhotosWindowMode old_mode;
   guint configure_id;
 };
 
@@ -164,7 +163,7 @@ photos_main_window_go_back (PhotosMainWindow *self)
     {
     case PHOTOS_WINDOW_MODE_PREVIEW:
       photos_base_manager_set_active_object (priv->item_mngr, NULL);
-      photos_mode_controller_set_window_mode (priv->mode_cntrlr, priv->old_mode);
+      photos_mode_controller_go_back (priv->mode_cntrlr);
       break;
 
     case PHOTOS_WINDOW_MODE_COLLECTIONS:
@@ -237,7 +236,7 @@ photos_main_window_handle_key_preview (PhotosMainWindow *self, GdkEventKey *even
       if (fullscreen)
         {
           photos_base_manager_set_active_object (priv->item_mngr, NULL);
-          photos_mode_controller_set_window_mode (priv->mode_cntrlr, priv->old_mode);
+          photos_mode_controller_go_back (priv->mode_cntrlr);
         }
     }
 
@@ -291,13 +290,6 @@ photos_main_window_key_press_event (GtkWidget *widget, GdkEventKey *event)
 }
 
 
-static void
-photos_main_window_window_mode_changed (PhotosMainWindow *self, PhotosWindowMode mode, PhotosWindowMode 
old_mode)
-{
-  self->priv->old_mode = old_mode;
-}
-
-
 static gboolean
 photos_main_window_window_state_event (GtkWidget *widget, GdkEventWindowState *event)
 {
@@ -384,16 +376,10 @@ photos_main_window_init (PhotosMainWindow *self)
   priv->item_mngr = photos_item_manager_dup_singleton ();
 
   priv->mode_cntrlr = photos_mode_controller_dup_singleton ();
-  priv->old_mode = PHOTOS_WINDOW_MODE_NONE;
   g_signal_connect_swapped (priv->mode_cntrlr,
                             "fullscreen-changed",
                             G_CALLBACK (photos_main_window_fullscreen_changed),
                             self);
-  g_signal_connect_object (priv->mode_cntrlr,
-                           "window-mode-changed",
-                           G_CALLBACK (photos_main_window_window_mode_changed),
-                           self,
-                           G_CONNECT_SWAPPED);
 
   priv->sel_cntrlr = photos_selection_controller_dup_singleton ();
 
diff --git a/src/photos-mode-controller.c b/src/photos-mode-controller.c
index 5b1ec2e..7ed0608 100644
--- a/src/photos-mode-controller.c
+++ b/src/photos-mode-controller.c
@@ -1,6 +1,6 @@
 /*
  * Photos - access, organize and share your photos on GNOME
- * Copyright © 2012, 2013 Red Hat, Inc.
+ * Copyright © 2012, 2013, 2014 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -34,6 +34,7 @@
 
 struct _PhotosModeControllerPrivate
 {
+  GQueue *history;
   PhotosWindowMode mode;
   gboolean fullscreen;
   gboolean can_fullscreen;
@@ -72,6 +73,17 @@ photos_mode_controller_constructor (GType type, guint n_construct_params, GObjec
 
 
 static void
+photos_mode_controller_finalize (GObject *object)
+{
+  PhotosModeController *self = PHOTOS_MODE_CONTROLLER (object);
+
+  g_queue_free (self->priv->history);
+
+  G_OBJECT_CLASS (photos_mode_controller_parent_class)->finalize (object);
+}
+
+
+static void
 photos_mode_controller_init (PhotosModeController *self)
 {
   PhotosModeControllerPrivate *priv;
@@ -79,6 +91,7 @@ photos_mode_controller_init (PhotosModeController *self)
   self->priv = photos_mode_controller_get_instance_private (self);
   priv = self->priv;
 
+  priv->history = g_queue_new ();
   priv->mode = PHOTOS_WINDOW_MODE_NONE;
   priv->fullscreen = FALSE;
   priv->can_fullscreen = FALSE;
@@ -91,6 +104,7 @@ photos_mode_controller_class_init (PhotosModeControllerClass *class)
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
   object_class->constructor = photos_mode_controller_constructor;
+  object_class->finalize = photos_mode_controller_finalize;
 
   signals[CAN_FULLSCREEN_CHANGED] = g_signal_new ("can-fullscreen-changed",
                                                   G_TYPE_FROM_CLASS (class),
@@ -159,6 +173,37 @@ photos_mode_controller_get_window_mode (PhotosModeController *self)
 
 
 void
+photos_mode_controller_go_back (PhotosModeController *self)
+{
+  PhotosModeControllerPrivate *priv = self->priv;
+  PhotosWindowMode old_mode;
+  PhotosWindowMode tmp;
+
+  if (g_queue_is_empty (priv->history))
+    return;
+
+  old_mode = (PhotosWindowMode) GPOINTER_TO_INT (g_queue_pop_head (priv->history));
+
+  /* Always go back to the overview when activated from the search
+   * provider. It is easier to special case it here instead of all
+   * over the code.
+   */
+  if (priv->mode == PHOTOS_WINDOW_MODE_PREVIEW && old_mode == PHOTOS_WINDOW_MODE_NONE)
+    old_mode = PHOTOS_WINDOW_MODE_OVERVIEW;
+
+  if (old_mode == PHOTOS_WINDOW_MODE_NONE)
+    return;
+
+  /* Swap the old and current modes */
+  tmp = old_mode;
+  old_mode = priv->mode;
+  priv->mode = tmp;
+
+  g_signal_emit (self, signals[WINDOW_MODE_CHANGED], 0, priv->mode, old_mode);
+}
+
+
+void
 photos_mode_controller_toggle_fullscreen (PhotosModeController *self)
 {
   photos_mode_controller_set_fullscreen (self, !self->priv->fullscreen);
@@ -208,19 +253,14 @@ photos_mode_controller_set_window_mode (PhotosModeController *self, PhotosWindow
   if (old_mode == mode)
     return;
 
-  /* Always go back to the overview when activated from the search
-   * provider. It is easier to special case it here instead of all
-   * over the code.
-   */
-  if (old_mode == PHOTOS_WINDOW_MODE_PREVIEW && mode == PHOTOS_WINDOW_MODE_NONE)
-    mode = PHOTOS_WINDOW_MODE_OVERVIEW;
-
   if (mode == PHOTOS_WINDOW_MODE_OVERVIEW
       || mode == PHOTOS_WINDOW_MODE_COLLECTIONS
       || mode == PHOTOS_WINDOW_MODE_FAVORITES
       || mode == PHOTOS_WINDOW_MODE_SEARCH)
     photos_mode_controller_set_can_fullscreen (self, FALSE);
 
+  g_queue_push_head (priv->history, GINT_TO_POINTER (old_mode));
   priv->mode = mode;
+
   g_signal_emit (self, signals[WINDOW_MODE_CHANGED], 0, priv->mode, old_mode);
 }
diff --git a/src/photos-mode-controller.h b/src/photos-mode-controller.h
index b072fb1..dfd1e1d 100644
--- a/src/photos-mode-controller.h
+++ b/src/photos-mode-controller.h
@@ -90,6 +90,8 @@ PhotosWindowMode       photos_mode_controller_get_fullscreen         (PhotosMode
 
 PhotosWindowMode       photos_mode_controller_get_window_mode        (PhotosModeController *self);
 
+void                   photos_mode_controller_go_back                (PhotosModeController *self);
+
 void                   photos_mode_controller_toggle_fullscreen      (PhotosModeController *self);
 
 void                   photos_mode_controller_set_can_fullscreen     (PhotosModeController *self,


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