From 6315669933b2e5e806f80bae3129a9f798609bd0 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Fri, 11 Dec 2020 15:32:05 +0100 Subject: [PATCH] Split app source file into two This allows creating different app from the same source code. --- clapper_src/app.js | 119 ++++------------------------------------- clapper_src/appBase.js | 112 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 108 deletions(-) create mode 100644 clapper_src/appBase.js diff --git a/clapper_src/app.js b/clapper_src/app.js index c7d38900..575b3808 100644 --- a/clapper_src/app.js +++ b/clapper_src/app.js @@ -1,136 +1,39 @@ -const { Gio, GObject, Gtk } = imports.gi; +const { GObject } = imports.gi; +const { AppBase } = imports.clapper_src.appBase; const { HeaderBar } = imports.clapper_src.headerbar; const { Widget } = imports.clapper_src.widget; const Debug = imports.clapper_src.debug; -const Menu = imports.clapper_src.menu; -const Misc = imports.clapper_src.misc; let { debug } = Debug; -let { settings } = Misc; var App = GObject.registerClass( -class ClapperApp extends Gtk.Application +class ClapperApp extends AppBase { - _init(opts) - { - super._init({ - application_id: Misc.appId - }); - - let defaults = { - playlist: [], - }; - Object.assign(this, defaults, opts); - - this.doneFirstActivate = false; - } - vfunc_startup() { super.vfunc_startup(); - let window = new Gtk.ApplicationWindow({ - application: this, - title: Misc.appName, - }); - window.isClapperApp = true; - window.add_css_class('nobackground'); - - if(!settings.get_boolean('render-shadows')) - window.add_css_class('gpufriendly'); - - if( - settings.get_boolean('dark-theme') - && settings.get_boolean('brighter-sliders') - ) - window.add_css_class('brightscale'); - - for(let action in Menu.actions) { - let simpleAction = new Gio.SimpleAction({ - name: action - }); - simpleAction.connect( - 'activate', () => Menu.actions[action](this.active_window) - ); - this.add_action(simpleAction); - } + this.active_window.isClapperApp = true; + this.active_window.add_css_class('nobackground'); let clapperWidget = new Widget(); - window.set_child(clapperWidget); + this.active_window.set_child(clapperWidget); + + let headerBar = new HeaderBar(this.active_window); + this.active_window.set_titlebar(headerBar); let size = clapperWidget.windowSize; - window.set_default_size(size[0], size[1]); + this.active_window.set_default_size(size[0], size[1]); debug(`restored window size: ${size[0]}x${size[1]}`); - - let clapperPath = Misc.getClapperPath(); - let uiBuilder = Gtk.Builder.new_from_file( - `${clapperPath}/ui/clapper.ui` - ); - let models = { - addMediaMenu: uiBuilder.get_object('addMediaMenu'), - settingsMenu: uiBuilder.get_object('settingsMenu'), - }; - let headerBar = new HeaderBar(window, models); - window.set_titlebar(headerBar); - } - - vfunc_activate() - { - super.vfunc_activate(); - - if(!this.doneFirstActivate) - this._onFirstActivate(); - - this.active_window.present(); - } - - run(arr) - { - super.run(arr || []); - } - - _onFirstActivate() - { - let gtkSettings = Gtk.Settings.get_default(); - settings.bind( - 'dark-theme', gtkSettings, - 'gtk-application-prefer-dark-theme', - Gio.SettingsBindFlags.GET - ); - this._onThemeChanged(gtkSettings); - gtkSettings.connect('notify::gtk-theme-name', this._onThemeChanged.bind(this)); - - this.windowShowSignal = this.active_window.connect( - 'show', this._onWindowShow.bind(this) - ); - - this.doneFirstActivate = true; } _onWindowShow(window) { - window.disconnect(this.windowShowSignal); - this.windowShowSignal = null; + super._onWindowShow(window); if(this.playlist.length) { let { player } = window.get_child(); player.set_playlist(this.playlist); } } - - _onThemeChanged(gtkSettings) - { - const theme = gtkSettings.gtk_theme_name; - debug(`user selected theme: ${theme}`); - - if(!theme.endsWith('-dark')) - return; - - /* We need to request a default theme with optional dark variant - to make the "gtk_application_prefer_dark_theme" setting work */ - const parsedTheme = theme.substring(0, theme.lastIndexOf('-')); - - gtkSettings.gtk_theme_name = parsedTheme; - debug(`set theme: ${parsedTheme}`); - } }); diff --git a/clapper_src/appBase.js b/clapper_src/appBase.js new file mode 100644 index 00000000..b2224612 --- /dev/null +++ b/clapper_src/appBase.js @@ -0,0 +1,112 @@ +const { Gio, GObject, Gtk } = imports.gi; +const Debug = imports.clapper_src.debug; +const Menu = imports.clapper_src.menu; +const Misc = imports.clapper_src.misc; + +let { debug } = Debug; +let { settings } = Misc; + +var AppBase = GObject.registerClass( +class ClapperAppBase extends Gtk.Application +{ + _init(opts) + { + opts = opts || {}; + + let defaults = { + idPostfix: '', + playlist: [], + }; + Object.assign(this, defaults, opts); + + super._init({ + application_id: Misc.appId + this.idPostfix + }); + + this.doneFirstActivate = false; + } + + vfunc_startup() + { + super.vfunc_startup(); + + let window = new Gtk.ApplicationWindow({ + application: this, + title: Misc.appName, + }); + + if(!settings.get_boolean('render-shadows')) + window.add_css_class('gpufriendly'); + + if( + settings.get_boolean('dark-theme') + && settings.get_boolean('brighter-sliders') + ) + window.add_css_class('brightscale'); + + for(let action in Menu.actions) { + let simpleAction = new Gio.SimpleAction({ + name: action + }); + simpleAction.connect( + 'activate', () => Menu.actions[action](this.active_window) + ); + this.add_action(simpleAction); + } + } + + vfunc_activate() + { + super.vfunc_activate(); + + if(!this.doneFirstActivate) + this._onFirstActivate(); + + this.active_window.present(); + } + + run(arr) + { + super.run(arr || []); + } + + _onFirstActivate() + { + let gtkSettings = Gtk.Settings.get_default(); + settings.bind( + 'dark-theme', gtkSettings, + 'gtk-application-prefer-dark-theme', + Gio.SettingsBindFlags.GET + ); + this._onThemeChanged(gtkSettings); + gtkSettings.connect('notify::gtk-theme-name', this._onThemeChanged.bind(this)); + + this.windowShowSignal = this.active_window.connect( + 'show', this._onWindowShow.bind(this) + ); + + this.doneFirstActivate = true; + } + + _onWindowShow(window) + { + window.disconnect(this.windowShowSignal); + this.windowShowSignal = null; + } + + _onThemeChanged(gtkSettings) + { + const theme = gtkSettings.gtk_theme_name; + debug(`user selected theme: ${theme}`); + + if(!theme.endsWith('-dark')) + return; + + /* We need to request a default theme with optional dark variant + to make the "gtk_application_prefer_dark_theme" setting work */ + const parsedTheme = theme.substring(0, theme.lastIndexOf('-')); + + gtkSettings.gtk_theme_name = parsedTheme; + debug(`set theme: ${parsedTheme}`); + } +});