[sushi] Move toolbar creation to the renderer



commit e090261d838248b802feae0ca1c1dbd1ef3615c8
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat Jun 22 15:08:26 2019 -0700

    Move toolbar creation to the renderer
    
    Instead of the window class. This allows us to remove some
    complexity from the window code, and will make it easier for us to
    implement the behavior of cancelling the autohide timeout on the
    toolbar once one of its buttons is clicked.

 src/ui/mainWindow.js | 72 ++++------------------------------------------------
 src/ui/renderer.js   | 63 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 69 deletions(-)
---
diff --git a/src/ui/mainWindow.js b/src/ui/mainWindow.js
index 11eb8e8..17e99c7 100644
--- a/src/ui/mainWindow.js
+++ b/src/ui/mainWindow.js
@@ -25,8 +25,6 @@
 
 const {Gdk, Gio, GLib, GObject, Gtk, Sushi} = imports.gi;
 
-const Mainloop = imports.mainloop;
-
 const Constants = imports.util.constants;
 const MimeHandler = imports.ui.mimeHandler;
 const Renderer = imports.ui.renderer;
@@ -122,8 +120,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
     _init(application) {
         this._renderer = null;
         this._lastWindowSize = [0, 0];
-        this._toolbar = null;
-        this._toolbarId = 0;
         this.file = null;
 
         super._init({ type: Gtk.WindowType.TOPLEVEL,
@@ -141,7 +137,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
         this._openButton.connect('clicked', this._onFileOpenClicked.bind(this));
         this._titlebar.pack_end(this._openButton);
 
-        this.connect('destroy', this._onDestroy.bind(this));
         this.connect('key-press-event', this._onKeyPressEvent.bind(this));
         this.connect('motion-notify-event', this._onMotionNotifyEvent.bind(this));
         this.connect('realize', this._onRealize.bind(this));
@@ -157,10 +152,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
     /**************************************************************************
      ****************** main object event callbacks ***************************
      **************************************************************************/
-    _onDestroy() {
-        this._removeToolbarTimeout();
-    }
-
     _onRealize() {
         // don't support maximize and minimize
         this.get_window().set_functions(Gdk.WMFunction.MOVE |
@@ -197,9 +188,8 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
     }
 
     _onMotionNotifyEvent() {
-        if (this._toolbar)
-            this._resetToolbar();
-
+        if (this._renderer.toolbar)
+            this._renderer.toolbar.resetTimeout();
         return false;
     }
 
@@ -212,8 +202,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
     }
 
     _onRendererFullscreen() {
-        this._removeToolbarTimeout();
-
         if (this._renderer.fullscreen)
             this.fullscreen();
         else
@@ -272,7 +260,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
                 try {
                     let fileInfo = obj.query_info_finish(res);
                     this._createView(fileInfo);
-                    this._createToolbar();
                 } catch(e) {
                     this._reportError(e);
                 }
@@ -289,6 +276,9 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
         this._renderer.show_all();
         this._renderer.expand = true;
         this._embed.add(this._renderer);
+
+        if (this._renderer.toolbar)
+            this._embed.add_overlay(this._renderer.toolbar);
     }
 
     _createView(fileInfo) {
@@ -308,58 +298,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
     /**************************************************************************
      ************************* toolbar helpers ********************************
      **************************************************************************/
-    _createToolbar() {
-        this._removeToolbarTimeout();
-
-        if (this._toolbar) {
-            this._toolbar.destroy();
-            this._toolbar = null;
-        }
-
-        if (this._renderer.populateToolbar) {
-            this._toolbar = new Gtk.Revealer({ valign: Gtk.Align.END,
-                                               hexpand: true,
-                                               margin_bottom: Constants.TOOLBAR_SPACING,
-                                               margin_start: Constants.TOOLBAR_SPACING,
-                                               margin_end: Constants.TOOLBAR_SPACING,
-                                               transition_duration: 250,
-                                               transition_type: Gtk.RevealerTransitionType.CROSSFADE,
-                                               visible: true });
-
-            let rendererToolbar = new Renderer.RendererToolbar();
-            this._toolbar.add(rendererToolbar);
-
-            this._renderer.populateToolbar(rendererToolbar);
-            this._toolbar.show_all();
-        }
-
-        if (!this._toolbar)
-            return;
-
-        this._embed.add_overlay(this._toolbar);
-    }
-
-    _removeToolbarTimeout() {
-        if (this._toolbarId != 0) {
-            Mainloop.source_remove(this._toolbarId);
-            this._toolbarId = 0;
-        }
-    }
-
-    _resetToolbar() {
-        if (this._toolbarId == 0)
-            this._toolbar.reveal_child = true;
-
-        this._removeToolbarTimeout();
-        this._toolbarId = Mainloop.timeout_add(1500, this._onToolbarTimeout.bind(this));
-    }
-
-    _onToolbarTimeout() {
-        this._toolbarId = 0;
-        this._toolbar.reveal_child = false;
-        return false;
-    }
-
     _updateTitlebar() {
         try {
             let appInfo = this.file.query_default_handler(null);
diff --git a/src/ui/renderer.js b/src/ui/renderer.js
index 19dc29c..6a66be3 100644
--- a/src/ui/renderer.js
+++ b/src/ui/renderer.js
@@ -1,5 +1,7 @@
 const {GLib, GObject, Gtk} = imports.gi;
 
+const Constants = imports.util.constants;
+
 var ResizePolicy = {
     MAX_SIZE: 0,
     NAT_SIZE: 1,
@@ -58,14 +60,71 @@ var Renderer = GObject.registerClass({
     get resizePolicy() {
         return ResizePolicy.MAX_SIZE;
     }
+
+    get toolbar() {
+        if (!this.populateToolbar)
+            return null;
+
+        if (!this._toolbar) {
+            this._toolbar = new RendererToolbar();
+            this.connect('destroy', () => { this._toolbar.destroy(); });
+            this.populateToolbar(this._toolbar.box);
+        }
+
+        return this._toolbar;
+    }
 });
 
-var RendererToolbar = GObject.registerClass({
+var RendererToolbarBox = GObject.registerClass({
     CssName: 'toolbar',
-}, class RendererToolbar extends Gtk.Box {
+}, class RendererToolbarBox extends Gtk.Box {
     _init() {
         super._init({ halign: Gtk.Align.CENTER,
                       hexpand: true });
         this.get_style_context().add_class('osd');
     }
 });
+
+var RendererToolbar = GObject.registerClass(class RendererToolbar extends Gtk.Revealer {
+    _init() {
+        this._revealTimeoutId = 0;
+
+        super._init({ valign: Gtk.Align.END,
+                      hexpand: true,
+                      margin_bottom: Constants.TOOLBAR_SPACING,
+                      margin_start: Constants.TOOLBAR_SPACING,
+                      margin_end: Constants.TOOLBAR_SPACING,
+                      transition_type: Gtk.RevealerTransitionType.CROSSFADE });
+
+        this.box = new RendererToolbarBox();
+        this.add(this.box);
+
+        this.connect('destroy', this._onDestroy.bind(this));
+    }
+
+    resetTimeout() {
+        if (this._revealTimeoutId == 0)
+            this.reveal_child = true;
+
+        this._removeRevealTimeout();
+        this._revealTimeoutId = GLib.timeout_add(
+            GLib.PRIORITY_DEFAULT, 1500, this._onRevealTimeout.bind(this));
+    }
+
+    _onDestroy() {
+        this._removeRevealTimeout();
+    }
+
+    _onRevealTimeout() {
+        this._revealTimeoutId = 0;
+        this.reveal_child = false;
+        return false;
+    }
+
+    _removeRevealTimeout() {
+        if (this._revealTimeoutId != 0) {
+            GLib.source_remove(this._revealTimeoutId);
+            this._revealTimeoutId = 0;
+        }
+    }
+});


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