From dea67e4712a6533bf960cd6428cc73e74d583995 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Sun, 5 Sep 2021 17:07:11 +0200 Subject: [PATCH] Prepare for resource loading Cleanup the Clapper install path detection code in a way to make gresources loading eventually possible for compiled builds --- src/actions.js | 5 +--- src/buttons.js | 8 +++---- src/headerbarBase.js | 5 +--- src/mainRemote.js | 9 +++----- src/misc.js | 55 +++++++++++++++++++++++++++----------------- src/prefs.js | 4 ++-- 6 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/actions.js b/src/actions.js index 1ac67648..bdbc4680 100644 --- a/src/actions.js +++ b/src/actions.js @@ -47,10 +47,7 @@ function handleAction(action, window) break; case 'shortcuts': if(!window.get_help_overlay()) { - const clapperPath = Misc.getClapperPath(); - const helpBuilder = Gtk.Builder.new_from_file( - `${clapperPath}/ui/help-overlay.ui` - ); + const helpBuilder = Misc.getBuilderForName('help-overlay.ui'); window.set_help_overlay(helpBuilder.get_object('help_overlay')); } clapperWidget.activate_action('win.show-help-overlay', null); diff --git a/src/buttons.js b/src/buttons.js index 05b17a30..1044ca89 100644 --- a/src/buttons.js +++ b/src/buttons.js @@ -60,7 +60,7 @@ class ClapperIconToggleButton extends CustomButton var PopoverSeparator = GObject.registerClass({ GTypeName: 'ClapperPopoverSeparator', - Template: `file://${Misc.getClapperPath()}/ui/popover-separator.ui`, + Template: Misc.getResourceUri('ui/popover-separator.ui'), InternalChildren: ['middle_label'], Properties: { 'label': GObject.ParamSpec.string( @@ -138,7 +138,7 @@ class ClapperPopoverButtonBase extends Gtk.MenuButton var ElapsedTimeButton = GObject.registerClass({ GTypeName: 'ClapperElapsedTimeButton', - Template: `file://${Misc.getClapperPath()}/ui/elapsed-time-button.ui`, + Template: Misc.getResourceUri('ui/elapsed-time-button.ui'), Children: ['scrolledWindow', 'speedScale'], }, class ClapperElapsedTimeButton extends PopoverButtonBase @@ -177,7 +177,7 @@ class ClapperElapsedTimeButton extends PopoverButtonBase var TrackSelectButton = GObject.registerClass({ GTypeName: 'ClapperTrackSelectButton', - Template: `file://${Misc.getClapperPath()}/ui/track-select-button.ui`, + Template: Misc.getResourceUri('ui/track-select-button.ui'), Children: ['popoverBox'], InternalChildren: ['scrolled_window', 'decoder_separator'], }, @@ -204,7 +204,7 @@ class ClapperTrackSelectButton extends PopoverButtonBase var VolumeButton = GObject.registerClass({ GTypeName: 'ClapperVolumeButton', - Template: `file://${Misc.getClapperPath()}/ui/volume-button.ui`, + Template: Misc.getResourceUri('ui/volume-button.ui'), Children: ['volumeScale'], }, class ClapperVolumeButton extends PopoverButtonBase diff --git a/src/headerbarBase.js b/src/headerbarBase.js index 1beee96f..65c7f9fc 100644 --- a/src/headerbarBase.js +++ b/src/headerbarBase.js @@ -25,10 +25,7 @@ class ClapperHeaderBarBase extends Gtk.Box this.isMenuOnLeft = true; this.hasPipIcons = false; - const clapperPath = Misc.getClapperPath(); - const uiBuilder = Gtk.Builder.new_from_file( - `${clapperPath}/ui/clapper.ui` - ); + const uiBuilder = Misc.getBuilderForName('clapper.ui'); this.menuWidget = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL, diff --git a/src/mainRemote.js b/src/mainRemote.js index 5d3f4b9a..216af851 100644 --- a/src/mainRemote.js +++ b/src/mainRemote.js @@ -4,14 +4,11 @@ imports.gi.versions.Soup = '2.4'; pkg.initGettext(); +const Misc = imports.src.misc; +Misc.appId += '.Remote'; + const { Gtk, Adw } = imports.gi; const { AppRemote } = imports.src.appRemote; -const Misc = imports.src.misc; - -const ID_POSTFIX = 'Remote'; - -Misc.clapperPath = `${pkg.datadir}/${Misc.appId}`; -Misc.appId += '.' + ID_POSTFIX; function main(argv) { diff --git a/src/misc.js b/src/misc.js index 63dd92e3..9e414316 100644 --- a/src/misc.js +++ b/src/misc.js @@ -10,8 +10,6 @@ var subsMimes = [ 'text/x-ssa', ]; -var clapperPath = null; - var settings = new Gio.Settings({ schema_id: appId, }); @@ -30,13 +28,41 @@ const subsKeys = Object.keys(subsTitles); let inhibitCookie; -function getClapperPath() +function getResourceUri(path) { - return (clapperPath) - ? clapperPath - : (pkg) - ? `${pkg.datadir}/${pkg.name}` - : '.'; + /* TODO: support gresources */ + let res = `file://${pkg.pkgdatadir}/${path}`; + + debug(`importing ${res}`); + + return res; +} + +function getBuilderForName(name) +{ + const uri = getResourceUri(`ui/${name}`); + + if(uri.startsWith('resource')) + return Gtk.Builder.new_from_resource(uri.substring(11)); + + return Gtk.Builder.new_from_file(uri.substring(7)); +} + +function loadCustomCss() +{ + const uri = getResourceUri(`css/styles.css`); + const cssProvider = new Gtk.CssProvider(); + + if(uri.startsWith('resource')) + cssProvider.load_from_resource(uri); + else + cssProvider.load_from_path(uri.substring(7)); + + Gtk.StyleContext.add_provider_for_display( + Gdk.Display.get_default(), + cssProvider, + Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + ); } function getClapperThemeIconUri() @@ -101,19 +127,6 @@ function getSubsTitle(infoTitle) return (found) ? subsTitles[found] : null; } -function loadCustomCss() -{ - const clapperPath = getClapperPath(); - const cssProvider = new Gtk.CssProvider(); - - cssProvider.load_from_path(`${clapperPath}/css/styles.css`); - Gtk.StyleContext.add_provider_for_display( - Gdk.Display.get_default(), - cssProvider, - Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION - ); -} - function setAppInhibit(isInhibit, window) { let isInhibited = false; diff --git a/src/prefs.js b/src/prefs.js index 4a28c969..2f07eab8 100644 --- a/src/prefs.js +++ b/src/prefs.js @@ -428,7 +428,7 @@ class ClapperPrefsPluginExpander extends Adw.ExpanderRow let PrefsPluginRankingSubpage = GObject.registerClass({ GTypeName: 'ClapperPrefsPluginRankingSubpage', - Template: `file://${Misc.getClapperPath()}/ui/preferences-plugin-ranking-subpage.ui`, + Template: Misc.getResourceUri('ui/preferences-plugin-ranking-subpage.ui'), InternalChildren: ['decoders_group'], }, class ClapperPrefsPluginRankingSubpage extends Gtk.Box @@ -506,7 +506,7 @@ class ClapperPrefsPluginRankingSubpage extends Gtk.Box var PrefsWindow = GObject.registerClass({ GTypeName: 'ClapperPrefsWindow', - Template: `file://${Misc.getClapperPath()}/ui/preferences-window.ui`, + Template: Misc.getResourceUri('ui/preferences-window.ui'), }, class ClapperPrefsWindow extends Adw.PreferencesWindow {