From 6afbbc767a172654d8972fdc292c9245636c1904 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Thu, 24 Dec 2020 00:23:49 +0100 Subject: [PATCH] Open new file in the same window. Closes #31 --- clapper_src/app.js | 30 +++++++++++++++++++++++++----- clapper_src/appBase.js | 26 ++++++++++++++------------ clapper_src/daemon.js | 4 ++-- clapper_src/main.js | 5 ++--- clapper_src/mainDaemon.js | 2 +- clapper_src/player.js | 31 +++++++++++++++++++------------ 6 files changed, 63 insertions(+), 35 deletions(-) diff --git a/clapper_src/app.js b/clapper_src/app.js index 575b3808..6efc2882 100644 --- a/clapper_src/app.js +++ b/clapper_src/app.js @@ -1,4 +1,4 @@ -const { GObject } = imports.gi; +const { Gio, GObject } = imports.gi; const { AppBase } = imports.clapper_src.appBase; const { HeaderBar } = imports.clapper_src.headerbar; const { Widget } = imports.clapper_src.widget; @@ -9,6 +9,17 @@ let { debug } = Debug; var App = GObject.registerClass( class ClapperApp extends AppBase { + _init() + { + super._init(); + + this.set_flags( + this.get_flags() + | Gio.ApplicationFlags.HANDLES_OPEN + ); + this.playlist = []; + } + vfunc_startup() { super.vfunc_startup(); @@ -27,13 +38,22 @@ class ClapperApp extends AppBase debug(`restored window size: ${size[0]}x${size[1]}`); } + vfunc_open(files, hint) + { + super.vfunc_open(files, hint); + + this.playlist = files; + this._handleAppStart(); + } + _onWindowShow(window) { super._onWindowShow(window); - if(this.playlist.length) { - let { player } = window.get_child(); - player.set_playlist(this.playlist); - } + if(!this.playlist.length) + return; + + let { player } = window.get_child(); + player.set_playlist(this.playlist); } }); diff --git a/clapper_src/appBase.js b/clapper_src/appBase.js index 2d93396a..fd382ae4 100644 --- a/clapper_src/appBase.js +++ b/clapper_src/appBase.js @@ -9,15 +9,8 @@ let { settings } = Misc; var AppBase = GObject.registerClass( class ClapperAppBase extends Gtk.Application { - _init(opts) + _init() { - opts = opts || {}; - - let defaults = { - playlist: [], - }; - Object.assign(this, defaults, opts); - super._init({ application_id: Misc.appId, }); @@ -58,10 +51,7 @@ class ClapperAppBase extends Gtk.Application { super.vfunc_activate(); - if(!this.doneFirstActivate) - this._onFirstActivate(); - - this.active_window.present(); + this._handleAppStart(); } run(arr) @@ -69,6 +59,15 @@ class ClapperAppBase extends Gtk.Application super.run(arr || []); } + _handleAppStart() + { + if(this.doneFirstActivate) + return this._onWindowShow(this.active_window); + + this._onFirstActivate(); + this.active_window.present(); + } + _onFirstActivate() { let gtkSettings = Gtk.Settings.get_default(); @@ -89,6 +88,9 @@ class ClapperAppBase extends Gtk.Application _onWindowShow(window) { + if(!this.windowShowSignal) + return; + window.disconnect(this.windowShowSignal); this.windowShowSignal = null; } diff --git a/clapper_src/daemon.js b/clapper_src/daemon.js index ed33ea55..df0ab96f 100644 --- a/clapper_src/daemon.js +++ b/clapper_src/daemon.js @@ -6,9 +6,9 @@ let { debug } = Debug; var Daemon = GObject.registerClass( class ClapperDaemon extends Gio.SubprocessLauncher { - _init(port) + _init() { - port = port || 8080; + let port = ARGV[0] || 8080; /* FIXME: show output when debugging is on */ const flags = Gio.SubprocessFlags.STDOUT_SILENCE diff --git a/clapper_src/main.js b/clapper_src/main.js index efa88d3b..c757e2c9 100644 --- a/clapper_src/main.js +++ b/clapper_src/main.js @@ -1,6 +1,7 @@ imports.gi.versions.Gdk = '4.0'; imports.gi.versions.Gtk = '4.0'; +const System = imports.system; const { Gst } = imports.gi; const { App } = imports.clapper_src.app; @@ -8,7 +9,5 @@ Gst.init(null); function main() { - let clapper = new App({ - playlist: ARGV - }).run(); + new App().run([System.programInvocationName].concat(ARGV)); } diff --git a/clapper_src/mainDaemon.js b/clapper_src/mainDaemon.js index 1f5b096b..93302a4e 100644 --- a/clapper_src/mainDaemon.js +++ b/clapper_src/mainDaemon.js @@ -2,5 +2,5 @@ const { Daemon } = imports.clapper_src.daemon; function main() { - new Daemon(ARGV[0]); + new Daemon(); } diff --git a/clapper_src/player.js b/clapper_src/player.js index 5d8e9816..513a2852 100644 --- a/clapper_src/player.js +++ b/clapper_src/player.js @@ -73,21 +73,27 @@ class ClapperPlayer extends PlayerBase set_media(source) { - if(!Gst.uri_is_valid(source)) - source = Gst.filename_to_uri(source); + let file; - if(!source) - return debug('parsing source to URI failed'); + if(source.get_path) + file = source; + else { + if(!Gst.uri_is_valid(source)) + source = Gst.filename_to_uri(source); - debug(`parsed source to URI: ${source}`); + if(!source) + return debug('parsing source to URI failed'); - if(Gst.Uri.get_protocol(source) !== 'file') { - this.is_local_file = false; - return this.set_uri(source); + debug(`parsed source to URI: ${source}`); + + if(Gst.Uri.get_protocol(source) !== 'file') { + this.is_local_file = false; + return this.set_uri(source); + } + + file = Gio.file_new_for_uri(source); } - let file = Gio.file_new_for_uri(source); - if(!file.query_exists(null)) { debug(`file does not exist: ${source}`, 'LEVEL_WARNING'); this._trackId++; @@ -98,11 +104,12 @@ class ClapperPlayer extends PlayerBase return this.set_media(this._playlist[this._trackId]); } - if(file.get_path().endsWith('.claps')) + let fileUri = file.get_uri(); + if(fileUri.endsWith('.claps')) return this.load_playlist_file(file); this.is_local_file = true; - this.set_uri(source); + this.set_uri(fileUri); } load_playlist_file(file)