[gnome-documents] Handle back button with an action



commit ec8369e87be7888dbe28cc5ccfbb161b74e6f2c1
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Oct 30 22:23:28 2016 -0700

    Handle back button with an action
    
    This greatly simplifies the code, and avoids manual event handlers.

 src/edit.js        |   12 +++++-----
 src/mainToolbar.js |    3 +-
 src/mainWindow.js  |   64 ++++++---------------------------------------------
 src/overview.js    |   10 +++++---
 src/preview.js     |   23 +++++++++++++-----
 5 files changed, 38 insertions(+), 74 deletions(-)
---
diff --git a/src/edit.js b/src/edit.js
index 8ec6c0e..5143f49 100644
--- a/src/edit.js
+++ b/src/edit.js
@@ -88,6 +88,11 @@ const EditView = new Lang.Class({
             this.getAction('view-current').enabled = true;
     },
 
+    goBack: function() {
+        Application.documentManager.setActiveItem(null);
+        Application.modeController.goBack(2);
+    },
+
     _viewCurrent: function() {
         Application.modeController.goBack();
     },
@@ -131,12 +136,7 @@ const EditToolbar = new Lang.Class({
         this.toolbar.set_show_close_button(true);
 
         // back button, on the left of the toolbar
-        let backButton = this.addBackButton();
-        backButton.connect('clicked', Lang.bind(this,
-            function() {
-                Application.documentManager.setActiveItem(null);
-                Application.modeController.goBack(2);
-            }));
+        this.addBackButton();
 
         let viewButton = new Gtk.Button({ label: _("View"),
                                           action_name: 'view.view-current' });
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 9c0c8e0..3a356e9 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -105,7 +105,8 @@ const MainToolbar = new Lang.Class({
 
     addBackButton: function() {
         let backButton = new Gtk.Button({ image: new Gtk.Image({ icon_name: 'go-previous-symbolic' }),
-                                          tooltip_text: _("Back") });
+                                          tooltip_text: _("Back"),
+                                          action_name: 'view.go-back' });
         this.toolbar.pack_start(backButton);
         return backButton;
     }
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 8109ec9..d4955f7 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -124,34 +124,6 @@ const MainWindow = new Lang.Class({
         Application.settings.set_boolean('window-maximized', maximized);
     },
 
-    _goBack: function() {
-        let windowMode = Application.modeController.getWindowMode();
-        let activeCollection = Application.documentManager.getActiveCollection();
-        let handled = true;
-
-        switch (windowMode) {
-        case WindowMode.WindowMode.NONE:
-        case WindowMode.WindowMode.DOCUMENTS:
-            handled = false;
-            break;
-        case WindowMode.WindowMode.EDIT:
-        case WindowMode.WindowMode.PREVIEW_EV:
-            Application.documentManager.setActiveItem(null);
-            Application.modeController.goBack();
-            break;
-        case WindowMode.WindowMode.COLLECTIONS:
-        case WindowMode.WindowMode.SEARCH:
-            if (activeCollection)
-                Application.documentManager.activatePreviousCollection();
-            break;
-        default:
-            throw(new Error('Not handled'));
-            break;
-        }
-
-        return handled;
-    },
-
     _onButtonPressEvent: function(widget, event) {
         let button = event.get_button()[1];
         let clickCount = event.get_click_count()[1];
@@ -163,39 +135,19 @@ const MainWindow = new Lang.Class({
         if (button != 8)
             return false;
 
-        return this._goBack();
-    },
-
-    _onKeyPressEvent: function(widget, event) {
-        if (this._handleBackKey(event))
-            return true;
-
-        let toolbar = this._embed.getMainToolbar();
-        if (toolbar.handleEvent(event))
+        let view = this._embed.getPreview();
+        let action = view.getAction('go-back');
+        if (action) {
+            action.activate(null);
             return true;
+        }
 
         return false;
     },
 
-    _isBackKey: function(event) {
-        let direction = this.get_direction();
-        let keyval = event.get_keyval()[1];
-        let state = event.get_state()[1];
-
-        let isBack = (((state & Gdk.ModifierType.MOD1_MASK) != 0 &&
-                       ((direction == Gtk.TextDirection.LTR && keyval == Gdk.KEY_Left) ||
-                       (direction == Gtk.TextDirection.RTL && keyval == Gdk.KEY_Right))) ||
-                      keyval == Gdk.KEY_Back);
-
-        return isBack;
-    },
-
-    _handleBackKey: function(event) {
-        let isBack = this._isBackKey(event);
-        if (!isBack)
-            return false;
-
-        return this._goBack();
+    _onKeyPressEvent: function(widget, event) {
+        let toolbar = this._embed.getMainToolbar();
+        return toolbar.handleEvent(event);
     },
 
     _quit: function() {
diff --git a/src/overview.js b/src/overview.js
index 866e589..b13a468 100644
--- a/src/overview.js
+++ b/src/overview.js
@@ -634,10 +634,6 @@ const OverviewToolbar = new Lang.Class({
             if (!this._collBackButton) {
                 this._collBackButton = this.addBackButton();
                 this._collBackButton.show();
-                this._collBackButton.connect('clicked', Lang.bind(this,
-                    function() {
-                        Application.documentManager.activatePreviousCollection();
-                    }));
             }
         } else {
             customTitle = this._stackSwitcher;
@@ -1069,6 +1065,8 @@ const OverviewStack = new Lang.Class({
 
     _getDefaultActions: function() {
         return [
+            { name: 'go-back',
+              callback: Lang.bind(this, this._goBack) },
             { name: 'selection-mode',
               callback: Utils.actionToggleCallback,
               state: GLib.Variant.new('b', false),
@@ -1104,6 +1102,10 @@ const OverviewStack = new Lang.Class({
         ];
     },
 
+    _goBack: function() {
+        Application.documentManager.activatePreviousCollection();
+    },
+
     _selectAll: function() {
         this.getAction('selection-mode').change_state(GLib.Variant.new('b', true));
         this.view.view.select_all();
diff --git a/src/preview.js b/src/preview.js
index 3a1fa8c..e4ea4ce 100644
--- a/src/preview.js
+++ b/src/preview.js
@@ -71,6 +71,12 @@ const Preview = new Lang.Class({
     },
 
     _getDefaultActions: function() {
+        let backAccels = ['Back'];
+        if (this.get_direction() == Gtk.TextDirection.LTR)
+            backAccels.push('<Alt>Left');
+        else
+            backAccels.push('<Alt>Right');
+
         return [
             { name: 'gear-menu',
               callback: Utils.actionToggleCallback,
@@ -85,7 +91,10 @@ const Preview = new Lang.Class({
               accels: ['<Primary>Page_Up', 'Left'] },
             { name: 'next-page',
               callback: Lang.bind(this, this.goNext),
-              accels: ['<Primary>Page_Down', 'Right'] }
+              accels: ['<Primary>Page_Down', 'Right'] },
+            { name: 'go-back',
+              callback: Lang.bind(this, this.goBack),
+              accels: backAccels }
         ];
     },
 
@@ -208,6 +217,11 @@ const Preview = new Lang.Class({
         return this.actionGroup.lookup_action(name);
     },
 
+    goBack: function() {
+        Application.documentManager.setActiveItem(null);
+        Application.modeController.goBack();
+    },
+
     goPrev: function() {
         throw (new Error('Not implemented'));
     },
@@ -260,12 +274,7 @@ const PreviewToolbar = new Lang.Class({
         this.toolbar.set_show_close_button(true);
 
         // back button, on the left of the toolbar
-        let backButton = this.addBackButton();
-        backButton.connect('clicked', Lang.bind(this,
-            function() {
-                Application.documentManager.setActiveItem(null);
-                Application.modeController.goBack();
-            }));
+        this.addBackButton();
 
         // menu button, on the right of the toolbar
         let menuButton = new Gtk.MenuButton({ image: new Gtk.Image ({ icon_name: 'open-menu-symbolic' }),


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