Dict, Trapper: prevent dismissal by past events

Add UIManager:discardEvents(), to allow dropping events
for a period of time.
Default duration of 600ms on eInk, 300ms otherwise.
Used when showing the dict result window, so that a tap
happening just when it's being shown won't discard it.
Used with Trapper:confirm()/:info(), to avoid taps made
in a previous processing to dismiss (and so, select the
cancel action) a ConfirmBox not yet painted/visible.
This commit is contained in:
poire-z
2021-01-01 14:34:55 +01:00
parent b6323f15fc
commit 6205f26047
5 changed files with 67 additions and 9 deletions

View File

@@ -39,6 +39,7 @@ local UIManager = {
_exit_code = nil,
_prevent_standby_count = 0,
_prev_prevent_standby_count = 0,
_discard_events_till = nil,
event_hook = require("ui/hook_container"):new()
}
@@ -797,10 +798,48 @@ function UIManager:quit()
end
end
--- Request events to be ignored for some duration
function UIManager:discardEvents(set_or_seconds)
if not set_or_seconds then -- remove any previously set
self._discard_events_till = nil
return
end
local usecs
if set_or_seconds == true then
-- Use an adequate delay to account for device refresh duration
-- so any events happening in this delay (ie. before a widget
-- is really painted on screen) are discarded.
if Device:hasEinkScreen() then
-- A screen refresh can take a few 100ms,
-- sometimes > 500ms on some devices/temperatures
usecs = 600000
else
-- On non-eInk screen, it's usually instantaneous
usecs = 300000
end
else -- we expect a number
usecs = set_or_seconds * MILLION
end
local now = { util.gettime() }
local now_us = now[1] * MILLION + now[2]
self._discard_events_till = now_us + usecs
end
--- Transmits an event to an active widget.
function UIManager:sendEvent(event)
if #self._window_stack == 0 then return end
-- Ensure discardEvents
if self._discard_events_till then
local now = { util.gettime() }
local now_us = now[1] * MILLION + now[2]
if now_us < self._discard_events_till then
return
else
self._discard_events_till = nil
end
end
local top_widget = self._window_stack[#self._window_stack]
-- top level widget has first access to the event
if top_widget.widget:handleEvent(event) then