[gnome-documents/wip/cosimoc/view-rework: 12/20] edit: make EditView a Preview subclass



commit e2050f00b9a27d13efb36d7eeb83429aadbdb412
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat Aug 6 19:58:28 2016 -0700

    edit: make EditView a Preview subclass
    
    Use the infrastructure previously introduced for previews to also manage
    the Edit mode.

 src/application.js |    2 -
 src/edit.js        |  102 +++++++++++++++++++++++-----------------------------
 src/embed.js       |   30 ++++-----------
 3 files changed, 53 insertions(+), 81 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index e93da59..b483d70 100644
--- a/src/application.js
+++ b/src/application.js
@@ -502,8 +502,6 @@ const Application = new Lang.Class({
               window_modes: [WindowMode.WindowMode.COLLECTIONS,
                              WindowMode.WindowMode.DOCUMENTS,
                              WindowMode.WindowMode.SEARCH] },
-            { name: 'view-current',
-              window_mode: WindowMode.WindowMode.EDIT },
             { name: 'search',
               callback: Utils.actionToggleCallback,
               state: GLib.Variant.new('b', false),
diff --git a/src/edit.js b/src/edit.js
index 02eb0e4..8a8a2c1 100644
--- a/src/edit.js
+++ b/src/edit.js
@@ -27,73 +27,78 @@ const Mainloop = imports.mainloop;
 
 const Application = imports.application;
 const MainToolbar = imports.mainToolbar;
+const Preview = imports.preview;
 const WindowMode = imports.windowMode;
 
 const _BLANK_URI = "about:blank";
 
 const EditView = new Lang.Class({
     Name: 'EditView',
-    Extends: Gtk.Overlay,
+    Extends: Preview.Preview,
 
-    _init: function() {
-        this._uri = null;
+    _init: function(overlay, mainWindow) {
+        this.parent(overlay, mainWindow);
 
-        this.parent();
+        let doc = Application.documentManager.getActiveItem();
+        if (doc.uri)
+            this._webView.load_uri(doc.uri);
+    },
 
-        let context = WebKit.WebContext.get_default();
+    createActions: function() {
+        return [
+            { name: 'view-current',
+              callback: Lang.bind(this, this._viewCurrent) }
+        ];
+    },
 
-        let cacheDir = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gnome-documents', 'webkit']);
-        context.set_disk_cache_directory(cacheDir);
+    createView: function() {
+        let overlay = new Gtk.Overlay();
 
-        let cookie_manager = context.get_cookie_manager();
-        let jarfile = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gnome-documents', 'cookies.sqlite']);
-        cookie_manager.set_persistent_storage(jarfile, WebKit.CookiePersistentStorage.SQLITE);
+        this._webView = new WebKit.WebView();
+        overlay.add(this._webView);
+        this._webView.show();
+        this._webView.connect('notify::estimated-load-progress', Lang.bind(this, this._onProgressChanged));
 
         this._progressBar = new Gtk.ProgressBar({ halign: Gtk.Align.FILL,
                                                   valign: Gtk.Align.START });
         this._progressBar.get_style_context().add_class('osd');
-        this.add_overlay(this._progressBar);
-
-        this._createView();
-
-        this.show_all();
+        overlay.add_overlay(this._progressBar);
+        this._progressBar.show();
 
-        this._viewAction = Application.application.lookup_action('view-current');
-        this._viewAction.enabled = false;
-        this._viewAction.connect('activate', Lang.bind(this,
-            function() {
-                Application.modeController.goBack();
-            }));
+        let context = this._webView.get_context();
+        let cacheDir = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gnome-documents', 'webkit']);
+        context.set_disk_cache_directory(cacheDir);
 
-        Application.documentManager.connect('load-started',
-                                            Lang.bind(this, this._onLoadStarted));
-        Application.documentManager.connect('load-finished',
-                                            Lang.bind(this, this._onLoadFinished));
+        let cookie_manager = context.get_cookie_manager();
+        let jarfile = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gnome-documents', 'cookies.sqlite']);
+        cookie_manager.set_persistent_storage(jarfile, WebKit.CookiePersistentStorage.SQLITE);
+        overlay.show_all();
+        return overlay;
+    },
 
+    createToolbar: function() {
+        return new EditToolbar();
     },
 
-    _onLoadStarted: function() {
-        this._viewAction.enabled = false;
+    onLoadStarted: function() {
+        this.getAction('view-current').enabled = false;
     },
 
-    _onLoadFinished: function(manager, doc, docModel) {
+    onLoadFinished: function(manager, doc) {
         if (doc.uri)
-            this._viewAction.enabled = true;
+            this.getAction('view-current').enabled = true;
     },
 
-    _createView: function() {
-        this.view = new WebKit.WebView();
-        this.add(this.view);
-        this.view.show();
-        this.view.connect('notify::estimated-load-progress', Lang.bind(this, this._onProgressChanged));
+    _viewCurrent: function() {
+        Application.modeController.goBack();
     },
 
     _onProgressChanged: function() {
-        if (!this.view.uri || this.view.uri == _BLANK_URI)
+        if (!this._webView.uri || this._webView.uri == _BLANK_URI)
             return;
 
-        let progress = this.view.estimated_load_progress;
-        let loading = this.view.is_loading;
+        let progress = this._webView.estimated_load_progress;
+        let loading = this._webView.is_loading;
 
         if (progress == 1.0 || !loading) {
             if (!this._timeoutId)
@@ -115,31 +120,14 @@ const EditView = new Lang.Class({
         this._timeoutId = 0;
         this._progressBar.hide();
         return false;
-    },
-
-    setUri: function(uri) {
-        if (this._uri == uri)
-            return;
-
-        if (!uri)
-            uri = _BLANK_URI;
-
-        this._uri = uri;
-        this.view.load_uri (uri);
-    },
-
-    getUri: function() {
-        return this._uri;
-    },
+    }
 });
 
 const EditToolbar = new Lang.Class({
     Name: 'EditToolbar',
     Extends: MainToolbar.MainToolbar,
 
-    _init: function(editView) {
-        this._editView = editView;
-
+    _init: function() {
         this.parent();
         this.toolbar.set_show_close_button(true);
 
@@ -152,7 +140,7 @@ const EditToolbar = new Lang.Class({
             }));
 
         let viewButton = new Gtk.Button({ label: _("View"),
-                                          action_name: 'app.view-current' });
+                                          action_name: 'view.view-current' });
         viewButton.get_style_context().add_class('suggested-action');
         this.toolbar.pack_end(viewButton);
 
diff --git a/src/embed.js b/src/embed.js
index 5ffcc18..debc0a9 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -85,9 +85,6 @@ const Embed = new Lang.Class({
         this._search = new View.ViewContainer(WindowMode.WindowMode.SEARCH);
         this._stack.add_named(this._search, 'search');
 
-        this._edit = new Edit.EditView();
-        this._stack.add_named(this._edit, 'edit');
-
         this._spinner = new Gtk.Spinner({ width_request: _ICON_SIZE,
                                           height_request: _ICON_SIZE,
                                           halign: Gtk.Align.CENTER,
@@ -216,6 +213,11 @@ const Embed = new Lang.Class({
     },
 
     _onVisibleChildChanged: function() {
+        // Avoid switching by accident if we just happen to destroy
+        // the previous view
+        if (this._clearingView)
+            return;
+
         let visibleChild = this._stack.visible_child;
         let windowMode = WindowMode.WindowMode.NONE;
 
@@ -251,7 +253,7 @@ const Embed = new Lang.Class({
             this._prepareForPreview(EPUBView.EPUBView);
             break;
         case WindowMode.WindowMode.EDIT:
-            this._prepareForEdit();
+            this._prepareForPreview(Edit.EditView);
             break;
         case WindowMode.WindowMode.NONE:
             break;
@@ -351,14 +353,14 @@ const Embed = new Lang.Class({
     },
 
     _clearViewState: function() {
+        this._clearingView = true;
         if (this._preview) {
             this._preview.destroy();
             this._preview = null;
         }
-        if (this._edit)
-            this._edit.setUri(null);
 
         this._window.insert_action_group('view', null);
+        this._clearingView = false;
     },
 
     _prepareForOverview: function(newMode, oldMode) {
@@ -416,22 +418,6 @@ const Embed = new Lang.Class({
         this._currentView = this._preview;
     },
 
-    _prepareForEdit: function() {
-        this._clearViewState();
-        if (this._toolbar)
-            this._toolbar.destroy();
-
-        let doc = Application.documentManager.getActiveItem();
-        this._edit.setUri(doc.uri);
-
-        // pack the toolbar
-        this._toolbar = new Edit.EditToolbar(this._preview);
-        this._titlebar.add(this._toolbar);
-
-        this._stack.set_visible_child_name('edit');
-        this._currentView = this._edit;
-    },
-
     getMainToolbar: function() {
         let windowMode = Application.modeController.getWindowMode();
         let fullscreen = Application.modeController.getFullscreen();


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