summaryrefslogtreecommitdiff
path: root/python/astal/widget.py
blob: 8f2285f0071d2c23809dcf72dd2cc246988ed4e9 (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
71
72
73
74
75
76
77
78
from gi.repository import Astal, Gtk
from .binding import Binding, kebabify


def set_child(self, child):
    if isinstance(self, Gtk.Bin):
        self.remove(self.get_child())
    if isinstance(self, Gtk.Container):
        self.add(child)


def astalify(ctor):
    ctor.set_css = Astal.widget_set_css
    ctor.get_css = Astal.widget_get_css

    ctor.set_class_name = lambda self, names: Astal.widget_set_class_names(self, names.split())
    ctor.get_class_name = lambda self: " ".join(Astal.widget_set_class_names(self))

    ctor.set_cursor = Astal.widget_set_cursor
    ctor.get_cursor = Astal.widget_get_cursor

    def widget(**kwargs):
        args = {}
        bindings = {}
        handlers = {}
        setup = None
        if not hasattr(kwargs, "visible"):
            kwargs["visible"] = True

        for key, value in kwargs.items():
            if key == "setup":
                setup = value
            if isinstance(value, Binding):
                bindings[key] = value
            if key.startswith("on_"):
                handlers[key] = value
            else:
                args[key] = value

        self = ctor(**args)

        for key, value in bindings.items():
            setter = getattr(self, f"set_{key}")
            setter(value.get())
            unsub = value.subscribe(setter)
            self.connect("destroy", lambda _: unsub())

        for key, value in handlers.items():
            self.connect(kebabify(key.replace("on_", "")), value)

        if setup:
            setup(self)

        return self

    return widget


Box = astalify(Astal.Box),
Button = astalify(Astal.Button),
CenterBox = astalify(Astal.CenterBox),
# TODO: CircularProgress
DrawingArea = astalify(Gtk.DrawingArea),
Entry = astalify(Gtk.Entry),
EventBox = astalify(Astal.EventBox),
# TODO: Fixed
# TODO: FlowBox
Icon = astalify(Astal.Icon),
Label = astalify(Gtk.Label),
LevelBar = astalify(Astal.LevelBar),
# TODO: ListBox
Overlay = astalify(Astal.Overlay),
Revealer = astalify(Gtk.Revealer),
Scrollable = astalify(Astal.Scrollable),
Slider = astalify(Astal.Slider),
# TODO: Stack
Switch = astalify(Gtk.Switch),
Window = astalify(Astal.Window),