From cd0503f5526c969773950586a860e98a6477b7b4 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Wed, 6 Jan 2016 18:22:45 -0500 Subject: [PATCH] layout: Support layout with a constructor This allow the most basic kind of stateful layouts to be created. It is now possible to have layout instances instead of global stateless layout arrange functions. --- lib/awful/tag.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/lib/awful/tag.lua b/lib/awful/tag.lua index 9b8668ec..331da6a1 100644 --- a/lib/awful/tag.lua +++ b/lib/awful/tag.lua @@ -34,6 +34,7 @@ local tag = { mt = {} } -- Private data local data = {} data.history = {} +data.dynamic_cache = setmetatable({}, { __mode = 'k' }) data.tags = setmetatable({}, { __mode = 'k' }) -- History functions @@ -390,6 +391,40 @@ function tag.getmwfact(t) return tag.getproperty(t, "mwfact") or 0.5 end +--- Set layout +-- @param layout a layout table or a constructor function +-- @param t The tag to modify +-- @return The layout +function tag.setlayout(layout, t) + + -- Check if the signature match a stateful layout + if type(layout) == "function" or ( + type(layout) == "table" + and getmetatable(layout) + and getmetatable(layout).__call + ) then + if not data.dynamic_cache[t] then + data.dynamic_cache[t] = {} + end + + local instance = data.dynamic_cache[t][layout] or layout(t) + + -- Always make sure the layout is notified it is enabled + if tag.selected(tag.getscreen(t)) == t and instance.wake_up then + instance:wake_up() + end + + -- Avoid creating the same layout twice, use layout:reset() to reset + if instance.is_dynamic then + data.dynamic_cache[t][layout] = instance + end + end + + tag.setproperty(t, "layout", layout, true) + + return layout +end + --- Set the spacing between clients -- @param useless_gap The spacing between clients -- @param t The tag to modify, if null tag.selected() is used.