awful.layout: Support the current tag layouts in `.inc()`.

Also "modernize" the code by leveraging gears.table for more logic.
This commit is contained in:
Emmanuel Lepage Vallee 2018-12-24 23:01:13 -05:00
parent 8757e15d30
commit eb3ca746ca
1 changed files with 28 additions and 24 deletions

View File

@ -112,31 +112,35 @@ function layout.inc(i, s, layouts)
end end
s = get_screen(s or ascreen.focused()) s = get_screen(s or ascreen.focused())
local t = s.selected_tag local t = s.selected_tag
layouts = layouts or layout.layouts
if t then if not t then return end
local curlayout = layout.get(s)
local curindex layouts = layouts or t.layouts or {}
for k, v in ipairs(layouts) do
if v == curlayout or curlayout._type == v then if #layouts == 0 then
curindex = k layouts = layout.layouts
break
end end
end
if not curindex then local cur_l = layout.get(s)
-- First try to match the object
local cur_idx = gtable.find_first_key(
layouts, function(_, v) return v == cur_l or cur_l._type == v end, true
)
-- Safety net: handle cases where another reference of the layout -- Safety net: handle cases where another reference of the layout
-- might be given (e.g. when (accidentally) cloning it). -- might be given (e.g. when (accidentally) cloning it).
for k, v in ipairs(layouts) do cur_idx = cur_idx or gtable.find_first_key(
if v.name == curlayout.name then layouts, function(_, v) return v.name == cur_l.name end, true
curindex = k )
break
end -- Trying to come up with some kind of fallback layouts to iterate would
end -- never produce a result the user expect, so if there is nothing to
end -- iterate over, do not iterate.
if curindex then if not cur_idx then return end
local newindex = gmath.cycle(#layouts, curindex + i)
local newindex = gmath.cycle(#layouts, cur_idx + i)
layout.set(layouts[newindex], t) layout.set(layouts[newindex], t)
end
end
end end
--- Set the layout function of the current tag. --- Set the layout function of the current tag.