2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/ui/widget/trapwidget.lua

167 lines
5.4 KiB
Lua
Raw Normal View History

--[[--
Invisible full screen widget for catching UI events.
(for use with or by Trapper to interrupt its processing).
Can optionally display a text message at bottom left of screen
(ie: "Loading…")
]]
local Blitbuffer = require("ffi/blitbuffer")
local BottomContainer = require("ui/widget/container/bottomcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local Device = require("device")
local Event = require("ui/event")
local Font = require("ui/font")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
local LeftContainer = require("ui/widget/container/leftcontainer")
local Size = require("ui/size")
local TextBoxWidget = require("ui/widget/textboxwidget")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
local Input = Device.input
local Screen = Device.screen
Clarify our OOP semantics across the codebase (#9586) Basically: * Use `extend` for class definitions * Use `new` for object instantiations That includes some minor code cleanups along the way: * Updated `Widget`'s docs to make the semantics clearer. * Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283) * Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass). * Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events. * Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier. * Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references. * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak). * Terminal: Make sure the shell is killed on plugin teardown. * InputText: Fix Home/End/Del physical keys to behave sensibly. * InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...). * OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of. * ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed! * Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local TrapWidget = InputContainer:extend{
modal = true,
dismiss_callback = function() end,
text = nil, -- will be invisible if no message given
face = Font:getFace("infofont"),
-- Whether to resend the event caught and used for dismissal
resend_event = false,
}
function TrapWidget:init()
local full_screen = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
if Device:hasKeys() then
self.key_events.AnyKeyPressed = { { Input.group.Any } }
end
if Device:isTouchDevice() then
self.ges_events = {
TapDismiss = {
GestureRange:new{ ges = "tap", range = full_screen, }
},
HoldDismiss = {
GestureRange:new{ ges = "hold", range = full_screen, }
},
SwipeDismiss = {
GestureRange:new{ ges = "swipe", range = full_screen, }
},
PanReleaseDismiss = { -- emitted on mousewheel event
GestureRange:new{ ges = "pan_release", range = full_screen, }
},
}
end
if self.text then
local textw = TextWidget:new{
text = self.text,
face = self.face,
}
-- Don't make our message reach full screen width, so
-- it looks like popping from bottom left corner
if textw:getWidth() > Screen:getWidth() * 0.9 then
-- Text too wide: use TextBoxWidget for multi lines display
textw = TextBoxWidget:new{
text = self.text,
face = self.face,
width = math.floor(Screen:getWidth() * 0.9)
}
end
local border_size = Size.border.default
self.frame = FrameContainer:new{
background = Blitbuffer.COLOR_WHITE,
bordersize = border_size,
margin = 0,
padding = 0,
padding_left = Size.padding.default,
padding_right = Size.padding.default,
textw,
}
-- To have our frame message a bit prettier with its left
-- and bottom borders not displayed, we make use of this
-- combination of Containers to push them off-screen
self[1] = CenterContainer:new{
dimen = full_screen:copy(),
BottomContainer:new{
dimen = Geom:new{
w = full_screen.w,
h = full_screen.h + 2*border_size,
},
LeftContainer:new{
dimen = Geom:new{
w = full_screen.w + 2*border_size,
h = self.frame:getSize().h,
},
self.frame,
}
}
}
else
-- So that UIManager knows no refresh is needed and
-- avoids some unnecessary refreshes
self.invisible = true
end
end
function TrapWidget:_dismissAndResend(evtype, ev)
self.dismiss_callback()
UIManager:close(self)
if self.resend_event and evtype and ev then
-- There may be some timing issues that could cause crashes, as we
-- use nextTick, if the dismiss_callback uses UIManager:scheduleIn()
-- or has set up some widget that may catch that event while not being
-- yet fully initialiazed.
-- (It happened mostly when I had some bug somewhere, and it was a quite
-- reliable sign of a bug somewhere, but the stacktrace was unrelated
-- to the bug location.)
UIManager:nextTick(function() UIManager:handleInputEvent(Event:new(evtype, ev)) end)
end
return true
end
function TrapWidget:onAnyKeyPressed(_, ev)
return self:_dismissAndResend("KeyPress", ev)
end
function TrapWidget:onTapDismiss(_, ev)
return self:_dismissAndResend("Gesture", ev)
end
function TrapWidget:onHoldDismiss(_, ev)
return self:_dismissAndResend("Gesture", ev)
end
function TrapWidget:onSwipeDismiss(_, ev)
return self:_dismissAndResend("Gesture", ev)
end
function TrapWidget:onPanReleaseDismiss(_, ev)
return self:_dismissAndResend("Gesture", ev)
end
function TrapWidget:onShow()
if self.frame then
UIManager:setDirty(self, function()
return "ui", self.frame.dimen
end)
end
return true
end
function TrapWidget:onCloseWidget()
if self.frame then
UIManager:setDirty(nil, function()
return "ui", self.frame.dimen
end)
end
end
return TrapWidget