[sushi/wip/cosimoc/no-clutter: 55/66] Consolidate window resize policies in MainWindow



commit aaeac47e5546d6838c67112e0c0566ee4f8f988c
Author: Cosimo Cecchi <cosimo endlessm com>
Date:   Thu Jun 6 22:34:58 2019 -0700

    Consolidate window resize policies in MainWindow
    
    Previously through getSizeForAllocation() each renderer could
    tweak the dimensions of the window.
    Add a ResizePolicy enum with all the cases we want to cover, and
    share that code among renderers.

 src/js/ui/fallbackRenderer.js | 24 +++---------------
 src/js/ui/mainWindow.js       | 57 ++++++++++++++++++++++++++++---------------
 src/js/ui/renderer.js         | 21 ++++++++++++++++
 src/js/viewers/audio.js       |  9 +++----
 src/js/viewers/evince.js      |  6 ++---
 src/js/viewers/font.js        | 14 +++--------
 src/js/viewers/gst.js         | 11 +++------
 src/js/viewers/html.js        |  5 ++--
 src/js/viewers/image.js       | 10 +++-----
 src/js/viewers/text.js        |  5 ++--
 10 files changed, 83 insertions(+), 79 deletions(-)
---
diff --git a/src/js/ui/fallbackRenderer.js b/src/js/ui/fallbackRenderer.js
index d9af592..5c9bb30 100644
--- a/src/js/ui/fallbackRenderer.js
+++ b/src/js/ui/fallbackRenderer.js
@@ -31,6 +31,7 @@ const Sushi = imports.gi.Sushi;
 const Gettext = imports.gettext.domain('sushi');
 const _ = Gettext.gettext;
 const Lang = imports.lang;
