Skip to content

Commit a97f799

Browse files
committed
Fixed a issue with frame events
Fixed Collection setItems not triggering render
1 parent a967cde commit a97f799

5 files changed

Lines changed: 45 additions & 37 deletions

File tree

src/elements/BaseElement.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ end
283283
function BaseElement:getActiveStates()
284284
local states = self.get("states")
285285
local result = {}
286-
286+
287287
for stateName, priority in pairs(states) do
288288
table.insert(result, {name = stateName, priority = priority})
289289
end
290-
290+
291291
table.sort(result, function(a, b) return a.priority > b.priority end)
292-
292+
293293
return result
294294
end
295295

@@ -326,10 +326,11 @@ end
326326
--- @return table self The BaseElement instance
327327
function BaseElement:fireEvent(event, ...)
328328
if self.get("eventCallbacks")[event] then
329+
local lastResult
329330
for _, callback in ipairs(self.get("eventCallbacks")[event]) do
330-
local result = callback(self, ...)
331-
return result
331+
lastResult = callback(self, ...)
332332
end
333+
return lastResult
333334
end
334335
return self
335336
end

src/elements/Collection.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ local CollectionEntry = require("libraries/collectionentry")
77
local Collection = setmetatable({}, VisualElement)
88
Collection.__index = Collection
99

10-
Collection.defineProperty(Collection, "items", {default={}, type = "table"})
10+
Collection.defineProperty(Collection, "items", {default={}, type = "table", canTriggerRender = true})
1111
---@property selectable boolean true Whether items can be selected
1212
Collection.defineProperty(Collection, "selectable", {default = true, type = "boolean"})
1313
---@property multiSelection boolean false Whether multiple items can be selected at once
1414
Collection.defineProperty(Collection, "multiSelection", {default = false, type = "boolean"})
1515
---@property selectedBackground color blue Background color for selected items
16-
Collection.defineProperty(Collection, "selectedBackground", {default = colors.blue, type = "color"})
16+
Collection.defineProperty(Collection, "selectedBackground", {default = colors.blue, type = "color", canTriggerRender = true})
1717
---@property selectedForeground color white Text color for selected items
18-
Collection.defineProperty(Collection, "selectedForeground", {default = colors.white, type = "color"})
18+
Collection.defineProperty(Collection, "selectedForeground", {default = colors.white, type = "color", canTriggerRender = true})
1919

2020
---@event onSelect {index number, item table} Fired when an item is selected
2121

src/elements/Container.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ for k, _ in pairs(elementManager:getElementList()) do
7272
expect(1, self, "table")
7373
local element = self.basalt.create(k, ...)
7474
self:addChild(element)
75-
element:postInit()
75+
--element:postInit()
7676
return element
7777
end
7878
Container["addDelayed"..capitalizedName] = function(self, prop)
@@ -294,7 +294,6 @@ function Container:unregisterChildEvent(child, eventName)
294294
end
295295
end
296296
self.set("childrenEventsSorted", false)
297-
self:updateRender()
298297
break
299298
end
300299
end
@@ -362,6 +361,12 @@ end
362361
--- @return boolean handled Whether the event was handled
363362
--- @return table? child The child that handled the event
364363
function Container:callChildrenEvent(visibleOnly, event, ...)
364+
if visibleOnly and not self.get("childrenEventsSorted") then
365+
for evt in pairs(self._values.childrenEvents) do
366+
self:sortChildrenEvents(evt)
367+
end
368+
end
369+
365370
local children = visibleOnly and self.get("visibleChildrenEvents") or self.get("childrenEvents")
366371
if children[event] then
367372
local events = children[event]
@@ -488,7 +493,7 @@ function Container:mouse_scroll(direction, x, y)
488493
if(VisualElement.mouse_scroll(self, direction, x, y))then
489494
local args = convertMousePosition(self, "mouse_scroll", direction, x, y)
490495
local success, child = self:callChildrenEvent(true, "mouse_scroll", table.unpack(args))
491-
return success
496+
return true
492497
end
493498
return false
494499
end

src/elements/Frame.lua

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,16 @@ local Frame = setmetatable({}, Container)
99
Frame.__index = Frame
1010

