app: Merge code into single subclass

We no longer ship the web app that was reusing parts of this code.
Combine it into single file without additional subclass.

Also put window creation inside first activate code instead of startup
in order to fix running app as a gapplication service.
This commit is contained in:
Rafał Dzięgiel
2022-05-23 13:46:26 +02:00
parent 306505dc4d
commit fce8ec59b8
2 changed files with 87 additions and 116 deletions

View File

@@ -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);
this.mapSignal = window.connect('map', this._onWindowMap.bind(this));
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);
}
vfunc_open(files, hint)
{
super.vfunc_open(files, hint);
this._openFilesAsync(files).then(() => this.activate()).catch(debug);
this.mapSignal = window.connect('map', this._onWindowMap.bind(this));
this.doneFirstActivate = true;
}
_onWindowMap(window)

View File

@@ -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;
}
});