diff options
author | Aylur <[email protected]> | 2024-09-12 22:56:08 +0000 |
---|---|---|
committer | Aylur <[email protected]> | 2024-09-12 22:56:08 +0000 |
commit | 6fce8da11ac7dad427e4594beffdf60f109768e7 (patch) | |
tree | 2c7e8894245ac60f3c2931b2c712c49d0dc2c54e | |
parent | 3a057f53a265b2a5530ae4888fb7992cd0180c89 (diff) |
fix: widget constructor assigning order
assign children first, so props that depend on them work
for example Stack.shown
-rw-r--r-- | core/gjs/src/astalify.ts | 39 | ||||
-rw-r--r-- | core/lua/astal/widget.lua | 26 | ||||
-rw-r--r-- | nix/lua.nix | 2 |
3 files changed, 36 insertions, 31 deletions
diff --git a/core/gjs/src/astalify.ts b/core/gjs/src/astalify.ts index 612239e..bfe6623 100644 --- a/core/gjs/src/astalify.ts +++ b/core/gjs/src/astalify.ts @@ -129,16 +129,17 @@ function ctor(self: any, config: any = {}, children: any = []) { const { setup, ...props } = config props.visible ??= true + // collect bindings const bindings = Object.keys(props).reduce((acc: any, prop) => { if (props[prop] instanceof Binding) { const binding = props[prop] - setProp(self, prop, binding.get()) delete props[prop] return [...acc, [prop, binding]] } return acc }, []) + // collect signal handlers const onHandlers = Object.keys(props).reduce((acc: any, key) => { if (key.startsWith("on")) { const sig = kebabify(key).split("-").slice(1).join("-") @@ -149,8 +150,21 @@ function ctor(self: any, config: any = {}, children: any = []) { return acc }, []) - Object.assign(self, props) + // set children + children = mergeBindings(children.flat(Infinity)) + if (children instanceof Binding) { + setChildren(self, children.get()) + self.connect("destroy", children.subscribe((v) => { + setChildren(self, v) + })) + } + else { + if (children.length > 0) { + setChildren(self, children) + } + } + // setup signal handlers for (const [signal, callback] of onHandlers) { if (typeof callback === "function") { self.connect(signal, callback) @@ -161,29 +175,20 @@ function ctor(self: any, config: any = {}, children: any = []) { } } - for (const [prop, bind] of bindings) { + // setup bindings handlers + for (const [prop, binding] of bindings) { if (prop === "child" || prop === "children") { - self.connect("destroy", bind.subscribe((v: any) => { + self.connect("destroy", binding.subscribe((v: any) => { setChildren(self, v) })) } - self.connect("destroy", bind.subscribe((v: any) => { + self.connect("destroy", binding.subscribe((v: any) => { setProp(self, prop, v) })) + setProp(self, prop, binding.get()) } - children = mergeBindings(children.flat(Infinity)) - if (children instanceof Binding) { - setChildren(self, children.get()) - self.connect("destroy", children.subscribe((v) => { - setChildren(self, v) - })) - } - else { - if (children.length > 0) - setChildren(self, children) - } - + Object.assign(self, props) setup?.(self) return self } diff --git a/core/lua/astal/widget.lua b/core/lua/astal/widget.lua index 2383dbb..5e74440 100644 --- a/core/lua/astal/widget.lua +++ b/core/lua/astal/widget.lua @@ -154,12 +154,12 @@ local function astalify(ctor) tbl.visible = true end - -- filter props + -- collect props local props = filter(tbl, function(_, key) return type(key) == "string" and key ~= "setup" end) - -- handle on_ handlers that are strings + -- collect signal handlers for prop, value in pairs(props) do if string.sub(prop, 0, 2) == "on" and type(value) ~= "function" then props[prop] = function() @@ -168,7 +168,7 @@ local function astalify(ctor) end end - -- handle bindings + -- collect bindings for prop, value in pairs(props) do if getmetatable(value) == Binding then bindings[prop] = value @@ -179,16 +179,6 @@ local function astalify(ctor) -- construct, attach bindings, add children local widget = ctor() - for prop, value in pairs(props) do - widget[prop] = value - end - - for prop, binding in pairs(bindings) do - widget.on_destroy = binding:subscribe(function(v) - widget[prop] = v - end) - end - if getmetatable(children) == Binding then set_children(widget, children:get()) widget.on_destroy = children:subscribe(function(v) @@ -200,6 +190,16 @@ local function astalify(ctor) end end + for prop, binding in pairs(bindings) do + widget.on_destroy = binding:subscribe(function(v) + widget[prop] = v + end) + end + + for prop, value in pairs(props) do + widget[prop] = value + end + if type(setup) == "function" then setup(widget) end diff --git a/nix/lua.nix b/nix/lua.nix index 0986836..6b0b802 100644 --- a/nix/lua.nix +++ b/nix/lua.nix @@ -62,7 +62,7 @@ in preFixup = '' gappsWrapperArgs+=( - --prefix PATH : ${pkgs.lib.makeBinPath extraPackages} + --prefix PATH : "${pkgs.lib.makeBinPath extraPackages}" ) ''; } |