[gnome-documents] all: port to GdMainToolbar



commit 582853d577163597488b612220fcfbebc3a932f6
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Dec 5 15:56:07 2011 -0500

    all: port to GdMainToolbar

 src/embed.js       |    2 +-
 src/mainToolbar.js |  312 +++++++++++++++-------------------------------------
 2 files changed, 90 insertions(+), 224 deletions(-)
---
diff --git a/src/embed.js b/src/embed.js
index d088dbb..c3f19e1 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -158,7 +158,7 @@ ViewEmbed.prototype  = {
     },
 
     _destroyFullscreenToolbar: function() {
-        this._fsToolbar.destroy();
+        this._fsToolbar.widget.destroy();
         this._fsToolbar = null;
     },
 
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 58067cb..74eae7c 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -19,6 +19,7 @@
  *
  */
 
+const Gd = imports.gi.Gd;
 const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Gtk = imports.gi.Gtk;
@@ -34,6 +35,38 @@ const Global = imports.global;
 const Tweener = imports.util.tweener;
 const WindowMode = imports.windowMode;
 
+function ViewSelector() {
+    this._init();
+}
+
+ViewSelector.prototype = {
+    _init: function() {
+        let iconView = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'view-grid-symbolic',
+                                                                     pixel_size: 16 }) });
+        iconView.get_style_context().add_class('linked');
+        iconView.get_style_context().add_class('raised');
+
+        let listView = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'view-list-symbolic',
+                                                                     pixel_size: 16 }) });
+        listView.get_style_context().add_class('linked');
+        listView.get_style_context().add_class('raised');
+
+        Global.settings.bind('list-view',
+                             iconView, 'active',
+                             Gio.SettingsBindFlags.INVERT_BOOLEAN);
+        Global.settings.bind('list-view',
+                             listView, 'active',
+                             Gio.SettingsBindFlags.DEFAULT);
+
+        this.widget = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
+                                    spacing: 0 });
+        this.widget.add(iconView);
+        this.widget.add(listView);
+
+        this.widget.show_all();
+    }
+};
+
 function MainToolbar() {
     this._init();
 }
