Renamed scrollbar

This commit is contained in:
Robert Jelic
2025-04-19 09:21:05 +02:00
parent 421c72acb4
commit 0be15d612a

View File

@@ -1,77 +1,77 @@
local VisualElement = require("elements/VisualElement") local VisualElement = require("elements/VisualElement")
local tHex = require("libraries/colorHex") local tHex = require("libraries/colorHex")
---A scrollbar element that can be attached to other elements to control their scroll properties ---A ScrollBar element that can be attached to other elements to control their scroll properties
---@class Scrollbar : VisualElement ---@class ScrollBar : VisualElement
local Scrollbar = setmetatable({}, VisualElement) local ScrollBar = setmetatable({}, VisualElement)
Scrollbar.__index = Scrollbar ScrollBar.__index = ScrollBar
---@property value number 0 Current scroll value ---@property value number 0 Current scroll value
Scrollbar.defineProperty(Scrollbar, "value", {default = 0, type = "number", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "value", {default = 0, type = "number", canTriggerRender = true})
---@property min number 0 Minimum scroll value ---@property min number 0 Minimum scroll value
Scrollbar.defineProperty(Scrollbar, "min", {default = 0, type = "number", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "min", {default = 0, type = "number", canTriggerRender = true})
---@property max number 100 Maximum scroll value ---@property max number 100 Maximum scroll value
Scrollbar.defineProperty(Scrollbar, "max", {default = 100, type = "number", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "max", {default = 100, type = "number", canTriggerRender = true})
---@property step number 1 Step size for scroll operations ---@property step number 1 Step size for scroll operations
Scrollbar.defineProperty(Scrollbar, "step", {default = 10, type = "number"}) ScrollBar.defineProperty(ScrollBar, "step", {default = 10, type = "number"})
---@property dragMultiplier number 1 How fast the scrollbar moves when dragging ---@property dragMultiplier number 1 How fast the ScrollBar moves when dragging
Scrollbar.defineProperty(Scrollbar, "dragMultiplier", {default = 1, type = "number"}) ScrollBar.defineProperty(ScrollBar, "dragMultiplier", {default = 1, type = "number"})
---@property symbol string " " Symbol used for the scrollbar handle ---@property symbol string " " Symbol used for the ScrollBar handle
Scrollbar.defineProperty(Scrollbar, "symbol", {default = " ", type = "string", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "symbol", {default = " ", type = "string", canTriggerRender = true})
---@property backgroundSymbol string "\127" Symbol used for the scrollbar background ---@property backgroundSymbol string "\127" Symbol used for the ScrollBar background
Scrollbar.defineProperty(Scrollbar, "symbolColor", {default = colors.gray, type = "color", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "symbolColor", {default = colors.gray, type = "color", canTriggerRender = true})
---@property symbolBackgroundColor color black Background color of the scrollbar handle ---@property symbolBackgroundColor color black Background color of the ScrollBar handle
Scrollbar.defineProperty(Scrollbar, "symbolBackgroundColor", {default = colors.black, type = "color", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "symbolBackgroundColor", {default = colors.black, type = "color", canTriggerRender = true})
---@property backgroundSymbol string "\127" Symbol used for the scrollbar background ---@property backgroundSymbol string "\127" Symbol used for the ScrollBar background
Scrollbar.defineProperty(Scrollbar, "backgroundSymbol", {default = "\127", type = "string", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "backgroundSymbol", {default = "\127", type = "string", canTriggerRender = true})
---@property attachedElement table? nil The element this scrollbar is attached to ---@property attachedElement table? nil The element this ScrollBar is attached to
Scrollbar.defineProperty(Scrollbar, "attachedElement", {default = nil, type = "table"}) ScrollBar.defineProperty(ScrollBar, "attachedElement", {default = nil, type = "table"})
---@property attachedProperty string? nil The property being controlled ---@property attachedProperty string? nil The property being controlled
Scrollbar.defineProperty(Scrollbar, "attachedProperty", {default = nil, type = "string"}) ScrollBar.defineProperty(ScrollBar, "attachedProperty", {default = nil, type = "string"})
---@property minValue number|function 0 Minimum value or function that returns it ---@property minValue number|function 0 Minimum value or function that returns it
Scrollbar.defineProperty(Scrollbar, "minValue", {default = 0, type = "number"}) ScrollBar.defineProperty(ScrollBar, "minValue", {default = 0, type = "number"})
---@property maxValue number|function 100 Maximum value or function that returns it ---@property maxValue number|function 100 Maximum value or function that returns it
Scrollbar.defineProperty(Scrollbar, "maxValue", {default = 100, type = "number"}) ScrollBar.defineProperty(ScrollBar, "maxValue", {default = 100, type = "number"})
---@property orientation string vertical Orientation of the scrollbar ("vertical" or "horizontal") ---@property orientation string vertical Orientation of the ScrollBar ("vertical" or "horizontal")
Scrollbar.defineProperty(Scrollbar, "orientation", {default = "vertical", type = "string", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "orientation", {default = "vertical", type = "string", canTriggerRender = true})
---@property handleSize number 2 Size of the scrollbar handle in characters ---@property handleSize number 2 Size of the ScrollBar handle in characters
Scrollbar.defineProperty(Scrollbar, "handleSize", {default = 2, type = "number", canTriggerRender = true}) ScrollBar.defineProperty(ScrollBar, "handleSize", {default = 2, type = "number", canTriggerRender = true})
Scrollbar.defineEvent(Scrollbar, "mouse_click") ScrollBar.defineEvent(ScrollBar, "mouse_click")
Scrollbar.defineEvent(Scrollbar, "mouse_release") ScrollBar.defineEvent(ScrollBar, "mouse_release")
Scrollbar.defineEvent(Scrollbar, "mouse_drag") ScrollBar.defineEvent(ScrollBar, "mouse_drag")
Scrollbar.defineEvent(Scrollbar, "mouse_scroll") ScrollBar.defineEvent(ScrollBar, "mouse_scroll")
--- Creates a new Scrollbar instance --- Creates a new ScrollBar instance
--- @shortDescription Creates a new Scrollbar instance --- @shortDescription Creates a new ScrollBar instance
--- @return Scrollbar self The newly created Scrollbar instance --- @return ScrollBar self The newly created ScrollBar instance
--- @private --- @private
function Scrollbar.new() function ScrollBar.new()
local self = setmetatable({}, Scrollbar):__init() local self = setmetatable({}, ScrollBar):__init()
self.class = Scrollbar self.class = ScrollBar
self.set("width", 1) self.set("width", 1)
self.set("height", 10) self.set("height", 10)
return self return self
end end
--- @shortDescription Initializes the Scrollbar instance --- @shortDescription Initializes the ScrollBar instance
--- @param props table The properties to initialize the element with --- @param props table The properties to initialize the element with
--- @param basalt table The basalt instance --- @param basalt table The basalt instance
--- @return Scrollbar self The initialized instance --- @return ScrollBar self The initialized instance
--- @protected --- @protected
function Scrollbar:init(props, basalt) function ScrollBar:init(props, basalt)
VisualElement.init(self, props, basalt) VisualElement.init(self, props, basalt)
self.set("type", "Scrollbar") self.set("type", "ScrollBar")
return self return self
end end
--- Attaches the scrollbar to an element's property --- Attaches the ScrollBar to an element's property
--- @shortDescription Attaches the scrollbar to an element's property --- @shortDescription Attaches the ScrollBar to an element's property
--- @param element BaseElement The element to attach to --- @param element BaseElement The element to attach to
--- @param config table Configuration {property = "propertyName", min = number|function, max = number|function} --- @param config table Configuration {property = "propertyName", min = number|function, max = number|function}
--- @return Scrollbar self The scrollbar instance --- @return ScrollBar self The ScrollBar instance
function Scrollbar:attach(element, config) function ScrollBar:attach(element, config)
self.set("attachedElement", element) self.set("attachedElement", element)
self.set("attachedProperty", config.property) self.set("attachedProperty", config.property)
self.set("minValue", config.min or 0) self.set("minValue", config.min or 0)
@@ -79,10 +79,10 @@ function Scrollbar:attach(element, config)
return self return self
end end
--- Updates the attached element's property based on the scrollbar value --- Updates the attached element's property based on the ScrollBar value
--- @shortDescription Updates the attached element's property based on the scrollbar value --- @shortDescription Updates the attached element's property based on the ScrollBar value
--- @return Scrollbar self The scrollbar instance --- @return ScrollBar self The ScrollBar instance
function Scrollbar:updateAttachedElement() function ScrollBar:updateAttachedElement()
local element = self.get("attachedElement") local element = self.get("attachedElement")
if not element then return end if not element then return end
@@ -113,7 +113,7 @@ end
--- @param y number The y position of the click --- @param y number The y position of the click
--- @return boolean Whether the event was handled --- @return boolean Whether the event was handled
--- @protected --- @protected
function Scrollbar:mouse_click(button, x, y) function ScrollBar:mouse_click(button, x, y)
if VisualElement.mouse_click(self, button, x, y) then if VisualElement.mouse_click(self, button, x, y) then
local size = getScrollbarSize(self) local size = getScrollbarSize(self)
local value = self.get("value") local value = self.get("value")
@@ -139,7 +139,7 @@ end
--- @param y number The y position of the drag --- @param y number The y position of the drag
--- @return boolean Whether the event was handled --- @return boolean Whether the event was handled
--- @protected --- @protected
function Scrollbar:mouse_drag(button, x, y) function ScrollBar:mouse_drag(button, x, y)
if(VisualElement.mouse_drag(self, button, x, y))then if(VisualElement.mouse_drag(self, button, x, y))then
local size = getScrollbarSize(self) local size = getScrollbarSize(self)
local handleSize = self.get("handleSize") local handleSize = self.get("handleSize")
@@ -163,7 +163,7 @@ end
--- @param y number The y position of the scroll --- @param y number The y position of the scroll
--- @return boolean Whether the event was handled --- @return boolean Whether the event was handled
--- @protected --- @protected
function Scrollbar:mouse_scroll(direction, x, y) function ScrollBar:mouse_scroll(direction, x, y)
if not self:isInBounds(x, y) then return false end if not self:isInBounds(x, y) then return false end
direction = direction > 0 and -1 or 1 direction = direction > 0 and -1 or 1
local step = self.get("step") local step = self.get("step")
@@ -175,9 +175,9 @@ function Scrollbar:mouse_scroll(direction, x, y)
return true return true
end end
--- @shortDescription Renders the scrollbar --- @shortDescription Renders the ScrollBar
--- @protected --- @protected
function Scrollbar:render() function ScrollBar:render()
VisualElement.render(self) VisualElement.render(self)
local size = getScrollbarSize(self) local size = getScrollbarSize(self)
@@ -208,4 +208,4 @@ function Scrollbar:render()
end end
end end
return Scrollbar return ScrollBar