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



commit 8f73c9dab9237e6dc591fa8d3db3c0f2e79a3438
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Jun 19 21:01:48 2016 -0700

    preview: factor out a searchbar common class

 src/epubview.js   |   96 +++++++++++++++-------------------------------------
 src/evinceview.js |   88 +++++++++++-------------------------------------
 src/preview.js    |   88 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/searchbar.js  |   59 ++++++++++++++++----------------
 4 files changed, 167 insertions(+), 164 deletions(-)
---
diff --git a/src/epubview.js b/src/epubview.js
index c4502b8..0de25a6 100644
--- a/src/epubview.js
+++ b/src/epubview.js
@@ -54,27 +54,12 @@ const EPUBView = new Lang.Class({
                                             Lang.bind(this, this._onLoadError));
         Application.modeController.connect('window-mode-changed',
                                            Lang.bind(this, this._onWindowModeChanged));
-
-        let findPrev = Application.application.lookup_action('find-prev');
-        findPrev.connect('activate', Lang.bind(this, this._findPrev));
-        let findNext = Application.application.lookup_action('find-next');
-        findNext.connect('activate',  Lang.bind(this, this._findNext));
     },
 
     createView: function() {
         return new Gepub.Widget();
     },
 
-    _findNext: function() {
-        let fc = this.view.get_find_controller();
-        fc.search_next();
-    },
-
-    _findPrev: function() {
-        let fc = this.view.get_find_controller();
-        fc.search_previous();
-    },
-
     _onWindowModeChanged: function() {
         let windowMode = Application.modeController.getWindowMode();
         if (windowMode != WindowMode.WindowMode.PREVIEW_EPUB)
@@ -123,78 +108,53 @@ const EPUBView = new Lang.Class({
 
     get numPages() {
         return this._epubdoc ? this._epubdoc.get_n_pages() : 0;
+    },
+
+    search: function(str) {
+        this.parent(str);
+
+        let fc = this.view.get_find_controller();
+        fc.search(str, WebKit2.FindOptions.CASE_INSENSITIVE, 0);
+    },
+
+    findNext: function() {
+        let fc = this.view.get_find_controller();
+        fc.search_next();
+    },
+
+    findPrev: function() {
+        let fc = this.view.get_find_controller();
+        fc.search_previous();
     }
 });
 
 const EPUBSearchbar = new Lang.Class({
     Name: 'EPUBSearchbar',
-    Extends: Searchbar.Searchbar,
-
-    _init: function(previewView) {
-        this._previewView = previewView;
-        this.parent();
-    },
+    Extends: Preview.PreviewSearchbar,
 
-    createSearchWidgets: function() {
-        this._searchContainer = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
-                                              halign: Gtk.Align.CENTER});
-        this._searchContainer.get_style_context().add_class('linked');
+    _init: function(preview) {
+        this.parent(preview);
 
-        this._searchEntry = new Gtk.SearchEntry({ width_request: 500 });
-        this._searchEntry.connect('activate', Lang.bind(this, function() {
-            Application.application.activate_action('find-next', null);
-        }));
-        this._searchContainer.add(this._searchEntry);
-
-        this._prev = new Gtk.Button({ action_name: 'app.find-prev' });
-        this._prev.set_image(new Gtk.Image({ icon_name: 'go-up-symbolic',
-                                             icon_size: Gtk.IconSize.MENU }));
-        this._prev.set_tooltip_text(_("Find Previous"));
-        this._searchContainer.add(this._prev);
-
-        this._next = new Gtk.Button({ action_name: 'app.find-next' });
-        this._next.set_image(new Gtk.Image({ icon_name: 'go-down-symbolic',
-                                             icon_size: Gtk.IconSize.MENU }));
-        this._next.set_tooltip_text(_("Find Next"));
-        this._searchContainer.add(this._next);
-
-        let fc = this._previewView.view.get_find_controller();
-        fc.connect('found-text', Lang.bind(this, function(w, match_count, data) {
-            this._onSearchChanged(this._previewView, match_count > 0);
+        let fc = this.preview.view.get_find_controller();
+        fc.connect('found-text', Lang.bind(this, function(view, matchCount, data) {
+            this._onSearchChanged(this.preview, matchCount > 0);
         }));
 
-        this._onSearchChanged(this._previewView, false);
+        this._onSearchChanged(this.preview, false);
     },
 
-    _onSearchChanged: function(view, results) {
+    _onSearchChanged: function(view, hasResults) {
         let findPrev = Application.application.lookup_action('find-prev');
         let findNext = Application.application.lookup_action('find-next');
-        findPrev.enabled = results;
-        findNext.enabled = results;
-    },
-
-    _search: function(str) {
-        let fc = this._previewView.view.get_find_controller();
-        fc.search(str, WebKit2.FindOptions.CASE_INSENSITIVE, 0);
-    },
-
-    entryChanged: function() {
-        this._search(this._searchEntry.get_text());
-    },
-
-    reveal: function() {
-        this.parent();
-        this._search(this._searchEntry.get_text());
+        findPrev.enabled = hasResults;
+        findNext.enabled = hasResults;
     },
 
     conceal: function() {
-        this._search('');
-        let fc = this._previewView.view.get_find_controller();
+        let fc = this.preview.view.get_find_controller();
         fc.search_finish();
 
-        this.searchChangeBlocked = true;
         this.parent();
-        this.searchChangeBlocked = false;
     }
 });
 
diff --git a/src/evinceview.js b/src/evinceview.js
index 6df1cfe..dccb20a 100644
--- a/src/evinceview.js
+++ b/src/evinceview.js
@@ -55,7 +55,6 @@ const EvinceView = new Lang.Class({
         this._hasSelection = false;
         this._viewSelectionChanged = false;
         this._fsToolbar = null;
-        this._lastSearch = '';
 
         this.parent(overlay);
 
@@ -92,16 +91,6 @@ const EvinceView = new Lang.Class({
                 this._evView.zoom_out();
             }));
 
-        let findPrev = Application.application.lookup_action('find-prev');
-        let findPrevId = findPrev.connect('activate', Lang.bind(this,
-            function() {
-                this._evView.find_previous();
-            }));
-        let findNext = Application.application.lookup_action('find-next');
-        let findNextId = findNext.connect('activate', Lang.bind(this,
-            function() {
-                this._evView.find_next();
-            }));
         this._copy = Application.application.lookup_action('copy');
         let copyId = this._copy.connect('activate', Lang.bind(this,
             function() {
@@ -139,8 +128,6 @@ const EvinceView = new Lang.Class({
             function() {
                 this._zoomIn.disconnect(zoomInId);
                 this._zoomOut.disconnect(zoomOutId);
-                findPrev.disconnect(findPrevId);
-                findNext.disconnect(findNextId);
                 this._copy.disconnect(copyId);
                 rotLeft.disconnect(rotLeftId);
                 rotRight.disconnect(rotRightId);
@@ -492,21 +479,21 @@ const EvinceView = new Lang.Class({
     },
 
     activateResult: function() {
-        this._evView.find_next();
+        this.findNext();
     },
 
-    startSearch: function(str) {
+    search: function(str) {
         if (!this._model)
             return;
 
+        this.parent(str);
+
         if (this._jobFind) {
             if (!this._jobFind.is_finished())
                 this._jobFind.cancel();
             this._jobFind = null;
         }
 
-        this._lastSearch = str;
-
         if (!str) {
             this._evView.queue_draw();
             return;
@@ -595,10 +582,6 @@ const EvinceView = new Lang.Class({
         return this._fsToolbar;
     },
 
-    get lastSearch() {
-        return this._lastSearch;
-    },
-
     goPrev: function() {
         this._evView.previous_page();
     },
@@ -619,6 +602,14 @@ const EvinceView = new Lang.Class({
         return this._model ? this._model.document.get_n_pages() : 0;
     },
 
+    findPrev: function() {
+        this._evView.find_previous();
+    },
+
+    findNext: function() {
+        this._evView.find_next();
+    },
+
     get evView() {
         return this._evView;
     }
@@ -793,40 +784,13 @@ const EvinceViewToolbar = new Lang.Class({
 
 const EvinceViewSearchbar = new Lang.Class({
     Name: 'EvinceViewSearchbar',
-    Extends: Searchbar.Searchbar,
+    Extends: Preview.PreviewSearchbar,
 
-    _init: function(previewView) {
-        this._previewView = previewView;
-        this._previewView.connectJS('search-changed', Lang.bind(this, this._onSearchChanged));
-
-        this.parent();
-    },
-
-    createSearchWidgets: function() {
-        this._searchContainer = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
-                                              halign: Gtk.Align.CENTER});
-        this._searchContainer.get_style_context().add_class('linked');
-
-        this._searchEntry = new Gtk.SearchEntry({ width_request: 500 });
-        this._searchEntry.connect('activate', Lang.bind(this,
-            function() {
-                Application.application.activate_action('find-next', null);
-            }));
-        this._searchContainer.add(this._searchEntry);
-
-        this._prev = new Gtk.Button({ action_name: 'app.find-prev' });
-        this._prev.set_image(new Gtk.Image({ icon_name: 'go-up-symbolic',
-                                             icon_size: Gtk.IconSize.MENU }));
-        this._prev.set_tooltip_text(_("Find Previous"));
-        this._searchContainer.add(this._prev);
+    _init: function(preview) {
+        this.parent(preview);
 
-        this._next = new Gtk.Button({ action_name: 'app.find-next' });
-        this._next.set_image(new Gtk.Image({ icon_name: 'go-down-symbolic',
-                                             icon_size: Gtk.IconSize.MENU }));
-        this._next.set_tooltip_text(_("Find Next"));
-        this._searchContainer.add(this._next);
-
-        this._onSearchChanged(this._previewView, false);
+        this.preview.connectJS('search-changed', Lang.bind(this, this._onSearchChanged));
+        this._onSearchChanged(this.preview, false);
     },
 
     _onSearchChanged: function(view, hasResults) {
@@ -837,28 +801,18 @@ const EvinceViewSearchbar = new Lang.Class({
     },
 
     entryChanged: function() {
-        this._previewView.evView.find_search_changed();
-        this._previewView.startSearch(this._searchEntry.get_text());
+        this.preview.evView.find_search_changed();
+        this.parent();
     },
 
     reveal: function() {
+        this.preview.evView.find_set_highlight_search(true);
         this.parent();
-
-        if (!this._searchEntry.get_text()) {
-            this._searchEntry.set_text(this._previewView.lastSearch);
-            this._searchEntry.select_region(0, -1);
-        }
-
-        this._previewView.evView.find_set_highlight_search(true);
-        this._previewView.startSearch(this._searchEntry.get_text());
     },
 
     conceal: function() {
-        this._previewView.evView.find_set_highlight_search(false);
-
-        this.searchChangeBlocked = true;
+        this.preview.evView.find_set_highlight_search(false);
         this.parent();
-        this.searchChangeBlocked = false;
     }
 });
 
diff --git a/src/preview.js b/src/preview.js
index c9b7d53..2a1f28a 100644
--- a/src/preview.js
+++ b/src/preview.js
@@ -6,18 +6,27 @@ const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 const Tweener = imports.tweener.tweener;
 
+const Application = imports.application;
 const ErrorBox = imports.errorBox;
+const Searchbar = imports.searchbar;
 
 const Preview = new Lang.Class({
     Name: 'Preview',
     Extends: Gtk.Stack,
 
     _init: function(overlay) {
+        this._lastSearch = '';
         this.overlay = overlay;
 
         this.parent({ homogeneous: true,
                       transition_type: Gtk.StackTransitionType.CROSSFADE });
 
+        let findPrev = Application.application.lookup_action('find-prev');
+        let findPrevId = findPrev.connect('activate', Lang.bind(this, this.findPrev));
+
+        let findNext = Application.application.lookup_action('find-next');
+        let findNextId = findNext.connect('activate', Lang.bind(this, this.findNext));
+
         this._errorBox = new ErrorBox.ErrorBox();
         this.add_named(this._errorBox, 'error');
 
@@ -27,6 +36,11 @@ const Preview = new Lang.Class({
 
         this.navControls = this.createNavControls();
         this.show_all();
+
+        this.connect('destroy', Lang.bind(this, function() {
+            findPrev.disconnect(findPrevId);
+            findNext.disconnect(findNextId);
+        }));
     },
 
     createNavControls: function() {
@@ -60,6 +74,22 @@ const Preview = new Lang.Class({
 
     get numPages() {
         return 0;
+    },
+
+    search: function(str) {
+        this._lastSearch = str;
+    },
+
+    get lastSearch() {
+        return this._lastSearch;
+    },
+
+    findPrev: function() {
+        throw (new Error('Not implemented'));
+    },
+
+    findNext: function() {
+        throw (new Error('Not implemented'));
     }
 });
 
@@ -261,3 +291,61 @@ const PreviewNavControls = new Lang.Class({
         this._tapGesture = null;
     }
 });
+
+const PreviewSearchbar = new Lang.Class({
+    Name: 'PreviewSearchbar',
+    Extends: Searchbar.Searchbar,
+
+    _init: function(preview) {
+        this.preview = preview;
+
+        this.parent();
+    },
+
+    createSearchWidget: function() {
+        let box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
+                                halign: Gtk.Align.CENTER});
+        box.get_style_context().add_class('linked');
+
+        this.searchEntry = new Gtk.SearchEntry({ width_request: 500 });
+        this.searchEntry.connect('activate', Lang.bind(this, function() {
+            Application.application.activate_action('find-next', null);
+        }));
+        box.add(this.searchEntry);
+
+        this._prev = new Gtk.Button({ action_name: 'app.find-prev' });
+        this._prev.set_image(new Gtk.Image({ icon_name: 'go-up-symbolic',
+                                             icon_size: Gtk.IconSize.MENU }));
+        this._prev.set_tooltip_text(_("Find Previous"));
+        box.add(this._prev);
+
+        this._next = new Gtk.Button({ action_name: 'app.find-next' });
+        this._next.set_image(new Gtk.Image({ icon_name: 'go-down-symbolic',
+                                             icon_size: Gtk.IconSize.MENU }));
+        this._next.set_tooltip_text(_("Find Next"));
+        box.add(this._next);
+
+        return box;
+    },
+
+    entryChanged: function() {
+        this.preview.search(this.searchEntry.get_text());
+    },
+
+    reveal: function() {
+        this.parent();
+
+        if (!this.searchEntry.get_text()) {
+            this.searchEntry.set_text(this.preview.lastSearch);
+            this.searchEntry.select_region(0, -1);
+        }
+
+        this.preview.search(this.searchEntry.get_text());
+    },
+
+    conceal: function() {
+        this.searchChangeBlocked = true;
+        this.parent();
+        this.searchChangeBlocked = false;
+    }
+});
diff --git a/src/searchbar.js b/src/searchbar.js
index 2cc0f13..67634c6 100644
--- a/src/searchbar.js
+++ b/src/searchbar.js
@@ -39,14 +39,13 @@ const Searchbar = new Lang.Class({
 
         this.parent();
 
-        // subclasses will create this._searchEntry and this._searchContainer
-        // GtkWidgets
-        this.createSearchWidgets();
+        // subclasses will create this.searchEntry
+        let searchWidget = this.createSearchWidget();
 
-        this.add(this._searchContainer);
-        this.connect_entry(this._searchEntry);
+        this.add(searchWidget);
+        this.connect_entry(this.searchEntry);
 
-        this._searchEntry.connect('search-changed', Lang.bind(this,
+        this.searchEntry.connect('search-changed', Lang.bind(this,
             function() {
                 if (this.searchChangeBlocked)
                     return;
@@ -80,8 +79,8 @@ const Searchbar = new Lang.Class({
             this.conceal();
     },
 
-    createSearchWidgets: function() {
-        log('Error: Searchbar implementations must override createSearchWidgets');
+    createSearchWidget: function() {
+        log('Error: Searchbar implementations must override createSearchWidget');
     },
 
     entryChanged: function() {
@@ -90,7 +89,7 @@ const Searchbar = new Lang.Class({
 
     handleEvent: function(event) {
         // Skip if the search bar is shown and the focus is elsewhere
-        if (this.search_mode_enabled && !this._searchEntry.is_focus)
+        if (this.search_mode_enabled && !this.searchEntry.is_focus)
             return false;
 
         let keyval = event.get_keyval()[1];
@@ -101,7 +100,7 @@ const Searchbar = new Lang.Class({
 
         let retval = this.handle_event(event);
         if (retval == Gdk.EVENT_STOP)
-            this._searchEntry.grab_focus_without_selecting();
+            this.searchEntry.grab_focus_without_selecting();
         return retval;
     },
 
@@ -113,7 +112,7 @@ const Searchbar = new Lang.Class({
         this.search_mode_enabled = false;
 
         // clear all the search properties when hiding the entry
-        this._searchEntry.set_text('');
+        this.searchEntry.set_text('');
     }
 });
 Utils.addJSSignalMethods(Searchbar.prototype);
@@ -169,7 +168,7 @@ const OverviewSearchbar = new Lang.Class({
         this._onActiveTypeChanged();
         this._onActiveMatchChanged();
 
-        this._searchEntry.set_text(Application.searchController.getString());
+        this.searchEntry.set_text(Application.searchController.getString());
         this.connect('destroy', Lang.bind(this,
             function() {
                 Application.sourceManager.disconnect(sourcesId);
@@ -179,12 +178,12 @@ const OverviewSearchbar = new Lang.Class({
             }));
     },
 
-    createSearchWidgets: function() {
+    createSearchWidget: function() {
         // create the search entry
-        this._searchEntry = new Gd.TaggedEntry({ width_request: 500 });
-        this._searchEntry.connect('tag-clicked',
+        this.searchEntry = new Gd.TaggedEntry({ width_request: 500 });
+        this.searchEntry.connect('tag-clicked',
             Lang.bind(this, this._onTagClicked));
-        this._searchEntry.connect('tag-button-clicked',
+        this.searchEntry.connect('tag-button-clicked',
             Lang.bind(this, this._onTagButtonClicked));
 
         this._sourceTag = new Gd.TaggedEntryTag();
@@ -195,7 +194,7 @@ const OverviewSearchbar = new Lang.Class({
         this._searchChangedId = Application.searchController.connect('search-string-changed',
             Lang.bind(this, this._onSearchStringChanged));
 
-        this._searchEntry.connect('destroy', Lang.bind(this,
+        this.searchEntry.connect('destroy', Lang.bind(this,
             function() {
                 Application.searchController.disconnect(this._searchChangedId);
             }));
@@ -204,17 +203,19 @@ const OverviewSearchbar = new Lang.Class({
         let dropdown = new Dropdown();
         this._dropdownButton = new Gtk.MenuButton({ popover: dropdown });
 
-        this._searchContainer = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
-                                              halign: Gtk.Align.CENTER });
-        this._searchContainer.get_style_context().add_class('linked');
+        let box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
+                                halign: Gtk.Align.CENTER });
+        box.get_style_context().add_class('linked');
 
-        this._searchContainer.add(this._searchEntry);
-        this._searchContainer.add(this._dropdownButton);
-        this._searchContainer.show_all();
+        box.add(this.searchEntry);
+        box.add(this._dropdownButton);
+        box.show_all();
+
+        return box;
     },
 
     entryChanged: function() {
-        let currentText = this._searchEntry.get_text();
+        let currentText = this.searchEntry.get_text();
 
         Application.searchController.disconnect(this._searchChangedId);
         Application.searchController.setString(currentText);
@@ -225,7 +226,7 @@ const OverviewSearchbar = new Lang.Class({
     },
 
     _onSearchStringChanged: function(controller, string) {
-        this._searchEntry.set_text(string);
+        this.searchEntry.set_text(string);
     },
 
     _onActiveCollectionChanged: function(manager, collection) {
@@ -237,7 +238,7 @@ const OverviewSearchbar = new Lang.Class({
         if (Application.searchController.getString() != '' ||
             searchType.id != 'all') {
             Application.searchTypeManager.setActiveItemById('all');
-            this._searchEntry.set_text('');
+            this.searchEntry.set_text('');
         }
     },
 
@@ -245,13 +246,13 @@ const OverviewSearchbar = new Lang.Class({
         let item = manager.getActiveItem();
 
         if (item.id == 'all') {
-            this._searchEntry.remove_tag(tag);
+            this.searchEntry.remove_tag(tag);
         } else {
             tag.set_label(item.name);
-            this._searchEntry.add_tag(tag);
+            this.searchEntry.add_tag(tag);
         }
 
-        this._searchEntry.grab_focus_without_selecting();
+        this.searchEntry.grab_focus_without_selecting();
     },
 
     _onActiveSourceChanged: function() {


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