[gnome-documents/wip/gepub] preview: factor out a common class for preview toolbars



commit c9b98cc293249ebb152b16fc97198a169aaba633
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Jun 21 21:22:04 2016 -0700

    preview: factor out a common class for preview toolbars
    
    We can now directly share most of this code.

 src/epubview.js   |   46 +++++---------------------------
 src/evinceview.js |   77 ++++++++++------------------------------------------
 src/lokview.js    |   63 -------------------------------------------
 src/preview.js    |   69 ++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 90 insertions(+), 165 deletions(-)
---
diff --git a/src/epubview.js b/src/epubview.js
index 4e19a39..94315bd 100644
--- a/src/epubview.js
+++ b/src/epubview.js
@@ -103,6 +103,10 @@ const EPUBView = new Lang.Class({
         fc.search(str, WebKit2.FindOptions.CASE_INSENSITIVE, 0);
     },
 
+    get canFind() {
+        return true;
+    },
+
     findNext: function() {
         let fc = this.view.get_find_controller();
         fc.search_next();
@@ -144,45 +148,9 @@ const EPUBSearchbar = new Lang.Class({
 
 const EPUBViewToolbar = new Lang.Class({
     Name: 'EPUBViewToolbar',
-    Extends: MainToolbar.MainToolbar,
-
-    _init: function(previewView) {
-        this._previewView = previewView;
-
-        this.parent();
-        this.toolbar.set_show_close_button(true);
-
-        this._handleEvent = false;
-        this._model = null;
-
-        this._searchAction = Application.application.lookup_action('search');
-        this._searchAction.enabled = 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();
-        }));
-
-        // search button, on the right of the toolbar
-        this.addSearchButton();
-
-        this._setToolbarTitle();
-        this.toolbar.show_all();
-    },
+    Extends: Preview.PreviewToolbar,
 
     createSearchbar: function() {
-        return new EPUBSearchbar(this._previewView);
-    },
-
-    _setToolbarTitle: function() {
-        let primary = null;
-        let doc = Application.documentManager.getActiveItem();
-
-        if (doc)
-            primary = doc.name;
-
-        this.toolbar.set_title(primary);
-    },
+        return new EPUBSearchbar(this.preview);
+    }
 });