+const Renderer = imports.ui.renderer;
 
 var FallbackRenderer = new Lang.Class({
     Name: 'FallbackRenderer',
@@ -43,7 +44,6 @@ var FallbackRenderer = new Lang.Class({
         this.moveOnClick = true;
         this.canFullScreen = false;
 
-        this._mainWindow = mainWindow;
         this._lastWidth = 0;
         this._lastHeight = 0;
 
@@ -149,7 +149,6 @@ var FallbackRenderer = new Lang.Class({
             this._updateIcon(this._fileLoader.icon);
 
         this._applyLabels();
-        this._mainWindow.resizeWindow();
     },
 
     _onDestroy : function() {
@@ -162,24 +161,7 @@ var FallbackRenderer = new Lang.Class({
         }
     },
 
-    getSizeForAllocation : function(allocation) {
-        let width = this.get_preferred_width()[1];
-        let height = this.get_preferred_height()[1];
-
-        /* never make it shrink; this could happen when the
-         * spinner hides.
-         */
-        if (width < this._lastWidth)
-            width = this._lastWidth;
-        else
-            this._lastWidth = width;
-
-        if (height < this._lastHeight)
-            height = this._lastHeight;
-        else
-            this._lastHeight = height;
-
-        /* return the natural */
-        return [width, height];
+    get resizePolicy() {
+        return Renderer.ResizePolicy.NAT_SIZE;
     }
 });
diff --git a/src/js/ui/mainWindow.js b/src/js/ui/mainWindow.js
index 445bac5..9d84822 100644
--- a/src/js/ui/mainWindow.js
+++ b/src/js/ui/mainWindow.js
@@ -35,10 +35,13 @@ const Sushi = imports.gi.Sushi;
 
 const Constants = imports.util.constants;
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
+const Utils = imports.ui.utils;
 
 const Embed = new Lang.Class({
     Name: 'Embed',
     Extends: Gtk.Overlay,
+    Signals: { 'size-request': {} },
 
     vfunc_get_request_mode: function() {
         return Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
@@ -50,19 +53,11 @@ const Embed = new Lang.Class({
         min = Math.max(min, Constants.VIEW_MIN);
         nat = Math.max(nat, Constants.VIEW_MIN);
 
-        return [min, nat];
-    }
-});
+        // FIXME: this is wrong, we should only be doing this
+        // when the renderer signals us to do so
+        this.emit('size-request');
 
-var RendererToolbar = new Lang.Class({
-    Name: 'RendererToolbar',
-    Extends: Gtk.Box,
-    CssName: 'toolbar',
-
-    _init : function() {
-        this.parent({ halign: Gtk.Align.CENTER,
-                      hexpand: true });
-        this.get_style_context().add_class('osd');
+        return [min, nat];
     }
 });
 
@@ -73,6 +68,7 @@ var MainWindow = new Lang.Class({
     _init : function(application) {
         this._isFullScreen = false;
         this._renderer = null;
+        this._lastWindowSize = [0, 0];
         this._view = null;
         this._toolbar = null;
         this._toolbarId = 0;
@@ -104,6 +100,7 @@ var MainWindow = new Lang.Class({
                      Lang.bind(this, this._onRealize));
 
         this._embed = new Embed();
+        this._embed.connect('size-request', this._onEmbedSizeRequest.bind(this));
         this.add(this._embed);
     },
 
@@ -159,13 +156,37 @@ var MainWindow = new Lang.Class({
     /**************************************************************************
      *********************** texture allocation *******************************
      **************************************************************************/
-    resizeWindow : function() {
+    _onEmbedSizeRequest : function() {
+        this._resizeWindow();
+    },
+
+    _resizeWindow : function() {
         if (this._isFullScreen)
             return;
 
-        let windowSize = this._renderer.getSizeForAllocation(
-            [Constants.VIEW_MAX_W, Constants.VIEW_MAX_H]);
-        this.resize(windowSize[0], windowSize[1]);
+        if (!this._renderer)
+            return;
+
+        let maxSize = [Constants.VIEW_MAX_W, Constants.VIEW_MAX_H];
+        let rendererSize = [this._renderer.get_preferred_width(), this._renderer.get_preferred_height()];
+        let natSize = [rendererSize[0][1], rendererSize[1][1]];
+        let windowSize;
+        let resizePolicy = this._renderer.resizePolicy;
+
+        if (resizePolicy == Renderer.ResizePolicy.MAX_SIZE)
+            windowSize = maxSize;
+        else if (resizePolicy == Renderer.ResizePolicy.NAT_SIZE)
+            windowSize = natSize;
+        else if (resizePolicy == Renderer.ResizePolicy.SCALED)
+            windowSize = Utils.getScaledSize(natSize, maxSize, false);
+        else if (resizePolicy == Renderer.ResizePolicy.STRETCHED)
+            windowSize = Utils.getScaledSize(natSize, maxSize, true);
+
+        if ((windowSize[0] > 0 && windowSize[0] != this._lastWindowSize[0]) ||
+            (windowSize[1] > 0 && windowSize[1] != this._lastWindowSize[1])) {
+            this._lastWindowSize = windowSize;
+            this.resize(windowSize[0], windowSize[1]);
+        }
     },
 
     _createRenderer : function(file) {
@@ -200,8 +221,6 @@ var MainWindow = new Lang.Class({
         this._renderer.show_all();
         this._renderer.expand = true;
         this._embed.add(this._renderer);
-
-        this.resizeWindow();
     },
 
     /**************************************************************************
@@ -225,7 +244,7 @@ var MainWindow = new Lang.Class({
                                                transition_type: Gtk.RevealerTransitionType.CROSSFADE,
                                                visible: true });
 
-            let rendererToolbar = new RendererToolbar();
+            let rendererToolbar = new Renderer.RendererToolbar();
             this._toolbar.add(rendererToolbar);
 
             this._renderer.populateToolbar(rendererToolbar);
diff --git a/src/js/ui/renderer.js b/src/js/ui/renderer.js
new file mode 100644
index 0000000..5ba3f94
--- /dev/null
+++ b/src/js/ui/renderer.js
@@ -0,0 +1,21 @@
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+
+var ResizePolicy = {
+    MAX_SIZE: 0,
+    NAT_SIZE: 1,
+    SCALED: 2,
+    STRETCHED: 3
+};
+
+var RendererToolbar = new Lang.Class({
+    Name: 'RendererToolbar',
+    Extends: Gtk.Box,
+    CssName: 'toolbar',
+
+    _init : function() {
+        this.parent({ halign: Gtk.Align.CENTER,
+                      hexpand: true });
+        this.get_style_context().add_class('osd');
+    }
+});
diff --git a/src/js/viewers/audio.js b/src/js/viewers/audio.js
index 5410985..9ba3e3b 100644
--- a/src/js/viewers/audio.js
+++ b/src/js/viewers/audio.js
@@ -35,6 +35,7 @@ const Lang = imports.lang;
 
 const Constants = imports.util.constants;
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const TotemMimeTypes = imports.util.totemMimeTypes;
 const Utils = imports.ui.utils;
 
@@ -247,12 +248,8 @@ const AudioRenderer = new Lang.Class({
         }
     },
 
-    getSizeForAllocation : function(allocation) {
-        let width = this.get_preferred_width()[1];
-        let height = this.get_preferred_height()[1];
-
-        /* return the natural */
-        return [ width, height ];
+    get resizePolicy() {
+        return Renderer.ResizePolicy.NAT_SIZE;
     },
 
     populateToolbar : function (toolbar) {
diff --git a/src/js/viewers/evince.js b/src/js/viewers/evince.js
index 8d1e281..7daefd6 100644
--- a/src/js/viewers/evince.js
+++ b/src/js/viewers/evince.js
@@ -34,6 +34,7 @@ const Lang = imports.lang;
 
 const Constants = imports.util.constants;
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const Utils = imports.ui.utils;
 
 const EvinceRenderer = new Lang.Class({
@@ -85,9 +86,8 @@ const EvinceRenderer = new Lang.Class({
         this._view.set_model(this._model);
     },
 
-    getSizeForAllocation : function(allocation) {
-        /* always give the view the maximum possible allocation */
-        return allocation;
+    get resizePolicy() {
+        return Renderer.ResizePolicy.MAX_SIZE;
     },
 
     populateToolbar : function(toolbar) {
diff --git a/src/js/viewers/font.js b/src/js/viewers/font.js
index 1141ce9..2ffdaac 100644
--- a/src/js/viewers/font.js
+++ b/src/js/viewers/font.js
@@ -29,6 +29,7 @@ const Lang = imports.lang;
 
 const Gtk = imports.gi.Gtk;
 const GLib = imports.gi.GLib;
+const Renderer = imports.ui.renderer;
 const Sushi = imports.gi.Sushi;
 
 const FontRenderer = new Lang.Class({
@@ -44,17 +45,8 @@ const FontRenderer = new Lang.Class({
         this._file = file;
     },
 
-    getSizeForAllocation : function(allocation) {
-        let size = [ this.get_preferred_size()[1].width,
-                     this.get_preferred_size()[1].height ];
-
-        if (size[0] > allocation[0])
-            size[0] = allocation[0];
-
-        if (size[1] > allocation[1])
-            size[1] = allocation[1];
-
-        return size;
+    get resizePolicy() {
+        return Renderer.ResizePolicy.MAX_SIZE;
     }
 });
 
diff --git a/src/js/viewers/gst.js b/src/js/viewers/gst.js
index d273ba2..9054bb2 100644
--- a/src/js/viewers/gst.js
+++ b/src/js/viewers/gst.js
@@ -28,6 +28,7 @@ const {GLib, Sushi} = imports.gi;
 const Lang = imports.lang;
 
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const TotemMimeTypes = imports.util.totemMimeTypes;
 const Utils = imports.ui.utils;
 
@@ -42,10 +43,6 @@ const GstRenderer = new Lang.Class({
         // fullscreen is handled internally by the widget
         this.canFullScreen = false;
 
-        this.connect('size-change', function() {
-            mainWindow.resizeWindow();
-        });
-
         this._autoplayId = GLib.idle_add(0, () => {
             this._autoplayId = 0;
             this.play();
@@ -62,10 +59,8 @@ const GstRenderer = new Lang.Class({
         }
     },
 
-    getSizeForAllocation : function(allocation) {
-        let baseSize = [this.get_preferred_width()[1],
-                        this.get_preferred_height()[1]];
-        return Utils.getScaledSize(baseSize, allocation, true);
+    get resizePolicy() {
+        return Renderer.ResizePolicy.STRETCHED;
     }
 });
 
diff --git a/src/js/viewers/html.js b/src/js/viewers/html.js
index aae7999..c6cebc4 100644
--- a/src/js/viewers/html.js
+++ b/src/js/viewers/html.js
@@ -30,6 +30,7 @@ const Sushi = imports.gi.Sushi;
 const WebKit = imports.gi.WebKit2;
 
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const Utils = imports.ui.utils;
 
 const HTMLRenderer = new Lang.Class({
@@ -52,8 +53,8 @@ const HTMLRenderer = new Lang.Class({
         this.load_uri(file.get_uri());
     },
 
-    getSizeForAllocation : function(allocation) {
-        return allocation;
+    get resizePolicy() {
+        return Renderer.ResizePolicy.MAX_SIZE;
     },
 
     populateToolbar : function(toolbar) {
diff --git a/src/js/viewers/image.js b/src/js/viewers/image.js
index 1baefdf..a03f947 100644
--- a/src/js/viewers/image.js
+++ b/src/js/viewers/image.js
@@ -37,6 +37,7 @@ const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const Utils = imports.ui.utils;
 
 const Image = new Lang.Class({
@@ -181,13 +182,8 @@ const ImageRenderer = new Lang.Class({
          }));
     },
 
-    getSizeForAllocation : function(allocation) {
-        if (!this.pix)
-            return allocation;
-
-        let width = this.pix.get_width();
-        let height = this.pix.get_height();
-        return Utils.getScaledSize([width, height], allocation, false);
+    get resizePolicy() {
+        return Renderer.ResizePolicy.SCALED;
     },
 
     _startTimeout : function() {
diff --git a/src/js/viewers/text.js b/src/js/viewers/text.js
index 78106d3..edc263c 100644
--- a/src/js/viewers/text.js
+++ b/src/js/viewers/text.js
@@ -34,6 +34,7 @@ const Lang = imports.lang;
 const Sushi = imports.gi.Sushi;
 
 const MimeHandler = imports.ui.mimeHandler;
+const Renderer = imports.ui.renderer;
 const Utils = imports.ui.utils;
 
 const TextRenderer = new Lang.Class({
@@ -92,8 +93,8 @@ const TextRenderer = new Lang.Class({
             this._view.set_show_line_numbers(true);
     },
 
-    getSizeForAllocation : function(allocation) {
-        return allocation;
+    get resizePolicy() {
+        return Renderer.ResizePolicy.MAX_SIZE;
     },
 
     populateToolbar : function(toolbar) {


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