[sushi/wip/cosimoc/no-clutter: 59/66] Simplify how renderers are registered
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [sushi/wip/cosimoc/no-clutter: 59/66] Simplify how renderers are registered
- Date: Sat, 8 Jun 2019 18:45:06 +0000 (UTC)
commit 59ab0bdfd58c966d11b399cd5c4447dbd66a698c
Author: Cosimo Cecchi <cosimo endlessm com>
Date: Fri Jun 7 12:46:06 2019 -0700
Simplify how renderers are registered
Right now we create a C binary for the only purpose of registering
the renderers beforehand.
That does not seem necessary; reimplement this in JS and simplify
MimeHandler while at it, so that we can remove the C binary in a
later commit.
src/js/ui/mainWindow.js | 4 +--
src/js/ui/mimeHandler.js | 74 ++++++++++++++++++------------------------------
src/js/viewers/audio.js | 7 ++---
src/js/viewers/evince.js | 10 ++-----
src/js/viewers/font.js | 9 ++----
src/js/viewers/gst.js | 7 ++---
src/js/viewers/html.js | 9 ++----
src/js/viewers/image.js | 12 +++-----
src/js/viewers/text.js | 13 ++-------
src/main.c | 38 -------------------------
10 files changed, 46 insertions(+), 137 deletions(-)
---
diff --git a/src/js/ui/mainWindow.js b/src/js/ui/mainWindow.js
index 05a3525..8a4ebbc 100644
--- a/src/js/ui/mainWindow.js
+++ b/src/js/ui/mainWindow.js
@@ -63,8 +63,6 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
this._toolbarId = 0;
this.file = null;
- this._mimeHandler = new MimeHandler.MimeHandler();
-
super._init({ type: Gtk.WindowType.TOPLEVEL,
skipPagerHint: true,
skipTaskbarHint: true,
@@ -184,7 +182,7 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
this.setTitle(this._fileInfo.get_display_name());
/* now prepare the real renderer */
- let klass = this._mimeHandler.getKlass(this._fileInfo.get_content_type());
+ let klass = MimeHandler.getKlass(this._fileInfo.get_content_type());
this._createView(file, klass);
this._createToolbar();
} catch(e) {
diff --git a/src/js/ui/mimeHandler.js b/src/js/ui/mimeHandler.js
index c3bad7a..0abac08 100644
--- a/src/js/ui/mimeHandler.js
+++ b/src/js/ui/mimeHandler.js
@@ -27,50 +27,32 @@ const {Gio} = imports.gi;
const FallbackRenderer = imports.ui.fallbackRenderer;
-let _mimeHandler = null;
-
-function MimeHandler() {
- if (_mimeHandler == null) {
- this._init();
- _mimeHandler = this;
- }
-
- return _mimeHandler;
-}
-
-function init() {
- let handler = new MimeHandler();
-}
-
-MimeHandler.prototype = {
- _init: function() {
- this._mimeTypes = [];
- },
-
- registerMime: function(mime, klass) {
- this._mimeTypes[mime] = klass;
- },
-
- registerMimeTypes: function(mimeTypes, klass) {
- for (let idx in mimeTypes)
- this.registerMime(mimeTypes[idx], klass);
- },
-
- getKlass: function(mime) {
- if (this._mimeTypes[mime]) {
- /* first, try a direct match with the mimetype itself */
- return this._mimeTypes[mime];
- } else {
- /* if this fails, try to see if we have any handlers
- * registered for a parent type.
- */
- for (let key in this._mimeTypes) {
- if (Gio.content_type_is_a (mime, key))
- return this._mimeTypes[key];
- }
-
- /* finally, resort to the fallback renderer */
- return FallbackRenderer.FallbackRenderer;
- }
- }
+var renderers = [];
+renderers.push(imports.viewers.audio);
+renderers.push(imports.viewers.evince);
+renderers.push(imports.viewers.font);
+renderers.push(imports.viewers.gst);
+renderers.push(imports.viewers.html);
+renderers.push(imports.viewers.image);
+renderers.push(imports.viewers.text);
+
+var getKlass = function(mime) {
+ let renderer = renderers.find((r) => {
+ // first, try a direct match with the mimetype itself
+ if (r.mimeTypes.includes(mime))
+ return true;
+
+ // if this fails, try to see if we have any handlers
+ // registered for a parent type
+ if (r.mimeTypes.some((rm) => Gio.content_type_is_a(mime, rm)))
+ return true;
+
+ return false;
+ });
+
+ if (renderer)
+ return renderer.Klass;
+
+ // finally, resort to the fallback renderer
+ return FallbackRenderer.FallbackRenderer;
}
diff --git a/src/js/viewers/audio.js b/src/js/viewers/audio.js
index adb18cc..31efe88 100644
--- a/src/js/viewers/audio.js
+++ b/src/js/viewers/audio.js
@@ -29,7 +29,6 @@ const Gettext = imports.gettext.domain('sushi');
const _ = Gettext.gettext;
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;
@@ -51,7 +50,7 @@ function _formatTimeString(timeVal) {
return str;
}
-const AudioRenderer = GObject.registerClass(class AudioRenderer extends Gtk.Box {
+var Klass = GObject.registerClass(class AudioRenderer extends Gtk.Box {
_init(file, mainWindow) {
super._init({ orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6 });
@@ -261,6 +260,4 @@ const AudioRenderer = GObject.registerClass(class AudioRenderer extends Gtk.Box
}
});
-let handler = new MimeHandler.MimeHandler();
-
-handler.registerMimeTypes(TotemMimeTypes.audioTypes, AudioRenderer);
+var mimeTypes = TotemMimeTypes.audioTypes;
diff --git a/src/js/viewers/evince.js b/src/js/viewers/evince.js
index c73096b..3f7bda5 100644
--- a/src/js/viewers/evince.js
+++ b/src/js/viewers/evince.js
@@ -29,11 +29,10 @@ const Gettext = imports.gettext.domain('sushi');
const _ = Gettext.gettext;
const Constants = imports.util.constants;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
const Utils = imports.ui.utils;
-const EvinceRenderer = GObject.registerClass(class EvinceRenderer extends Gtk.ScrolledWindow {
+var Klass = GObject.registerClass(class EvinceRenderer extends Gtk.ScrolledWindow {
_init(file, mainWindow) {
super._init({ visible: true });
@@ -110,11 +109,7 @@ const EvinceRenderer = GObject.registerClass(class EvinceRenderer extends Gtk.Sc
}
});
-let handler = new MimeHandler.MimeHandler();
-
EvinceDocument.init();
-let mimeTypes = Sushi.query_supported_document_types();
-handler.registerMimeTypes(mimeTypes, EvinceRenderer);
let officeTypes = [
'application/vnd.oasis.opendocument.text',
@@ -128,5 +123,4 @@ let officeTypes = [
'application/vnd.ms-powerpoint',
'application/rtf'
];
-
-handler.registerMimeTypes(officeTypes, EvinceRenderer);
+var mimeTypes = Sushi.query_supported_document_types().concat(officeTypes);
diff --git a/src/js/viewers/font.js b/src/js/viewers/font.js
index 329e42e..b4e073c 100644
--- a/src/js/viewers/font.js
+++ b/src/js/viewers/font.js
@@ -25,10 +25,9 @@
const {GLib, GObject, Gtk, Sushi} = imports.gi;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
-const FontRenderer = GObject.registerClass(class FontRenderer extends Sushi.FontWidget {
+var Klass = GObject.registerClass(class FontRenderer extends Sushi.FontWidget {
_init(file, mainWindow) {
super._init({ uri: file.get_uri(),
visible: true })
@@ -43,13 +42,9 @@ const FontRenderer = GObject.registerClass(class FontRenderer extends Sushi.Font
}
});
-let handler = new MimeHandler.MimeHandler();
-
-let mimeTypes = [
+var mimeTypes = [
'application/x-font-ttf',
'application/x-font-otf',
'application/x-font-pcf',
'application/x-font-type1'
];
-
-handler.registerMimeTypes(mimeTypes, FontRenderer);
diff --git a/src/js/viewers/gst.js b/src/js/viewers/gst.js
index c73de5c..d32c4ef 100644
--- a/src/js/viewers/gst.js
+++ b/src/js/viewers/gst.js
@@ -25,12 +25,11 @@
const {GLib, GObject, Sushi} = imports.gi;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
const TotemMimeTypes = imports.util.totemMimeTypes;
const Utils = imports.ui.utils;
-const GstRenderer = GObject.registerClass(class GstRenderer extends Sushi.MediaBin {
+var Klass = GObject.registerClass(class GstRenderer extends Sushi.MediaBin {
_init(file, mainWindow) {
super._init({ uri: file.get_uri() });
@@ -59,6 +58,4 @@ const GstRenderer = GObject.registerClass(class GstRenderer extends Sushi.MediaB
}
});
-let handler = new MimeHandler.MimeHandler();
-
-handler.registerMimeTypes(TotemMimeTypes.videoTypes, GstRenderer);
+var mimeTypes = TotemMimeTypes.videoTypes;
diff --git a/src/js/viewers/html.js b/src/js/viewers/html.js
index eb9bad8..62f2049 100644
--- a/src/js/viewers/html.js
+++ b/src/js/viewers/html.js
@@ -25,11 +25,10 @@
const {Gtk, GLib, GObject, Sushi, WebKit2} = imports.gi;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
const Utils = imports.ui.utils;
-const HTMLRenderer = GObject.registerClass(class HTMLRenderer extends WebKit2.WebView {
+var Klass = GObject.registerClass(class HTMLRenderer extends WebKit2.WebView {
_init(file, mainWindow) {
super._init();
@@ -62,10 +61,6 @@ const HTMLRenderer = GObject.registerClass(class HTMLRenderer extends WebKit2.We
}
});
-let handler = new MimeHandler.MimeHandler();
-
-let mimeTypes = [
+var mimeTypes = [
'text/html'
];
-
-handler.registerMimeTypes(mimeTypes, HTMLRenderer);
diff --git a/src/js/viewers/image.js b/src/js/viewers/image.js
index b4bbfb6..cdf1af0 100644
--- a/src/js/viewers/image.js
+++ b/src/js/viewers/image.js
@@ -31,7 +31,6 @@ const Gettext = imports.gettext.domain('sushi');
const _ = Gettext.gettext;
const Mainloop = imports.mainloop;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
const Utils = imports.ui.utils;
@@ -120,7 +119,7 @@ const Image = GObject.registerClass({
}
});
-const ImageRenderer = GObject.registerClass(class ImageRenderer extends Image {
+var Klass = GObject.registerClass(class ImageRenderer extends Image {
_init(file, mainWindow) {
super._init();
@@ -198,10 +197,7 @@ const ImageRenderer = GObject.registerClass(class ImageRenderer extends Image {
}
});
-let handler = new MimeHandler.MimeHandler();
-
+var mimeTypes = [];
let formats = GdkPixbuf.Pixbuf.get_formats();
-for (let idx in formats) {
- let mimeTypes = formats[idx].get_mime_types();
- handler.registerMimeTypes(mimeTypes, ImageRenderer);
-}
+for (let idx in formats)
+ mimeTypes = mimeTypes.concat(formats[idx].get_mime_types());
diff --git a/src/js/viewers/text.js b/src/js/viewers/text.js
index 06a1bf6..6ebf280 100644
--- a/src/js/viewers/text.js
+++ b/src/js/viewers/text.js
@@ -27,11 +27,10 @@ imports.gi.versions.GtkSource = '4';
const {Gdk, Gio, GLib, GObject, Gtk, GtkSource, Sushi} = imports.gi;
-const MimeHandler = imports.ui.mimeHandler;
const Renderer = imports.ui.renderer;
const Utils = imports.ui.utils;
-const TextRenderer = GObject.registerClass(class TextRenderer extends Gtk.ScrolledWindow {
+var Klass = GObject.registerClass(class TextRenderer extends Gtk.ScrolledWindow {
_init(file, mainWindow) {
super._init();
@@ -93,13 +92,7 @@ const TextRenderer = GObject.registerClass(class TextRenderer extends Gtk.Scroll
}
});
-let handler = new MimeHandler.MimeHandler();
-
-/* register for text/plain and let the mime handler call us
- * for child types.
- */
-let mimeTypes = [
+// register for text/plain and let the mime handler call us for child types
+var mimeTypes = [
'text/plain'
];
-
-handler.registerMimeTypes(mimeTypes, TextRenderer);
diff --git a/src/main.c b/src/main.c
index ec15051..4dacdd0 100644
--- a/src/main.c
+++ b/src/main.c
@@ -54,42 +54,6 @@ parse_options (int *argc, char ***argv)
g_option_context_free (ctx);
}
-static void
-register_all_viewers (GjsContext *ctx)
-{
- GDir *dir;
- const gchar *name;
- gchar *path;
- GError *error = NULL;
-
- dir = g_dir_open (SUSHI_PKGDATADIR "/js/viewers", 0, &error);
-
- if (dir == NULL) {
- g_warning ("Can't open module directory: %s\n", error->message);
- g_error_free (error);
- return;
- }
-
- name = g_dir_read_name (dir);
-
- while (name != NULL) {
- path = g_build_filename (SUSHI_PKGDATADIR "/js/viewers",
- name, NULL);
- if (!gjs_context_eval_file (ctx,
- path,
- NULL,
- &error)) {
- g_warning ("Unable to parse viewer %s: %s", name, error->message);
- g_clear_error (&error);
- }
-
- g_free (path);
- name = g_dir_read_name (dir);
- }
-
- g_dir_close (dir);
-}
-
int
main (int argc, char **argv)
{
@@ -103,8 +67,6 @@ main (int argc, char **argv)
js_context = gjs_context_new_with_search_path (NULL);
error = NULL;
- register_all_viewers (js_context);
-
if (!gjs_context_eval (js_context,
"const Main = imports.ui.main;\n"
"Main.run();\n",
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]