-
Notifications
You must be signed in to change notification settings - Fork 42
/
gtk-4-custom-widget.js
118 lines (93 loc) · 3 KB
/
gtk-4-custom-widget.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* test-gtk-4.js
*/
const gi = require('..')
const GLib = gi.require('GLib', '2.0')
const Gtk = gi.require('Gtk', '4.0')
const Gdk = gi.require('Gdk', '4.0')
const Graphene = gi.require('Graphene', '1.0')
Gtk.init()
/* Define our custom widget */
class CustomWidget extends Gtk.Widget {
customMethod() {}
measure(orientation, forSize) {
const [minWidth, natWidth] = [100, 200]
const [minHeight, natHeight] = [20, 40]
const isHorizontal = orientation === Gtk.Orientation.HORIZONTAL
const minimum = isHorizontal ? minWidth : minHeight
const natural = isHorizontal ? natWidth : natHeight
const minimumBaseline = !isHorizontal ? minWidth : minHeight
const naturalBaseline = !isHorizontal ? natWidth : natHeight
return [minimum, natural, minimumBaseline, naturalBaseline]
}
snapshot(snapshot) {
const width = this.getAllocatedWidth()
const color = Gdk.RGBA.create('red')
const rect = Graphene.Rect.create(10, 10, width / 2, 10)
snapshot.appendColor(color, rect)
}
}
gi.registerClass(CustomWidget)
/* Setup & start the application */
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)
/* Event handlers */
function onActivate() {
const window = new Gtk.ApplicationWindow(app)
window.setTitle('Window')
window.setDefaultSize(200, 200)
window.on('close-request', onQuit)
const ui = getUI()
const builder = Gtk.Builder.newFromString(ui, ui.length)
const root = builder.getObject('root')
const custom = new CustomWidget()
root.append(custom)
const actionButton = builder.getObject('actionButton')
actionButton.on('clicked', () => {
console.log('clicked')
custom.customMethod()
})
const closeButton = builder.getObject('closeButton')
closeButton.on('clicked', () => window.close())
window.setChild(root)
window.present()
gi.startLoop()
loop.run()
}
function onQuit() {
loop.quit()
app.quit()
return false
}
function getUI() {
return `
<?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>
`
}