Skip to content

Commit

Permalink
list: Support unselectable header rows
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-wolf committed Apr 19, 2024
1 parent b3b10e6 commit a889e9e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
13 changes: 10 additions & 3 deletions rpn/matrixeditor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local bindings = require 'config.bindings'

local t = {}

function t.display_sync(ctrl, init)
function t.display_sync(ctrl, init, setup)
local co = coroutine.running()
local dlg = t.display(ctrl, init)

Expand All @@ -24,6 +24,10 @@ function t.display_sync(ctrl, init)
coroutine.resume(co)
end

if setup then
setup(dlg)
end

assert(co)
coroutine.yield(co)
return res
Expand Down Expand Up @@ -200,17 +204,18 @@ function t.display(ctrl, init)
function dlg.next_cell(direction)
local m, n = data:size()
local row, col = grid:get_selection()
local roff, coff = grid:get_header()

if direction == 'right' then
if col == n and n > 1 and m > 1 then
row = row + 1
col = 1
col = 1 + coff
else
col = col + 1
end
elseif direction == 'down' then
if row == m and m > 1 and n > 1 then
row = 1
row = 1 + roff
col = col + 1
else
row = row + 1
Expand Down Expand Up @@ -391,6 +396,8 @@ function t.display(ctrl, init)
data = matrix.new()
end

dlg.edit = edit
dlg.grid = grid
dlg.grid_resize(math.max(6, data.m),
math.max(6, data.n))
update_selection()
Expand Down
28 changes: 20 additions & 8 deletions views/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -319,33 +319,34 @@ end
---@param col? number|'end'
function ui.list:set_selection(row, col)
local max_row = self:item_len()
local roff, coff = self:get_header()

row = row or self.sel or 1
col = col or self.col or 1
row = math.max(row or self.sel or 1, roff)
col = math.max(col or self.col or 1, coff)
if row == 'end' then
row = max_row
end

if row < 1 then
if row < roff + 1 then
row = max_row
elseif row > max_row then
row = 1
row = roff + 1
end

if col == 'end' then
col = #self.columns
end

if self.columns then
if col < 1 then
if col < coff + 1 then
col = #self.columns
elseif col > #self.columns then
col = 1
col = coff + 1
end
end

self.sel = tonumber(row) or 1
self.col = tonumber(col) or 1
self.sel = tonumber(row) or roff + 1
self.col = tonumber(col) or coff + 1

local cell_frame = self:cell_frame(self.sel, self.col)
if cell_frame then
Expand Down Expand Up @@ -381,6 +382,17 @@ function ui.list:scroll_to_item(row, col)
end
end

function ui.list:set_header(rows, cols)
self.header = { rows or 0, cols or 0 }
end

function ui.list:get_header()
if self.header then
return self.header[1] or 0, self.header[2] or 0
end
return 0, 0
end

-- Events

function ui.list:on_up()
Expand Down

0 comments on commit a889e9e

Please sign in to comment.