From 6b427e73a8ec203c15ca42dc17090da175e0d553 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Sat, 30 Nov 2019 20:11:39 -0500 Subject: [PATCH] tag: Add a request::layouts signal and append/remove layout. --- lib/awful/layout/init.lua | 30 ++++++-- lib/awful/tag.lua | 70 +++++++++++++++++++ objects/tag.c | 9 +++ tests/examples/text/awful/layout/remove.lua | 23 ++++++ .../text/awful/layout/remove.output.txt | 5 ++ 5 files changed, 130 insertions(+), 7 deletions(-) create mode 100644 tests/examples/text/awful/layout/remove.lua create mode 100644 tests/examples/text/awful/layout/remove.output.txt diff --git a/lib/awful/layout/init.lua b/lib/awful/layout/init.lua index 47bbcbf2..c32b3286 100644 --- a/lib/awful/layout/init.lua +++ b/lib/awful/layout/init.lua @@ -259,6 +259,8 @@ end --- Remove a layout from the list of default layouts. -- +-- @DOC_text_awful_layout_remove_EXAMPLE@ +-- -- @staticfct awful.layout.remove_default_layout -- @tparam layout to_remove A valid tag layout. -- @treturn boolean True if the layout was found and removed. @@ -455,13 +457,27 @@ local mt = { end, __newindex = function(_, key, value) if key == "layouts" then - gdebug.print_warning( - "`awful.layout.layouts` was set before `request::default_layouts` could ".. - "be called. Please use `awful.layout.append_default_layouts` to ".. - " avoid this problem" - ) - capi.tag.disconnect_signal("new", init_layouts) - init_layouts = nil + assert(type(value) == "table", "`awful.layout.layouts` needs a table.") + + -- Do not ask for layouts if they were already provided. + if init_layouts then + gdebug.print_warning( + "`awful.layout.layouts` was set before `request::default_layouts` could ".. + "be called. Please use `awful.layout.append_default_layouts` to ".. + " avoid this problem" + ) + + capi.tag.disconnect_signal("new", init_layouts) + init_layouts = nil + elseif #default_layouts > 0 then + gdebug.print_warning( + "`awful.layout.layouts` was set after `request::default_layouts` was ".. + "used to get the layouts. This is probably an accident. Use ".. + "`awful.layout.remove_default_layout` to get rid of this warning." + ) + end + + default_layouts = value else rawset(layout, key, value) end diff --git a/lib/awful/tag.lua b/lib/awful/tag.lua index 0d886704..69123a4a 100644 --- a/lib/awful/tag.lua +++ b/lib/awful/tag.lua @@ -867,6 +867,22 @@ function tag.object.get_layouts(self) local cls = custom_layouts(self) + -- Request some layouts. Maybe a new module was added? + if #cls == 0 and not tag.getproperty(self, "_layouts_requested") then + tag.setproperty(self, "_layouts_requested", true) + local old_count = #cls + self:emit_signal("request::layouts", "awful", {}) + + -- When request::layouts is used, assume it takes precedence over + -- the fallback. + if #cls > old_count then + tag.setproperty(self, "_layouts", gtable.clone(cls, false)) + return tag.getproperty(self, "_layouts") + end + + return tag.object.get_layouts(self) + end + -- Without the clone, the custom_layouts would grow return #cls > 0 and gtable.merge(gtable.clone(cls, false), alayout.layouts) or alayout.layouts @@ -882,6 +898,60 @@ function tag.object.set_layouts(self, layouts) self:emit_signal("property::layouts") end +function tag.object.append_layout(self, layout) + -- If the layouts are manually modified, don't request more. + tag.setproperty(self, "_layouts_requested", true) + + local cls = tag.getproperty(self, "_layouts") + + if not cls then + cls = custom_layouts(self) + end + + table.insert(cls, layout) + self:emit_signal("property::layouts") +end + +function tag.object.append_layouts(self, layouts) + -- If the layouts are manually modified, don't request more. + tag.setproperty(self, "_layouts_requested", true) + + local cls = tag.getproperty(self, "_layouts") + + if not cls then + cls = custom_layouts(self) + end + + for _, l in ipairs(layouts) do + table.insert(cls, l) + end + self:emit_signal("property::layouts") +end + +function tag.object.remove_layout(self, layout) + local cls = tag.getproperty(self, "_layouts") + + if not cls then + cls = custom_layouts(self) + end + + local pos = {} + for k, l in ipairs(cls) do + if l == layout then + table.insert(pos, k) + end + end + + if #pos > 0 then + for i=#pos, 1, -1 do + table.remove(cls, i) + end + self:emit_signal("property::layouts") + end + + return #pos > 0 +end + function tag.object.get_layout(t) local l = tag.getproperty(t, "layout") if l then return l end diff --git a/objects/tag.c b/objects/tag.c index de8e7a8e..e0dbc6ff 100644 --- a/objects/tag.c +++ b/objects/tag.c @@ -220,6 +220,15 @@ * @see awful.layout.remove_default_layout */ +/** This signals is emitted when a tag needs layouts for the first time. + * + * If no handler implement it, it will fallback to the content added by + * `request::default_layouts` + * + * @signal request::layouts + * @tparam string context The context (currently always "awful"). + * @tparam table hints A, currently empty, table with hints. + */ /** When a client gets tagged with this tag. * @signal tagged diff --git a/tests/examples/text/awful/layout/remove.lua b/tests/examples/text/awful/layout/remove.lua new file mode 100644 index 00000000..500f5243 --- /dev/null +++ b/tests/examples/text/awful/layout/remove.lua @@ -0,0 +1,23 @@ +--DOC_GEN_OUTPUT --DOC_HIDE +local awful = { layout = require("awful.layout"), --DOC_HIDE + suit= require("awful.layout.suit")} --DOC_HIDE + +awful.layout.append_default_layouts({ + awful.layout.suit.floating, + awful.layout.suit.tile, + awful.layout.suit.max, +}) + +for _, l in ipairs(awful.layout.layouts) do + print("Before:", l.name) +end + +--DOC_NEWLINE + +awful.layout.remove_default_layout(awful.layout.suit.tile) + +--DOC_NEWLINE + +for _, l in ipairs(awful.layout.layouts) do + print("After:", l.name) +end diff --git a/tests/examples/text/awful/layout/remove.output.txt b/tests/examples/text/awful/layout/remove.output.txt new file mode 100644 index 00000000..40d6558c --- /dev/null +++ b/tests/examples/text/awful/layout/remove.output.txt @@ -0,0 +1,5 @@ +Before: floating +Before: tile +Before: max +After: floating +After: max