From f63ec294981ad58283f8a6ed8101448ae24cedcd Mon Sep 17 00:00:00 2001 From: Robert Jelic <36573031+NoryiE@users.noreply.github.com> Date: Thu, 15 May 2025 01:03:50 +0200 Subject: [PATCH] Bugfix Fixed program only using relative path Fixed list not sending scroll events Added observer to scrollbar's :attach method Fixed vertical sliders --- src/elements/List.lua | 1 + src/elements/Program.lua | 4 ++-- src/elements/Scrollbar.lua | 11 +++++++++++ src/elements/Slider.lua | 18 ++++++++++++++---- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/elements/List.lua b/src/elements/List.lua index 2d63b95..099de8a 100644 --- a/src/elements/List.lua +++ b/src/elements/List.lua @@ -167,6 +167,7 @@ function List:mouse_scroll(direction, x, y) offset = math.min(maxOffset, math.max(0, offset + direction)) self.set("offset", offset) + self:fireEvent("mouse_scroll", direction, x, y) return true end return false diff --git a/src/elements/Program.lua b/src/elements/Program.lua index 64820c6..6c5a1ee 100644 --- a/src/elements/Program.lua +++ b/src/elements/Program.lua @@ -43,7 +43,7 @@ end ---@private function BasaltProgram:run(path, width, height) self.window = window.create(self.program:getBaseFrame():getTerm(), 1, 1, width, height, false) - local pPath = shell.resolveProgram(path) + local pPath = shell.resolveProgram(path) or fs.exists(path) and path or nil if(pPath~=nil)then if(fs.exists(pPath)) then local file = fs.open(pPath, "r") @@ -68,7 +68,7 @@ function BasaltProgram:run(path, width, height) self.coroutine = coroutine.create(function() local program = load(content, "@/" .. path, nil, env) if program then - local result = program(path, table.unpack(self.args)) + local result = program(table.unpack(self.args)) return result end end) diff --git a/src/elements/Scrollbar.lua b/src/elements/Scrollbar.lua index aed9a44..6f86b06 100644 --- a/src/elements/Scrollbar.lua +++ b/src/elements/Scrollbar.lua @@ -76,6 +76,17 @@ function ScrollBar:attach(element, config) self.set("attachedProperty", config.property) self.set("minValue", config.min or 0) self.set("maxValue", config.max or 100) + element:observe(config.property, function(_, value) + if value then + local min = self.get("minValue") + local max = self.get("maxValue") + if min == max then return end + + self.set("value", math.floor( + (value - min) / (max - min) * 100 + 0.5 + )) + end + end) return self end diff --git a/src/elements/Slider.lua b/src/elements/Slider.lua index 630d2d1..610cc6b 100644 --- a/src/elements/Slider.lua +++ b/src/elements/Slider.lua @@ -1,4 +1,5 @@ local VisualElement = require("elements/VisualElement") +local tHex = require("libraries/colorHex") --- This is the slider class. It provides a draggable slider control that can be either horizontal or vertical, --- with customizable colors and value ranges. @@ -11,7 +12,13 @@ Slider.defineProperty(Slider, "step", {default = 1, type = "number", canTriggerR ---@property max number 100 Maximum value for value conversion (maps slider position to this range) Slider.defineProperty(Slider, "max", {default = 100, type = "number"}) ---@property horizontal boolean true Whether the slider is horizontal (false for vertical) -Slider.defineProperty(Slider, "horizontal", {default = true, type = "boolean", canTriggerRender = true}) +Slider.defineProperty(Slider, "horizontal", {default = true, type = "boolean", canTriggerRender = true, setter=function(self, value) + if value then + self.set("backgroundEnabled", false) + else + self.set("backgroundEnabled", true) + end +end}) ---@property barColor color gray Color of the slider track Slider.defineProperty(Slider, "barColor", {default = colors.gray, type = "color", canTriggerRender = true}) ---@property sliderColor color blue Color of the slider handle @@ -21,6 +28,7 @@ Slider.defineProperty(Slider, "sliderColor", {default = colors.blue, type = "col Slider.defineEvent(Slider, "mouse_click") Slider.defineEvent(Slider, "mouse_drag") Slider.defineEvent(Slider, "mouse_up") +Slider.defineEvent(Slider, "mouse_scroll") --- Creates a new Slider instance --- @shortDescription Creates a new Slider instance @@ -90,6 +98,7 @@ function Slider:mouse_scroll(direction, x, y) self:updateRender() return true end + return false end --- @shortDescription Renders the slider with track and handle @@ -101,17 +110,18 @@ function Slider:render() local horizontal = self.get("horizontal") local step = self.get("step") - local barChar = horizontal and "\140" or "│" + local barChar = horizontal and "\140" or " " local text = string.rep(barChar, horizontal and width or height) if horizontal then self:textFg(1, 1, text, self.get("barColor")) self:textBg(step, 1, " ", self.get("sliderColor")) else + local bg = self.get("background") for y = 1, height do - self:textFg(1, y, barChar, self.get("barColor")) + self:textBg(1, y, " ", bg) end - self:textFg(1, step, "\140", self.get("sliderColor")) + self:textBg(1, step, " ", self.get("sliderColor")) end end