Add `TreeView` helper functions to get previous/next item

This commit is contained in:
Guldoman 2022-06-01 03:55:55 +02:00
parent 295e6b7e5a
commit e94c996a26
No known key found for this signature in database
GPG Key ID: EA928C8BDA1A8825
1 changed files with 38 additions and 28 deletions

View File

@ -389,6 +389,7 @@ function TreeView:draw()
end end
end end
function TreeView:get_parent(item) function TreeView:get_parent(item)
local parent_path = common.dirname(item.abs_filename) local parent_path = common.dirname(item.abs_filename)
if not parent_path then return end if not parent_path then return end
@ -399,6 +400,40 @@ function TreeView:get_parent(item)
end end
end end
function TreeView:get_item(item, where)
local last_item, last_x, last_y, last_w, last_h
local stop = false
for it, x, y, w, h in self:each_item() do
if not item and where >= 0 then
return it, x, y, w, h
end
if item == it then
if where < 0 and last_item then
break
elseif where == 0 or (where < 0 and not last_item) then
return it, x, y, w, h
end
stop = true
elseif stop then
item = it
return it, x, y, w, h
end
last_item, last_x, last_y, last_w, last_h = it, x, y, w, h
end
return last_item, last_x, last_y, last_w, last_h
end
function TreeView:get_next(item)
return self:get_item(item, 1)
end
function TreeView:get_previous(item)
return self:get_item(item, -1)
end
function TreeView:toggle_expand(toggle) function TreeView:toggle_expand(toggle)
local item = self.selected_item local item = self.selected_item
@ -548,38 +583,13 @@ command.add(nil, {
command.add(TreeView, { command.add(TreeView, {
["treeview:next"] = function() ["treeview:next"] = function()
local item = view.selected_item local item, _, item_y = view:get_next(view.selected_item)
local item_y
local stop = false
for it, _, y in view:each_item() do
if item == it then
stop = true
elseif stop then
item = it
item_y = y
break
end
end
view:set_selection(item, item_y) view:set_selection(item, item_y)
end, end,
["treeview:previous"] = function() ["treeview:previous"] = function()
local last_item local item, _, item_y = view:get_previous(view.selected_item)
local last_item_y view:set_selection(item, item_y)
for it, _, y in view:each_item() do
if it == view.selected_item then
if not last_item then
last_item = it
last_item_y = y
end
break
end
last_item = it
last_item_y = y
end
view:set_selection(last_item, last_item_y)
end, end,
["treeview:open"] = function() ["treeview:open"] = function()