summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAylur <[email protected]>2024-10-09 03:08:51 +0000
committerAylur <[email protected]>2024-10-09 03:08:51 +0000
commitd6cbbdfb3f6e81c8265af36c88fd9547d93f9cbd (patch)
treea4f018b52cde3e4b10967f22d05861c4d4b9c73d
parent2fa83eb09047e603835c462ff0d8a04eba016807 (diff)
core: implement generic no_implicit_destroy prop
-rw-r--r--core/gjs/src/astalify.ts8
-rw-r--r--core/lua/astal/widget.lua42
-rw-r--r--core/lua/test.lua13
-rw-r--r--core/src/widget/box.vala11
-rw-r--r--core/src/widget/stack.vala11
5 files changed, 54 insertions, 31 deletions
diff --git a/core/gjs/src/astalify.ts b/core/gjs/src/astalify.ts
index 76c2081..85d7531 100644
--- a/core/gjs/src/astalify.ts
+++ b/core/gjs/src/astalify.ts
@@ -77,14 +77,14 @@ export default function astalify<
const ch = this.get_child()
if (ch)
this.remove(ch)
- // if (ch && !children.includes(ch) && !this.noImplicitDestroy)
- // ch?.destroy()
+ if (ch && !children.includes(ch) && !this.noImplicitDestroy)
+ ch?.destroy()
}
else if (this instanceof Gtk.Container) {
for (const ch of this.get_children()) {
this.remove(ch)
- // if (!children.includes(ch) && !this.noImplicitDestroy)
- // ch?.destroy()
+ if (!children.includes(ch) && !this.noImplicitDestroy)
+ ch?.destroy()
}
}
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,
diff --git a/core/lua/test.lua b/core/lua/test.lua
new file mode 100644
index 0000000..2494403
--- /dev/null
+++ b/core/lua/test.lua
@@ -0,0 +1,13 @@
+local a = require("astal")
+
+a.App:start({
+ instance_name = "Hello",
+ main = function()
+ a.Widget.Window({
+ no_implicit_destroy = true,
+ a.Widget.Button({
+ label = "hello",
+ }),
+ })
+ end,
+})
diff --git a/core/src/widget/box.vala b/core/src/widget/box.vala
index 0008fd4..943c821 100644
--- a/core/src/widget/box.vala
+++ b/core/src/widget/box.vala
@@ -6,11 +6,6 @@ public class Box : Gtk.Box {
set { orientation = value ? Gtk.Orientation.VERTICAL : Gtk.Orientation.HORIZONTAL; }
}
- /**
- * whether to implicity destroy previous children when setting them
- */
- public bool no_implicit_destroy { get; set; default = false; }
-
public List<weak Gtk.Widget> children {
set { _set_children(value); }
owned get { return get_children(); }
@@ -42,11 +37,7 @@ public class Box : Gtk.Box {
private void _set_children(List<weak Gtk.Widget> arr) {
foreach(var child in get_children()) {
- if (!no_implicit_destroy && arr.find(child).length() == 0) {
- child.destroy();
- } else {
- remove(child);
- }
+ remove(child);
}
foreach(var child in arr)
diff --git a/core/src/widget/stack.vala b/core/src/widget/stack.vala
index 00adf7f..02f9959 100644
--- a/core/src/widget/stack.vala
+++ b/core/src/widget/stack.vala
@@ -1,9 +1,4 @@
public class Astal.Stack : Gtk.Stack {
- /**
- * whether to implicity destroy previous children when setting them
- */
- public bool no_implicit_destroy { get; set; default = false; }
-
public string shown {
get { return visible_child_name; }
set { visible_child_name = value; }
@@ -16,11 +11,7 @@ public class Astal.Stack : Gtk.Stack {
private void _set_children(List<weak Gtk.Widget> arr) {
foreach(var child in get_children()) {
- if (!no_implicit_destroy && arr.find(child).length() == 0) {
- child.destroy();
- } else {
- remove(child);
- }
+ remove(child);
}
var i = 0;