From cdf5b9b9b8adad6f61cdcec322301cab690c5ac0 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 20 Mar 2021 00:57:06 +0000 Subject: [PATCH] show tooltips only when hovering over text --- data/plugins/treeview.lua | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 6158842f..5d76bd6b 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -155,23 +155,36 @@ function TreeView:each_item() end +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 + + function TreeView:on_mouse_moved(px, py, ...) TreeView.super.on_mouse_moved(self, px, py, ...) if self.dragging_scrollbar then return end - local selected = false + local item_changed, tooltip_changed 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 - selected = true + item_changed = true self.hovered_item = item - self.tooltip.x, self.tooltip.y = px, py - self.tooltip.begin = system.get_time() + + 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 break end end - if not selected then - self.hovered_item = nil - end + if not item_changed then self.hovered_item = nil end + if not tooltip_changed then self.tooltip.x, self.tooltip.y = nil, nil end end @@ -222,7 +235,7 @@ function TreeView:update() end local duration = system.get_time() - self.tooltip.begin - if self.hovered_item and duration > tooltip_delay then + if self.hovered_item and self.tooltip.x and duration > tooltip_delay then self:move_towards(self.tooltip, "alpha", tooltip_alpha, tooltip_alpha_step) else self.tooltip.alpha = 0