From bba48fc1bfa257eed0ff3f9ec2c674dceadba0b4 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Wed, 22 Nov 2023 18:58:31 +0100 Subject: [PATCH] Widgets: Unschedule timeouts on early close (#11126) Affects Notification, InfoMessage & QRMessage Includes a drive-by fix for Trapper interactions that have been broken for... a long while ;). --- frontend/ui/trapper.lua | 6 +++-- frontend/ui/widget/infomessage.lua | 42 ++++++++++++++--------------- frontend/ui/widget/notification.lua | 12 ++++++++- frontend/ui/widget/qrmessage.lua | 20 +++++++++++--- 4 files changed, 53 insertions(+), 27 deletions(-) diff --git a/frontend/ui/trapper.lua b/frontend/ui/trapper.lua index 678f01f46..0cc0a0f07 100644 --- a/frontend/ui/trapper.lua +++ b/frontend/ui/trapper.lua @@ -179,8 +179,10 @@ function Trapper:info(text, fast_refresh) return false end if self.current_widget then - -- Re-show current widget that was dismissed - -- (this is fine for our simple InfoMessage) + -- Resurect a dead widget. This should only be performed by trained Necromancers. + -- Do NOT do this at home, kids. + -- Some state *might* be lost, but the basics should survive... + self.current_widget:init() UIManager:show(self.current_widget) end UIManager:forceRePaint() diff --git a/frontend/ui/widget/infomessage.lua b/frontend/ui/widget/infomessage.lua index 88d4f06f8..a9671e8a5 100644 --- a/frontend/ui/widget/infomessage.lua +++ b/frontend/ui/widget/infomessage.lua @@ -50,6 +50,7 @@ local InfoMessage = InputContainer:extend{ monospace_font = false, text = "", timeout = nil, -- in seconds + _timeout_func = nil, width = nil, -- The width of the InfoMessage. Keep it nil to use default value. height = nil, -- The height of the InfoMessage. If this field is set, a scrollbar may be shown. -- The image shows at the left of the InfoMessage. Image data will be freed @@ -205,10 +206,24 @@ function InfoMessage:init() end function InfoMessage:onCloseWidget() + -- If we were closed early, drop the scheduled timeout + if self._timeout_func then + UIManager:unschedule(self._timeout_func) + self._timeout_func = nil + end + if self._delayed_show_action then UIManager:unschedule(self._delayed_show_action) self._delayed_show_action = nil end + if self.dismiss_callback then + self.dismiss_callback() + -- NOTE: Dirty hack for Trapper, which needs to pull a Lazarus on dead widgets while preserving the callback's integrity ;). + if not self.is_infomessage then + self.dismiss_callback = nil + end + end + if self.invisible then -- Still invisible, no setDirty needed return @@ -242,16 +257,13 @@ function InfoMessage:onShow() -- Discard queued and upcoming input events to avoid accidental dismissal Input:inhibitInputUntil(true) end - -- schedule us to close ourself if timeout provided + -- schedule a close on timeout, if any if self.timeout then - UIManager:scheduleIn(self.timeout, function() - -- In case we're provided with dismiss_callback, also call it on timeout - if self.dismiss_callback then - self.dismiss_callback() - self.dismiss_callback = nil - end + self._timeout_func = function() + self._timeout_func = nil UIManager:close(self) - end) + end + UIManager:scheduleIn(self.timeout, self._timeout_func) end return true end @@ -269,20 +281,8 @@ function InfoMessage:paintTo(bb, x, y) InputContainer.paintTo(self, bb, x, y) end -function InfoMessage:dismiss() - if self._delayed_show_action then - UIManager:unschedule(self._delayed_show_action) - self._delayed_show_action = nil - end - if self.dismiss_callback then - self.dismiss_callback() - self.dismiss_callback = nil - end - UIManager:close(self) -end - function InfoMessage:onTapClose() - self:dismiss() + UIManager:close(self) if self.readonly ~= true then return true end diff --git a/frontend/ui/widget/notification.lua b/frontend/ui/widget/notification.lua index 9ecf8141f..ea8e64793 100644 --- a/frontend/ui/widget/notification.lua +++ b/frontend/ui/widget/notification.lua @@ -57,6 +57,7 @@ local Notification = InputContainer:extend{ margin = Size.margin.default, padding = Size.padding.default, timeout = 2, -- default to 2 seconds + _timeout_func = nil, toast = true, -- closed on any event, and let the event propagate to next top widget _shown_list = {}, -- actual static class member, array of stacked notifications (value is show (well, init) time or false). @@ -200,6 +201,11 @@ function Notification:onCloseWidget() UIManager:setDirty(nil, function() return "ui", self.frame.dimen end) + -- If we were closed early, drop the scheduled timeout + if self._timeout_func then + UIManager:unschedule(self._timeout_func) + self._timeout_func = nil + end end function Notification:onShow() @@ -207,7 +213,11 @@ function Notification:onShow() return "ui", self.frame.dimen end) if self.timeout then - UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end) + self._timeout_func = function() + self._timeout_func = nil + UIManager:close(self) + end + UIManager:scheduleIn(self.timeout, self._timeout_func) end if #self._past_messages >= MAX_NB_PAST_MESSAGES then diff --git a/frontend/ui/widget/qrmessage.lua b/frontend/ui/widget/qrmessage.lua index 606aa1357..bc74ec99d 100644 --- a/frontend/ui/widget/qrmessage.lua +++ b/frontend/ui/widget/qrmessage.lua @@ -33,10 +33,11 @@ local Size = require("ui/size") local QRMessage = InputContainer:extend{ modal = true, timeout = nil, -- in seconds + _timeout_func = nil, text = nil, -- The text to encode. width = nil, -- The width. Keep it nil to use original width. height = nil, -- The height. Keep it nil to use original height. - dismiss_callback = function() end, + dismiss_callback = nil, alpha = nil, scale_factor = 1, } @@ -83,6 +84,16 @@ function QRMessage:onCloseWidget() UIManager:setDirty(nil, function() return "ui", self[1][1].dimen -- i.e., frame end) + -- If we were closed early, drop the scheduled timeout + if self._timeout_func then + UIManager:unschedule(self._timeout_func) + self._timeout_func = nil + end + + if self.dismiss_callback then + self.dismiss_callback() + self.dismiss_callback = nil + end end function QRMessage:onShow() @@ -91,13 +102,16 @@ function QRMessage:onShow() return "ui", self[1][1].dimen end) if self.timeout then - UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end) + self._timeout_func = function() + self._timeout_func = nil + UIManager:close(self) + end + UIManager:scheduleIn(self.timeout, self._timeout_func) end return true end function QRMessage:onTapClose() - self.dismiss_callback() UIManager:close(self) end QRMessage.onAnyKeyPressed = QRMessage.onTapClose