From 0b1864378baee3f9104a301195762881d9a82e60 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:24:42 +0200 Subject: [PATCH] Add Clapper usage as GTK widget --- clapper_src/app.js | 11 +++++---- clapper_src/buttons.js | 6 ++--- clapper_src/headerbar.js | 23 ++---------------- clapper_src/menu.js | 5 ++-- clapper_src/misc.js | 21 +++++++++++++++++ clapper_src/widget.js | 44 +++++++++++++++++++++++----------- clapper_src/window.js | 18 -------------- examples/app-example.js | 48 ++++++++++++++++++++++++++++++++++++++ examples/widget-example.js | 40 +++++++++++++++++++++++++++++++ gjs-1.0/clapper.js.in | 6 ++++- gjs-1.0/meson.build | 1 + 11 files changed, 159 insertions(+), 64 deletions(-) delete mode 100644 clapper_src/window.js create mode 100644 examples/app-example.js create mode 100644 examples/widget-example.js diff --git a/clapper_src/app.js b/clapper_src/app.js index 3310d362..d0dfc31d 100644 --- a/clapper_src/app.js +++ b/clapper_src/app.js @@ -2,10 +2,10 @@ const { Gio, GObject, Gtk } = imports.gi; const { HeaderBar } = imports.clapper_src.headerbar; const { Widget } = imports.clapper_src.widget; const Menu = imports.clapper_src.menu; +const Misc = imports.clapper_src.misc; -const APP_NAME = 'Clapper' || pkg.name.substring( - pkg.name.lastIndexOf('.') + 1 -); +const APP_NAME = 'Clapper'; +const APP_ID = 'com.github.rafostar.Clapper'; var App = GObject.registerClass( class ClapperApp extends Gtk.Application @@ -13,7 +13,7 @@ class ClapperApp extends Gtk.Application _init(opts) { super._init({ - application_id: pkg.name + application_id: APP_ID }); let defaults = { @@ -40,8 +40,9 @@ class ClapperApp extends Gtk.Application ); this.add_action(simpleAction); } + let clapperPath = Misc.getClapperPath(); let uiBuilder = Gtk.Builder.new_from_file( - `${pkg.datadir}/${pkg.name}/ui/clapper.ui` + `${clapperPath}/ui/clapper.ui` ); let models = { settingsMenu: uiBuilder.get_object('settingsMenu') diff --git a/clapper_src/buttons.js b/clapper_src/buttons.js index f64ae251..c635efad 100644 --- a/clapper_src/buttons.js +++ b/clapper_src/buttons.js @@ -139,10 +139,8 @@ class ClapperPopoverButton extends IconButton _onClosed() { - let root = this.get_root(); - let clapperWidget = root.get_child(); - - clapperWidget.player.widget.grab_focus(); + let { player } = this.get_ancestor(Gtk.Grid); + player.widget.grab_focus(); this.popover.unparent(); this.unset_state_flags(Gtk.StateFlags.CHECKED); diff --git a/clapper_src/headerbar.js b/clapper_src/headerbar.js index 9745c3b4..230dd1a2 100644 --- a/clapper_src/headerbar.js +++ b/clapper_src/headerbar.js @@ -1,4 +1,4 @@ -const { GLib, GObject, Gst, Gtk, Pango } = imports.gi; +const { GObject, Gtk, Pango } = imports.gi; var HeaderBar = GObject.registerClass( class ClapperHeaderBar extends Gtk.HeaderBar @@ -25,27 +25,8 @@ class ClapperHeaderBar extends Gtk.HeaderBar this.pack_end(fullscreenButton); } - updateHeaderBar(mediaInfo) + updateHeaderBar(title, subtitle) { - let title = mediaInfo.get_title(); - let subtitle = mediaInfo.get_uri(); - - if(Gst.Uri.get_protocol(subtitle) === 'file') { - subtitle = GLib.path_get_basename( - GLib.filename_from_uri(subtitle)[0] - ); - } - - if(!title) { - title = (!subtitle) - ? this.defaultTitle - : (subtitle.includes('.')) - ? subtitle.split('.').slice(0, -1).join('.') - : subtitle; - - subtitle = null; - } - this.titleLabel.label = title; this.subtitleLabel.visible = (subtitle !== null); diff --git a/clapper_src/menu.js b/clapper_src/menu.js index b75726f2..ba1d6110 100644 --- a/clapper_src/menu.js +++ b/clapper_src/menu.js @@ -1,4 +1,5 @@ const { GObject, Gst, Gtk } = imports.gi; +const Misc = imports.clapper_src.misc; var actions = [ about @@ -26,11 +27,11 @@ function about(window, appName) let aboutDialog = new Gtk.AboutDialog({ program_name: appName, comments: 'A GNOME media player powered by GStreamer', - version: pkg.version, + version: Misc.getClapperVersion(), authors: ['Rafał Dzięgiel'], artists: ['Rafał Dzięgiel'], license_type: Gtk.License.GPL_3_0, - logo_icon_name: pkg.name, + logo_icon_name: 'com.github.rafostar.Clapper', website: 'https://github.com/Rafostar/clapper', modal: true, system_information: osInfo, diff --git a/clapper_src/misc.js b/clapper_src/misc.js index 27faae29..a6516a83 100644 --- a/clapper_src/misc.js +++ b/clapper_src/misc.js @@ -1,9 +1,30 @@ const { GstPlayer, Gtk } = imports.gi; const Debug = imports.clapper_src.debug; +var clapperPath; +var clapperVersion; + let { debug } = Debug; let inhibitCookie; +function getClapperPath() +{ + return (clapperPath) + ? clapperPath + : (pkg) + ? `${pkg.datadir}/${pkg.name}` + : '.'; +} + +function getClapperVersion() +{ + return (clapperVersion) + ? clapperVersion + : (pkg) + ? pkg.version + : ''; +} + function inhibitForState(state, window) { let isInhibited = false; diff --git a/clapper_src/widget.js b/clapper_src/widget.js index af3a74b6..21f099dd 100644 --- a/clapper_src/widget.js +++ b/clapper_src/widget.js @@ -1,4 +1,4 @@ -const { Gdk, GLib, GObject, Gtk, GstPlayer } = imports.gi; +const { Gdk, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi; const { Controls } = imports.clapper_src.controls; const Debug = imports.clapper_src.debug; const Misc = imports.clapper_src.misc; @@ -7,8 +7,13 @@ const Revealers = imports.clapper_src.revealers; let { debug } = Debug; -var Widget = GObject.registerClass( -class ClapperWidget extends Gtk.Grid +var Widget = GObject.registerClass({ + Signals: { + 'fullscreen-changed': { + param_types: [GObject.TYPE_BOOLEAN] + } + } +}, class ClapperWidget extends Gtk.Grid { _init(opts) { @@ -16,8 +21,9 @@ class ClapperWidget extends Gtk.Grid super._init(); + let clapperPath = Misc.getClapperPath(); let defaults = { - cssPath: `${pkg.datadir}/${pkg.name}/css/styles.css`, + cssPath: `${clapperPath}/css/styles.css`, }; Object.assign(this, defaults, opts); @@ -188,18 +194,29 @@ class ClapperWidget extends Gtk.Grid updateTitles(mediaInfo) { - let root = this.get_root(); - if(!root) return; + let title = mediaInfo.get_title(); + let subtitle = mediaInfo.get_uri(); - let title; + if(Gst.Uri.get_protocol(subtitle) === 'file') { + subtitle = GLib.path_get_basename( + GLib.filename_from_uri(subtitle)[0] + ); + } + if(!title) { + title = (!subtitle) + ? this.defaultTitle + : (subtitle.includes('.')) + ? subtitle.split('.').slice(0, -1).join('.') + : subtitle; + + subtitle = null; + } + + let root = this.get_root(); let headerbar = root.get_titlebar(); - if(headerbar && headerbar.updateHeaderBar) { - headerbar.updateHeaderBar(mediaInfo); - title = headerbar.titleLabel.label; - } - else - title = mediaInfo.get_title() || mediaInfo.get_uri(); + if(headerbar && headerbar.updateHeaderBar) + headerbar.updateHeaderBar(title, subtitle); this.revealerTop.setMediaTitle(title); } @@ -368,6 +385,7 @@ class ClapperWidget extends Gtk.Grid this.showControls(isFullscreen); this.player.widget.grab_focus(); + this.emit('fullscreen-changed', isFullscreen); debug(`interface in fullscreen mode: ${isFullscreen}`); } diff --git a/clapper_src/window.js b/clapper_src/window.js deleted file mode 100644 index 5748a589..00000000 --- a/clapper_src/window.js +++ /dev/null @@ -1,18 +0,0 @@ -const { Gdk, GObject, Gtk } = imports.gi; - -var Window = GObject.registerClass( -class ClapperWindow extends Gtk.ApplicationWindow -{ - _init(application, title) - { - super._init({ - application: application, - title: title, - }); - } - - updateTitlebar(mediaInfo) - { - - } -}); diff --git a/examples/app-example.js b/examples/app-example.js new file mode 100644 index 00000000..1f812baa --- /dev/null +++ b/examples/app-example.js @@ -0,0 +1,48 @@ +imports.gi.versions.Gtk = '4.0'; + +const Gtk = imports.gi.Gtk; +const Clapper = imports.clapper; + +let app = new Gtk.Application({ + application_id: 'com.clapper.AppExample' +}); + +app.connect('activate', () => { + let window = new Gtk.ApplicationWindow({ + application: app, + title: 'Clapper App Example', + }); + let grid = new Gtk.Grid({ + halign: Gtk.Align.CENTER, + valign: Gtk.Align.CENTER, + margin_start: 20, + margin_end: 20, + margin_top: 10, + margin_bottom: 10, + }); + let box = new Gtk.Box({ + orientation: Gtk.Orientation.HORIZONTAL, + spacing: 8, + }); + let button = new Gtk.Button({ + icon_name: 'folder-videos-symbolic', + }); + let label = new Gtk.Label({ + label: 'Click this button to play Big Buck Bunny!', + }); + + button.connect('clicked', () => { + let clapper = new Clapper.App({ + playlist: ['http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4'] + }); + clapper.run(); + }); + + box.append(label); + box.append(button); + grid.attach(box, 0, 0, 1, 1); + + window.set_child(grid); + window.present(); +}); +app.run([]); diff --git a/examples/widget-example.js b/examples/widget-example.js new file mode 100644 index 00000000..df904aa6 --- /dev/null +++ b/examples/widget-example.js @@ -0,0 +1,40 @@ +imports.gi.versions.Gtk = '4.0'; + +const Gtk = imports.gi.Gtk; +const Clapper = imports.clapper; + +let app = new Gtk.Application({ + application_id: 'com.clapper.WidgetExample' +}); + +app.connect('activate', () => { + let window = new Gtk.ApplicationWindow({ + application: app, + title: 'Clapper Widget Example', + }); + let box = new Gtk.Box({ + orientation: Gtk.Orientation.VERTICAL, + }); + let label = new Gtk.Label({ + label: [ + 'Clapper is used as a widget and placed below.', + 'Double click it to enter fullscreen!', + ].join('\n') + }); + let widget = new Clapper.Widget(); + + widget.connect('fullscreen-changed', (widget, isFullscreen) => { + label.set_visible(!isFullscreen); + }); + window.connect('show', () => { + let media = 'http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4'; + widget.player.set_media(media); + }); + + box.append(label); + box.append(widget); + + window.set_child(box); + window.present(); +}); +app.run([]); diff --git a/gjs-1.0/clapper.js.in b/gjs-1.0/clapper.js.in index c426d524..cd18d3da 100644 --- a/gjs-1.0/clapper.js.in +++ b/gjs-1.0/clapper.js.in @@ -1,8 +1,12 @@ imports.gi.versions.Gdk = '4.0'; imports.gi.versions.Gtk = '4.0'; -imports.searchPath.unshift('@importspath@'); + +const CLAPPER_PATH = '@importspath@'; +imports.searchPath.unshift(CLAPPER_PATH); const ClapperSrc = imports.clapper_src; +ClapperSrc.misc.clapperPath = CLAPPER_PATH; +ClapperSrc.misc.clapperVersion = '@PACKAGE_VERSION@'; var { App } = ClapperSrc.app; var { Widget } = ClapperSrc.widget; diff --git a/gjs-1.0/meson.build b/gjs-1.0/meson.build index 88b55ae6..1b952bf8 100644 --- a/gjs-1.0/meson.build +++ b/gjs-1.0/meson.build @@ -3,6 +3,7 @@ gjsdir = join_paths(sharedir, 'gjs-1.0') importspath = join_paths(sharedir, meson.project_name()) gjs_conf = configuration_data() +gjs_conf.set('PACKAGE_VERSION', meson.project_version()) gjs_conf.set('importspath', importspath) configure_file(