blob: 890a855508db33650962995a7baef24ac6c56891 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2021 Andy Holmes <andyholmes@gnome.org>
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import Gtk from 'gi://Gtk?version=4.0';
Gtk.init();
/* In this example the template contents are loaded from the file as a string.
*
* The `Template` property of the class definition will accept:
* - a `Uint8Array` or `GLib.Bytes` of XML
* - an absolute file URI, such as `file:///home/user/window.ui`
* - a GResource URI, such as `resource:///org/gnome/AppName/window.ui`
*/
const file = Gio.File.new_for_path('gtk4-template.ui');
const [, template] = file.load_contents(null);
const ExampleWindow = GObject.registerClass({
GTypeName: 'ExampleWindow',
Template: template,
Children: [
'box',
],
InternalChildren: [
'button',
],
}, class ExampleWindow extends Gtk.Window {
constructor(params = {}) {
super(params);
// The template has been initialized and you can access the children
this.box.visible = true;
// Internal children are set on the instance prefixed with a `_`
this._button.visible = true;
}
// The signal handler bound in the UI file
_onButtonClicked(button) {
if (this instanceof Gtk.Window)
log('Callback scope is bound to `ExampleWindow`');
button.label = 'Button was clicked!';
}
});
// Create a window that stops the program when it is closed
const loop = GLib.MainLoop.new(null, false);
const win = new ExampleWindow();
win.connect('close-request', () => loop.quit());
win.present();
loop.run();
|