From 80ac01706d04fe4741eaf673447dd0aa17acb098 Mon Sep 17 00:00:00 2001 From: Rafostar <40623528+Rafostar@users.noreply.github.com> Date: Tue, 1 Sep 2020 10:50:30 +0200 Subject: [PATCH] Move GtkWindow logic to separate file --- clapper_src/app.js | 60 +++++++++++++++++-------------------------- clapper_src/window.js | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 clapper_src/window.js diff --git a/clapper_src/app.js b/clapper_src/app.js index cbd818d6..90f7398c 100644 --- a/clapper_src/app.js +++ b/clapper_src/app.js @@ -1,14 +1,15 @@ -const { Gdk, GLib, GObject, Gtk } = imports.gi; -const { Player } = imports.clapper_src.player; +const { GLib, GObject, Gtk } = imports.gi; const { Interface } = imports.clapper_src.interface; +const { Player } = imports.clapper_src.player; +const { Window } = imports.clapper_src.window; const APP_NAME = 'Clapper'; var App = GObject.registerClass({ Signals: { - 'player-ready': { + 'ready': { param_types: [GObject.TYPE_BOOLEAN] - } + }, } }, class ClapperApp extends Gtk.Application { @@ -18,9 +19,12 @@ var App = GObject.registerClass({ super._init(); - this.isFullscreen = false; + this.window = null; + this.interface = null; + this.player = null; + this.connect('startup', () => this._buildUI()); - this.connect('activate', () => this._openDialog()); + this.connect('activate', () => this._openWindow()); } run(arr) @@ -29,61 +33,43 @@ var App = GObject.registerClass({ super.run(arr); } - toggleFullscreen() - { - let isUn = (this.isFullscreen) ? 'un' : ''; - this.appWindow[`${isUn}fullscreen`](); - } - _buildUI() { - this.appWindow = new Gtk.ApplicationWindow({ - application: this, - title: APP_NAME, - border_width: 0, - resizable: true, - window_position: Gtk.WindowPosition.CENTER, - width_request: 960, - height_request: 642 - }); - this.appWindow.connect( - 'window-state-event', this._onWindowStateEvent.bind(this) + this.window = new Window(this, APP_NAME); + this.window.connect('realize', this._onWindowRealize.bind(this)); + this.window.connect( + 'fullscreen-changed', this._onWindowFullscreenChanged.bind(this) ); this.interface = new Interface(); this.interface.controls.toggleFullscreenButton.connect( - 'clicked', this.toggleFullscreen.bind(this) + 'clicked', () => this.window.toggleFullscreen() ); - this.appWindow.add(this.interface); - this.appWindow.connect('realize', this._onRealize.bind(this)); + this.window.add(this.interface); } - _openDialog() + _openWindow() { - this.appWindow.show_all(); + this.window.show_all(); } - _onRealize() + _onWindowRealize() { this.player = new Player(); this.interface.addPlayer(this.player); this.player.widget.show_all(); - this.emit('player-ready', true); + this.emit('ready', true); } - _onWindowStateEvent(widget, event) + _onWindowFullscreenChanged(window, isFullscreen) { - let window = event.get_window(); - let state = window.get_state(); - - this.isFullscreen = Boolean(state & Gdk.WindowState.FULLSCREEN); - this.interface.controls.toggleFullscreenButton.image = (this.isFullscreen) + this.interface.controls.toggleFullscreenButton.image = (isFullscreen) ? this.interface.controls.unfullscreenImage : this.interface.controls.fullscreenImage; - let action = (this.isFullscreen) ? 'hide' : 'show'; + let action = (isFullscreen) ? 'hide' : 'show'; this.interface.controls[action](); } }); diff --git a/clapper_src/window.js b/clapper_src/window.js new file mode 100644 index 00000000..153f22a2 --- /dev/null +++ b/clapper_src/window.js @@ -0,0 +1,48 @@ +const { Gdk, GObject, Gtk } = imports.gi; + +var Window = GObject.registerClass({ + Signals: { + 'fullscreen-changed': { + param_types: [GObject.TYPE_BOOLEAN] + }, + } +}, class ClapperWindow extends Gtk.ApplicationWindow +{ + _init(application, title) + { + super._init({ + application: application, + title: title || 'Clapper', + border_width: 0, + resizable: true, + window_position: Gtk.WindowPosition.CENTER, + width_request: 960, + height_request: 642 + }); + this.isFullscreen = false; + + this.connect( + 'window-state-event', this._onWindowStateEvent.bind(this) + ); + } + + toggleFullscreen() + { + let un = (this.isFullscreen) ? 'un' : ''; + this[`${un}fullscreen`](); + } + + _onWindowStateEvent(self, event) + { + let window = event.get_window(); + let state = window.get_state(); + + let isFullscreen = Boolean(state & Gdk.WindowState.FULLSCREEN); + + if(this.isFullscreen === isFullscreen) + return; + + this.isFullscreen = isFullscreen; + this.emit('fullscreen-changed', this.isFullscreen); + } +});