diff options
author | Aylur <[email protected]> | 2024-05-19 02:39:53 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-05-19 02:39:53 +0200 |
commit | 1425b396b08f0e91d45bbd0f92b1309115c7c870 (patch) | |
tree | 8af1a899a14d8a01a9ef50e248c077b48aed25bc /python/astal/widget.py |
init 0.1.0
Diffstat (limited to 'python/astal/widget.py')
-rw-r--r-- | python/astal/widget.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/python/astal/widget.py b/python/astal/widget.py new file mode 100644 index 0000000..7070e8f --- /dev/null +++ b/python/astal/widget.py @@ -0,0 +1,65 @@ +from gi.repository import Astal, Gtk +from .binding import Binding + + +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(key.replace("on_", ""), value) + + if setup: + setup(self) + + return self + + return widget + + +Window = astalify(Astal.Window) +Box = astalify(Astal.Box) +Button = astalify(Astal.Button) +CenterBox = astalify(Astal.CenterBox) +Label = astalify(Gtk.Label) +Icon = astalify(Astal.Icon) +EventBox = astalify(Astal.EventBox) |