add: stack widget

This commit is contained in:
Aylur
2024-09-11 22:39:15 +00:00
parent 137345755c
commit d203255ec2
8 changed files with 51 additions and 7 deletions

View File

@@ -20,11 +20,15 @@ function setChildren(parent: Gtk.Widget, children: Gtk.Widget[]) {
parent.remove(ch)
}
// FIXME: add rest of the edge cases like Stack
// TODO: add more container types
if (parent instanceof Astal.Box) {
parent.set_children(children)
}
else if (parent instanceof Astal.Stack) {
parent.set_children(children)
}
else if (parent instanceof Astal.CenterBox) {
parent.startWidget = children[0]
parent.centerWidget = children[1]

View File

@@ -49,7 +49,7 @@ const ctors = {
revealer: Widget.Revealer,
scrollable: Widget.Scrollable,
slider: Widget.Slider,
// TODO: stack
stack: Widget.Stack,
switch: Widget.Switch,
window: Widget.Window,
}

View File

@@ -100,7 +100,10 @@ export type SliderProps = ConstructProps<Astal.Slider, Astal.Slider.ConstructorP
onDragged: []
}>
// TODO: Stack
// Stack
export type Stack = Widget<Astal.Stack>
export const Stack = astalify<typeof Astal.Stack, StackProps, "Stack">(Astal.Stack)
export type StackProps = ConstructProps<Astal.Stack, Astal.Stack.ConstructorProps>
// Switch
export type Switch = Widget<Gtk.Switch>

View File

@@ -62,9 +62,11 @@ local function set_children(parent, children)
end
end
-- FIXME: add rest of the edge cases like Stack
-- TODO: add more container types
if Astal.Box:is_type_of(parent) then
parent:set_children(children)
elseif Astal.Stack:is_type_of(parent) then
parent:set_children(children)
elseif Astal.CenterBox:is_type_of(parent) then
parent.start_widget = children[1]
parent.center_widget = children[2]
@@ -223,7 +225,7 @@ local Widget = {
Revealer = astalify(Gtk.Revealer),
Scrollable = astalify(Astal.Scrollable),
Slider = astalify(Astal.Slider),
-- TODO: Stack
Stack = astalify(Astal.Stack),
Switch = astalify(Gtk.Switch),
Window = astalify(Astal.Window),
}

View File

@@ -39,6 +39,7 @@ sources = [
'widget/overlay.vala',
'widget/scrollable.vala',
'widget/slider.vala',
'widget/stack.vala',
'widget/widget.vala',
'widget/window.vala',
'astal.vala',

View File

@@ -7,7 +7,7 @@ public class Box : Gtk.Box {
}
/**
* wether to implicity destroy previous children when setting them
* whether to implicity destroy previous children when setting them
*/
public bool no_implicit_destroy { get; set; default = false; }

View File

@@ -0,0 +1,34 @@
public class Astal.Switch : Gtk.Stack {
/**
* whether to implicity destroy previous children when setting them
*/
public bool no_implicit_destroy { get; set; default = false; }
public string shown {
get { return visible_child_name; }
set { visible_child_name = value; }
}
public List<weak Gtk.Widget> children {
set { _set_children(value); }
owned get { return get_children(); }
}
private void _set_children(List<weak Gtk.Widget> arr) {
foreach(var child in get_children()) {
if (no_implicit_destroy)
remove(child);
else if (arr.find(child).length() == 0)
child.destroy();
}
var i = 0;
foreach(var child in arr) {
if (child.name != null) {
add_named(child, child.name);
} else {
add_named(child, (++i).to_string());
}
}
}
}

View File

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