Merge pull request #633 from Elv13/upstream_dynamic_p4

layout: Support layout with a constructor
This commit is contained in:
Emmanuel Lepage Vallée 2016-01-18 22:47:56 -05:00
commit 7ffb238ae8
1 changed files with 35 additions and 0 deletions

View File

@ -34,6 +34,7 @@ local tag = { mt = {} }
-- Private data -- Private data
local data = {} local data = {}
data.history = {} data.history = {}
data.dynamic_cache = setmetatable({}, { __mode = 'k' })
data.tags = setmetatable({}, { __mode = 'k' }) data.tags = setmetatable({}, { __mode = 'k' })
-- History functions -- History functions
@ -397,6 +398,40 @@ function tag.getmwfact(t)
return tag.getproperty(t, "mwfact") or 0.5 return tag.getproperty(t, "mwfact") or 0.5
end 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 --- Set the spacing between clients
-- @param useless_gap The spacing between clients -- @param useless_gap The spacing between clients
-- @param t The tag to modify, if null tag.selected() is used. -- @param t The tag to modify, if null tag.selected() is used.