[gnome-documents] window: split the window mode control into a separate object



commit 4d6814bad4b788c595922a20f00910f57814e15d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Sep 6 11:21:53 2011 -0400

    window: split the window mode control into a separate object
    
    Preparing for window/embed split.

 src/Makefile-js.am |    1 +
 src/application.js |    2 +
 src/global.js      |    1 +
 src/mainToolbar.js |   32 +++++++++++++++---------
 src/mainWindow.js  |   43 ++++++++++-----------------------
 src/windowMode.js  |   66 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 103 insertions(+), 42 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 643452f..48c902f 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -26,6 +26,7 @@ dist_js_DATA = \
     trackerUtils.js \
     utils.js \
     view.js \
+    windowMode.js \
     format.js \
     path.js
 
diff --git a/src/application.js b/src/application.js
index a6f8bff..05b694d 100644
--- a/src/application.js
+++ b/src/application.js
@@ -48,6 +48,7 @@ const SelectionController = imports.selectionController;
 const Sources = imports.sources;
 const TrackerController = imports.trackerController;
 const Tweener = imports.util.tweener;
+const WindowMode = imports.windowMode;
 
 const _GD_DBUS_PATH = '/org/gnome/Documents';
 
@@ -151,6 +152,7 @@ Application.prototype = {
                         Global.documentManager = new Documents.DocumentManager();
                         Global.trackerController = new TrackerController.TrackerController();
                         Global.changeMonitor = new ChangeMonitor.TrackerChangeMonitor();
+                        Global.modeController = new WindowMode.ModeController();
 
                         this._mainWindow = new MainWindow.MainWindow();
                         this.activate();
diff --git a/src/global.js b/src/global.js
index 18252ac..74fc9e1 100644
--- a/src/global.js
+++ b/src/global.js
@@ -25,6 +25,7 @@ let connection = null;
 let documentManager = null;
 let errorHandler = null;
 let goaClient = null;
+let modeController = null;
 let offsetController = null;
 let queryBuilder = null;
 let selectionController = null;
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 6157bc8..87aec30 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -28,19 +28,18 @@ const _ = imports.gettext.gettext;
 
 const Lang = imports.lang;
 const Mainloop = imports.mainloop;
-const Signals = imports.signals;
 
 const Global = imports.global;
-const MainWindow = imports.mainWindow;
+const WindowMode = imports.windowMode;
 
 const _SEARCH_ENTRY_TIMEOUT = 200;
 
-function MainToolbar(windowMode) {
-    this._init(windowMode);
+function MainToolbar() {
+    this._init();
 }
 
 MainToolbar.prototype = {
-    _init: function(windowMode) {
+    _init: function() {
         this._model = null;
         this._document = null;
         this._searchEntryTimeout = 0;
@@ -48,7 +47,10 @@ MainToolbar.prototype = {
         this.widget = new Gtk.Toolbar({ icon_size: Gtk.IconSize.MENU });
         this.widget.get_style_context().add_class(Gtk.STYLE_CLASS_MENUBAR);
 
-        this.setWindowMode(windowMode);
+        this._windowModeId =
+            Global.modeController.connect('window-mode-changed',
+                                          Lang.bind(this, this._onWindowModeChanged));
+        this._onWindowModeChanged();
     },
 
     _clearToolbar: function() {
@@ -137,7 +139,7 @@ MainToolbar.prototype = {
 
         back.connect('clicked', Lang.bind(this,
             function() {
-                this.emit('back-clicked');
+                Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
             }));
 
         let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
@@ -191,12 +193,14 @@ MainToolbar.prototype = {
         }
     },
 
-    setWindowMode: function(windowMode) {
+    _onWindowModeChanged: function() {
+        let mode = Global.modeController.getWindowMode();
+
         this._clearToolbar();
 
-        if (windowMode == MainWindow.WindowMode.OVERVIEW)
+        if (mode == WindowMode.WindowMode.OVERVIEW)
             this._populateForOverview();
-        else if (windowMode == MainWindow.WindowMode.PREVIEW)
+        else if (mode == WindowMode.WindowMode.PREVIEW)
             this._populateForPreview();
     },
 
@@ -217,9 +221,13 @@ MainToolbar.prototype = {
 
     getSearchEntry: function() {
         return this._searchEntry;
+    },
+
+    destroy: function() {
+        Global.modeController.disconnect(this._windowModeId);
+        this.widget.destroy();
     }
 };
-Signals.addSignalMethods(MainToolbar.prototype);
 
 function FullscreenToolbar() {
     this._init();
@@ -229,7 +237,7 @@ FullscreenToolbar.prototype = {
     __proto__: MainToolbar.prototype,
 
     _init: function() {
-        MainToolbar.prototype._init.call(this, MainWindow.WindowMode.PREVIEW);
+        MainToolbar.prototype._init.call(this);
 
         this.actor = new GtkClutter.Actor({ contents: this.widget,
                                             opacity: 0 });
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 013674d..6300f3c 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -43,6 +43,7 @@ const Preview = imports.preview;
 const SpinnerBox = imports.spinnerBox;
 const TrackerUtils = imports.trackerUtils;
 const Tweener = imports.util.tweener;
+const WindowMode = imports.windowMode;
 
 const _ = imports.gettext.gettext;
 
@@ -53,12 +54,6 @@ const _PDF_LOADER_TIMEOUT = 300;
 
 const _FULLSCREEN_TOOLBAR_TIMEOUT = 2;
 
-const WindowMode = {
-    NONE: 0,
-    OVERVIEW: 1,
-    PREVIEW: 2
-};
-
 function MainWindow() {
     this._init();
 }
@@ -75,7 +70,6 @@ MainWindow.prototype = {
         this._loaderTimeout = 0;
         this._lastFilter = '';
         this._scrolledWindowId = 0;
-        this._windowMode = WindowMode.NONE;
 
         this.window = new GtkClutter.Window({ type: Gtk.WindowType.TOPLEVEL,
                                        window_position: Gtk.WindowPosition.CENTER,
@@ -97,6 +91,9 @@ MainWindow.prototype = {
         Global.errorHandler.connect('load-error',
                                     Lang.bind(this, this._onLoadError));
 
+        Global.modeController.connect('window-mode-changed',
+                                      Lang.bind(this, this._onWindowModeChanged));
+
         this._grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL });
         this.window.add(this._grid);
 
@@ -106,9 +103,7 @@ MainWindow.prototype = {
         this._viewContainer = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
         this._grid.add(this._viewContainer);
 
-        this._toolbar = new MainToolbar.MainToolbar(this._windowMode);
-        this._toolbar.connect('back-clicked',
-                              Lang.bind(this, this._onToolbarBackClicked));
+        this._toolbar = new MainToolbar.MainToolbar();
         this._viewContainer.add(this._toolbar.widget);
 
         this._scrolledWin = new Gtk.ScrolledWindow({ hexpand: true,
@@ -121,7 +116,8 @@ MainWindow.prototype = {
         this._viewContainer.add(this._loadMore.widget);
 
         this._grid.show_all();
-        this._setWindowMode(WindowMode.OVERVIEW);
+
+        Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
     },
 
     _onKeyPressEvent: function(widget, event) {
@@ -152,7 +148,7 @@ MainWindow.prototype = {
         }
 
         if (keyval == Gdk.KEY_Escape) {
-            this._setWindowMode(WindowMode.OVERVIEW);
+            Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
             return true;
         }
 
@@ -222,14 +218,8 @@ MainWindow.prototype = {
         this._initView();
     },
 
-    _setWindowMode: function(windowMode) {
-        if (this._windowMode == windowMode)
-            return;
-
-        this._windowMode = windowMode;
-        this._toolbar.setWindowMode(this._windowMode);
-
-        if (this._windowMode == WindowMode.OVERVIEW)
+    _onWindowModeChanged: function(controller, mode) {
+        if (mode == WindowMode.WindowMode.OVERVIEW)
             this._prepareForOverview();
         else
             this._prepareForPreview();
@@ -276,9 +266,6 @@ MainWindow.prototype = {
         this._fsToolbar = new MainToolbar.FullscreenToolbar();
         this._fsToolbar.setModel(this._docModel, this._document);
 
-        this._fsToolbar.connect('back-clicked',
-                                Lang.bind(this, this._onToolbarBackClicked));
-
         let vScrollbar = this._scrolledWin.get_vscrollbar();
 
         let sizeConstraint = new Clutter.BindConstraint
@@ -345,7 +332,7 @@ MainWindow.prototype = {
     _onPdfLoaderTimeout: function() {
         this._loaderTimeout = 0;
 
-        this._setWindowMode(WindowMode.PREVIEW);
+        Global.modeController.setWindowMode(WindowMode.WindowMode.PREVIEW);
 
         let spinnerBox = new SpinnerBox.SpinnerBox();
         this._scrolledWin.add_with_viewport(spinnerBox.widget);
@@ -362,7 +349,7 @@ MainWindow.prototype = {
             this._loaderTimeout = 0;
         }
 
-        this._setWindowMode(WindowMode.PREVIEW);
+        Global.modeController.setWindowMode(WindowMode.WindowMode.PREVIEW);
         this._preview = new Preview.PreviewView(model, document);
 
         if (this._fsToolbar)
@@ -426,10 +413,6 @@ MainWindow.prototype = {
         return false;
     },
 
-    _onToolbarBackClicked: function() {
-        this._setWindowMode(WindowMode.OVERVIEW);
-    },
-
     _onLoadError: function(manager, message, exception) {
         if (this._loaderTimeout != 0) {
             Mainloop.source_remove(this._loaderTimeout);
@@ -441,7 +424,7 @@ MainWindow.prototype = {
         if (exception.toString().indexOf('Operation was cancelled') != -1)
             return;
 
-        this._setWindowMode(WindowMode.PREVIEW);
+        Global.modeController.setWindowMode(WindowMode.WindowMode.PREVIEW);
 
         let errorBox = new ErrorBox.ErrorBox(message, exception.toString());
         this._scrolledWin.add_with_viewport(errorBox.widget);
diff --git a/src/windowMode.js b/src/windowMode.js
new file mode 100644
index 0000000..747fcc4
--- /dev/null
+++ b/src/windowMode.js
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2011 Red Hat, Inc.
+ *
+ * Gnome Documents is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * Gnome Documents is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with Gnome Documents; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Cosimo Cecchi <cosimoc redhat com>
+ *
+ */
+
+const Signals = imports.signals;
+
+const WindowMode = {
+    NONE: 0,
+    OVERVIEW: 1,
+    PREVIEW: 2
+};
+
+function ModeController() {
+    this._init();
+};
+
+ModeController.prototype = {
+    _init: function() {
+        this._mode = WindowMode.NONE;
+    },
+
+    setWindowMode: function(mode) {
+        if (this._mode == mode)
+            return;
+
+        this._mode = mode;
+        this.emit('window-mode-changed', this._mode);
+    },
+
+    getWindowMode: function() {
+        return this._mode;
+    },
+
+    setFullscreen: function(fullscreen) {
+        if (this._mode != WindowMode.PREVIEW)
+            return;
+
+        if (this._fullscreen == fullscreen)
+            return;
+
+        this._fullscreen = fullscreen;
+        this.emit('fullscreen-changed');
+    },
+
+    getFullscreen: function() {
+        return this._fullscreen;
+    }
+};
+Signals.addSignalMethods(ModeController.prototype);



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