[sushi/wip/cosimoc/no-clutter: 34/50] Change the way the main window is resized



commit 39f74d1c55f6a26aff79aa28f9dacbed98de016c
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Apr 3 12:46:23 2017 -0700

    Change the way the main window is resized
    
    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 |  34 ++-----------
 src/js/ui/mainWindow.js       | 110 +++++++++++++++++++++++-------------------
 src/js/ui/renderer.js         |  21 ++++++++
 src/js/util/constants.js      |   2 -
 src/js/viewers/audio.js       |  16 ++----
 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 +-
 11 files changed, 107 insertions(+), 127 deletions(-)
---
diff --git a/src/js/ui/fallbackRenderer.js b/src/js/ui/fallbackRenderer.js
index 94bc063..ec724ac 100644
--- a/src/js/ui/fallbackRenderer.js
+++ b/src/js/ui/fallbackRenderer.js
@@ -31,8 +31,7 @@ const Sushi = imports.gi.Sushi;
 const Gettext = imports.gettext.domain('sushi');
 const _ = Gettext.gettext;
 const Lang = imports.lang;
-
-const Constants = imports.util.constants;
+const Renderer = imports.ui.renderer;
 
 var FallbackRenderer = new Lang.Class({
     Name: 'FallbackRenderer',
@@ -45,10 +44,6 @@ var FallbackRenderer = new Lang.Class({
         this.moveOnClick = true;
         this.canFullScreen = false;
 
-        this._mainWindow = mainWindow;
-        this._lastWidth = 0;
-        this._lastHeight = 0;
-
         this._fileLoader = new Sushi.FileLoader();
         this._fileLoader.file = file;
         this._fileLoaderId =
@@ -151,7 +146,6 @@ var FallbackRenderer = new Lang.Class({
             this._updateIcon(this._fileLoader.icon);
 
         this._applyLabels();
-        this._mainWindow.refreshSize();
     },
 
     _onDestroy : function() {
@@ -164,29 +158,7 @@ var FallbackRenderer = new Lang.Class({
         }
     },
 
-    getSizeForAllocation : function(allocation) {
-        let width = this.get_preferred_width()[1];
-        let height = this.get_preferred_height()[1];
-
-        if (width < Constants.VIEW_MIN &&
-            height < Constants.VIEW_MIN) {
-            width = Constants.VIEW_MIN;
-        }
-
-        /* 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 7e72188..59f7153 100644
--- a/src/js/ui/mainWindow.js
+++ b/src/js/ui/mainWindow.js
@@ -35,16 +35,40 @@ 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;
 
-var RendererToolbar = new Lang.Class({
-    Name: 'RendererToolbar',
-    Extends: Gtk.Box,
-    CssName: 'toolbar',
+const Embed = new Lang.Class({
+    Name: 'Embed',
+    Extends: Gtk.Overlay,
+    Signals: { 'size-request': {} },
 
-    _init : function() {
-        this.parent({ halign: Gtk.Align.CENTER,
-                      hexpand: true });
-        this.get_style_context().add_class('osd');
+    vfunc_get_request_mode: function() {
+        return Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
+    },
+
+    vfunc_get_preferred_width: function() {
+        let [min, nat] = this.parent();
+
+        min = Math.max(min, Constants.VIEW_MIN);
+        nat = Math.max(nat, Constants.VIEW_MIN);
+
+        // FIXME: this is wrong, we should only be doing this
+        // when the renderer signals us to do so
+        this.emit('size-request');
+
+        return [min, nat];
+    }
+
+    vfunc_get_preferred_height_for_width(forWidth) {
+        let [min, nat] = super.vfunc_get_preferred_height_for_width(forWidth);
+
+        if (forWidth <= Constants.VIEW_MIN) {
+            min = Math.max(min, Constants.VIEW_MIN);
+            nat = Math.max(nat, Constants.VIEW_MIN);
+        }
+
+        return [min, nat];
     }
 });
 
@@ -55,6 +79,7 @@ var MainWindow = new Lang.Class({
     _init : function(application) {
         this._isFullScreen = false;
         this._renderer = null;
+        this._lastWindowSize = [0, 0];
         this._toolbar = null;
         this._toolbarId = 0;
         this.file = null;
@@ -87,7 +112,8 @@ var MainWindow = new Lang.Class({
                          Lang.bind(this, this._onButtonPressEvent));
         this.add(eventBox);
 
-        this._embed = new Gtk.Overlay();
+        this._embed = new Embed();
+        this._embed.connect('size-request', this._onEmbedSizeRequest.bind(this));
         eventBox.add(this._embed);
     },
 
@@ -143,45 +169,37 @@ var MainWindow = new Lang.Class({
     /**************************************************************************
      *********************** texture allocation *******************************
      **************************************************************************/
-    _getTextureSize : function() {
-        let screenSize = [ this.get_window().get_width(),
-                           this.get_window().get_height() ];
-
-        let availableWidth = this._isFullScreen ? screenSize[0] : Constants.VIEW_MAX_W - 2 * 
Constants.VIEW_PADDING_X;
-        let availableHeight = this._isFullScreen ? screenSize[1] : Constants.VIEW_MAX_H - 
Constants.VIEW_PADDING_Y;
-
-        let textureSize = this._renderer.getSizeForAllocation([availableWidth, availableHeight], 
this._isFullScreen);
-
-        return textureSize;
-    },
-
-    _getWindowSize : function() {
-        let textureSize = this._getTextureSize();
-        let windowSize = textureSize;
-
-        if (textureSize[0] < (Constants.VIEW_MIN - 2 * Constants.VIEW_PADDING_X) &&
-            textureSize[1] < (Constants.VIEW_MIN - Constants.VIEW_PADDING_Y)) {
-            windowSize = [ Constants.VIEW_MIN, Constants.VIEW_MIN ];
-        } else if (!this._isFullScreen) {
-            windowSize = [ windowSize[0] + 2 * Constants.VIEW_PADDING_X,
-                           windowSize[1] + Constants.VIEW_PADDING_Y ];
-        }
-
-        return windowSize;
+    _onEmbedSizeRequest : function() {
+        this._resizeWindow();
     },
 
-    _positionTexture : function() {
-        let windowSize = this._getWindowSize();
-
-        if (this._lastWindowSize &&
-            windowSize[0] == this._lastWindowSize[0] &&
-            windowSize[1] == this._lastWindowSize[1])
+    _resizeWindow : function() {
+        if (this._isFullScreen)
             return;
 
-        this._lastWindowSize = windowSize;
+        if (!this._renderer)
+            return;
 
-        if (!this._isFullScreen)
+        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) {
@@ -216,8 +234,6 @@ var MainWindow = new Lang.Class({
         this._renderer.show_all();
         this._renderer.expand = true;
         this._embed.add(this._renderer);
-
-        this.refreshSize();
     },
 
     /**************************************************************************
@@ -241,7 +257,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);
@@ -300,10 +316,6 @@ var MainWindow = new Lang.Class({
         this.set_title(label);
     },
 
-    refreshSize : function() {
-        this._positionTexture();
-    },
-
     toggleFullScreen : function() {
         if (!this._renderer.canFullScreen)
             return false;
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/util/constants.js b/src/js/util/constants.js
index 97e9192..5faf38e 100644
--- a/src/js/util/constants.js
+++ b/src/js/util/constants.js
@@ -25,8 +25,6 @@
 
 var VIEW_MIN = 400;
 var VIEW_BACKGROUND_OPACITY = 220;
-var VIEW_PADDING_Y = 28;
-var VIEW_PADDING_X = 2;
 var VIEW_MAX_W = 800;
 var VIEW_MAX_H = 600;
 var TOOLBAR_SPACING = 32;
diff --git a/src/js/viewers/audio.js b/src/js/viewers/audio.js
index d725574..c98d5c5 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;
 
@@ -198,7 +199,7 @@ const AudioRenderer = new Lang.Class({
 
         this._artFetcher.taglist = tags;
 
-        this._mainWindow.refreshSize();
+        this._mainWindow.queue_allocate();
     },
 
     _updateProgressBar : function() {
@@ -249,17 +250,8 @@ const AudioRenderer = new Lang.Class({
         }
     },
 
-    getSizeForAllocation : function(allocation) {
-        let width = this.get_preferred_width()[1];
-        let height = this.get_preferred_height()[1];
-
-        if (width[1] < Constants.VIEW_MIN &&
-            height[1] < Constants.VIEW_MIN) {
-            width[1] = Constants.VIEW_MIN;
-        }
-
-        /* 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 6b9f6dc..1cb4380 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({
@@ -84,9 +85,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 def1c52..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.refreshSize();
-        });
-
         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 1b5ef38..366066b 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({
@@ -182,13 +183,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 132c83f..fa325cd 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;
 
 function _getGeditScheme() {
@@ -95,8 +96,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]