diff --git a/src/evinceview.js b/src/evinceview.js
index 04c8ad6..8e9d4d5 100644
--- a/src/evinceview.js
+++ b/src/evinceview.js
@@ -630,6 +630,10 @@ const EvinceView = new Lang.Class({
         return this._model ? this._model.document.get_n_pages() : 0;
     },
 
+    get canFind() {
+        return true;
+    },
+
     scroll: function(direction) {
         this._evView.scroll(direction, false);
     },
@@ -696,55 +700,29 @@ const EvinceViewNavControls = new Lang.Class({
 
 const EvinceViewToolbar = new Lang.Class({
     Name: 'EvinceViewToolbar',
-    Extends: MainToolbar.MainToolbar,
-
-    _init: function(previewView) {
-        this._previewView = previewView;
+    Extends: Preview.PreviewToolbar,
 
-        this.parent();
-        this.toolbar.set_show_close_button(true);
+    _init: function(preview) {
+        this.parent(preview);
 
         this._handleEvent = false;
-
         this._searchAction = Application.application.lookup_action('search');
         this._searchAction.enabled = false;
 
-        this._previewView.getAction('gear-menu').enabled = false;
-
-        // 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._searchAction.enabled = true;
-            }));
-
-        // menu button, on the right of the toolbar
-        let previewMenu = this._getEvinceViewMenu();
-        let menuButton = new Gtk.MenuButton({ image: new Gtk.Image ({ icon_name: 'open-menu-symbolic' }),
-                                              menu_model: previewMenu,
-                                              action_name: 'view.gear-menu' });
-        this.toolbar.pack_end(menuButton);
+        this.preview.getAction('gear-menu').enabled = false;
 
-        // search button, on the right of the toolbar
-        this.addSearchButton();
         if (Application.application.isBooks) {
             this.addFullscreenButton();
             this.addNightmodeButton();
         }
 
-        this._setToolbarTitle();
-        this.toolbar.show_all();
-
-        this.connect('destroy', Lang.bind(this,
-            function() {
-                this._searchAction.enabled = true;
-            }));
+        this.connect('destroy', Lang.bind(this, function() {
+            this._searchAction.enabled = true;
+        }));
     },
 
     _enableSearch: function() {
-        let hasPages = this._previewView.hasPages;
+        let hasPages = this.preview.hasPages;
         let canFind = true;
 
         try {
@@ -762,39 +740,14 @@ const EvinceViewToolbar = new Lang.Class({
         this._searchAction.enabled = (hasPages && canFind);
     },
 
-    _getEvinceViewMenu: function() {
-        let builder = new Gtk.Builder();
-        builder.add_from_resource('/org/gnome/Documents/ui/preview-menu.ui');
-        let menu = builder.get_object('preview-menu');
-
-        let doc = Application.documentManager.getActiveItem();
-        if (doc && doc.defaultAppName) {
-            let section = builder.get_object('open-section');
-            section.remove(0);
-            section.prepend(_("Open with %s").format(doc.defaultAppName), 'view.open-current');
-        }
-
-        return menu;
-    },
-
     createSearchbar: function() {
-        return new EvinceViewSearchbar(this._previewView);
-    },
-
-    _setToolbarTitle: function() {
-        let primary = null;
-        let doc = Application.documentManager.getActiveItem();
-
-        if (doc)
-            primary = doc.name;
-
-        this.toolbar.set_title(primary);
+        return new EvinceViewSearchbar(this.preview);
     },
 
     setModel: function() {
-        this._previewView.getAction('gear-menu').enabled = true;
+        this.preview.getAction('gear-menu').enabled = true;
         this._enableSearch();
-        this._setToolbarTitle();
+        this.updateTitle();
     }
 });
 
diff --git a/src/lokview.js b/src/lokview.js
index 892922c..7dbb3a3 100644
--- a/src/lokview.js
+++ b/src/lokview.js
@@ -128,10 +128,6 @@ const LOKView = new Lang.Class({
         ];
     },
 
-    createToolbar: function() {
-        return new LOKViewToolbar(this);
-    },
-
     createView: function() {
         let sw = new Gtk.ScrolledWindow({ hexpand: true,
                                           vexpand: true });
@@ -263,62 +259,3 @@ const LOKView = new Lang.Class({
     }
 });
 Signals.addSignalMethods(LOKView.prototype);
-
-const LOKViewToolbar = new Lang.Class({
-    Name: 'LOKViewToolbar',
-    Extends: MainToolbar.MainToolbar,
-
-    _init: function(lokView) {
-        this._lokView = lokView;
-
-        this.parent();
-        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();
-            }));
-
-        // menu button, on the right of the toolbar
-        let lokViewMenu = this._getLOKViewMenu();
-        let menuButton = new Gtk.MenuButton({ image: new Gtk.Image ({ icon_name: 'open-menu-symbolic' }),
-                                              menu_model: lokViewMenu,
-                                              action_name: 'view.gear-menu' });
-        this.toolbar.pack_end(menuButton);
-
-        this._setToolbarTitle();
-        this.toolbar.show_all();
-    },
-
-    _getLOKViewMenu: function() {
-        let builder = new Gtk.Builder();
-        builder.add_from_resource('/org/gnome/Documents/ui/preview-menu.ui');
-        let menu = builder.get_object('preview-menu');
-        let section = builder.get_object('open-section');
-
-        let doc = Application.documentManager.getActiveItem();
-        if (doc && doc.defaultAppName) {
-            section.remove(0);
-            section.prepend(_("Open with %s").format(doc.defaultAppName), 'view.open-current');
-        }
-
-        return menu;
-    },
-
-    handleEvent: function(event) {
-        return false;
-    },
-
-    _setToolbarTitle: function() {
-        let primary = null;
-        let doc = Application.documentManager.getActiveItem();
-
-        if (doc)
-            primary = doc.name;
-
-        this.toolbar.set_title(primary);
-    }
-});
diff --git a/src/preview.js b/src/preview.js
index a380d58..365e719 100644
--- a/src/preview.js
+++ b/src/preview.js
@@ -10,6 +10,7 @@ const Tweener = imports.tweener.tweener;
 
 const Application = imports.application;
 const ErrorBox = imports.errorBox;
+const MainToolbar = imports.mainToolbar;
 const Properties = imports.properties;
 const Searchbar = imports.searchbar;
 const Utils = imports.utils;
@@ -110,7 +111,7 @@ const Preview = new Lang.Class({
     },
 
     createToolbar: function() {
-        throw(new Error('Not implemented'));
+        return new PreviewToolbar(this);
     },
 
     createView: function() {
@@ -161,6 +162,10 @@ const Preview = new Lang.Class({
         return this._lastSearch;
     },
 
+    get canFind() {
+        return false;
+    },
+
     findPrev: function() {
         throw (new Error('Not implemented'));
     },
@@ -178,6 +183,68 @@ const Preview = new Lang.Class({
     }
 });
 
+const PreviewToolbar = new Lang.Class({
+    Name: 'PreviewToolbar',
+    Extends: MainToolbar.MainToolbar,
+
+    _init: function(preview) {
+        this.preview = preview;
+
+        this.parent();
+        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();
+            }));
+
+        // menu button, on the right of the toolbar
+        let menuButton = new Gtk.MenuButton({ image: new Gtk.Image ({ icon_name: 'open-menu-symbolic' }),
+                                              menu_model: this._getPreviewMenu(),
+                                              action_name: 'view.gear-menu' });
+        this.toolbar.pack_end(menuButton);
+
+        // search button, on the right of the toolbar
+        if (this.preview.canFind)
+            this.addSearchButton();
+
+        this.updateTitle();
+        this.toolbar.show_all();
+    },
+
+    _getPreviewMenu: function() {
+        let builder = new Gtk.Builder();
+        builder.add_from_resource('/org/gnome/Documents/ui/preview-menu.ui');
+        let menu = builder.get_object('preview-menu');
+
+        let doc = Application.documentManager.getActiveItem();
+        if (doc && doc.defaultAppName) {
+            let section = builder.get_object('open-section');
+            section.remove(0);
+            section.prepend(_("Open with %s").format(doc.defaultAppName), 'view.open-current');
+        }
+
+        return menu;
+    },
+
+    createSearchbar: function() {
+        return new PreviewSearchbar(this.preview);
+    },
+
+    updateTitle: function() {
+        let primary = null;
+        let doc = Application.documentManager.getActiveItem();
+
+        if (doc)
+            primary = doc.name;
+
+        this.toolbar.set_title(primary);
+    }
+});
+
 const _AUTO_HIDE_TIMEOUT = 2;
 const PREVIEW_NAVBAR_MARGIN = 30;
 


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