From e500368ce4445de7faefd8d4d1df11aa00aebef6 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Mon, 29 Nov 2021 23:26:03 +0100 Subject: [PATCH 1/3] Allow `TreeView` item icon and text styling --- data/plugins/treeview.lua | 58 +++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 2e66083a..5dcb035d 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -288,9 +288,27 @@ function TreeView:draw_tooltip() end -function TreeView:color_for_item(abs_filename) - -- other plugins can override this to customize the color of each icon - return nil +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 end @@ -304,40 +322,32 @@ function TreeView:draw() local active_filename = doc and system.absolute_path(doc.filename or "") for item, x,y,w,h in self:each_item() do - local color = style.text - - -- highlight active_view doc - if item.abs_filename == active_filename then - color = style.accent - end + local icon_char, icon_font, icon_color = + self:get_item_icon(item, item.abs_filename == active_filename, + item == self.hovered_item) + local item_text, item_font, item_color = + self:get_item_text(item, item.abs_filename == active_filename, + item == self.hovered_item) -- hovered item background if item == self.hovered_item then renderer.draw_rect(x, y, w, h, style.line_highlight) - color = style.accent end - -- allow for color overrides - local icon_color = self:color_for_item(item.abs_filename) or color - -- icons x = x + item.depth * style.padding.x + style.padding.x if item.type == "dir" then - local icon1 = item.expanded and "-" or "+" - local icon2 = item.expanded and "D" or "d" - common.draw_text(style.icon_font, color, icon1, nil, x, y, 0, h) - x = x + style.padding.x - common.draw_text(style.icon_font, icon_color, icon2, nil, x, y, 0, h) - x = x + icon_width - else - x = x + style.padding.x - common.draw_text(style.icon_font, icon_color, "f", nil, x, y, 0, h) - x = x + icon_width + local expand_icon = item.expanded and "-" or "+" + local expand_color = item == self.hovered_item and style.accent or style.text + common.draw_text(style.icon_font, expand_color, expand_icon, nil, x, y, 0, h) end + x = x + style.padding.x + common.draw_text(icon_font, icon_color, icon_char, nil, x, y, 0, h) + x = x + icon_width -- text x = x + spacing - x = common.draw_text(style.font, color, item.name, nil, x, y, 0, h) + x = common.draw_text(item_font, item_color, item_text, nil, x, y, 0, h) end self:draw_scrollbar() From fdb29f28cf79fbdb7b5d935e32d565bb92377185 Mon Sep 17 00:00:00 2001 From: Guldoman Date: Tue, 7 Dec 2021 21:44:20 +0100 Subject: [PATCH 2/3] Split `TreeView:draw` into multiple functions This allows plugins to override each aspect of TreeView item drawing. --- data/plugins/treeview.lua | 85 ++++++++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 5dcb035d..18d622e1 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -43,6 +43,9 @@ function TreeView:new() self.cache = {} self.tooltip = { x = 0, y = 0, begin = 0, alpha = 0 } + self.item_icon_width = 0 + self.item_text_spacing = 0 + local on_dirmonitor_modify = core.on_dirmonitor_modify function core.on_dirmonitor_modify(dir, filepath) if self.cache[dir.name] then @@ -260,6 +263,9 @@ function TreeView:update() self.tooltip.alpha = 0 end + self.item_icon_width = style.icon_font:get_width("D") + self.item_text_spacing = style.icon_font:get_width("f") / 2 + TreeView.super.update(self) end @@ -312,42 +318,63 @@ function TreeView:get_item_text(item, active, hovered) end +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 + 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) +end + + function TreeView:draw() self:draw_background(style.background2) - local icon_width = style.icon_font:get_width("D") - local spacing = style.icon_font:get_width("f") / 2 - 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 - local icon_char, icon_font, icon_color = - self:get_item_icon(item, item.abs_filename == active_filename, - item == self.hovered_item) - local item_text, item_font, item_color = - self:get_item_text(item, item.abs_filename == active_filename, - item == self.hovered_item) - - -- hovered item background - if item == self.hovered_item then - renderer.draw_rect(x, y, w, h, style.line_highlight) - end - - -- icons - x = x + item.depth * style.padding.x + style.padding.x - if item.type == "dir" then - local expand_icon = item.expanded and "-" or "+" - local expand_color = item == self.hovered_item and style.accent or style.text - common.draw_text(style.icon_font, expand_color, expand_icon, nil, x, y, 0, h) - end - x = x + style.padding.x - common.draw_text(icon_font, icon_color, icon_char, nil, x, y, 0, h) - x = x + icon_width - - -- text - x = x + spacing - x = common.draw_text(item_font, item_color, item_text, nil, x, y, 0, h) + self:draw_item(item, + item.abs_filename == active_filename, + item == self.hovered_item, + x, y, w, h) end self:draw_scrollbar() From c16d6b3d8d02c35006ed277a8d98c53b2a77d61d Mon Sep 17 00:00:00 2001 From: Guldoman Date: Tue, 7 Dec 2021 21:45:20 +0100 Subject: [PATCH 3/3] Avoid drawing hidden `TreeView` items --- data/plugins/treeview.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 18d622e1..8edee393 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -366,15 +366,18 @@ end function TreeView:draw() self:draw_background(style.background2) + local _y, _h = self.position.y, self.size.y 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 - self:draw_item(item, - item.abs_filename == active_filename, - item == self.hovered_item, - x, y, w, h) + if y + h >= _y and y < _y + _h then + self:draw_item(item, + item.abs_filename == active_filename, + item == self.hovered_item, + x, y, w, h) + end end self:draw_scrollbar()