Move GtkWindow logic to separate file

This commit is contained in:
Rafostar
2020-09-01 10:50:30 +02:00
parent e35d18505e
commit 80ac01706d
2 changed files with 71 additions and 37 deletions

View File

@@ -1,14 +1,15 @@
const { Gdk, GLib, GObject, Gtk } = imports.gi; const { GLib, GObject, Gtk } = imports.gi;
const { Player } = imports.clapper_src.player;
const { Interface } = imports.clapper_src.interface; const { Interface } = imports.clapper_src.interface;
const { Player } = imports.clapper_src.player;
const { Window } = imports.clapper_src.window;
const APP_NAME = 'Clapper'; const APP_NAME = 'Clapper';
var App = GObject.registerClass({ var App = GObject.registerClass({
Signals: { Signals: {
'player-ready': { 'ready': {
param_types: [GObject.TYPE_BOOLEAN] param_types: [GObject.TYPE_BOOLEAN]
} },
} }
}, class ClapperApp extends Gtk.Application }, class ClapperApp extends Gtk.Application
{ {
@@ -18,9 +19,12 @@ var App = GObject.registerClass({
super._init(); super._init();
this.isFullscreen = false; this.window = null;
this.interface = null;
this.player = null;
this.connect('startup', () => this._buildUI()); this.connect('startup', () => this._buildUI());
this.connect('activate', () => this._openDialog()); this.connect('activate', () => this._openWindow());
} }
run(arr) run(arr)
@@ -29,61 +33,43 @@ var App = GObject.registerClass({
super.run(arr); super.run(arr);
} }
toggleFullscreen()
{
let isUn = (this.isFullscreen) ? 'un' : '';
this.appWindow[`${isUn}fullscreen`]();
}
_buildUI() _buildUI()
{ {
this.appWindow = new Gtk.ApplicationWindow({ this.window = new Window(this, APP_NAME);
application: this, this.window.connect('realize', this._onWindowRealize.bind(this));
title: APP_NAME, this.window.connect(
border_width: 0, 'fullscreen-changed', this._onWindowFullscreenChanged.bind(this)
resizable: true,
window_position: Gtk.WindowPosition.CENTER,
width_request: 960,
height_request: 642
});
this.appWindow.connect(
'window-state-event', this._onWindowStateEvent.bind(this)
); );
this.interface = new Interface(); this.interface = new Interface();
this.interface.controls.toggleFullscreenButton.connect( this.interface.controls.toggleFullscreenButton.connect(
'clicked', this.toggleFullscreen.bind(this) 'clicked', () => this.window.toggleFullscreen()
); );
this.appWindow.add(this.interface); this.window.add(this.interface);
this.appWindow.connect('realize', this._onRealize.bind(this));
} }
_openDialog() _openWindow()
{ {
this.appWindow.show_all(); this.window.show_all();
} }
_onRealize() _onWindowRealize()
{ {
this.player = new Player(); this.player = new Player();
this.interface.addPlayer(this.player); this.interface.addPlayer(this.player);
this.player.widget.show_all(); 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(); this.interface.controls.toggleFullscreenButton.image = (isFullscreen)
let state = window.get_state();
this.isFullscreen = Boolean(state & Gdk.WindowState.FULLSCREEN);
this.interface.controls.toggleFullscreenButton.image = (this.isFullscreen)
? this.interface.controls.unfullscreenImage ? this.interface.controls.unfullscreenImage
: this.interface.controls.fullscreenImage; : this.interface.controls.fullscreenImage;
let action = (this.isFullscreen) ? 'hide' : 'show'; let action = (isFullscreen) ? 'hide' : 'show';
this.interface.controls[action](); this.interface.controls[action]();
} }
}); });

48
clapper_src/window.js Normal file
View File

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