Make `treeview:collapse` select parent if current item can't collapse

This commit is contained in:
Guldoman 2022-03-18 16:15:53 +01:00
parent 2d5af22dc9
commit 3ec0f38446
No known key found for this signature in database
GPG Key ID: C08A498EC7F1AFDD
1 changed files with 20 additions and 1 deletions

View File

@ -427,6 +427,16 @@ function TreeView:draw()
end
end
function TreeView:get_parent(item)
local parent_path = common.dirname(item.abs_filename)
if not parent_path then return end
for it, _, y in self:each_item() do
if it.abs_filename == parent_path then
return it, y
end
end
end
function TreeView:toggle_expand(toggle)
local item = self.selected_item
@ -619,7 +629,16 @@ command.add(TreeView, {
end,
["treeview:collapse"] = function()
view:toggle_expand(false)
if view.selected_item then
if view.selected_item.type == "dir" and view.selected_item.expanded then
view:toggle_expand(false)
else
local parent_item, y = view:get_parent(view.selected_item)
if parent_item then
view:set_selection(parent_item, y)
end
end
end
end,
["treeview:expand"] = function()