Skip to content

Commit b5536fb

Browse files
committed
Small Tree changes to selection color
1 parent a774e11 commit b5536fb

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/elements/Tree.lua

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local VisualElement = require("elements/VisualElement")
22
local sub = string.sub
3+
local tHex = require("libraries/colorHex")
34
---@cofnigDescription The tree element provides a hierarchical view of nodes that can be expanded and collapsed, with support for selection and scrolling.
45

56

@@ -24,10 +25,10 @@ Tree.defineProperty(Tree, "expandedNodes", {default = {}, type = "table", canTri
2425
Tree.defineProperty(Tree, "scrollOffset", {default = 0, type = "number", canTriggerRender = true})
2526
---@property horizontalOffset number 0 Current horizontal scroll position
2627
Tree.defineProperty(Tree, "horizontalOffset", {default = 0, type = "number", canTriggerRender = true})
27-
---@property nodeColor color white Color of unselected nodes
28-
Tree.defineProperty(Tree, "nodeColor", {default = colors.white, type = "color"})
29-
---@property selectedColor color lightBlue Background color of selected node
30-
Tree.defineProperty(Tree, "selectedColor", {default = colors.lightBlue, type = "color"})
28+
---@property selectedForegroundColor color white foreground color of selected node
29+
Tree.defineProperty(Tree, "selectedForegroundColor", {default = colors.white, type = "color"})
30+
---@property selectedBackgroundColor color lightBlue background color of selected node
31+
Tree.defineProperty(Tree, "selectedBackgroundColor", {default = colors.lightBlue, type = "color"})
3132

3233
Tree.defineEvent(Tree, "mouse_click")
3334
Tree.defineEvent(Tree, "mouse_scroll")
@@ -197,15 +198,23 @@ function Tree:render()
197198
symbol = expandedNodes[node] and "\31" or "\16"
198199
end
199200

200-
local bg = node == selectedNode and self.get("selectedColor") or self.get("background")
201-
local fullText = indent .. symbol .." " .. (node.text or "Node")
201+
local isSelected = node == selectedNode
202+
local _bg = isSelected and self.get("selectedBackgroundColor") or self.get("background")
203+
local _fg = isSelected and self.get("selectedForegroundColor") or self.get("foreground")
204+
205+
local fullText = indent .. symbol .. " " .. (node.text or "Node")
202206
local text = sub(fullText, horizontalOffset + 1, horizontalOffset + self.get("width"))
207+
local paddedText = text .. string.rep(" ", self.get("width") - #text)
208+
209+
local bg = tHex[_bg]:rep(#paddedText) or tHex[colors.black]:rep(#paddedText)
210+
local fg = tHex[_fg]:rep(#paddedText) or tHex[colors.white]:rep(#paddedText)
203211

204-
self:textFg(1, y, text .. string.rep(" ", self.get("width") - #text), self.get("foreground"))
212+
self:blit(1, y, paddedText, fg, bg)
205213
else
206-
self:textFg(1, y, string.rep(" ", self.get("width")), self.get("foreground"), self.get("background"))
214+
self:blit(1, y, string.rep(" ", self.get("width")), tHex[self.get("foreground")]:rep(self.get("width")), tHex[self.get("background")]:rep(self.get("width")))
207215
end
208216
end
209217
end
210218

219+
211220
return Tree

0 commit comments

Comments
 (0)