-
Notifications
You must be signed in to change notification settings - Fork 42
/
gtk-4.js
74 lines (61 loc) · 1.92 KB
/
gtk-4.js
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
62
63
64
65
66
67
68
69
70
71
72
73
/*
* gtk4.js
*/
const gi = require('..')
const GLib = gi.require('GLib', '2.0')
const Gtk = gi.require('Gtk', '4.0')
const printHello = () => console.log('Hello')
const loop = GLib.MainLoop.new(null, false)
const app = new Gtk.Application('com.github.romgrk.node-gtk.demo', 0)
app.on('activate', onActivate)
const status = app.run([])
console.log('Finished with status:', status)
function onActivate() {
const window = new Gtk.ApplicationWindow(app)
window.setTitle('Window')
window.setDefaultSize(200, 200)
window.on('close-request', onQuit)
const ui = `
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk" version="4.0"/>
<object class="GtkBox" id="root">
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="helloLabel">
<property name="vexpand">1</property>
<property name="label">Hello World!</property>
</object>
</child>
<child>
<object class="GtkButton" id="actionButton">
<property name="label" translatable="yes">Action</property>
<property name="receives_default">1</property>
</object>
</child>
<child>
<object class="GtkButton" id="closeButton">
<property name="label" translatable="yes">Close</property>
<property name="receives_default">1</property>
</object>
</child>
</object>
</interface>
`
const builder = Gtk.Builder.newFromString(ui, ui.length)
const root = builder.getObject('root')
const actionButton = builder.getObject('actionButton')
actionButton.on('clicked', printHello)
const closeButton = builder.getObject('closeButton')
closeButton.on('clicked', () => window.close())
window.setChild(root)
window.show()
window.present()
gi.startLoop()
loop.run()
}
function onQuit() {
loop.quit()
app.quit()
return false
}