[sushi/wip/cosimoc/no-clutter: 38/50] Simplify how renderers are registered
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [sushi/wip/cosimoc/no-clutter: 38/50] Simplify how renderers are registered
- Date: Mon, 17 Jun 2019 18:35:48 +0000 (UTC)
commit ce3721e29cc0a1616976695238b316af8624f855
Author: Cosimo Cecchi <cosimoc gnome org>
Date: Sat Jun 15 12:19:08 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 | 59 +++++++++++++++---------------------------------
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, 36 insertions(+), 132 deletions(-)
---
diff --git a/src/js/ui/mainWindow.js b/src/js/ui/mainWindow.js
index 66366b7..929f67e 100644
--- a/src/js/ui/mainWindow.js
+++ b/src/js/ui/mainWindow.js
@@ -73,8 +73,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,
@@ -197,7 +195,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..7b914fe 100644
--- a/src/js/ui/mimeHandler.js
+++ b/src/js/ui/mimeHandler.js
@@ -27,50 +27,27 @@ const {Gio} = imports.gi;
const FallbackRenderer = imports.ui.fallbackRenderer;
-let _mimeHandler = null;
+var renderers = [];
+for (let i in imports.viewers)
+ renderers.push(imports.viewers[i]);
-function MimeHandler() {
- if (_mimeHandler == null) {
- this._init();
- _mimeHandler = this;
- }
+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;
- return _mimeHandler;
-}
-
-function init() {
- let handler = new MimeHandler();
-}
-
-MimeHandler.prototype = {
- _init: function() {
- this._mimeTypes = [];
- },
-
- registerMime: function(mime, klass) {
- this._mimeTypes[mime] = klass;
- },
+ // 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;
- registerMimeTypes: function(mimeTypes, klass) {
- for (let idx in mimeTypes)
- this.registerMime(mimeTypes[idx], klass);
- },
+ return false;
+ });
- 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];
- }
+ if (renderer)
+ return renderer.Klass;
- /* finally, resort to the fallback renderer */
- return FallbackRenderer.FallbackRenderer;
- }
- }
+ // 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 21227f3..5a85455 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,
min_content_height: Constants.VIEW_MIN,
@@ -109,11 +108,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',
@@ -127,5 +122,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 386b644..6f4e3cc 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();
@@ -199,10 +198,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 3705f8e..54fbafa 100644
--- a/src/js/viewers/text.js
+++ b/src/js/viewers/text.js
@@ -27,7 +27,6 @@ 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;
@@ -45,7 +44,7 @@ function _getGeditScheme() {
return geditScheme;
}
-const TextRenderer = GObject.registerClass(class TextRenderer extends Gtk.ScrolledWindow {
+var Klass = GObject.registerClass(class TextRenderer extends Gtk.ScrolledWindow {
_init(file, mainWindow) {
super._init();
@@ -96,13 +95,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]