grid: Use the 'real' grid layout

This commit is contained in:
Emmanuel Lepage Vallee 2016-07-04 02:50:15 -04:00
parent 30084a84d5
commit 9be99465f2
2 changed files with 33 additions and 53 deletions

View File

@ -1,80 +1,55 @@
local setmetatable = setmetatable local setmetatable = setmetatable
local ipairs = ipairs
local math = math
local wibox = require( "wibox" ) local wibox = require( "wibox" )
local module = {} local module = {}
local function left(data) local function left(data)
data.next_item.selected = true data.next_item.selected = true
end end
local function right(data) local function right(data)
data.previous_item.selected = true data.previous_item.selected = true
end end
local function up(data) local function up(data)
local idx,rc,col = data.current_index,data.rowcount,data.column local idx,rc,col = data.current_index,data.rowcount,data.column
idx = idx-col idx = idx-col
if idx <= 0 then if idx <= 0 then
idx = rc + idx + 1 idx = rc + idx + 1
end end
data.items[idx].selected = true data.items[idx].selected = true
end end
local function down(data) local function down(data)
local idx,rc,col = data.current_index,data.rowcount,data.column local idx,rc,col = data.current_index,data.rowcount,data.column
idx = idx+col idx = idx+col
if idx > rc then if idx > rc then
idx = idx - rc - 1 idx = idx - rc - 1
end end
data.items[idx].selected = true data.items[idx].selected = true
end end
function module:setup_key_hooks(data) function module.setup_key_hooks(data)
data:add_key_hook({}, "Up" , "press", up ) data:add_key_hook({}, "Up" , "press", up )
data:add_key_hook({}, "&" , "press", up ) data:add_key_hook({}, "Down" , "press", down )
data:add_key_hook({}, "Down" , "press", down ) data:add_key_hook({}, "Left" , "press", left )
data:add_key_hook({}, "KP_Enter", "press", down ) data:add_key_hook({}, "Right" , "press", right )
data:add_key_hook({}, "Left" , "press", left )
data:add_key_hook({}, "\"" , "press", left )
data:add_key_hook({}, "Right" , "press", right )
data:add_key_hook({}, "#" , "press", right )
end end
--Get preferred item geometry --Get preferred item geometry
local function item_fit(data,item,...) local function item_fit(data,item,...)
return data.item_height, data.item_height return data.item_height, data.item_height
end end
local function new(data) local function new(data)
local mode = data.column ~= nil return wibox.layout {
local rows = {} column_count = data.column,
local l = wibox.layout.fixed[mode and "horizontal" or "vertical"]() row_count = data.row or (not data.column and 2 or nil),
local constraint = mode and data.column or data.row or 2 item_fit = item_fit,
for i=1,constraint do setup_key_hooks = module.setup_key_hooks,
local l2 = wibox.layout.fixed[mode and "vertical" or "horizontal"]() layout = wibox.layout.grid --FIXME this is monkeypatched
l:add(l2) }
rows[#rows+1] = l2
end
l.fit = function(a1,a2,a3)
local r1,r2 = data.item_height*math.ceil(data.rowcount/constraint),data.item_height*constraint
return (mode and r2 or r1),(mode and r1 or r2)
end
l.add = function(_, it)
for k,v in ipairs(rows) do
v:reset()
end
local rc = data.rowcount+1
for i=1,rc do
rows[((i-1)%constraint)+1]:add((rc == i and it.widget or data.items[i].widget))
end
return true
end
--TODO only load the layouts when draw() is called
l.item_fit = item_fit
return l
end end
return setmetatable(module, { __call = function(_, ...) return new(...) end }) return setmetatable(module, { __call = function(_, ...) return new(...) end })
-- kate: space-indent on; indent-width 2; replace-tabs on; -- kate: space-indent on; indent-width 4; replace-tabs on;

View File

@ -1,3 +1,8 @@
-- Do some evil monkeypatching for upstreamable widgets
local wibox = require("wibox")
wibox.layout.grid = require("radical.widgets.grid")
return { return {
checkbox = require( "radical.widgets.checkbox" ), checkbox = require( "radical.widgets.checkbox" ),
scroll = require( "radical.widgets.scroll" ), scroll = require( "radical.widgets.scroll" ),