diff options
author | Aylur <[email protected]> | 2024-10-09 03:08:51 +0000 |
---|---|---|
committer | Aylur <[email protected]> | 2024-10-09 03:08:51 +0000 |
commit | d6cbbdfb3f6e81c8265af36c88fd9547d93f9cbd (patch) | |
tree | a4f018b52cde3e4b10967f22d05861c4d4b9c73d /core/lua/astal | |
parent | 2fa83eb09047e603835c462ff0d8a04eba016807 (diff) |
core: implement generic no_implicit_destroy prop
Diffstat (limited to 'core/lua/astal')
-rw-r--r-- | core/lua/astal/widget.lua | 42 |
1 files changed, 35 insertions, 7 deletions
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, |