1111
---@property draggable boolean false Whether the frame is draggable
12-
Frame.defineProperty(Frame, "draggable", {default = false, type = "boolean", setter=function(self, value)
13-
if value then
14-
self:listenEvent("mouse_click", true)
15-
self:listenEvent("mouse_up", true)
16-
self:listenEvent("mouse_drag", true)
17-
end
18-
return value
19-
end})
12+
Frame.defineProperty(Frame, "draggable", {default = false, type = "boolean"})
2013
---@property draggingMap table {} The map of dragging positions
2114
Frame.defineProperty(Frame, "draggingMap", {default = {{x=1, y=1, width="width", height=1}}, type = "table"})
2215
---@property scrollable boolean false Whether the frame is scrollable
23-
Frame.defineProperty(Frame, "scrollable", {default = false, type = "boolean", setter=function(self, value)
24-
if value then
25-
self:listenEvent("mouse_scroll", true)
26-
end
27-
return value
28-
end})
16+
Frame.defineProperty(Frame, "scrollable", {default = false, type = "boolean"})
17+
18+
Frame.defineEvent(Frame, "mouse_click")
19+
Frame.defineEvent(Frame, "mouse_drag")
20+
Frame.defineEvent(Frame, "mouse_up")
21+
Frame.defineEvent(Frame, "mouse_scroll")
2922

3023
--- Creates a new Frame instance
3124
--- @shortDescription Creates a new Frame instance
@@ -59,7 +52,7 @@ end
5952
--- @return boolean handled Whether the event was handled
6053
--- @protected
6154
function Frame:mouse_click(button, x, y)
62-
if VisualElement.mouse_click(self, button, x, y) then
55+
if self:isInBounds(x, y) then
6356
if self.get("draggable") then
6457
local relX, relY = self:getRelativePosition(x, y)
6558
local draggingMap = self.get("draggingMap")
@@ -150,23 +143,33 @@ function Frame:getChildrenHeight()
150143
return maxHeight
151144
end
152145

146+
local function convertMousePosition(self, event, ...)
147+
local args = {...}
148+
if event and event:find("mouse_") then
149+
local button, absX, absY = ...
150+
local xOffset, yOffset = self.get("offsetX"), self.get("offsetY")
151+
local relX, relY = self:getRelativePosition(absX + xOffset, absY + yOffset)
152+
args = {button, relX, relY}
153+
end
154+
return args
155+
end
156+
153157
--- @shortDescription Handles mouse scroll events
154158
--- @param direction number The scroll direction
155159
--- @param x number The x position of the scroll
156160
--- @param y number The y position of the scroll
157161
--- @return boolean handled Whether the event was handled
158162
--- @protected
159163
function Frame:mouse_scroll(direction, x, y)
160-
if Container.mouse_scroll(self, direction, x, y) then
161-
return true
162-
end
163-
164-
if self.get("scrollable") then
165-
local relX, relY = self:getRelativePosition(x, y)
166-
local width = self.get("width")
167-
local height = self.get("height")
164+
if(VisualElement.mouse_scroll(self, direction, x, y))then
165+
local args = convertMousePosition(self, "mouse_scroll", direction, x, y)
166+
local success, child = self:callChildrenEvent(true, "mouse_scroll", table.unpack(args))
167+
if success then
168+
return true
169+
end
170+
if self.get("scrollable") then
171+
local height = self.get("height")
168172

169-
if relX >= 1 and relX <= width and relY >= 1 and relY <= height then
170173
local childrenHeight = self:getChildrenHeight()
171174
local currentOffset = self.get("offsetY")
172175
local maxScroll = math.max(0, childrenHeight - height)
@@ -178,7 +181,6 @@ function Frame:mouse_scroll(direction, x, y)
178181
return true
179182
end
180183
end
181-
182184
return false
183185
end
184186

src/elements/VisualElement.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ VisualElement.combineProperties(VisualElement, "size", "width", "height")
7272
VisualElement.combineProperties(VisualElement, "color", "foreground", "background")
7373

7474
---@event onClick {button string, x number, y number} Fired on mouse click
75-
---@event onMouseUp {button, x, y} Fired on mouse button release
75+
---@event onClickUp {button, x, y} Fired on mouse button release
7676
---@event onRelease {button, x, y} Fired when mouse leaves while clicked
7777
---@event onDrag {button, x, y} Fired when mouse moves while clicked
7878
---@event onScroll {direction, x, y} Fired on mouse scroll

0 commit comments

Comments
 (0)