From 13ebb51c38bd1ffb70e48d714fc5c826a0df9921 Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Sat, 13 Mar 2021 15:44:40 +0000 Subject: [PATCH] add tooltips to treeview.lua (#100) --- data/plugins/treeview.lua | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 82b6790e..e602b38e 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -7,6 +7,8 @@ local style = require "core.style" local View = require "core.view" local default_treeview_size = 200 * SCALE +local tooltip_offset = 8 * SCALE +local border_width = 1 local function get_depth(filename) local n = 1 @@ -27,6 +29,7 @@ function TreeView:new() self.target_size = default_treeview_size self.cache = {} self.last = {} + self.mouse = { x = 0, y = 0 } end @@ -149,6 +152,10 @@ function TreeView:on_mouse_moved(px, py, ...) self.hovered_item = nil 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 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 break 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 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() self:draw_background(style.background2) @@ -255,6 +294,7 @@ function TreeView:draw() end self:draw_scrollbar() + core.root_view:defer_draw(self.draw_tooltip, self) end