2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local command = require "core.command"
|
|
|
|
local config = require "core.config"
|
|
|
|
local keymap = require "core.keymap"
|
|
|
|
local style = require "core.style"
|
|
|
|
local View = require "core.view"
|
2021-06-24 20:07:50 +02:00
|
|
|
local ContextMenu = require "core.contextmenu"
|
|
|
|
local RootView = require "core.rootview"
|
2022-03-20 04:10:26 +01:00
|
|
|
local CommandView = require "core.commandview"
|
2021-06-24 20:07:50 +02:00
|
|
|
|
2022-03-18 08:57:14 +01:00
|
|
|
config.plugins.treeview = common.merge({
|
2022-03-18 09:23:32 +01:00
|
|
|
-- Default treeview width
|
2024-02-11 18:51:12 +01:00
|
|
|
size = 200 * SCALE
|
2021-12-24 14:32:28 +01:00
|
|
|
}, config.plugins.treeview)
|
|
|
|
|
2021-06-24 20:07:50 +02:00
|
|
|
local tooltip_offset = style.font:get_height()
|
2021-03-18 16:18:36 +01:00
|
|
|
local tooltip_border = 1
|
2021-03-20 02:03:16 +01:00
|
|
|
local tooltip_delay = 0.5
|
2021-03-18 16:18:36 +01:00
|
|
|
local tooltip_alpha = 255
|
2021-03-20 02:04:07 +01:00
|
|
|
local tooltip_alpha_rate = 1
|
2021-03-18 16:18:36 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local function get_depth(filename)
|
2020-12-28 10:26:27 +01:00
|
|
|
local n = 1
|
2023-11-29 15:55:38 +01:00
|
|
|
for _ in filename:gmatch(PATHSEP) do
|
2019-12-28 12:16:32 +01:00
|
|
|
n = n + 1
|
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
2021-03-18 16:18:36 +01:00
|
|
|
local function replace_alpha(color, alpha)
|
|
|
|
local r, g, b = table.unpack(color)
|
|
|
|
return { r, g, b, alpha }
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-16 10:44:31 +02:00
|
|
|
local TreeView = View:extend()
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function TreeView:new()
|
|
|
|
TreeView.super.new(self)
|
|
|
|
self.scrollable = true
|
|
|
|
self.visible = true
|
2021-02-27 12:13:11 +01:00
|
|
|
self.init_size = true
|
2021-12-24 14:32:28 +01:00
|
|
|
self.target_size = config.plugins.treeview.size
|
2019-12-28 12:16:32 +01:00
|
|
|
self.cache = {}
|
2021-03-18 16:18:36 +01:00
|
|
|
self.tooltip = { x = 0, y = 0, begin = 0, alpha = 0 }
|
2024-02-11 18:51:12 +01:00
|
|
|
self.cursor_pos = { x = 0, y = 0 }
|
2021-11-24 06:16:54 +01:00
|
|
|
|
2021-12-07 21:44:20 +01:00
|
|
|
self.item_icon_width = 0
|
|
|
|
self.item_text_spacing = 0
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2020-12-26 16:48:08 +01:00
|
|
|
|
2021-02-27 12:13:11 +01:00
|
|
|
function TreeView:set_target_size(axis, value)
|
|
|
|
if axis == "x" then
|
|
|
|
self.target_size = value
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
function TreeView:get_cached(dir, item, dirname)
|
2020-12-26 21:22:57 +01:00
|
|
|
local dir_cache = self.cache[dirname]
|
|
|
|
if not dir_cache then
|
|
|
|
dir_cache = {}
|
|
|
|
self.cache[dirname] = dir_cache
|
|
|
|
end
|
2021-01-19 16:39:15 +01:00
|
|
|
-- to discriminate top directories from regular files or subdirectories
|
|
|
|
-- we add ':' at the end of the top directories' filename. it will be
|
|
|
|
-- used only to identify the entry into the cache.
|
|
|
|
local cache_name = item.filename .. (item.topdir and ":" or "")
|
|
|
|
local t = dir_cache[cache_name]
|
2022-10-05 00:26:33 +02:00
|
|
|
if not t or t.type ~= item.type then
|
2019-12-28 12:16:32 +01:00
|
|
|
t = {}
|
2021-01-10 12:56:15 +01:00
|
|
|
local basename = common.basename(item.filename)
|
2020-12-28 10:26:27 +01:00
|
|
|
if item.topdir then
|
2020-12-27 11:32:52 +01:00
|
|
|
t.filename = basename
|
|
|
|
t.expanded = true
|
2020-12-28 10:26:27 +01:00
|
|
|
t.depth = 0
|
2020-12-28 15:51:16 +01:00
|
|
|
t.abs_filename = dirname
|
2020-12-28 10:26:27 +01:00
|
|
|
else
|
|
|
|
t.filename = item.filename
|
|
|
|
t.depth = get_depth(item.filename)
|
2024-02-17 18:47:15 +01:00
|
|
|
t.abs_filename = common.basepath(dirname) .. item.filename
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
2020-12-27 09:47:58 +01:00
|
|
|
t.name = basename
|
2019-12-28 12:16:32 +01:00
|
|
|
t.type = item.type
|
2021-12-28 14:32:39 +01:00
|
|
|
t.dir_name = dir.name -- points to top level "dir" item
|
2021-01-19 16:39:15 +01:00
|
|
|
dir_cache[cache_name] = t
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:get_name()
|
2022-04-27 00:29:05 +02:00
|
|
|
return nil
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:get_item_height()
|
|
|
|
return style.font:get_height() + style.padding.y
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-25 23:25:56 +02:00
|
|
|
function TreeView:invalidate_cache(dirname)
|
|
|
|
for _, v in pairs(self.cache[dirname]) do
|
|
|
|
v.skip = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function TreeView:check_cache()
|
2020-12-26 21:22:57 +01:00
|
|
|
for i = 1, #core.project_directories do
|
|
|
|
local dir = core.project_directories[i]
|
2021-07-12 18:21:27 +02:00
|
|
|
-- invalidate cache's skip values if directory is declared dirty
|
|
|
|
if dir.is_dirty and self.cache[dir.name] then
|
|
|
|
self:invalidate_cache(dir.name)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
dir.is_dirty = false
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:each_item()
|
|
|
|
return coroutine.wrap(function()
|
|
|
|
self:check_cache()
|
2020-12-30 16:15:33 +01:00
|
|
|
local count_lines = 0
|
2019-12-28 12:16:32 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
|
|
|
local y = oy + style.padding.y
|
|
|
|
local w = self.size.x
|
|
|
|
local h = self:get_item_height()
|
|
|
|
|
2020-12-26 21:22:57 +01:00
|
|
|
for k = 1, #core.project_directories do
|
|
|
|
local dir = core.project_directories[k]
|
2021-07-12 18:21:27 +02:00
|
|
|
local dir_cached = self:get_cached(dir, dir.item, dir.name)
|
2020-12-26 21:22:57 +01:00
|
|
|
coroutine.yield(dir_cached, ox, y, w, h)
|
2020-12-30 16:15:33 +01:00
|
|
|
count_lines = count_lines + 1
|
2019-12-28 12:16:32 +01:00
|
|
|
y = y + h
|
2020-12-26 21:22:57 +01:00
|
|
|
local i = 1
|
2022-06-17 21:35:23 +02:00
|
|
|
if dir.files then -- if consumed max sys file descriptors this can be nil
|
|
|
|
while i <= #dir.files and dir_cached.expanded do
|
|
|
|
local item = dir.files[i]
|
|
|
|
local cached = self:get_cached(dir, item, dir.name)
|
|
|
|
|
|
|
|
coroutine.yield(cached, ox, y, w, h)
|
|
|
|
count_lines = count_lines + 1
|
|
|
|
y = y + h
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
if not cached.expanded then
|
|
|
|
if cached.skip then
|
|
|
|
i = cached.skip
|
|
|
|
else
|
|
|
|
local depth = cached.depth
|
|
|
|
while i <= #dir.files do
|
|
|
|
if get_depth(dir.files[i].filename) <= depth then break end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
cached.skip = i
|
2020-12-26 16:48:08 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-06-17 21:35:23 +02:00
|
|
|
end -- while files
|
|
|
|
end
|
2020-12-26 16:48:08 +01:00
|
|
|
end -- for directories
|
2020-12-30 16:15:33 +01:00
|
|
|
self.count_lines = count_lines
|
2019-12-28 12:16:32 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2024-02-11 18:51:12 +01:00
|
|
|
function TreeView:set_selection(selection, selection_y)
|
2021-12-12 09:07:51 +01:00
|
|
|
self.selected_item = selection
|
|
|
|
if selection and selection_y
|
|
|
|
and (selection_y <= 0 or selection_y >= self.size.y) then
|
2024-02-11 18:51:12 +01:00
|
|
|
|
2021-12-12 09:07:51 +01:00
|
|
|
local lh = self:get_item_height()
|
2024-02-11 18:51:12 +01:00
|
|
|
if selection_y >= self.size.y - lh then
|
2021-12-12 09:07:51 +01:00
|
|
|
selection_y = selection_y - self.size.y + lh
|
|
|
|
end
|
|
|
|
local _, y = self:get_content_offset()
|
2024-02-11 18:51:12 +01:00
|
|
|
self.scroll.to.y = selection and (selection_y - y)
|
2021-12-12 09:07:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-20 01:57:06 +01:00
|
|
|
function TreeView:get_text_bounding_box(item, x, y, w, h)
|
|
|
|
local icon_width = style.icon_font:get_width("D")
|
|
|
|
local xoffset = item.depth * style.padding.x + style.padding.x + icon_width
|
|
|
|
x = x + xoffset
|
|
|
|
w = style.font:get_width(item.name) + 2 * style.padding.x
|
|
|
|
return x, y, w, h
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 16:15:33 +01:00
|
|
|
function TreeView:on_mouse_moved(px, py, ...)
|
2022-03-07 21:52:32 +01:00
|
|
|
if not self.visible then return end
|
2024-02-11 18:51:12 +01:00
|
|
|
self.cursor_pos.x = px
|
|
|
|
self.cursor_pos.y = py
|
2022-10-16 02:12:15 +02:00
|
|
|
if TreeView.super.on_mouse_moved(self, px, py, ...) then
|
|
|
|
-- mouse movement handled by the View (scrollbar)
|
2022-03-20 04:46:57 +01:00
|
|
|
self.hovered_item = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-03-20 01:57:06 +01:00
|
|
|
local item_changed, tooltip_changed
|
2019-12-28 12:16:32 +01:00
|
|
|
for item, x,y,w,h in self:each_item() do
|
|
|
|
if px > x and py > y and px <= x + w and py <= y + h then
|
2021-03-20 01:57:06 +01:00
|
|
|
item_changed = true
|
2019-12-28 12:16:32 +01:00
|
|
|
self.hovered_item = item
|
2021-06-24 20:07:50 +02:00
|
|
|
|
2021-03-20 01:57:06 +01:00
|
|
|
x,y,w,h = self:get_text_bounding_box(item, x,y,w,h)
|
|
|
|
if px > x and py > y and px <= x + w and py <= y + h then
|
|
|
|
tooltip_changed = true
|
|
|
|
self.tooltip.x, self.tooltip.y = px, py
|
|
|
|
self.tooltip.begin = system.get_time()
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2021-03-20 01:57:06 +01:00
|
|
|
if not item_changed then self.hovered_item = nil end
|
|
|
|
if not tooltip_changed then self.tooltip.x, self.tooltip.y = nil, nil end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:update()
|
|
|
|
-- update width
|
2021-02-27 12:13:11 +01:00
|
|
|
local dest = self.visible and self.target_size or 0
|
2020-05-07 15:03:37 +02:00
|
|
|
if self.init_size then
|
2021-02-27 12:13:11 +01:00
|
|
|
self.size.x = dest
|
|
|
|
self.init_size = false
|
|
|
|
else
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self.size, "x", dest, nil, "treeview")
|
2020-05-07 15:03:37 +02:00
|
|
|
end
|
2021-06-24 20:07:50 +02:00
|
|
|
|
2024-02-11 18:51:12 +01:00
|
|
|
if not self.visible then return end
|
2022-03-07 21:52:32 +01:00
|
|
|
|
2021-03-18 16:18:36 +01:00
|
|
|
local duration = system.get_time() - self.tooltip.begin
|
2021-03-20 01:57:06 +01:00
|
|
|
if self.hovered_item and self.tooltip.x and duration > tooltip_delay then
|
2022-04-26 02:35:35 +02:00
|
|
|
self:move_towards(self.tooltip, "alpha", tooltip_alpha, tooltip_alpha_rate, "treeview")
|
2021-03-18 16:18:36 +01:00
|
|
|
else
|
|
|
|
self.tooltip.alpha = 0
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-12-07 21:44:20 +01:00
|
|
|
self.item_icon_width = style.icon_font:get_width("D")
|
|
|
|
self.item_text_spacing = style.icon_font:get_width("f") / 2
|
|
|
|
|
2021-12-12 09:07:51 +01:00
|
|
|
-- this will make sure hovered_item is updated
|
2024-02-11 18:51:12 +01:00
|
|
|
-- we don't want events when the thing is scrolling fast
|
|
|
|
local dy = math.abs(self.scroll.to.y - self.scroll.y)
|
|
|
|
if self.scroll.to.y ~= 0 and dy < self:get_item_height() then
|
|
|
|
self:on_mouse_moved(self.cursor_pos.x, self.cursor_pos.y, 0, 0)
|
2023-03-16 22:29:33 +01:00
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
TreeView.super.update(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 16:15:33 +01:00
|
|
|
function TreeView:get_scrollable_size()
|
|
|
|
return self.count_lines and self:get_item_height() * (self.count_lines + 1) or math.huge
|
|
|
|
end
|
|
|
|
|
2021-03-13 16:44:40 +01:00
|
|
|
|
|
|
|
function TreeView:draw_tooltip()
|
2021-03-14 04:09:18 +01:00
|
|
|
local text = common.home_encode(self.hovered_item.abs_filename)
|
2021-03-13 16:44:40 +01:00
|
|
|
local w, h = style.font:get_width(text), style.font:get_height(text)
|
|
|
|
|
2021-03-18 16:18:36 +01:00
|
|
|
local x, y = self.tooltip.x + tooltip_offset, self.tooltip.y + tooltip_offset
|
2021-03-13 16:44:40 +01:00
|
|
|
w, h = w + style.padding.x, h + style.padding.y
|
|
|
|
|
|
|
|
if x + w > core.root_view.root_node.size.x then -- check if we can span right
|
|
|
|
x = x - w -- span left instead
|
|
|
|
end
|
|
|
|
|
2021-03-18 16:18:36 +01:00
|
|
|
local bx, by = x - tooltip_border, y - tooltip_border
|
|
|
|
local bw, bh = w + 2 * tooltip_border, h + 2 * tooltip_border
|
|
|
|
renderer.draw_rect(bx, by, bw, bh, replace_alpha(style.text, self.tooltip.alpha))
|
|
|
|
renderer.draw_rect(x, y, w, h, replace_alpha(style.background2, self.tooltip.alpha))
|
|
|
|
common.draw_text(style.font, replace_alpha(style.text, self.tooltip.alpha), text, "center", x, y, w, h)
|
2021-03-13 16:44:40 +01:00
|
|
|
end
|
2020-12-30 16:15:33 +01:00
|
|
|
|
2021-03-18 16:18:36 +01:00
|
|
|
|
2021-11-29 23:26:03 +01:00
|
|
|
function TreeView:get_item_icon(item, active, hovered)
|
|
|
|
local character = "f"
|
|
|
|
if item.type == "dir" then
|
|
|
|
character = item.expanded and "D" or "d"
|
|
|
|
end
|
|
|
|
local font = style.icon_font
|
|
|
|
local color = style.text
|
|
|
|
if active or hovered then
|
|
|
|
color = style.accent
|
|
|
|
end
|
|
|
|
return character, font, color
|
|
|
|
end
|
|
|
|
|
|
|
|
function TreeView:get_item_text(item, active, hovered)
|
|
|
|
local text = item.name
|
|
|
|
local font = style.font
|
|
|
|
local color = style.text
|
|
|
|
if active or hovered then
|
|
|
|
color = style.accent
|
|
|
|
end
|
|
|
|
return text, font, color
|
2021-11-21 09:24:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-12-07 21:44:20 +01:00
|
|
|
function TreeView:draw_item_text(item, active, hovered, x, y, w, h)
|
|
|
|
local item_text, item_font, item_color = self:get_item_text(item, active, hovered)
|
|
|
|
common.draw_text(item_font, item_color, item_text, nil, x, y, 0, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:draw_item_icon(item, active, hovered, x, y, w, h)
|
|
|
|
local icon_char, icon_font, icon_color = self:get_item_icon(item, active, hovered)
|
|
|
|
common.draw_text(icon_font, icon_color, icon_char, nil, x, y, 0, h)
|
|
|
|
return self.item_icon_width + self.item_text_spacing
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:draw_item_body(item, active, hovered, x, y, w, h)
|
|
|
|
x = x + self:draw_item_icon(item, active, hovered, x, y, w, h)
|
|
|
|
self:draw_item_text(item, active, hovered, x, y, w, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:draw_item_chevron(item, active, hovered, x, y, w, h)
|
|
|
|
if item.type == "dir" then
|
|
|
|
local chevron_icon = item.expanded and "-" or "+"
|
|
|
|
local chevron_color = hovered and style.accent or style.text
|
|
|
|
common.draw_text(style.icon_font, chevron_color, chevron_icon, nil, x, y, 0, h)
|
|
|
|
end
|
|
|
|
return style.padding.x
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:draw_item_background(item, active, hovered, x, y, w, h)
|
|
|
|
if hovered then
|
2021-12-12 09:07:51 +01:00
|
|
|
local hover_color = { table.unpack(style.line_highlight) }
|
|
|
|
hover_color[4] = 160
|
|
|
|
renderer.draw_rect(x, y, w, h, hover_color)
|
|
|
|
elseif active then
|
2021-12-07 21:44:20 +01:00
|
|
|
renderer.draw_rect(x, y, w, h, style.line_highlight)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function TreeView:draw_item(item, active, hovered, x, y, w, h)
|
|
|
|
self:draw_item_background(item, active, hovered, x, y, w, h)
|
|
|
|
|
|
|
|
x = x + item.depth * style.padding.x + style.padding.x
|
|
|
|
x = x + self:draw_item_chevron(item, active, hovered, x, y, w, h)
|
|
|
|
|
|
|
|
self:draw_item_body(item, active, hovered, x, y, w, h)
|
2021-11-21 09:24:24 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function TreeView:draw()
|
2022-03-07 21:52:32 +01:00
|
|
|
if not self.visible then return end
|
2019-12-28 12:16:32 +01:00
|
|
|
self:draw_background(style.background2)
|
2021-12-07 21:45:20 +01:00
|
|
|
local _y, _h = self.position.y, self.size.y
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
local doc = core.active_view.doc
|
|
|
|
local active_filename = doc and system.absolute_path(doc.filename or "")
|
|
|
|
|
|
|
|
for item, x,y,w,h in self:each_item() do
|
2021-12-07 21:45:20 +01:00
|
|
|
if y + h >= _y and y < _y + _h then
|
|
|
|
self:draw_item(item,
|
2021-12-12 09:07:51 +01:00
|
|
|
item == self.selected_item,
|
2021-12-07 21:45:20 +01:00
|
|
|
item == self.hovered_item,
|
|
|
|
x, y, w, h)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-30 16:15:33 +01:00
|
|
|
|
|
|
|
self:draw_scrollbar()
|
2022-03-18 16:10:24 +01:00
|
|
|
if self.hovered_item and self.tooltip.x and self.tooltip.alpha > 0 then
|
2021-03-18 16:18:36 +01:00
|
|
|
core.root_view:defer_draw(self.draw_tooltip, self)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-06-01 03:55:55 +02:00
|
|
|
|
2022-03-18 16:15:53 +01:00
|
|
|
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
|
|
|
|
|
2022-06-01 03:55:55 +02:00
|
|
|
|
|
|
|
function TreeView:get_item(item, where)
|
|
|
|
local last_item, last_x, last_y, last_w, last_h
|
|
|
|
local stop = false
|
2022-06-01 03:57:14 +02:00
|
|
|
|
2022-06-01 03:55:55 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-02-11 18:51:12 +01:00
|
|
|
function TreeView:toggle_expand(toggle)
|
|
|
|
local item = self.selected_item
|
2022-03-18 08:57:14 +01:00
|
|
|
|
|
|
|
if not item then return end
|
|
|
|
|
|
|
|
if item.type == "dir" then
|
|
|
|
if type(toggle) == "boolean" then
|
|
|
|
item.expanded = toggle
|
|
|
|
else
|
|
|
|
item.expanded = not item.expanded
|
|
|
|
end
|
|
|
|
local hovered_dir = core.project_dir_by_name(item.dir_name)
|
|
|
|
if hovered_dir and hovered_dir.files_limit then
|
2022-05-04 05:13:49 +02:00
|
|
|
core.update_project_subdir(hovered_dir, item.depth == 0 and "" or item.filename, item.expanded)
|
2022-03-18 08:57:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
-- init
|
|
|
|
local view = TreeView()
|
|
|
|
local node = core.root_view:get_active_node()
|
2022-08-09 20:53:30 +02:00
|
|
|
view.node = node:split("left", view, {x = true}, true)
|
2021-02-15 00:36:39 +01:00
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
-- The toolbarview plugin is special because it is plugged inside
|
|
|
|
-- a treeview pane which is itelf provided in a plugin.
|
|
|
|
-- We therefore break the usual plugin's logic that would require each
|
|
|
|
-- plugin to be independent of each other. In addition it is not the
|
|
|
|
-- plugin module that plug itself in the active node but it is plugged here
|
|
|
|
-- in the treeview node.
|
2021-06-24 20:07:50 +02:00
|
|
|
local toolbar_view = nil
|
2022-09-14 06:14:13 +02:00
|
|
|
local toolbar_plugin, ToolbarView = pcall(require, "plugins.toolbarview")
|
2021-07-13 16:14:00 +02:00
|
|
|
if config.plugins.toolbarview ~= false and toolbar_plugin then
|
2021-06-24 20:07:50 +02:00
|
|
|
toolbar_view = ToolbarView()
|
2022-08-09 20:53:30 +02:00
|
|
|
view.node:split("down", toolbar_view, {y = true})
|
2021-02-27 19:39:56 +01:00
|
|
|
local min_toolbar_width = toolbar_view:get_min_width()
|
2021-12-24 14:32:28 +01:00
|
|
|
view:set_target_size("x", math.max(config.plugins.treeview.size, min_toolbar_width))
|
2021-02-15 15:45:56 +01:00
|
|
|
command.add(nil, {
|
|
|
|
["toolbar:toggle"] = function()
|
|
|
|
toolbar_view:toggle_visible()
|
|
|
|
end,
|
|
|
|
})
|
2021-02-15 00:36:39 +01:00
|
|
|
end
|
2021-02-09 18:41:32 +01:00
|
|
|
|
2021-06-24 20:07:50 +02:00
|
|
|
-- Add a context menu to the treeview
|
|
|
|
local menu = ContextMenu()
|
|
|
|
|
|
|
|
local on_view_mouse_pressed = RootView.on_view_mouse_pressed
|
|
|
|
local on_mouse_moved = RootView.on_mouse_moved
|
|
|
|
local root_view_update = RootView.update
|
|
|
|
local root_view_draw = RootView.draw
|
|
|
|
|
|
|
|
function RootView:on_mouse_moved(...)
|
|
|
|
if menu:on_mouse_moved(...) then return end
|
|
|
|
on_mouse_moved(self, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
function RootView.on_view_mouse_pressed(button, x, y, clicks)
|
|
|
|
-- We give the priority to the menu to process mouse pressed events.
|
|
|
|
if button == "right" then
|
|
|
|
view.tooltip.alpha = 0
|
|
|
|
view.tooltip.x, view.tooltip.y = nil, nil
|
|
|
|
end
|
|
|
|
local handled = menu:on_mouse_pressed(button, x, y, clicks)
|
|
|
|
return handled or on_view_mouse_pressed(button, x, y, clicks)
|
|
|
|
end
|
|
|
|
|
|
|
|
function RootView:update(...)
|
|
|
|
root_view_update(self, ...)
|
|
|
|
menu:update()
|
|
|
|
end
|
|
|
|
|
|
|
|
function RootView:draw(...)
|
|
|
|
root_view_draw(self, ...)
|
|
|
|
menu:draw()
|
|
|
|
end
|
|
|
|
|
2022-01-07 10:40:26 +01:00
|
|
|
local on_quit_project = core.on_quit_project
|
|
|
|
function core.on_quit_project()
|
|
|
|
view.cache = {}
|
|
|
|
on_quit_project()
|
|
|
|
end
|
|
|
|
|
2021-06-24 20:07:50 +02:00
|
|
|
local function is_project_folder(path)
|
2022-06-01 03:57:14 +02:00
|
|
|
for _,dir in pairs(core.project_directories) do
|
|
|
|
if dir.name == path then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_primary_project_folder(path)
|
2021-11-28 07:16:53 +01:00
|
|
|
return core.project_dir == path
|
2021-06-24 20:07:50 +02:00
|
|
|
end
|
|
|
|
|
2023-01-30 18:36:17 +01:00
|
|
|
|
|
|
|
local function treeitem() return view.hovered_item or view.selected_item end
|
|
|
|
|
|
|
|
|
|
|
|
menu:register(function() return core.active_view:is(TreeView) and treeitem() end, {
|
2021-06-24 20:07:50 +02:00
|
|
|
{ text = "Open in System", command = "treeview:open-in-system" },
|
|
|
|
ContextMenu.DIVIDER
|
|
|
|
})
|
|
|
|
|
|
|
|
menu:register(
|
|
|
|
function()
|
2023-01-30 18:36:17 +01:00
|
|
|
local item = treeitem()
|
|
|
|
return core.active_view:is(TreeView) and item and not is_project_folder(item.abs_filename)
|
2021-06-24 20:07:50 +02:00
|
|
|
end,
|
|
|
|
{
|
|
|
|
{ text = "Rename", command = "treeview:rename" },
|
|
|
|
{ text = "Delete", command = "treeview:delete" },
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
menu:register(
|
|
|
|
function()
|
2023-01-30 18:36:17 +01:00
|
|
|
local item = treeitem()
|
|
|
|
return core.active_view:is(TreeView) and item and item.type == "dir"
|
2021-06-24 20:07:50 +02:00
|
|
|
end,
|
|
|
|
{
|
|
|
|
{ text = "New File", command = "treeview:new-file" },
|
|
|
|
{ text = "New Folder", command = "treeview:new-folder" },
|
|
|
|
}
|
|
|
|
)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-06-01 03:57:14 +02:00
|
|
|
menu:register(
|
|
|
|
function()
|
2023-01-30 18:36:17 +01:00
|
|
|
local item = treeitem()
|
|
|
|
return core.active_view:is(TreeView) and item
|
|
|
|
and not is_primary_project_folder(item.abs_filename)
|
|
|
|
and is_project_folder(item.abs_filename)
|
2022-06-01 03:57:14 +02:00
|
|
|
end,
|
|
|
|
{
|
|
|
|
{ text = "Remove directory", command = "treeview:remove-project-directory" },
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-20 04:10:26 +01:00
|
|
|
local previous_view = nil
|
|
|
|
|
2021-06-24 20:07:50 +02:00
|
|
|
-- Register the TreeView commands and keymap
|
2019-12-28 12:16:32 +01:00
|
|
|
command.add(nil, {
|
|
|
|
["treeview:toggle"] = function()
|
2021-02-27 12:13:11 +01:00
|
|
|
view.visible = not view.visible
|
2022-03-18 08:57:14 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["treeview:toggle-focus"] = function()
|
|
|
|
if not core.active_view:is(TreeView) then
|
2022-03-20 04:10:26 +01:00
|
|
|
if core.active_view:is(CommandView) then
|
|
|
|
previous_view = core.last_active_view
|
|
|
|
else
|
|
|
|
previous_view = core.active_view
|
|
|
|
end
|
|
|
|
if not previous_view then
|
2022-03-20 06:05:07 +01:00
|
|
|
previous_view = core.root_view:get_primary_node().active_view
|
2022-03-20 04:10:26 +01:00
|
|
|
end
|
2022-03-18 08:57:14 +01:00
|
|
|
core.set_active_view(view)
|
|
|
|
if not view.selected_item then
|
|
|
|
for it, _, y in view:each_item() do
|
|
|
|
view:set_selection(it, y)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
2022-03-20 06:05:07 +01:00
|
|
|
core.set_active_view(
|
|
|
|
previous_view or core.root_view:get_primary_node().active_view
|
|
|
|
)
|
2022-03-18 08:57:14 +01:00
|
|
|
end
|
2021-12-12 09:07:51 +01:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2023-01-30 18:36:17 +01:00
|
|
|
command.add(
|
|
|
|
function()
|
|
|
|
return not menu.show_context_menu and core.active_view:extends(TreeView), TreeView
|
|
|
|
end, {
|
2021-12-12 09:07:51 +01:00
|
|
|
["treeview:next"] = function()
|
2022-06-01 03:55:55 +02:00
|
|
|
local item, _, item_y = view:get_next(view.selected_item)
|
2021-12-12 09:07:51 +01:00
|
|
|
view:set_selection(item, item_y)
|
|
|
|
end,
|
|
|
|
|
|
|
|
["treeview:previous"] = function()
|
2022-06-01 03:55:55 +02:00
|
|
|
local item, _, item_y = view:get_previous(view.selected_item)
|
|
|
|
view:set_selection(item, item_y)
|
2021-12-12 09:07:51 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["treeview:open"] = function()
|
|
|
|
local item = view.selected_item
|
|
|
|
if not item then return end
|
|
|
|
if item.type == "dir" then
|
2022-03-18 08:57:14 +01:00
|
|
|
view:toggle_expand()
|
2021-12-12 09:07:51 +01:00
|
|
|
else
|
|
|
|
core.try(function()
|
|
|
|
if core.last_active_view and core.active_view == view then
|
|
|
|
core.set_active_view(core.last_active_view)
|
|
|
|
end
|
2024-02-11 18:51:12 +01:00
|
|
|
local doc_filename = core.normalize_to_project_dir(item.abs_filename)
|
|
|
|
core.root_view:open_doc(core.open_doc(doc_filename))
|
2021-12-12 09:07:51 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
|
|
|
|
["treeview:deselect"] = function()
|
|
|
|
view.selected_item = nil
|
2022-03-18 08:57:14 +01:00
|
|
|
end,
|
2022-05-23 21:12:37 +02:00
|
|
|
|
2022-04-27 00:29:05 +02:00
|
|
|
["treeview:select"] = function()
|
|
|
|
view:set_selection(view.hovered_item)
|
|
|
|
end,
|
2022-05-23 21:12:37 +02:00
|
|
|
|
2022-04-27 00:29:05 +02:00
|
|
|
["treeview:select-and-open"] = function()
|
2022-05-23 21:12:37 +02:00
|
|
|
if view.hovered_item then
|
2022-04-27 00:29:05 +02:00
|
|
|
view:set_selection(view.hovered_item)
|
|
|
|
command.perform "treeview:open"
|
|
|
|
end
|
|
|
|
end,
|
2022-03-18 08:57:14 +01:00
|
|
|
|
|
|
|
["treeview:collapse"] = function()
|
2022-03-18 16:15:53 +01:00
|
|
|
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
|
2022-03-18 08:57:14 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["treeview:expand"] = function()
|
2022-06-01 03:57:14 +02:00
|
|
|
local item = view.selected_item
|
|
|
|
if not item or item.type ~= "dir" then return end
|
|
|
|
|
|
|
|
if item.expanded then
|
|
|
|
local next_item, _, next_y = view:get_next(item)
|
|
|
|
if next_item.depth > item.depth then
|
|
|
|
view:set_selection(next_item, next_y)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
view:toggle_expand(true)
|
|
|
|
end
|
2022-03-18 08:57:14 +01:00
|
|
|
end,
|
2023-01-30 18:36:17 +01:00
|
|
|
})
|
2022-03-20 06:58:39 +01:00
|
|
|
|
2022-03-20 03:33:41 +01:00
|
|
|
|
2022-03-20 06:58:39 +01:00
|
|
|
command.add(
|
|
|
|
function()
|
2022-08-14 01:56:58 +02:00
|
|
|
local item = treeitem()
|
2023-01-30 18:36:17 +01:00
|
|
|
return item ~= nil and (core.active_view == view or menu.show_context_menu), item
|
2022-03-20 06:58:39 +01:00
|
|
|
end, {
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:delete"] = function(item)
|
2022-06-01 03:57:14 +02:00
|
|
|
local filename = item.abs_filename
|
|
|
|
local relfilename = item.filename
|
|
|
|
if item.dir_name ~= core.project_dir then
|
|
|
|
-- add secondary project dirs names to the file path to show
|
2024-02-17 18:47:15 +01:00
|
|
|
relfilename = common.basepath(common.basename(item.dir_name)) .. PATHSEP .. relfilename
|
2022-06-01 03:57:14 +02:00
|
|
|
end
|
2022-03-20 03:33:41 +01:00
|
|
|
local file_info = system.get_file_info(filename)
|
|
|
|
local file_type = file_info.type == "dir" and "Directory" or "File"
|
|
|
|
-- Ask before deleting
|
|
|
|
local opt = {
|
2023-02-06 18:38:00 +01:00
|
|
|
{ text = "Yes", default_yes = true },
|
|
|
|
{ text = "No", default_no = true }
|
2022-03-20 03:33:41 +01:00
|
|
|
}
|
|
|
|
core.nag_view:show(
|
|
|
|
string.format("Delete %s", file_type),
|
|
|
|
string.format(
|
|
|
|
"Are you sure you want to delete the %s?\n%s: %s",
|
|
|
|
file_type:lower(), file_type, relfilename
|
|
|
|
),
|
|
|
|
opt,
|
|
|
|
function(item)
|
|
|
|
if item.text == "Yes" then
|
|
|
|
if file_info.type == "dir" then
|
|
|
|
local deleted, error, path = common.rm(filename, true)
|
|
|
|
if not deleted then
|
|
|
|
core.error("Error: %s - \"%s\" ", error, path)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local removed, error = os.remove(filename)
|
|
|
|
if not removed then
|
|
|
|
core.error("Error: %s - \"%s\"", error, filename)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
core.log("Deleted \"%s\"", filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
2022-12-26 18:41:14 +01:00
|
|
|
end,
|
2021-12-12 09:07:51 +01:00
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:rename"] = function(item)
|
2022-06-01 03:57:14 +02:00
|
|
|
local old_filename = item.filename
|
|
|
|
local old_abs_filename = item.abs_filename
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Rename", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = old_filename,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(filename)
|
2022-06-01 03:57:14 +02:00
|
|
|
local abs_filename = filename
|
|
|
|
if not common.is_absolute_path(filename) then
|
2024-02-17 18:47:15 +01:00
|
|
|
abs_filename = common.basepath(item.dir_name) .. filename
|
2022-06-01 03:57:14 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
local res, err = os.rename(old_abs_filename, abs_filename)
|
|
|
|
if res then -- successfully renamed
|
|
|
|
for _, doc in ipairs(core.docs) do
|
|
|
|
if doc.abs_filename and old_abs_filename == doc.abs_filename then
|
|
|
|
doc:set_filename(filename, abs_filename) -- make doc point to the new filename
|
|
|
|
doc:reset_syntax()
|
|
|
|
break -- only first needed
|
|
|
|
end
|
2021-09-02 19:09:29 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.log("Renamed \"%s\" to \"%s\"", old_filename, filename)
|
|
|
|
else
|
|
|
|
core.error("Error while renaming \"%s\" to \"%s\": %s", old_abs_filename, abs_filename, err)
|
2021-09-02 19:09:29 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
end,
|
2022-06-01 03:57:14 +02:00
|
|
|
suggest = function(text)
|
|
|
|
return common.path_suggest(text, item.dir_name)
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2021-06-24 20:07:50 +02:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:new-file"] = function(item)
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2022-06-01 03:57:14 +02:00
|
|
|
if not is_project_folder(item.abs_filename) then
|
2022-06-08 02:09:48 +02:00
|
|
|
text = item.filename .. PATHSEP
|
2021-06-24 20:07:50 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Filename", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(filename)
|
2024-02-17 18:47:15 +01:00
|
|
|
local doc_filename = common.basepath(item.dir_name) .. filename
|
2022-06-01 03:57:14 +02:00
|
|
|
core.log(doc_filename)
|
2022-05-30 22:06:47 +02:00
|
|
|
local file = io.open(doc_filename, "a+")
|
|
|
|
file:write("")
|
|
|
|
file:close()
|
2024-02-11 18:51:12 +01:00
|
|
|
core.root_view:open_doc(core.open_doc(doc_filename))
|
2022-05-30 22:06:47 +02:00
|
|
|
core.log("Created %s", doc_filename)
|
|
|
|
end,
|
2022-06-01 03:57:14 +02:00
|
|
|
suggest = function(text)
|
|
|
|
return common.path_suggest(text, item.dir_name)
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2021-06-24 20:07:50 +02:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:new-folder"] = function(item)
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2022-06-01 03:57:14 +02:00
|
|
|
if not is_project_folder(item.abs_filename) then
|
2022-06-08 02:09:48 +02:00
|
|
|
text = item.filename .. PATHSEP
|
2021-06-24 20:07:50 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Folder Name", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(filename)
|
2024-02-17 18:47:15 +01:00
|
|
|
local dir_path = common.basepath(item.dir_name) .. filename
|
2022-05-30 22:06:47 +02:00
|
|
|
common.mkdirp(dir_path)
|
|
|
|
core.log("Created %s", dir_path)
|
|
|
|
end,
|
2022-06-01 03:57:14 +02:00
|
|
|
suggest = function(text)
|
|
|
|
return common.path_suggest(text, item.dir_name)
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2021-06-24 20:07:50 +02:00
|
|
|
end,
|
|
|
|
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:open-in-system"] = function(item)
|
2021-06-24 20:07:50 +02:00
|
|
|
if PLATFORM == "Windows" then
|
2022-08-14 01:56:58 +02:00
|
|
|
system.exec(string.format("start \"\" %q", item.abs_filename))
|
2024-02-11 23:11:30 +01:00
|
|
|
elseif string.find(PLATFORM, "Mac") or PLATFORM == "MorphOS" then
|
2022-08-14 01:56:58 +02:00
|
|
|
system.exec(string.format("open %q", item.abs_filename))
|
2022-03-06 06:59:22 +01:00
|
|
|
elseif PLATFORM == "Linux" or string.find(PLATFORM, "BSD") then
|
2022-08-14 01:56:58 +02:00
|
|
|
system.exec(string.format("xdg-open %q", item.abs_filename))
|
2024-02-11 23:11:30 +01:00
|
|
|
elseif PLATFORM == "AmigaOS 4" then
|
|
|
|
system.exec(string.format("WBRUN %q SHOW=all VIEWBY=name", item.abs_filename))
|
2021-06-24 20:07:50 +02:00
|
|
|
end
|
2022-04-27 00:29:05 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
})
|
|
|
|
|
2022-09-14 06:14:13 +02:00
|
|
|
local projectsearch = pcall(require, "plugins.projectsearch")
|
|
|
|
if projectsearch then
|
|
|
|
menu:register(function()
|
2023-01-30 18:36:17 +01:00
|
|
|
local item = treeitem()
|
|
|
|
return item and item.type == "dir"
|
2024-02-11 23:11:30 +01:00
|
|
|
end, {
|
|
|
|
{ text = "Find in directory", command = "treeview:search-in-directory" }
|
2022-09-14 06:14:13 +02:00
|
|
|
})
|
|
|
|
command.add(function()
|
|
|
|
return view.hovered_item and view.hovered_item.type == "dir"
|
|
|
|
end, {
|
|
|
|
["treeview:search-in-directory"] = function(item)
|
|
|
|
command.perform("project-search:find", view.hovered_item.abs_filename)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-06-01 03:57:14 +02:00
|
|
|
command.add(function()
|
|
|
|
local item = treeitem()
|
|
|
|
return item
|
|
|
|
and not is_primary_project_folder(item.abs_filename)
|
2022-08-14 01:56:58 +02:00
|
|
|
and is_project_folder(item.abs_filename), item
|
2022-06-01 03:57:14 +02:00
|
|
|
end, {
|
2022-08-14 01:56:58 +02:00
|
|
|
["treeview:remove-project-directory"] = function(item)
|
|
|
|
core.remove_project_directory(item.dir_name)
|
2022-06-01 03:57:14 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2021-12-12 09:07:51 +01:00
|
|
|
keymap.add {
|
|
|
|
["ctrl+\\"] = "treeview:toggle",
|
|
|
|
["up"] = "treeview:previous",
|
|
|
|
["down"] = "treeview:next",
|
2022-03-18 08:57:14 +01:00
|
|
|
["left"] = "treeview:collapse",
|
|
|
|
["right"] = "treeview:expand",
|
2021-12-12 09:07:51 +01:00
|
|
|
["return"] = "treeview:open",
|
|
|
|
["escape"] = "treeview:deselect",
|
|
|
|
["delete"] = "treeview:delete",
|
2022-04-27 00:29:05 +02:00
|
|
|
["ctrl+return"] = "treeview:new-folder",
|
|
|
|
["lclick"] = "treeview:select-and-open",
|
|
|
|
["mclick"] = "treeview:select",
|
|
|
|
["ctrl+lclick"] = "treeview:new-folder"
|
2021-12-12 09:07:51 +01:00
|
|
|
}
|
2021-06-24 20:07:50 +02:00
|
|
|
|
2022-05-23 21:12:37 +02:00
|
|
|
-- The config specification used by gui generators
|
|
|
|
config.plugins.treeview.config_spec = {
|
|
|
|
name = "Treeview",
|
|
|
|
{
|
|
|
|
label = "Size",
|
|
|
|
description = "Default treeview width.",
|
|
|
|
path = "size",
|
|
|
|
type = "number",
|
2022-06-22 06:42:26 +02:00
|
|
|
default = toolbar_view and math.ceil(toolbar_view:get_min_width() / SCALE)
|
2022-06-22 07:35:10 +02:00
|
|
|
or 200 * SCALE,
|
2022-06-22 06:42:26 +02:00
|
|
|
min = toolbar_view and toolbar_view:get_min_width() / SCALE
|
2022-06-22 07:35:10 +02:00
|
|
|
or 200 * SCALE,
|
2022-05-23 21:12:37 +02:00
|
|
|
get_value = function(value)
|
|
|
|
return value / SCALE
|
|
|
|
end,
|
|
|
|
set_value = function(value)
|
|
|
|
return value * SCALE
|
|
|
|
end,
|
|
|
|
on_apply = function(value)
|
2022-06-22 06:42:26 +02:00
|
|
|
view:set_target_size("x", math.max(
|
2022-06-22 07:35:10 +02:00
|
|
|
value, toolbar_view and toolbar_view:get_min_width() or 200 * SCALE
|
2022-06-22 06:42:26 +02:00
|
|
|
))
|
2022-05-23 21:12:37 +02:00
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label = "Hide on Startup",
|
|
|
|
description = "Show or hide the treeview on startup.",
|
|
|
|
path = "visible",
|
|
|
|
type = "toggle",
|
|
|
|
default = false,
|
|
|
|
on_apply = function(value)
|
|
|
|
view.visible = not value
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 20:07:50 +02:00
|
|
|
-- Return the treeview with toolbar and contextmenu to allow
|
|
|
|
-- user or plugin modifications
|
|
|
|
view.toolbar = toolbar_view
|
|
|
|
view.contextmenu = menu
|
|
|
|
|
|
|
|
return view
|