From d6cbbdfb3f6e81c8265af36c88fd9547d93f9cbd Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 9 Oct 2024 03:08:51 +0000 Subject: core: implement generic no_implicit_destroy prop --- core/lua/astal/widget.lua | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) (limited to 'core/lua/astal/widget.lua') diff --git a/core/lua/astal/widget.lua b/core/lua/astal/widget.lua index 89cc4d5..8f49409 100644 --- a/core/lua/astal/widget.lua +++ b/core/lua/astal/widget.lua @@ -43,6 +43,15 @@ flatten = function(tbl) return copy end +local function includes(tbl, elem) + for _, value in pairs(tbl) do + if value == elem then + return true + end + end + return false +end + local function set_children(parent, children) children = map(flatten(children), function(item) if Gtk.Widget:is_type_of(item) then @@ -56,15 +65,19 @@ local function set_children(parent, children) -- remove if Gtk.Bin:is_type_of(parent) then - local rm = parent:get_child() - if rm ~= nil then - parent:remove(rm) + local ch = parent:get_child() + if ch ~= nil then + parent:remove(ch) + end + if ch ~= nil and not includes(children, ch) and not parent.no_implicit_destroy then + ch:destroy() end - elseif Gtk.Container:is_type_of(parent) and - not (Astal.Box:is_type_of(parent) or - Astal.Stack:is_type_of(parent)) then + elseif Gtk.Container:is_type_of(parent) then for _, ch in ipairs(parent:get_children()) do parent:remove(ch) + if ch ~= nil and not includes(children, ch) and not parent.no_implicit_destroy then + ch:destroy() + end end end @@ -177,7 +190,7 @@ local function astalify(ctor) for prop, value in pairs(props) do if string.sub(prop, 0, 2) == "on" and type(value) ~= "function" then props[prop] = function() - exec_async(value, print, print) + exec_async(value, print) end end end @@ -282,6 +295,21 @@ Gtk.Widget._attribute.click_through = { set = Astal.widget_set_click_through, } +local no_implicit_destroy = {} +Gtk.Widget._attribute.no_implicit_destroy = { + get = function(self) + return no_implicit_destroy[self] or false + end, + set = function(self, v) + if no_implicit_destroy[self] == nil then + self.on_destroy = function() + no_implicit_destroy[self] = nil + end + end + no_implicit_destroy[self] = v + end, +} + Astal.Box._attribute.children = { get = Astal.Box.get_children, set = Astal.Box.set_children, -- cgit v1.2.3 From 0def6d7e9fd8b66220171ee228f0e845ed6edb57 Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 9 Oct 2024 21:02:24 +0200 Subject: core(gjs, lua): widget constructor set props before children --- core/lua/astal/widget.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core/lua/astal/widget.lua') diff --git a/core/lua/astal/widget.lua b/core/lua/astal/widget.lua index 8f49409..73fe3b6 100644 --- a/core/lua/astal/widget.lua +++ b/core/lua/astal/widget.lua @@ -206,6 +206,10 @@ local function astalify(ctor) -- construct, attach bindings, add children local widget = ctor() + for prop, value in pairs(props) do + widget[prop] = value + end + if getmetatable(children) == Binding then set_children(widget, children:get()) widget.on_destroy = children:subscribe(function(v) @@ -223,10 +227,6 @@ local function astalify(ctor) end) end - for prop, value in pairs(props) do - widget[prop] = value - end - if type(setup) == "function" then setup(widget) end -- cgit v1.2.3 From 075c7e34aa0fbdc07c86965ab5a2ae79b92d3fd3 Mon Sep 17 00:00:00 2001 From: Aylur Date: Thu, 10 Oct 2024 00:13:37 +0000 Subject: core: reverse constructor props order to make stack shown property behave as expected --- core/lua/astal/widget.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core/lua/astal/widget.lua') diff --git a/core/lua/astal/widget.lua b/core/lua/astal/widget.lua index 73fe3b6..8f49409 100644 --- a/core/lua/astal/widget.lua +++ b/core/lua/astal/widget.lua @@ -206,10 +206,6 @@ local function astalify(ctor) -- construct, attach bindings, add children local widget = ctor() - for prop, value in pairs(props) do - widget[prop] = value - end - if getmetatable(children) == Binding then set_children(widget, children:get()) widget.on_destroy = children:subscribe(function(v) @@ -227,6 +223,10 @@ local function astalify(ctor) end) end + for prop, value in pairs(props) do + widget[prop] = value + end + if type(setup) == "function" then setup(widget) end -- cgit v1.2.3