add tooltips to treeview.lua (#100)

This commit is contained in:
Takase 2021-03-13 15:44:40 +00:00 committed by GitHub
parent 97f8442195
commit 13ebb51c38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 40 additions and 0 deletions

View File

@ -7,6 +7,8 @@ local style = require "core.style"
local View = require "core.view" local View = require "core.view"
local default_treeview_size = 200 * SCALE local default_treeview_size = 200 * SCALE
local tooltip_offset = 8 * SCALE
local border_width = 1
local function get_depth(filename) local function get_depth(filename)
local n = 1 local n = 1
@ -27,6 +29,7 @@ function TreeView:new()
self.target_size = default_treeview_size self.target_size = default_treeview_size
self.cache = {} self.cache = {}
self.last = {} self.last = {}
self.mouse = { x = 0, y = 0 }
end end
@ -149,6 +152,10 @@ function TreeView:on_mouse_moved(px, py, ...)
self.hovered_item = nil self.hovered_item = nil
for item, x,y,w,h in self:each_item() do 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 if px > x and py > y and px <= x + w and py <= y + h then
if item.abs_filename ~= self.mouse.last then
self.mouse.x, self.mouse.y = px, py
self.mouse.last = item.abs_filename
end
self.hovered_item = item self.hovered_item = item
break break
end end
@ -210,6 +217,38 @@ function TreeView:get_scrollable_size()
return self.count_lines and self:get_item_height() * (self.count_lines + 1) or math.huge return self.count_lines and self:get_item_height() * (self.count_lines + 1) or math.huge
end end
function TreeView:get_item_width(item)
local icon_width = style.icon_font:get_width("D")
local spacing = style.icon_font:get_width("f") / 2
local x = item.depth * style.padding.x + style.padding.x
x = x + style.padding.x
x = x + icon_width
x = x + spacing
x = x + style.font:get_width(item.name)
return x
end
function TreeView:draw_tooltip()
if not self.hovered_item then return end
if self:get_item_width(self.hovered_item) < self.size.x then return end
local text = self.hovered_item.abs_filename
local w, h = style.font:get_width(text), style.font:get_height(text)
local x, y = self.mouse.x + tooltip_offset, self.mouse.y + tooltip_offset
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
local bx, by = x - border_width, y - border_width
local bw, bh = w + 2 * border_width, h + 2 * border_width
renderer.draw_rect(bx, by, bw, bh, style.text)
renderer.draw_rect(x, y, w, h, style.background2)
common.draw_text(style.font, style.text, text, "center", x, y, w, h)
end
function TreeView:draw() function TreeView:draw()
self:draw_background(style.background2) self:draw_background(style.background2)
@ -255,6 +294,7 @@ function TreeView:draw()
end end
self:draw_scrollbar() self:draw_scrollbar()
core.root_view:defer_draw(self.draw_tooltip, self)
end end