diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index c19c5895d..4710c3884 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -395,7 +395,7 @@ function UIManager:sendEvent(event) -- if the event is not consumed, active widgets (from top to bottom) can -- access it. NOTE: _window_stack can shrink on close event local checked_widgets = {top_widget} - for i = #self._window_stack-1, 1, -1 do + for i = #self._window_stack, 1, -1 do local widget = self._window_stack[i] if checked_widgets[widget] == nil then if widget.widget.is_always_active then diff --git a/spec/unit/uimanager_spec.lua b/spec/unit/uimanager_spec.lua index 1b2a530dd..c502b5ef6 100644 --- a/spec/unit/uimanager_spec.lua +++ b/spec/unit/uimanager_spec.lua @@ -232,9 +232,7 @@ describe("UIManager spec", function() end) it("should handle stack change when checking for active widgets", function() - -- this senario should only happen when other active widgets - -- are closed by the one widget's handleEvent - + -- senario 1: 2nd widget removes the 3rd widget in the stack local call_signals = {0, 0, 0} UIManager._window_stack = { { @@ -269,5 +267,40 @@ describe("UIManager spec", function() assert.is.same(call_signals[1], 1) assert.is.same(call_signals[2], 0) assert.is.same(call_signals[3], 1) + + -- senario 2: top widget removes itself + call_signals = {0, 0, 0} + UIManager._window_stack = { + { + widget = { + is_always_active = true, + handleEvent = function() + call_signals[1] = call_signals[1] + 1 + end + } + }, + { + widget = { + is_always_active = true, + handleEvent = function() + call_signals[2] = call_signals[2] + 1 + end + } + }, + { + widget = { + is_always_active = true, + handleEvent = function() + call_signals[3] = call_signals[3] + 1 + table.remove(UIManager._window_stack, 3) + end + } + }, + } + + UIManager:sendEvent("foo") + assert.is.same(call_signals[1], 1) + assert.is.same(call_signals[2], 1) + assert.is.same(call_signals[3], 1) end) end)