Skip to content

Commit 7cb65a1

Browse files
committed
add tip to subclassing page
1 parent 194736e commit 7cb65a1

File tree

1 file changed

+50
-15
lines changed

1 file changed

+50
-15
lines changed

src/content/docs/config/subclassing-gtk-widgets.md

+50-15
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ description: Using GTK widgets not builtin
88
Use them like regular GTK widgets
99

1010
```js
11-
import Gtk from 'gi://Gtk';
11+
import Gtk from 'gi://Gtk'
1212

1313
const calendar = new Gtk.Calendar({
1414
showDayNames: false,
1515
showHeading: true,
16-
});
16+
})
1717
```
1818

1919
You can subclass Gtk.Widget not builtin to behave like AGS widgets.
@@ -30,7 +30,7 @@ const myCalendar = Calendar({
3030
setup(self) {
3131
self.bind()
3232
}
33-
});
33+
})
3434
```
3535

3636
:::tip
@@ -48,12 +48,12 @@ and utilize [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/C
4848

4949
```js
5050
function CounterButton(({ color = 'aqua', ...rest })) {
51-
const count = Variable(0);
51+
const count = Variable(0)
5252

5353
const label = Widget.Label({
5454
label: count.bind().as(v => `count: ${v}`),
5555
style: `color: ${color}`,
56-
});
56+
})
5757

5858
return Widget.Button({
5959
...rest, // spread passed parameters
@@ -66,7 +66,7 @@ function CounterButton(({ color = 'aqua', ...rest })) {
6666
const button = CounterButton({
6767
color: 'blue',
6868
className: 'my-widget',
69-
});
69+
})
7070
```
7171

7272
This approach comes with the limitation that parameters passed to these
@@ -87,27 +87,27 @@ class CounterButton extends Gtk.Button {
8787
// the super constructor will take care of setting the count prop
8888
// so you don't have to explicitly set count in the constructor
8989
constructor(props) {
90-
super(props);
90+
super(props)
9191

9292
const label = new Gtk.Label({
9393
label: `${this.count}`,
94-
});
94+
})
9595

96-
this.add(label);
96+
this.add(label)
9797

9898
this.connect('clicked', () => {
99-
this.count++;
100-
label.label = `${this.count}`;
101-
});
99+
this.count++
100+
label.label = `${this.count}`
101+
})
102102
}
103103

104104
get count() {
105-
return this._count || 0;
105+
return this._count || 0
106106
}
107107

108108
set count(num) {
109-
this._count = num;
110-
this.notify('count');
109+
this._count = num
110+
this.notify('count')
111111
}
112112
}
113113
```
@@ -125,4 +125,39 @@ const counterButton = new CounterButton({
125125
counterButton.connect('notify::count', ({ count }) => {
126126
print(count);
127127
})
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
128161
```
162+
163+
:::

0 commit comments

Comments
 (0)