Skip to content

Commit d203255

Browse files
committed
add: stack widget
1 parent 1373457 commit d203255

File tree

8 files changed

+51
-7
lines changed

8 files changed

+51
-7
lines changed

core/gjs/src/astalify.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ function setChildren(parent: Gtk.Widget, children: Gtk.Widget[]) {
2020
parent.remove(ch)
2121
}
2222

23-
// FIXME: add rest of the edge cases like Stack
23+
// TODO: add more container types
2424
if (parent instanceof Astal.Box) {
2525
parent.set_children(children)
2626
}
2727

28+
else if (parent instanceof Astal.Stack) {
29+
parent.set_children(children)
30+
}
31+
2832
else if (parent instanceof Astal.CenterBox) {
2933
parent.startWidget = children[0]
3034
parent.centerWidget = children[1]

core/gjs/src/jsx/jsx-runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const ctors = {
4949
revealer: Widget.Revealer,
5050
scrollable: Widget.Scrollable,
5151
slider: Widget.Slider,
52-
// TODO: stack
52+
stack: Widget.Stack,
5353
switch: Widget.Switch,
5454
window: Widget.Window,
5555
}

core/gjs/src/widgets.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export type SliderProps = ConstructProps<Astal.Slider, Astal.Slider.ConstructorP
100100
onDragged: []
101101
}>
102102

103-
// TODO: Stack
103+
// Stack
104+
export type Stack = Widget<Astal.Stack>
105+
export const Stack = astalify<typeof Astal.Stack, StackProps, "Stack">(Astal.Stack)
106+
export type StackProps = ConstructProps<Astal.Stack, Astal.Stack.ConstructorProps>
104107

105108
// Switch
106109
export type Switch = Widget<Gtk.Switch>

core/lua/astal/widget.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ local function set_children(parent, children)
6262
end
6363
end
6464

65-
-- FIXME: add rest of the edge cases like Stack
65+
-- TODO: add more container types
6666
if Astal.Box:is_type_of(parent) then
6767
parent:set_children(children)
68+
elseif Astal.Stack:is_type_of(parent) then
69+
parent:set_children(children)
6870
elseif Astal.CenterBox:is_type_of(parent) then
6971
parent.start_widget = children[1]
7072
parent.center_widget = children[2]
@@ -223,7 +225,7 @@ local Widget = {
223225
Revealer = astalify(Gtk.Revealer),
224226
Scrollable = astalify(Astal.Scrollable),
225227
Slider = astalify(Astal.Slider),
226-
-- TODO: Stack
228+
Stack = astalify(Astal.Stack),
227229
Switch = astalify(Gtk.Switch),
228230
Window = astalify(Astal.Window),
229231
}

core/src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ sources = [
3939
'widget/overlay.vala',
4040
'widget/scrollable.vala',
4141
'widget/slider.vala',
42+
'widget/stack.vala',
4243
'widget/widget.vala',
4344
'widget/window.vala',
4445
'astal.vala',

core/src/widget/box.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Box : Gtk.Box {
77
}
88

99
/**
10-
* wether to implicity destroy previous children when setting them
10+
* whether to implicity destroy previous children when setting them
1111
*/
1212
public bool no_implicit_destroy { get; set; default = false; }
1313

core/src/widget/stack.vala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Astal.Switch : Gtk.Stack {
2+
/**
3+
* whether to implicity destroy previous children when setting them
4+
*/
5+
public bool no_implicit_destroy { get; set; default = false; }
6+
7+
public string shown {
8+
get { return visible_child_name; }
9+
set { visible_child_name = value; }
10+
}
11+
12+
public List<weak Gtk.Widget> children {
13+
set { _set_children(value); }
14+
owned get { return get_children(); }
15+
}
16+
17+
private void _set_children(List<weak Gtk.Widget> arr) {
18+
foreach(var child in get_children()) {
19+
if (no_implicit_destroy)
20+
remove(child);
21+
else if (arr.find(child).length() == 0)
22+
child.destroy();
23+
}
24+
25+
var i = 0;
26+
foreach(var child in arr) {
27+
if (child.name != null) {
28+
add_named(child, child.name);
29+
} else {
30+
add_named(child, (++i).to_string());
31+
}
32+
}
33+
}
34+
}

docs/ags/first-widgets.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ return <box>
350350
```
351351

352352
:::warning
353-
Only bind children of the `box` widget. Gtk does not cleanup widgets by default,
353+
Only bind children of the `box` or the `stack` widget. Gtk does not cleanup widgets by default,
354354
they have to be explicitly destroyed. The box widget is a special container that
355355
will implicitly call `.destroy()` on its removed child widgets.
356356
You can disable this behavior by setting the `noImplicityDestroy` property.

0 commit comments

Comments
 (0)