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



commit e8b2d3431e44d30e4f8edb2c67c600784b81d567
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Oct 30 21:33:15 2016 -0700

    Move selection-mode state to a GAction
    
    The action is owned by the view. This allows us to clean up things quite
    a bit, and use the action accelerators instead of a custom event
    listener.

 src/documents.js  |    5 ----
 src/mainWindow.js |   13 ----------
 src/overview.js   |   65 +++++++++++++++++++++++++++++++----------------------
 src/selections.js |   48 +++++++++-----------------------------
 4 files changed, 50 insertions(+), 81 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index c030553..504dbe4 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -652,11 +652,6 @@ const DocCommon = new Lang.Class({
 
                 let printOp = EvView.PrintOperation.new(docModel.get_document());
 
-                printOp.connect('begin-print', Lang.bind(this,
-                    function() {
-                        Application.selectionController.setSelectionMode(false);
-                    }));
-
                 printOp.connect('done', Lang.bind(this,
                     function(op, res) {
                         if (res == Gtk.PrintOperationResult.ERROR) {
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 265e5da..9435209 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -184,7 +184,6 @@ const MainWindow = new Lang.Class({
         case WindowMode.WindowMode.COLLECTIONS:
         case WindowMode.WindowMode.DOCUMENTS:
         case WindowMode.WindowMode.SEARCH:
-            return this._handleKeyOverview(event);
         case WindowMode.WindowMode.EDIT:
             return false;
         default:
@@ -270,18 +269,6 @@ const MainWindow = new Lang.Class({
         return false;
     },
 
-    _handleKeyOverview: function(event) {
-        let keyval = event.get_keyval()[1];
-
-        if (Application.selectionController.getSelectionMode() &&
-            keyval == Gdk.KEY_Escape) {
-            Application.selectionController.setSelectionMode(false);
-            return true;
-        }
-
-        return false;
-    },
-
     _quit: function() {
         // remove configure event handler if still there
         if (this._configureId != 0) {
diff --git a/src/overview.js b/src/overview.js
index 7336612..866e589 100644
--- a/src/overview.js
+++ b/src/overview.js
@@ -534,7 +534,8 @@ const OverviewToolbar = new Lang.Class({
         this._stackSwitcher.show();
 
         // setup listeners to mode changes that affect the toolbar layout
-        let selectionModeId = Application.selectionController.connect('selection-mode-changed',
+        let selectionModeAction = this._view.getAction('selection-mode');
+        let selectionModeStateId = selectionModeAction.connect('notify::state',
             Lang.bind(this, this._resetToolbarMode));
         this._resetToolbarMode();
 
@@ -548,7 +549,7 @@ const OverviewToolbar = new Lang.Class({
                     this._activeCollection.disconnect(this._infoUpdatedId);
 
                 this._clearStateData();
-                Application.selectionController.disconnect(selectionModeId);
+                selectionModeAction.disconnect(selectionModeStateId);
             }));
     },
 
@@ -574,7 +575,7 @@ const OverviewToolbar = new Lang.Class({
     },
 
     _setToolbarTitle: function() {
-        let selectionMode = Application.selectionController.getSelectionMode();
+        let selectionMode = this._view.getAction('selection-mode').state.get_boolean();
         let activeCollection = Application.documentManager.getActiveCollection();
         let primary = null;
 
@@ -612,12 +613,9 @@ const OverviewToolbar = new Lang.Class({
         this.toolbar.get_style_context().add_class('selection-mode');
         this.toolbar.set_custom_title(this._selectionMenu);
 
-        let selectionButton = new Gtk.Button({ label: _("Cancel") });
+        let selectionButton = new Gtk.Button({ label: _("Cancel"),
+                                               action_name: 'view.selection-mode' });
         this.toolbar.pack_end(selectionButton);
-        selectionButton.connect('clicked', Lang.bind(this,
-            function() {
-                Application.selectionController.setSelectionMode(false);
-            }));
 
         // connect to selection changes while in this mode
         this._selectionChangedId =
@@ -672,12 +670,9 @@ const OverviewToolbar = new Lang.Class({
         this._checkCollectionWidgets();
 
         let selectionButton = new Gtk.Button({ image: new Gtk.Image ({ icon_name: 'object-select-symbolic' 
}),
-                                               tooltip_text: _("Select Items") });
+                                               tooltip_text: _("Select Items"),
+                                               action_name: 'view.selection-mode' });
         this.toolbar.pack_end(selectionButton);
-        selectionButton.connect('clicked', Lang.bind(this,
-            function() {
-                Application.selectionController.setSelectionMode(true);
-            }));
 
         this._addViewMenuButton();
         this.addSearchButton('view.search');
@@ -726,7 +721,7 @@ const OverviewToolbar = new Lang.Class({
     _resetToolbarMode: function() {
         this._clearToolbar();
 
-        let selectionMode = Application.selectionController.getSelectionMode();
+        let selectionMode = this._view.getAction('selection-mode').state.get_boolean();
         if (selectionMode)
             this._populateForSelectionMode();
         else
@@ -756,9 +751,10 @@ const ViewContainer = new Lang.Class({
     Name: 'ViewContainer',
     Extends: Gtk.Stack,
 
-    _init: function(windowMode) {
+    _init: function(overview, windowMode) {
         this._edgeHitId = 0;
         this._mode = windowMode;
+        this._overview = overview;
 
         this._model = new ViewModel(this._mode);
 
@@ -792,10 +788,9 @@ const ViewContainer = new Lang.Class({
         this.view.connect('notify::view-type',
                           Lang.bind(this, this._onViewTypeChanged));
 
-        // setup selection controller => view
-        Application.selectionController.connect('selection-mode-changed',
-            Lang.bind(this, this._onSelectionModeChanged));
-        this._onSelectionModeChanged();
+        let selectionModeAction = this._overview.getAction('selection-mode');
+        selectionModeAction.connect('notify::state', Lang.bind(this, this._onSelectionModeChanged));
+        this._onSelectionModeChanged(selectionModeAction);
 
         Application.modeController.connect('window-mode-changed',
             Lang.bind(this, this._onWindowModeChanged));
@@ -911,7 +906,7 @@ const ViewContainer = new Lang.Class({
     },
 
     _onSelectionModeRequest: function() {
-        Application.selectionController.setSelectionMode(true);
+        this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', true));
     },
 
     _onItemActivated: function(widget, id, path) {
@@ -988,8 +983,8 @@ const ViewContainer = new Lang.Class({
         Application.selectionController.setSelection(newSelection);
     },
 
-    _onSelectionModeChanged: function() {
-        let selectionMode = Application.selectionController.getSelectionMode();
+    _onSelectionModeChanged: function(action) {
+        let selectionMode = action.state.get_boolean();
         this.view.set_selection_mode(selectionMode);
     },
 
@@ -1050,7 +1045,7 @@ const OverviewStack = new Lang.Class({
         this.pack_start(this._stack, true, true, 0);
 
         // create the toolbar for selected items, it's hidden by default
-        this._selectionToolbar = new Selections.SelectionToolbar();
+        this._selectionToolbar = new Selections.SelectionToolbar(this);
         this.pack_end(this._selectionToolbar, false, false, 0);
 
         let actions = this._getDefaultActions();
@@ -1058,14 +1053,14 @@ const OverviewStack = new Lang.Class({
         Utils.populateActionGroup(this.actionGroup, actions, 'view');
 
         // now create the actual content widgets
-        this._documents = new ViewContainer(WindowMode.WindowMode.DOCUMENTS);
+        this._documents = new ViewContainer(this, WindowMode.WindowMode.DOCUMENTS);
         let label = Application.application.isBooks ? _('Books') : _("Documents");
         this._stack.add_titled(this._documents, 'documents', label);
 
-        this._collections = new ViewContainer(WindowMode.WindowMode.COLLECTIONS);
+        this._collections = new ViewContainer(this, WindowMode.WindowMode.COLLECTIONS);
         this._stack.add_titled(this._collections, 'collections', _("Collections"));
 
-        this._search = new ViewContainer(WindowMode.WindowMode.SEARCH);
+        this._search = new ViewContainer(this, WindowMode.WindowMode.SEARCH);
         this._stack.add_named(this._search, 'search');
 
         this._stack.connect('notify::visible-child',
@@ -1074,6 +1069,10 @@ const OverviewStack = new Lang.Class({
 
     _getDefaultActions: function() {
         return [
+            { name: 'selection-mode',
+              callback: Utils.actionToggleCallback,
+              state: GLib.Variant.new('b', false),
+              stateChanged: Lang.bind(this, this._updateSelectionMode) },
             { name: 'select-all',
               callback: Lang.bind(this, this._selectAll),
               accels: ['<Primary>a'] },
@@ -1106,7 +1105,7 @@ const OverviewStack = new Lang.Class({
     },
 
     _selectAll: function() {
-        Application.selectionController.setSelectionMode(true);
+        this.getAction('selection-mode').change_state(GLib.Variant.new('b', true));
         this.view.view.select_all();
     },
 
@@ -1142,6 +1141,18 @@ const OverviewStack = new Lang.Class({
         this.view.model.set_sort_column_id(sortBy, sortType);
     },
 
+    _updateSelectionMode: function(action) {
+        let selectionMode = action.state.get_boolean();
+
+        if (selectionMode) {
+            Application.application.set_accels_for_action('view.selection-mode', ['Escape']);
+            this._selectionToolbar.show();
+        } else {
+            Application.application.set_accels_for_action('view.selection-mode', []);
+            this._selectionToolbar.hide();
+        }
+    },
+
     _initSearchSource: function(action) {
         Application.sourceManager.connect('active-changed', Lang.bind(this, function(manager, activeItem) {
             action.state = GLib.Variant.new('s', activeItem.id);
diff --git a/src/selections.js b/src/selections.js
index b864c8f..1ce0c1b 100644
--- a/src/selections.js
+++ b/src/selections.js
@@ -798,7 +798,6 @@ const SelectionController = new Lang.Class({
 
     _init: function() {
         this._selection = [];
-        this._selectionMode = false;
 
         Application.documentManager.connect('item-removed',
             Lang.bind(this, this._onDocumentRemoved));
@@ -840,18 +839,6 @@ const SelectionController = new Lang.Class({
             return;
 
         this._isFrozen = freeze;
-    },
-
-    setSelectionMode: function(setting) {
-        if (this._selectionMode == setting)
-            return;
-
-        this._selectionMode = setting;
-        this.emit('selection-mode-changed', this._selectionMode);
-    },
-
-    getSelectionMode: function() {
-        return this._selectionMode;
     }
 });
 Signals.addSignalMethods(SelectionController.prototype);
@@ -869,9 +856,10 @@ const SelectionToolbar = new Lang.Class({
                         'toolbarProperties',
                         'toolbarCollection' ],
 
-    _init: function() {
+    _init: function(overview) {
         this._itemListeners = {};
         this._insideRefresh = false;
+        this._overview = overview;
 
         this.parent();
 
@@ -892,10 +880,9 @@ const SelectionToolbar = new Lang.Class({
         Application.documentManager.connect('active-collection-changed',
             Lang.bind(this, this._updateCollectionsButton));
 
-        Application.selectionController.connect('selection-mode-changed',
-            Lang.bind(this, this._onSelectionModeChanged));
         Application.selectionController.connect('selection-changed',
             Lang.bind(this, this._onSelectionChanged));
+        this._onSelectionChanged();
     },
 
     _updateCollectionsButton: function() {
@@ -907,22 +894,11 @@ const SelectionToolbar = new Lang.Class({
             this._toolbarCollection.show();
     },
 
-    _onSelectionModeChanged: function(controller, mode) {
-        if (mode)
-            this._onSelectionChanged();
-        else
-            this.hide();
-    },
-
     _onSelectionChanged: function() {
-        if (!Application.selectionController.getSelectionMode())
-            return;
-
         let selection = Application.selectionController.getSelection();
         this._setItemListeners(selection);
 
         this._setItemVisibility();
-        this.show();
     },
 
     _setItemListeners: function(selection) {
@@ -1004,16 +980,15 @@ const SelectionToolbar = new Lang.Class({
             return;
 
         let dialog = new OrganizeCollectionDialog(toplevel);
-        dialog.connect('destroy', Lang.bind(this,
-            function() {
-                Application.selectionController.setSelectionMode(false);
-            }));
+        dialog.connect('destroy', Lang.bind(this, function() {
+            this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
+        }));
     },
 
     _onToolbarOpen: function(widget) {
-        let selection = Application.selectionController.getSelection();
-        Application.selectionController.setSelectionMode(false);
+        this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
 
+        let selection = Application.selectionController.getSelection();
         selection.forEach(Lang.bind(this,
             function(urn) {
                 let doc = Application.documentManager.getItemById(urn);
@@ -1039,7 +1014,7 @@ const SelectionToolbar = new Lang.Class({
             }));
 
         let deleteNotification = new Notifications.DeleteNotification(docs);
-        Application.selectionController.setSelectionMode(false);
+        this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
     },
 
     _onToolbarProperties: function(widget) {
@@ -1049,7 +1024,7 @@ const SelectionToolbar = new Lang.Class({
         dialog.connect('response', Lang.bind(this,
             function(widget, response) {
                 dialog.destroy();
-                Application.selectionController.setSelectionMode(false);
+                this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
             }));
     },
 
@@ -1059,7 +1034,7 @@ const SelectionToolbar = new Lang.Class({
        dialog.connect('response', Lang.bind(this,
            function(widget, response) {
                dialog.destroy();
-               Application.selectionController.setSelectionMode(false);
+               this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
            }));
     },
 
@@ -1071,5 +1046,6 @@ const SelectionToolbar = new Lang.Class({
 
         let doc = Application.documentManager.getItemById(selection[0]);
         doc.print(this.get_toplevel());
+        this._overview.getAction('selection-mode').change_state(GLib.Variant.new('b', false));
     },
 });


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