@@ -8,12 +8,12 @@ description: Using GTK widgets not builtin
8
8
Use them like regular GTK widgets
9
9
10
10
``` js
11
- import Gtk from ' gi://Gtk' ;
11
+ import Gtk from ' gi://Gtk'
12
12
13
13
const calendar = new Gtk.Calendar ({
14
14
showDayNames: false ,
15
15
showHeading: true ,
16
- });
16
+ })
17
17
```
18
18
19
19
You can subclass Gtk.Widget not builtin to behave like AGS widgets.
@@ -30,7 +30,7 @@ const myCalendar = Calendar({
30
30
setup (self ) {
31
31
self .bind ()
32
32
}
33
- });
33
+ })
34
34
```
35
35
36
36
::: tip
@@ -48,12 +48,12 @@ and utilize [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/C
48
48
49
49
``` js
50
50
function CounterButton (({ color = ' aqua' , ... rest })) {
51
- const count = Variable (0 );
51
+ const count = Variable (0 )
52
52
53
53
const label = Widget .Label ({
54
54
label: count .bind ().as (v => ` count: ${ v} ` ),
55
55
style: ` color: ${ color} ` ,
56
- });
56
+ })
57
57
58
58
return Widget .Button ({
59
59
... rest, // spread passed parameters
@@ -66,7 +66,7 @@ function CounterButton(({ color = 'aqua', ...rest })) {
66
66
const button = CounterButton ({
67
67
color: ' blue' ,
68
68
className: ' my-widget' ,
69
- });
69
+ })
70
70
```
71
71
72
72
This approach comes with the limitation that parameters passed to these
@@ -87,27 +87,27 @@ class CounterButton extends Gtk.Button {
87
87
// the super constructor will take care of setting the count prop
88
88
// so you don't have to explicitly set count in the constructor
89
89
constructor (props ) {
90
- super (props);
90
+ super (props)
91
91
92
92
const label = new Gtk.Label ({
93
93
label: ` ${ this .count } ` ,
94
- });
94
+ })
95
95
96
- this .add (label);
96
+ this .add (label)
97
97
98
98
this .connect (' clicked' , () => {
99
- this .count ++ ;
100
- label .label = ` ${ this .count } ` ;
101
- });
99
+ this .count ++
100
+ label .label = ` ${ this .count } `
101
+ })
102
102
}
103
103
104
104
get count () {
105
- return this ._count || 0 ;
105
+ return this ._count || 0
106
106
}
107
107
108
108
set count (num ) {
109
- this ._count = num;
110
- this .notify (' count' );
109
+ this ._count = num
110
+ this .notify (' count' )
111
111
}
112
112
}
113
113
```
@@ -125,4 +125,39 @@ const counterButton = new CounterButton({
125
125
counterButton .connect (' notify::count' , ({ count }) => {
126
126
print (count);
127
127
})
128
+
129
+ counterButton .count += 1
130
+ ```
131
+
132
+ ::: tip
133
+ You will never actually need to subclass, you can * fake* gobject props using Variables
134
+
135
+ ``` js
136
+ function CounterButton ({ count, ... props }) {
137
+ const counter = Variable (count)
138
+
139
+ const button = Widget .Button ({
140
+ on_clicked : () => counter .value += 1 ,
141
+ child: Widget .Label ({
142
+ label: counter .bind ().as (c => ` ${ c} ` ),
143
+ }),
144
+ ... props,
145
+ })
146
+
147
+ return Object .assign (button, {
148
+ count: counter,
149
+ })
150
+ }
151
+
152
+ const counterButton = CounterButton ({
153
+ count: 0 ,
154
+ })
155
+
156
+ counterButton .count .connect (" changed" , ({ value }) => {
157
+ print (value)
158
+ })
159
+
160
+ counterButton .count .value += 1
128
161
```
162
+
163
+ :::
0 commit comments