diff --git a/src/app.js b/src/app.js index e888cc83..0e6d47da 100644 --- a/src/app.js +++ b/src/app.js @@ -1,27 +1,93 @@ -const { Gio, GObject, Gdk, Gtk } = imports.gi; -const { AppBase } = imports.src.appBase; +const { Gio, GLib, GObject, Gtk } = imports.gi; const { Widget } = imports.src.widget; const Debug = imports.src.debug; +const FileOps = imports.src.fileOps; +const Misc = imports.src.misc; +const Actions = imports.src.actions; const { debug } = Debug; +const { settings } = Misc; var App = GObject.registerClass({ GTypeName: 'ClapperApp', }, -class ClapperApp extends AppBase +class ClapperApp extends Gtk.Application { _init() { - super._init(); + super._init({ + application_id: Misc.appId, + flags: Gio.ApplicationFlags.HANDLES_OPEN, + }); - this.flags |= Gio.ApplicationFlags.HANDLES_OPEN; + this.doneFirstActivate = false; + this.isFileAppend = false; + this.mapSignal = null; } - vfunc_startup() + vfunc_open(files, hint) { - super.vfunc_startup(); + super.vfunc_open(files, hint); + + this._openFilesAsync(files).then(() => this.activate()).catch(debug); + } + + vfunc_activate() + { + super.vfunc_activate(); + + if(!this.doneFirstActivate) + this._onFirstActivate(); + + this.active_window.present_with_time( + Math.floor(GLib.get_monotonic_time() / 1000) + ); + } + + async _openFilesAsync(files) + { + const urisArr = []; + + for(let file of files) { + const uri = file.get_uri(); + if(!uri.startsWith('file:')) { + urisArr.push(uri); + continue; + } + + /* If file is not a dir its URI will be returned in an array */ + const uris = await FileOps.getDirFilesUrisPromise(file).catch(debug); + if(uris && uris.length) + urisArr.push(...uris); + } + + const [playlist, subs] = Misc.parsePlaylistFiles(urisArr); + const { player } = this.active_window.get_child(); + const action = (this.isFileAppend) ? 'append' : 'set'; + + if(playlist && playlist.length) + player[`${action}_playlist`](playlist); + if(subs) + player.set_subtitles(subs); + + /* Restore default behavior */ + this.isFileAppend = false; + } + + _onFirstActivate() + { + const window = new Gtk.ApplicationWindow({ + application: this, + title: Misc.appName, + }); + + window.add_css_class('adwrounded'); + + if(!settings.get_boolean('render-shadows')) + window.add_css_class('gpufriendly'); + + window.add_css_class('gpufriendlyfs'); - const window = this.active_window; const clapperWidget = new Widget(); const dummyHeaderbar = new Gtk.Box({ can_focus: false, @@ -33,14 +99,20 @@ class ClapperApp extends AppBase window.set_child(clapperWidget); window.set_titlebar(dummyHeaderbar); + for(let name in Actions.actions) { + const simpleAction = new Gio.SimpleAction({ name }); + simpleAction.connect('activate', (action) => + Actions.handleAction(action, window) + ); + this.add_action(simpleAction); + + const accels = Actions.actions[name]; + if(accels) + this.set_accels_for_action(`app.${name}`, accels); + } + this.mapSignal = window.connect('map', this._onWindowMap.bind(this)); - } - - vfunc_open(files, hint) - { - super.vfunc_open(files, hint); - - this._openFilesAsync(files).then(() => this.activate()).catch(debug); + this.doneFirstActivate = true; } _onWindowMap(window) diff --git a/src/appBase.js b/src/appBase.js deleted file mode 100644 index be72d61d..00000000 --- a/src/appBase.js +++ /dev/null @@ -1,101 +0,0 @@ -const { Gio, GLib, GObject, Gtk } = imports.gi; -const Debug = imports.src.debug; -const FileOps = imports.src.fileOps; -const Misc = imports.src.misc; -const Actions = imports.src.actions; - -const { debug } = Debug; -const { settings } = Misc; - -var AppBase = GObject.registerClass({ - GTypeName: 'ClapperAppBase', -}, -class ClapperAppBase extends Gtk.Application -{ - _init() - { - super._init({ - application_id: Misc.appId, - }); - - this.doneFirstActivate = false; - this.isFileAppend = false; - } - - vfunc_startup() - { - super.vfunc_startup(); - - const window = new Gtk.ApplicationWindow({ - application: this, - title: Misc.appName, - }); - - /* FIXME: AFAIK there is no way to detect theme rounded corners. - * Having 2/4 corners rounded in floating mode is not good. */ - window.add_css_class('adwrounded'); - - if(!settings.get_boolean('render-shadows')) - window.add_css_class('gpufriendly'); - - window.add_css_class('gpufriendlyfs'); - } - - vfunc_activate() - { - super.vfunc_activate(); - - if(!this.doneFirstActivate) - this._onFirstActivate(); - - this.active_window.present_with_time( - Math.floor(GLib.get_monotonic_time() / 1000) - ); - } - - async _openFilesAsync(files) - { - const urisArr = []; - - for(let file of files) { - const uri = file.get_uri(); - if(!uri.startsWith('file:')) { - urisArr.push(uri); - continue; - } - - /* If file is not a dir its URI will be returned in an array */ - const uris = await FileOps.getDirFilesUrisPromise(file).catch(debug); - if(uris && uris.length) - urisArr.push(...uris); - } - - const [playlist, subs] = Misc.parsePlaylistFiles(urisArr); - const { player } = this.active_window.get_child(); - const action = (this.isFileAppend) ? 'append' : 'set'; - - if(playlist && playlist.length) - player[`${action}_playlist`](playlist); - if(subs) - player.set_subtitles(subs); - - /* Restore default behavior */ - this.isFileAppend = false; - } - - _onFirstActivate() - { - for(let name in Actions.actions) { - const simpleAction = new Gio.SimpleAction({ name }); - simpleAction.connect('activate', (action) => - Actions.handleAction(action, this.active_window) - ); - this.add_action(simpleAction); - - const accels = Actions.actions[name]; - if(accels) - this.set_accels_for_action(`app.${name}`, accels); - } - this.doneFirstActivate = true; - } -});