@@ -41,34 +74,14 @@ function MainToolbar() {
 MainToolbar.prototype = {
     _init: function() {
         this._model = null;
-        this._overviewBack = null;
-        this._selectionWhereLabel = null;
-        this._selectionLabel = null;
-        this._pageLabel = null;
-        this._titleLabel = null;
-        this._whereLabel = null;
 
         this._collectionId = 0;
         this._selectionChangedId = 0;
 
-        this.widget = new Gtk.Toolbar({ icon_size: Gtk.IconSize.MENU });
+        this.widget = new Gd.MainToolbar({ icon_size: Gtk.IconSize.MENU });
         this.widget.get_style_context().add_class(Gtk.STYLE_CLASS_MENUBAR);
         this.widget.show();
 
-        this._leftGroup = new Gtk.ToolItem({ margin_right: 12 });
-        this.widget.insert(this._leftGroup, -1);
-
-        this._centerGroup = new Gtk.ToolItem();
-        this._centerGroup.set_expand(true);
-        this.widget.insert(this._centerGroup, -1);
-
-        this._rightGroup = new Gtk.ToolItem({ margin_left: 12 });
-        this.widget.insert(this._rightGroup, -1);
-
-        this._sizeGroup = new Gtk.SizeGroup();
-        this._sizeGroup.add_widget(this._leftGroup);
-        this._sizeGroup.add_widget(this._rightGroup);
-
         this.actor = new GtkClutter.Actor({ contents: this.widget });
 
         // setup listeners to mode changes that affect the toolbar layout
@@ -79,15 +92,42 @@ MainToolbar.prototype = {
             Global.modeController.connect('window-mode-changed',
                                           Lang.bind(this, this._onWindowModeChanged));
         this._onWindowModeChanged();
+
+        this.widget.connect('destroy', Lang.bind(this,
+            function() {
+                this._onToolbarClear();
+
+                if (this._windowModeId != 0) {
+                    Global.modeController.disconnect(this._windowModeId);
+                    this._windowModeId = 0;
+                }
+
+                if (this._selectionModeId != 0) {
+                    Global.selectionController.disconnect(this._selectionModeId);
+                    this._selectionModeId = 0;
+                }
+            }));
+
+        // setup listeners from toolbar actions to window mode changes
+        this.widget.connect('selection-mode-request', Lang.bind(this,
+            function(toolbar, requestMode) {
+                Global.selectionController.setSelectionMode(requestMode);
+            }));
+
+        this.widget.connect('go-back-request', Lang.bind(this,
+            function(toolbar) {
+                let mode = Global.modeController.getWindowMode();
+                if (mode == WindowMode.WindowMode.PREVIEW)
+                    Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
+                else
+                    Global.collectionManager.setActiveItem(null);
+            }));
+
+        this.widget.connect('clear-request', Lang.bind(this, this._onToolbarClear));
     },
 
-    _clearToolbar: function() {
+    _onToolbarClear: function() {
         this._model = null;
-        this._whereLabel = null;
-        this._selectionLabel = null;
-        this._selectionWhereLabel = null;
-        this._pageLabel = null;
-        this._titleLabel = null;
 
         if (this._collectionId != 0) {
             Global.collectionManager.disconnect(this._collectionId);
@@ -98,121 +138,32 @@ MainToolbar.prototype = {
             Global.selectionController.disconnect(this._selectionChangedId);
             this._selectionChangedId = 0;
         }
-
-        // destroy all the children of the groups
-        let child = this._leftGroup.get_child();
-        if (child)
-            child.destroy();
-
-        child = this._centerGroup.get_child();
-        if (child)
-            child.destroy();
-
-        child = this._rightGroup.get_child();
-        if (child)
-            child.destroy();
-
-        let context = this.widget.get_style_context();
-        if (context.has_class('documents-selection-mode')) {
-            context.remove_class('documents-selection-mode');
-            this.widget.reset_style();
-        }
-    },
-
-    _buildViewSelector: function() {
-        let iconView = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'view-grid-symbolic',
-                                                                     pixel_size: 16 }) });
-        iconView.get_style_context().add_class('linked');
-        iconView.get_style_context().add_class('raised');
-
-        let listView = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'view-list-symbolic',
-                                                                     pixel_size: 16 }) });
-        listView.get_style_context().add_class('linked');
-        listView.get_style_context().add_class('raised');
-
-        Global.settings.bind('list-view',
-                             iconView, 'active',
-                             Gio.SettingsBindFlags.INVERT_BOOLEAN);
-        Global.settings.bind('list-view',
-                             listView, 'active',
-                             Gio.SettingsBindFlags.DEFAULT);
-
-        let box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
-                                spacing: 0 });
-        box.add(iconView);
-        box.add(listView);
-
-        return box;
     },
 
     _updateSelectionLabel: function() {
         let length = Global.selectionController.getSelection().length;
         let collection = Global.collectionManager.getActiveItem();
-
-        // if we are inside a collection, the selected label is dim and needs to be
-        // spaced from the collection label
-        if (collection) {
-            this._selectionWhereLabel.show();
-            this._selectionWhereLabel.set_markup ('<b>' + collection.name + '</b>');
-
-            this._selectionLabel.margin_left = 12;
-        } else {
-            this._selectionWhereLabel.hide();
-            this._selectionLabel.margin_left = 0;
-        }
-
-        let selectionLabelCtx = this._selectionLabel.get_style_context();
-
-        if (length == 0 || collection) {
-            selectionLabelCtx.add_class('dim-label');
-            this._selectionLabel.reset_style();
-        } else {
-            if (selectionLabelCtx.has_class('dim-label')) {
-                selectionLabelCtx.remove_class('dim-label');
-                this._selectionLabel.reset_style();
-            }
-        }
-
-        let markup = '';
+        let primary = null;
+        let detail = null;
 
         if (length == 0)
-            markup = _("Click on items to select them");
+            detail = _("Click on items to select them");
         else
-            markup = (_("%d selected").format(length));
+            detail = (_("%d selected").format(length));
 
-        if (collection)
-            markup = '(' + markup + ')';
-        else
-            markup = '<b>' + markup + '</b>';
+        if (collection) {
+            primary = collection.name;
+            detail = '(' + detail + ')';
+        } else if (length != 0) {
+            primary = detail;
+            detail = null;
+        }
 
-        this._selectionLabel.set_markup(markup);
+        this.widget.set_labels(primary, detail);
     },
 
     _populateForSelectionMode: function() {
-        this.widget.get_style_context().add_class('documents-selection-mode');
-        this.widget.reset_style();
-
-        // centered label
-        this._selectionWhereLabel = new Gtk.Label({ no_show_all: true });
-        this._selectionLabel = new Gtk.Label();
-
-        let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
-                                  valign: Gtk.Align.CENTER,
-                                  halign: Gtk.Align.CENTER });
-        grid.add(this._selectionWhereLabel);
-        grid.add(this._selectionLabel);
-        this._centerGroup.add(grid);
-
-        // right section
-        let cancel = new Gtk.Button({ use_stock: true,
-                                      label: _("Done") });
-        cancel.get_style_context().add_class('raised');
-        this._rightGroup.add(cancel);
-
-        cancel.connect('clicked', Lang.bind(this,
-            function() {
-                Global.selectionController.setSelectionMode(false);
-            }));
+        this.widget.set_mode(Gd.MainToolbarMode.SELECTION);
 
         // connect to selection changes while in this mode
         this._selectionChangedId =
