[sushi/wip/cosimoc/no-clutter: 31/50] Make renderers GtkWidgets
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [sushi/wip/cosimoc/no-clutter: 31/50] Make renderers GtkWidgets
- Date: Mon, 17 Jun 2019 18:35:13 +0000 (UTC)
commit ccd47a4e55b2247422e3cd6702baaf238e0ca86d
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Sat Jun 15 12:18:15 2019 -0700
Make renderers GtkWidgets
This simplifies management of renderers, which can be instantiated
directly as widgets after this commit.
src/js/ui/application.js | 4 +--
src/js/ui/fallbackRenderer.js | 25 ++++++++-----------
src/js/ui/mainWindow.js | 57 ++++++++++---------------------------------
src/js/ui/mimeHandler.js | 7 +++---
src/js/ui/utils.js | 2 +-
src/js/viewers/audio.js | 26 +++++++++-----------
src/js/viewers/evince.js | 21 +++++++---------
src/js/viewers/font.js | 18 ++++++--------
src/js/viewers/gst.js | 22 ++++++++---------
src/js/viewers/html.js | 18 ++++++--------
src/js/viewers/image.js | 25 +++++++++----------
src/js/viewers/text.js | 14 ++++-------
12 files changed, 91 insertions(+), 148 deletions(-)
---
diff --git a/src/js/ui/application.js b/src/js/ui/application.js
index 6cb8ef5..be566db 100644
--- a/src/js/ui/application.js
+++ b/src/js/ui/application.js
@@ -88,7 +88,7 @@ var Application = new Lang.Class({
},
Close: function() {
- this._mainWindow.close();
+ this._mainWindow.destroy();
},
ShowFile : function(uri, xid, closeIfAlreadyShown) {
@@ -96,7 +96,7 @@ var Application = new Lang.Class({
if (closeIfAlreadyShown &&
this._mainWindow.file &&
this._mainWindow.file.equal(file)) {
- this._mainWindow.close();
+ this._mainWindow.destroy();
return;
}
this._mainWindow.setParent(xid);
diff --git a/src/js/ui/fallbackRenderer.js b/src/js/ui/fallbackRenderer.js
index 4968700..94bc063 100644
--- a/src/js/ui/fallbackRenderer.js
+++ b/src/js/ui/fallbackRenderer.js
@@ -36,16 +36,15 @@ const Constants = imports.util.constants;
var FallbackRenderer = new Lang.Class({
Name: 'FallbackRenderer',
+ Extends: Gtk.Box,
- _init : function() {
- this._fileLoader = null;
- this._fileLoaderId = 0;
+ _init : function(file, mainWindow) {
+ this.parent({ orientation: Gtk.Orientation.HORIZONTAL,
+ spacing: 6 });
this.moveOnClick = true;
this.canFullScreen = false;
- },
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._lastWidth = 0;
this._lastHeight = 0;
@@ -56,10 +55,8 @@ var FallbackRenderer = new Lang.Class({
this._fileLoader.connect('notify',
Lang.bind(this, this._onFileInfoChanged));
- this._box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
- spacing: 6 });
this._image = new Gtk.Image();
- this._box.pack_start(this._image, false, false, 0);
+ this.pack_start(this._image, false, false, 0);
this._updateIcon(new Gio.ThemedIcon({ name: 'text-x-generic' }));
let vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL,
@@ -67,7 +64,7 @@ var FallbackRenderer = new Lang.Class({
margin_top: 48,
margin_start: 12,
margin_end: 12 });
- this._box.pack_start(vbox, false, false, 0);
+ this.pack_start(vbox, false, false, 0);
let hbox = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6 });
@@ -96,9 +93,7 @@ var FallbackRenderer = new Lang.Class({
this._applyLabels();
- this._box.show_all();
-
- return this._box;
+ this.connect('destroy', this._onDestroy.bind(this));
},
_applyLabels : function() {
@@ -159,7 +154,7 @@ var FallbackRenderer = new Lang.Class({
this._mainWindow.refreshSize();
},
- clear : function() {
+ _onDestroy : function() {
if (this._fileLoader) {
this._fileLoader.disconnect(this._fileLoaderId);
this._fileLoaderId = 0;
@@ -170,8 +165,8 @@ var FallbackRenderer = new Lang.Class({
},
getSizeForAllocation : function(allocation) {
- let width = this._box.get_preferred_width()[1];
- let height = this._box.get_preferred_height()[1];
+ let width = this.get_preferred_width()[1];
+ let height = this.get_preferred_height()[1];
if (width < Constants.VIEW_MIN &&
height < Constants.VIEW_MIN) {
diff --git a/src/js/ui/mainWindow.js b/src/js/ui/mainWindow.js
index 808eb5e..df9b91d 100644
--- a/src/js/ui/mainWindow.js
+++ b/src/js/ui/mainWindow.js
@@ -43,7 +43,6 @@ var MainWindow = new Lang.Class({
_init : function(application) {
this._isFullScreen = false;
this._renderer = null;
- this._view = null;
this._toolbar = null;
this._toolbarId = 0;
this.file = null;
@@ -84,7 +83,7 @@ var MainWindow = new Lang.Class({
****************** main object event callbacks ***************************
**************************************************************************/
_onDeleteEvent : function() {
- this._clearAndQuit();
+ this.destroy();
},
_onRealize: function() {
@@ -100,7 +99,7 @@ var MainWindow = new Lang.Class({
if (key == Gdk.KEY_Escape ||
key == Gdk.KEY_space ||
key == Gdk.KEY_q)
- this._clearAndQuit();
+ this.destroy();
if (key == Gdk.KEY_f ||
key == Gdk.KEY_F11)
@@ -174,18 +173,6 @@ var MainWindow = new Lang.Class({
},
_createRenderer : function(file) {
- if (this._renderer) {
- if (this._renderer.clear)
- this._renderer.clear();
-
- this._renderer = null;
- }
-
- /* create a temporary spinner renderer, that will timeout and show itself
- * if the loading takes too long.
- */
- this._renderer = new SpinnerBox.SpinnerBox();
-
file.query_info_async
(Gio.FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME + ',' +
Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
@@ -197,27 +184,27 @@ var MainWindow = new Lang.Class({
this.setTitle(this._fileInfo.get_display_name());
/* now prepare the real renderer */
- this._renderer = this._mimeHandler.getObject(this._fileInfo.get_content_type());
- this._createView(file);
+ let klass = this._mimeHandler.getKlass(this._fileInfo.get_content_type());
+ this._createView(file, klass);
this._createToolbar();
} catch(e) {
/* FIXME: report the error */
- logError(e, 'Error calling prepare() on viewer');
+ logError(e, 'Error creating viewer');
}})
);
},
- _createView : function(file) {
- if (this._view) {
- this._view.destroy();
- this._view = null;
+ _createView : function (file, klass) {
+ if (this._renderer) {
+ this._renderer.destroy()
+ this._renderer = null;
}
- this._view = this._renderer.render(file, this);
- this._view.expand = true;
- this._view.show();
+ this._renderer = new klass(file, this);
+ this._renderer.show_all();
+ this._renderer.expand = true;
+ this._embed.add(this._renderer);
- this._embed.add(this._view);
this.refreshSize();
},
@@ -281,16 +268,6 @@ var MainWindow = new Lang.Class({
return false;
},
- /**************************************************************************
- *********************** Window move/fade helpers *************************
- **************************************************************************/
- _clearAndQuit : function() {
- if (this._renderer.clear)
- this._renderer.clear();
-
- this.destroy();
- },
-
/**************************************************************************
************************ public methods **********************************
**************************************************************************/
@@ -308,10 +285,6 @@ var MainWindow = new Lang.Class({
setFile : function(file) {
this.file = file;
this._createRenderer(file);
- this._createView();
- this._createToolbar();
-
- this.show_all();
},
setTitle : function(label) {
@@ -337,9 +310,5 @@ var MainWindow = new Lang.Class({
}
return this._isFullScreen;
- },
-
- close : function() {
- this._clearAndQuit();
}
});
diff --git a/src/js/ui/mimeHandler.js b/src/js/ui/mimeHandler.js
index f82264d..b7542e9 100644
--- a/src/js/ui/mimeHandler.js
+++ b/src/js/ui/mimeHandler.js
@@ -58,11 +58,10 @@ MimeHandler.prototype = {
this.registerMime(mimeTypes[idx], klass);
},
- getObject: function(mime) {
+ getKlass: function(mime) {
if (this._mimeTypes[mime]) {
/* first, try a direct match with the mimetype itself */
- let klass = this._mimeTypes[mime];
- return new klass();
+ return this._mimeTypes[mime];
} else {
/* if this fails, try to see if we have any handlers
* registered for a parent type.
@@ -73,7 +72,7 @@ MimeHandler.prototype = {
}
/* finally, resort to the fallback renderer */
- return new FallbackRenderer.FallbackRenderer();
+ return FallbackRenderer.FallbackRenderer;
}
}
}
diff --git a/src/js/ui/utils.js b/src/js/ui/utils.js
index 007142a..ce25f4f 100644
--- a/src/js/ui/utils.js
+++ b/src/js/ui/utils.js
@@ -90,7 +90,7 @@ function createOpenButton(file, mainWindow) {
file.get_uri(),
timestamp);
- mainWindow.close();
+ mainWindow.destroy();
} catch (e) {
log('Unable to execute the default application for ' +
file.get_uri() + ' : ' + e.toString());
diff --git a/src/js/viewers/audio.js b/src/js/viewers/audio.js
index b84d058..ebe3b0c 100644
--- a/src/js/viewers/audio.js
+++ b/src/js/viewers/audio.js
@@ -56,30 +56,30 @@ function _formatTimeString(timeVal) {
const AudioRenderer = new Lang.Class({
Name: 'AudioRenderer',
+ Extends: Gtk.Box,
+
+ _init : function(file, mainWindow) {
+ this.parent({ orientation: Gtk.Orientation.HORIZONTAL,
+ spacing: 6 });
- _init : function() {
this.moveOnClick = true;
this.canFullScreen = false;
- },
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._file = file;
this._createPlayer(file);
- this._box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
- spacing: 6 });
this._image = new Gtk.Image({ icon_name: 'media-optical-symbolic',
pixel_size: 256 });
- this._box.pack_start(this._image, false, false, 0);
+ this.pack_start(this._image, false, false, 0);
let vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL,
spacing: 1,
margin_top: 48,
margin_start: 12,
margin_end: 12 });
- this._box.pack_start(vbox, false, false, 0);
+ this.pack_start(vbox, false, false, 0);
this._titleLabel = new Gtk.Label();
this._titleLabel.set_halign(Gtk.Align.START);
@@ -93,9 +93,7 @@ const AudioRenderer = new Lang.Class({
this._albumLabel.set_halign(Gtk.Align.START);
vbox.pack_start(this._albumLabel, false, false, 0);
- this._box.show_all();
-
- return this._box;
+ this.connect('destroy', this._onDestroy.bind(this));
},
_createPlayer : function(file) {
@@ -121,7 +119,7 @@ const AudioRenderer = new Lang.Class({
Lang.bind(this, this._onCoverArtChanged)));
},
- clear : function(file) {
+ _onDestroy : function() {
this._playerNotifies.forEach(Lang.bind(this,
function(id) {
this._player.disconnect(id);
@@ -251,8 +249,8 @@ const AudioRenderer = new Lang.Class({
},
getSizeForAllocation : function(allocation) {
- let width = this._box.get_preferred_width();
- let height = this._box.get_preferred_height();
+ 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) {
@@ -260,7 +258,7 @@ const AudioRenderer = new Lang.Class({
}
/* return the natural */
- return [ width[1], height[1] ];
+ return [ width, height ];
},
populateToolbar : function (toolbar) {
diff --git a/src/js/viewers/evince.js b/src/js/viewers/evince.js
index 1c5ed9f..883f26b 100644
--- a/src/js/viewers/evince.js
+++ b/src/js/viewers/evince.js
@@ -38,15 +38,16 @@ const Utils = imports.ui.utils;
const EvinceRenderer = new Lang.Class({
Name: 'EvinceRenderer',
+ Extends: Gtk.ScrolledWindow,
- _init : function(args) {
- EvDoc.init();
+ _init : function(file, mainWindow) {
+ this.parent({ visible: true,
+ min_content_height: Constants.VIEW_MIN,
+ min_content_width: Constants.VIEW_MIN });
this.moveOnClick = false;
this.canFullScreen = true;
- },
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._file = file;
@@ -55,16 +56,11 @@ const EvinceRenderer = new Lang.Class({
Lang.bind(this, this._onDocumentLoaded));
this._pdfLoader.uri = file.get_uri();
- this._scrolledWin = new Gtk.ScrolledWindow();
- this._scrolledWin.set_min_content_width(Constants.VIEW_MIN);
- this._scrolledWin.set_min_content_height(Constants.VIEW_MIN);
- this._scrolledWin.show();
-
this._view = EvView.View.new();
this._view.show();
- this._scrolledWin.add(this._view);
+ this.add(this._view);
- return this._scrolledWin;
+ this.connect('destroy', this._onDestroy.bind(this));
},
_updatePageLabel : function() {
@@ -135,13 +131,14 @@ const EvinceRenderer = new Lang.Class({
toolbar.insert(toolbarZoom, -1);
},
- clear : function() {
+ _onDestroy : function() {
this._pdfLoader = null;
}
});
let handler = new MimeHandler.MimeHandler();
+EvDoc.init();
let mimeTypes = Sushi.query_supported_document_types();
handler.registerMimeTypes(mimeTypes, EvinceRenderer);
diff --git a/src/js/viewers/font.js b/src/js/viewers/font.js
index 666d9ac..1141ce9 100644
--- a/src/js/viewers/font.js
+++ b/src/js/viewers/font.js
@@ -33,24 +33,20 @@ const Sushi = imports.gi.Sushi;
const FontRenderer = new Lang.Class({
Name: 'FontRenderer',
+ Extends: Sushi.FontWidget,
+
+ _init : function(file, mainWindow) {
+ this.parent({ uri: file.get_uri(),
+ visible: true })
- _init : function(args) {
this.moveOnClick = true;
this.canFullScreen = true;
- },
-
- render : function(file, mainWindow) {
this._file = file;
-
- this._fontWidget = new Sushi.FontWidget({ uri: file.get_uri() });
- this._fontWidget.show();
-
- return this._fontWidget;
},
getSizeForAllocation : function(allocation) {
- let size = [ this._fontWidget.get_preferred_size()[1].width,
- this._fontWidget.get_preferred_size()[1].height ];
+ let size = [ this.get_preferred_size()[1].width,
+ this.get_preferred_size()[1].height ];
if (size[0] > allocation[0])
size[0] = allocation[0];
diff --git a/src/js/viewers/gst.js b/src/js/viewers/gst.js
index c6f65fe..def1c52 100644
--- a/src/js/viewers/gst.js
+++ b/src/js/viewers/gst.js
@@ -33,40 +33,38 @@ const Utils = imports.ui.utils;
const GstRenderer = new Lang.Class({
Name: 'GstRenderer',
+ Extends: Sushi.MediaBin,
+
+ _init : function(file, mainWindow) {
+ this.parent({ uri: file.get_uri() });
- _init : function(args) {
this.moveOnClick = true;
// fullscreen is handled internally by the widget
this.canFullScreen = false;
- },
- render : function(file, mainWindow) {
- this._player = new Sushi.MediaBin({ uri: file.get_uri() });
- this._player.connect('size-change', function() {
+ this.connect('size-change', function() {
mainWindow.refreshSize();
});
this._autoplayId = GLib.idle_add(0, () => {
this._autoplayId = 0;
- this._player.play();
+ this.play();
return false;
});
- return this._player;
+ this.connect('destroy', this._onDestroy.bind(this));
},
- clear : function() {
+ _onDestroy : function() {
if (this._autoplayId > 0) {
GLib.source_remove(this._autoplayId);
this._autoplayId = 0;
- } else {
- this._player.stop();
}
},
getSizeForAllocation : function(allocation) {
- let baseSize = [this._player.get_preferred_width()[1],
- this._player.get_preferred_height()[1]];
+ let baseSize = [this.get_preferred_width()[1],
+ this.get_preferred_height()[1]];
return Utils.getScaledSize(baseSize, allocation, true);
}
});
diff --git a/src/js/viewers/html.js b/src/js/viewers/html.js
index 6360b9d..b3a3ca4 100644
--- a/src/js/viewers/html.js
+++ b/src/js/viewers/html.js
@@ -34,26 +34,22 @@ const Utils = imports.ui.utils;
const HTMLRenderer = new Lang.Class({
Name: 'HTMLRenderer',
+ Extends: WebKit.WebView,
+
+ _init : function(file, mainWindow) {
+ this.parent();
- _init : function(args) {
this.moveOnClick = false;
this.canFullScreen = true;
- },
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._file = file;
- this._webView = new WebKit.WebView();
- this._webView.show_all();
-
/* disable the default context menu of the web view */
- this._webView.connect ("context-menu",
- function() {return true;});
-
- this._webView.load_uri(file.get_uri());
+ this.connect('context-menu',
+ function() {return true;});
- return this._webView;
+ this.load_uri(file.get_uri());
},
getSizeForAllocation : function(allocation) {
diff --git a/src/js/viewers/image.js b/src/js/viewers/image.js
index f3d24b5..1f45110 100644
--- a/src/js/viewers/image.js
+++ b/src/js/viewers/image.js
@@ -128,24 +128,24 @@ const Image = new Lang.Class({
const ImageRenderer = new Lang.Class({
Name: 'ImageRenderer',
+ Extends: Image,
+
+ _init : function(file, mainWindow) {
+ this.parent();
- _init : function(args) {
this._timeoutId = 0;
this.moveOnClick = true;
this.canFullScreen = true;
- },
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._file = file;
this._createImageTexture(file);
- return this._texture;
+
+ this.connect('destroy', this._onDestroy.bind(this));
},
_createImageTexture : function(file) {
- this._texture = new Image();
-
file.read_async
(GLib.PRIORITY_DEFAULT, null,
Lang.bind(this,
@@ -166,8 +166,7 @@ const ImageRenderer = new Lang.Class({
let anim = GdkPixbuf.PixbufAnimation.new_from_stream_finish(res);
this._iter = anim.get_iter(null);
- let pix = this._iter.get_pixbuf().apply_embedded_orientation();
- this._texture.pix = pix;
+ this.pix = this._iter.get_pixbuf().apply_embedded_orientation();
if (!anim.is_static_image())
this._startTimeout();
@@ -184,11 +183,11 @@ const ImageRenderer = new Lang.Class({
},
getSizeForAllocation : function(allocation) {
- if (!this._texture.pix)
+ if (!this.pix)
return allocation;
- let width = this._texture.pix.get_width();
- let height = this._texture.pix.get_height();
+ let width = this.pix.get_width();
+ let height = this.pix.get_height();
return Utils.getScaledSize([width, height], allocation, false);
},
@@ -203,7 +202,7 @@ const ImageRenderer = new Lang.Class({
toolbar.insert(toolbarZoom, 0);
},
- destroy : function () {
+ _onDestroy : function () {
/* We should do the check here because it is possible
* that we never created a source if our image is
* not animated. */
@@ -216,7 +215,7 @@ const ImageRenderer = new Lang.Class({
_advanceImage : function () {
this._iter.advance(null);
let pix = this._iter.get_pixbuf().apply_embedded_orientation();
- this._texture.set_from_pixbuf(pix);
+ this.set_from_pixbuf(pix);
return true;
},
});
diff --git a/src/js/viewers/text.js b/src/js/viewers/text.js
index 5e47029..62785e1 100644
--- a/src/js/viewers/text.js
+++ b/src/js/viewers/text.js
@@ -52,13 +52,13 @@ function _getGeditScheme() {
const TextRenderer = new Lang.Class({
Name: 'TextRenderer',
+ Extends: Gtk.ScrolledWindow,
+
+ _init : function(file, mainWindow) {
+ this.parent();
- _init : function(args) {
this.moveOnClick = false;
this.canFullScreen = true;
- },
-
- render : function(file, mainWindow) {
this._mainWindow = mainWindow;
this._file = file;
@@ -79,11 +79,7 @@ const TextRenderer = new Lang.Class({
return false;
}));
- this._scrolledWin = new Gtk.ScrolledWindow();
- this._scrolledWin.add(this._view);
- this._scrolledWin.show_all();
-
- return this._scrolledWin;
+ this.add(this._view);
},
_onBufferLoaded : function(loader, buffer) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]