summaryrefslogtreecommitdiff
path: root/core/src/widget/box.vala
blob: 39dee92dab19d066cb26e42da97995fa0669e411 (plain)
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
namespace Astal {
public class Box : Gtk.Box {
    [CCode (notify = false)]
    public bool vertical {
        get { return orientation == Gtk.Orientation.VERTICAL; }
        set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
    }

    /**
     * wether to implicity destroy previous children when setting them
     */
    public bool implicit_destroy { get; set; default = true; }

    public List<weak Gtk.Widget> children {
        set { _set_children(value); }
        owned get { return get_children(); }
    }

    public new Gtk.Widget child {
        owned get { return _get_child(); }
        set { _set_child(value); }
    }

    construct {
        notify["orientation"].connect(() => {
            notify_property("vertical");
        });
    }

    private void _set_child(Gtk.Widget child) {
        var list = new List<weak Gtk.Widget>();
        list.append(child);
        _set_children(list);
    }

    private Gtk.Widget? _get_child() {
        foreach(var child in get_children())
            return child;

        return null;
    }

    private void _set_children(List<weak Gtk.Widget> arr) {
        foreach(var child in get_children()) {
            if (implicit_destroy && arr.find(child).length() == 0)
                child.destroy();
            else
                remove(child);
        }

        foreach(var child in arr)
            add(child);
    }

    public Box(bool vertical, List<weak Gtk.Widget> children) {
        this.vertical = vertical;
        _set_children(children);
    }

    public Box.newh(List<weak Gtk.Widget> children) {
        this.vertical = false;
        _set_children(children);
    }

    public Box.newv(List<weak Gtk.Widget> children) {
        this.vertical = true;
        _set_children(children);
    }
}
}