From ac06e84851a592a2536e799c7d381796ef81d758 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Mon, 26 Oct 2020 21:25:13 +0100 Subject: [PATCH] Open files via native file chooser --- clapper_src/app.js | 3 ++- clapper_src/filechooser.js | 46 ++++++++++++++++++++++++++++++++++++++ clapper_src/headerbar.js | 11 +++++++-- clapper_src/menu.js | 8 +++++++ clapper_src/widget.js | 9 +++++++- ui/clapper.ui | 8 +++++++ 6 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 clapper_src/filechooser.js diff --git a/clapper_src/app.js b/clapper_src/app.js index d0dfc31d..c0aab679 100644 --- a/clapper_src/app.js +++ b/clapper_src/app.js @@ -45,7 +45,8 @@ class ClapperApp extends Gtk.Application `${clapperPath}/ui/clapper.ui` ); let models = { - settingsMenu: uiBuilder.get_object('settingsMenu') + addMediaMenu: uiBuilder.get_object('addMediaMenu'), + settingsMenu: uiBuilder.get_object('settingsMenu'), }; let headerBar = new HeaderBar(window, models); window.set_titlebar(headerBar); diff --git a/clapper_src/filechooser.js b/clapper_src/filechooser.js new file mode 100644 index 00000000..babe4110 --- /dev/null +++ b/clapper_src/filechooser.js @@ -0,0 +1,46 @@ +const { Gio, GObject, Gtk } = imports.gi; + +var FileChooser = GObject.registerClass( +class ClapperFileChooser extends Gtk.FileChooserNative +{ + _init(window, appName) + { + super._init({ + transient_for: window, + }); + + let filter = new Gtk.FileFilter({ + name: 'Media Files', + }); + filter.add_mime_type('video/*'); + filter.add_mime_type('audio/*'); + filter.add_mime_type('application/claps'); + this.add_filter(filter); + + this.responseSignal = this.connect('response', this._onResponse.bind(this)); + } + + present() + { + /* File chooser closes itself when nobody is holding its ref */ + this.ref(); + + super.show(); + } + + _onResponse(filechooser, response) + { + this.disconnect(this.responseSignal); + this.responseSignal = null; + + if(response === Gtk.ResponseType.ACCEPT) { + let file = this.get_file(); + let { player } = this.get_transient_for().get_child(); + + player.set_media(file.get_uri()); + } + + this.unref(); + this.run_dispose(); + } +}); diff --git a/clapper_src/headerbar.js b/clapper_src/headerbar.js index 230dd1a2..27af8a83 100644 --- a/clapper_src/headerbar.js +++ b/clapper_src/headerbar.js @@ -11,15 +11,22 @@ class ClapperHeaderBar extends Gtk.HeaderBar this.set_title_widget(this._createWidgetForWindow(window)); + let addMediaButton = new Gtk.MenuButton({ + icon_name: 'list-add-symbolic', + }); + let addMediaPopover = new HeaderBarPopover(models.addMediaMenu); + addMediaButton.set_popover(addMediaPopover); + this.pack_start(addMediaButton); + let openMenuButton = new Gtk.MenuButton({ - icon_name: 'open-menu-symbolic' + icon_name: 'open-menu-symbolic', }); let settingsPopover = new HeaderBarPopover(models.settingsMenu); openMenuButton.set_popover(settingsPopover); this.pack_end(openMenuButton); let fullscreenButton = new Gtk.Button({ - icon_name: 'view-fullscreen-symbolic' + icon_name: 'view-fullscreen-symbolic', }); fullscreenButton.connect('clicked', () => this.get_parent().fullscreen()); this.pack_end(fullscreenButton); diff --git a/clapper_src/menu.js b/clapper_src/menu.js index 70b1b7db..f9e49b7a 100644 --- a/clapper_src/menu.js +++ b/clapper_src/menu.js @@ -1,8 +1,10 @@ const { GObject, Gst, Gtk } = imports.gi; const Misc = imports.clapper_src.misc; const Prefs = imports.clapper_src.prefs; +const { FileChooser } = imports.clapper_src.filechooser; var actions = [ + openLocal, prefs, about, ]; @@ -11,6 +13,12 @@ var accels = [ ['app.quit', ['q']], ]; +function openLocal(window, appName) +{ + let fileChooser = new FileChooser(window); + fileChooser.present(); +} + function prefs(window, appName) { let prefsWidget = Prefs.buildPrefsWidget(); diff --git a/clapper_src/widget.js b/clapper_src/widget.js index b7c91c65..87eb0134 100644 --- a/clapper_src/widget.js +++ b/clapper_src/widget.js @@ -278,8 +278,10 @@ var Widget = GObject.registerClass({ { switch(state) { case GstPlayer.PlayerState.BUFFERING: - if(!player.is_local_file) + if(!player.is_local_file) { this.needsTracksUpdate = true; + } + debug('player state changed to: BUFFERING'); break; case GstPlayer.PlayerState.STOPPED: this.controls.currentPosition = 0; @@ -290,8 +292,12 @@ var Widget = GObject.registerClass({ player.disconnect(this.mediaInfoSignal); this.mediaInfoSignal = null; } + this.controls.togglePlayButton.setPrimaryIcon(); + debug('player state changed to: STOPPED'); + break; case GstPlayer.PlayerState.PAUSED: this.controls.togglePlayButton.setPrimaryIcon(); + debug('player state changed to: PAUSED'); break; case GstPlayer.PlayerState.PLAYING: this.controls.togglePlayButton.setSecondaryIcon(); @@ -301,6 +307,7 @@ var Widget = GObject.registerClass({ 'media_info_updated', this._onMediaInfoUpdated.bind(this) ); } + debug('player state changed to: PLAYING'); break; default: break; diff --git a/ui/clapper.ui b/ui/clapper.ui index c61b488d..3c61483b 100644 --- a/ui/clapper.ui +++ b/ui/clapper.ui @@ -1,5 +1,13 @@ + +
+ + Open Local File... + app.openLocal + +
+