2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/frontend/ui/widget/qrmessage.lua
NiLuJe 45a4aac3d3
Notification: Fence the *display* update in an attempt to avoid upsetting some boards... (#10083)
Re: https://github.com/koreader/koreader/issues/9806#issuecomment-1416827447

Depends on https://github.com/koreader/koreader-base/pull/1576

Includes assorted cosmetics tweaks related to duplicate `setDirty` calls when instantiating widgets that already have an `onShow` handler doing it. (I left widgets doing it in `update` instead of `init` alone, on the assumption that callers *may* be relying on that behavior when updating widgets at runtime. This might actually never matter, and it certainly didn't for ScreenSaverWidget, which is why I removed it from there ;p).
2023-02-07 01:01:05 +01:00

106 lines
2.9 KiB
Lua

--[[--
Widget that displays a qr code.
It vanishes on key press or after a given timeout.
Example:
local UIManager = require("ui/uimanager")
local _ = require("gettext")
local Screen = require("device").screen
local sample
sample = QRMessage:new{
text = _("my message"),
height = Screen:scaleBySize(400),
width = Screen:scaleBySize(400),
timeout = 5, -- This widget will vanish in 5 seconds.
}
UIManager:show(sample)
]]
local Blitbuffer = require("ffi/blitbuffer")
local CenterContainer = require("ui/widget/container/centercontainer")
local Device = require("device")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local QRWidget = require("ui/widget/qrwidget")
local InputContainer = require("ui/widget/container/inputcontainer")
local UIManager = require("ui/uimanager")
local Input = Device.input
local Screen = Device.screen
local Size = require("ui/size")
local QRMessage = InputContainer:extend{
modal = true,
timeout = nil, -- in seconds
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,
alpha = nil,
scale_factor = 1,
}
function QRMessage:init()
if Device:hasKeys() then
self.key_events.AnyKeyPressed = { { Input.group.Any } }
end
if Device:isTouchDevice() then
self.ges_events.TapClose = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
}
end
local padding = Size.padding.fullscreen
local image_widget = QRWidget:new{
text = self.text,
width = self.width and (self.width - 2 * padding),
height = self.height and (self.height - 2 * padding),
alpha = self.alpha,
scale_factor = self.scale_factor,
}
local frame = FrameContainer:new{
background = Blitbuffer.COLOR_WHITE,
padding = padding,
image_widget,
}
self[1] = CenterContainer:new{
dimen = Screen:getSize(),
frame,
}
end
function QRMessage:onCloseWidget()
UIManager:setDirty(nil, function()
return "ui", self[1][1].dimen -- i.e., frame
end)
end
function QRMessage:onShow()
-- triggered by the UIManager after we got successfully shown (not yet painted)
UIManager:setDirty(self, function()
return "ui", self[1][1].dimen
end)
if self.timeout then
UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end)
end
return true
end
function QRMessage:onTapClose()
self.dismiss_callback()
UIManager:close(self)
end
QRMessage.onAnyKeyPressed = QRMessage.onTapClose
return QRMessage