diff --git a/layout/grid.lua b/layout/grid.lua index 1c356e2..a3608f8 100644 --- a/layout/grid.lua +++ b/layout/grid.lua @@ -1,80 +1,55 @@ local setmetatable = setmetatable -local ipairs = ipairs -local math = math local wibox = require( "wibox" ) local module = {} local function left(data) - data.next_item.selected = true + data.next_item.selected = true end local function right(data) - data.previous_item.selected = true + data.previous_item.selected = true end local function up(data) - local idx,rc,col = data.current_index,data.rowcount,data.column - idx = idx-col - if idx <= 0 then - idx = rc + idx + 1 - end - data.items[idx].selected = true + local idx,rc,col = data.current_index,data.rowcount,data.column + idx = idx-col + if idx <= 0 then + idx = rc + idx + 1 + end + data.items[idx].selected = true end local function down(data) - local idx,rc,col = data.current_index,data.rowcount,data.column - idx = idx+col - if idx > rc then - idx = idx - rc - 1 - end - data.items[idx].selected = true + local idx,rc,col = data.current_index,data.rowcount,data.column + idx = idx+col + if idx > rc then + idx = idx - rc - 1 + end + data.items[idx].selected = true end -function module:setup_key_hooks(data) - data:add_key_hook({}, "Up" , "press", up ) - data:add_key_hook({}, "&" , "press", up ) - data:add_key_hook({}, "Down" , "press", down ) - data:add_key_hook({}, "KP_Enter", "press", down ) - 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 ) +function module.setup_key_hooks(data) + data:add_key_hook({}, "Up" , "press", up ) + data:add_key_hook({}, "Down" , "press", down ) + data:add_key_hook({}, "Left" , "press", left ) + data:add_key_hook({}, "Right" , "press", right ) end --Get preferred item geometry local function item_fit(data,item,...) - return data.item_height, data.item_height + return data.item_height, data.item_height end local function new(data) - local mode = data.column ~= nil - local rows = {} - local l = wibox.layout.fixed[mode and "horizontal" or "vertical"]() - local constraint = mode and data.column or data.row or 2 - for i=1,constraint do - local l2 = wibox.layout.fixed[mode and "vertical" or "horizontal"]() - 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 + return wibox.layout { + column_count = data.column, + row_count = data.row or (not data.column and 2 or nil), + item_fit = item_fit, + setup_key_hooks = module.setup_key_hooks, + layout = wibox.layout.grid --FIXME this is monkeypatched + } 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; diff --git a/widgets/init.lua b/widgets/init.lua index 536e562..86dbe74 100644 --- a/widgets/init.lua +++ b/widgets/init.lua @@ -1,3 +1,8 @@ +-- Do some evil monkeypatching for upstreamable widgets +local wibox = require("wibox") + +wibox.layout.grid = require("radical.widgets.grid") + return { checkbox = require( "radical.widgets.checkbox" ), scroll = require( "radical.widgets.scroll" ),