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.
This commit is contained in:
Emmanuel Lepage Vallee 2016-01-06 18:22:45 -05:00
parent 0d1c98a314
commit cd0503f552
1 changed files with 35 additions and 0 deletions

View File

@ -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.