@@ -224,48 +175,10 @@ MainToolbar.prototype = {
     },
 
     _populateForOverview: function() {
-        // left section
-        this._overviewBack = new Gtk.Button({ child: new Gtk.Image({ icon_name: 'go-previous-symbolic',
-                                                                     pixel_size: 16,
-                                                                     visible: true }),
-                                              no_show_all: true,
-                                              halign: Gtk.Align.START });
-        this._overviewBack.get_style_context().add_class('raised');
-        this._leftGroup.add(this._overviewBack);
-
-        this._overviewBack.connect('clicked', Lang.bind(this,
-            function() {
-                // go back to the general overview
-                Global.collectionManager.setActiveItem(null);
-            }));
+        this.widget.set_mode(Gd.MainToolbarMode.OVERVIEW);
 
-        // centered label
-        this._whereLabel = new Gtk.Label();
-        this._centerGroup.add(this._whereLabel);
-
-        // right section
-        let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
-                                  column_spacing: 12 });
-        this._rightGroup.add(grid);
-
-        // view mode selector
-        let selector = this._buildViewSelector();
-        grid.add(selector);
-
-        // selection mode toggle
-        let button = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'emblem-default-symbolic',
-                                                                   pixel_size: 16 }) });
-        button.get_style_context().add_class('raised');
-        grid.add(button);
-
-        button.connect('toggled', Lang.bind(this,
-            function(button) {
-                // toggle selection mode if the button is toggled
-                let isToggled = button.get_active();
-                Global.selectionController.setSelectionMode(isToggled);
-            }));
-        // set initial state
-        button.set_active(Global.selectionController.getSelectionMode());
+        let viewSel = new ViewSelector();
+        this.widget.add_widget(viewSel.widget, Gd.MainToolbarPosition.RIGHT);
 
         // connect to active collection changes while in this mode
         this._collectionId =
@@ -280,38 +193,16 @@ MainToolbar.prototype = {
         let item = Global.collectionManager.getActiveItem();
 
         if (item) {
-            this._overviewBack.show();
-            this._whereLabel.set_markup(('<b>%s</b>').format(item.name));
+            this.widget.set_back_visible(true);
+            this.widget.set_labels(item.name, null);
         } else {
-            this._overviewBack.hide();
-            this._whereLabel.set_text('');
+            this.widget.set_back_visible(false);
+            this.widget.set_labels(_("New and Recent"), null);
         }
     },
 
     _populateForPreview: function(model) {
-        // left section
-        let back = new Gtk.ToggleButton({ child: new Gtk.Image({ icon_name: 'go-previous-symbolic',
-                                                                 pixel_size: 16 }) });
-        back.get_style_context().add_class('raised');
-        this._leftGroup.add(back);
-
-        back.connect('clicked', Lang.bind(this,
-            function() {
-                Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
-            }));
-
-        // centered grid with labels
-        let grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
-                                  halign: Gtk.Align.CENTER,
-                                  valign: Gtk.Align.CENTER });
-        this._centerGroup.add(grid);
-
-        this._titleLabel = new Gtk.Label({ ellipsize: Pango.EllipsizeMode.END });
-        grid.add(this._titleLabel);
-
-        this._pageLabel = new Gtk.Label({ margin_left: 12 });
-        this._pageLabel.get_style_context().add_class('dim-label');
-        grid.add(this._pageLabel);
+        this.widget.set_mode(Gd.MainToolbarMode.PREVIEW);
 
         this._updateModelLabels();
 
@@ -322,9 +213,6 @@ MainToolbar.prototype = {
         let pageLabel = null;
         let doc = Global.documentManager.getActiveItem();
 
-        let titleLabel = ('<b>%s</b>').format(GLib.markup_escape_text(doc.name, -1));
-        this._titleLabel.set_markup(titleLabel);
-
         if (this._model) {
             let curPage, totPages;
 
@@ -334,19 +222,12 @@ MainToolbar.prototype = {
             pageLabel = _("(%d of %d)").format(curPage + 1, totPages);
         }
 
-        if (pageLabel) {
-            this._pageLabel.show();
-            this._pageLabel.set_text(pageLabel);
-        } else {
-            this._pageLabel.hide();
-        }
+        this.widget.set_labels(doc.name, pageLabel);
     },
 
     _onWindowModeChanged: function() {
         let mode = Global.modeController.getWindowMode();
 
-        this._clearToolbar();
-
         if (mode == WindowMode.WindowMode.OVERVIEW)
             this._populateForOverview();
         else if (mode == WindowMode.WindowMode.PREVIEW)
@@ -358,7 +239,6 @@ MainToolbar.prototype = {
             return;
 
         let mode = Global.selectionController.getSelectionMode();
-        this._clearToolbar();
 
         if (mode)
             this._populateForSelectionMode();
@@ -377,20 +257,6 @@ MainToolbar.prototype = {
             }));
 
         this._updateModelLabels();
-    },
-
-    destroy: function() {
-        if (this._windowModeId != 0) {
-            Global.modeController.disconnect(this._windowModeId);
-            this._windowModeId = 0;
-        }
-
-        if (this._selectionModeId != 0) {
-            Global.selectionController.disconnect(this._selectionModeId);
-            this._selectionModeId = 0;
-        }
-
-        this.widget.destroy();
     }
 };
 



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