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

199 lines
7.6 KiB
Lua
Raw Normal View History

local Device = require("device")
local Event = require("ui/event")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local FrameContainer = require("ui/widget/container/framecontainer")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local UIManager = require("ui/uimanager")
local util = require("util")
local _ = require("gettext")
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 ScreenSaverWidget = InputContainer:extend{
name = "ScreenSaver",
widget = nil,
background = nil,
}
function ScreenSaverWidget:init()
if Device:hasKeys() then
self.key_events.AnyKeyPressed = { { Device.input.group.Any } }
end
if Device:isTouchDevice() then
if G_reader_settings:readSetting("screensaver_delay") == "gesture" then
self:setupGestureEvents()
end
if not self.has_exit_screensaver_gesture then
-- Exit with gesture not enabled, or no configured gesture found: allow exiting with tap
local range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
self.ges_events.Tap = { GestureRange:new{ ges = "tap", range = range } }
end
end
self:update()
end
function ScreenSaverWidget:setupGestureEvents()
-- The configured gesture(s) won't trigger, because this widget is at top
-- of the UI stack and will prevent ReaderUI/Filemanager from getting
-- and handling any configured gesture event.
-- We need to find all the ones configured for the "exit_screensaver" action,
-- and clone them so they are handled by this widget.
local ReaderUI = require("apps/reader/readerui")
local ui = ReaderUI:_getRunningInstance()
if not ui then
local FileManager = require("apps/filemanager/filemanager")
ui = FileManager.instance
end
if ui and ui.gestures and ui.gestures.gestures then
local multiswipe_already_met = false
for gesture, actions in pairs(ui.gestures.gestures) do
if util.stringStartsWith(gesture, "multiswipe") then
-- All multiswipes are handled by the single handler for "multiswipe"
-- We only need to clone one of them
gesture = "multiswipe"
end
if actions["exit_screensaver"] and (gesture ~= "multiswipe" or not multiswipe_already_met) then
if gesture == "multiswipe" then
multiswipe_already_met = true
end
-- Clone the gesture found in our self.ges_events
local ui_gesture = ui._zones[gesture]
if ui_gesture and ui_gesture.handler then
-- We can reuse its GestureRange object
self.ges_events[gesture] = { ui_gesture.gs_range }
-- For each of them, we need a distinct event and its handler.
-- This handler will call the original handler (created by gestures.koplugin)
-- which, after some checks (like swipe distance and direction, and multiswipe
-- directions), will emit normally the configured real ExitScreensaver event,
-- that this widget (being at top of the UI stack) will get and that
-- onExitScreensaver() will handle.
local event_name = "TriggerExitScreensaver_" .. gesture
self.ges_events[gesture].event = event_name
self["on"..event_name] = function(this, args, ev)
ui_gesture.handler(ev)
return true
end
end
end
end
end
if next(self.ges_events) then -- we found a gesture configured
self.has_exit_screensaver_gesture = true
-- Override handleEvent(), so we can stop any event from propagating to widgets
-- below this one (we may get some from other multiswipe as the handler does
-- not filter the one we are insterested with, but also when multiple actions
-- are assigned to a single gesture).
self.handleEvent = function(this, event)
InputContainer.handleEvent(this, event)
return true
end
self.key_events = {} -- also disable exit with keys
end
end
function ScreenSaverWidget:showWaitForGestureMessage(msg)
-- We just paint an InfoMessage on screen directly: we don't want
-- another widget that we would need to prevent catching events
local infomsg = InfoMessage:new{
text = self.has_exit_screensaver_gesture
and _("Waiting for specific gesture to exit screensaver.")
or _("No exit screensaver gesture configured. Tap to exit.")
}
infomsg:paintTo(Screen.bb, 0, 0)
infomsg:onShow() -- get the screen refreshed
infomsg:free()
end
function ScreenSaverWidget:update()
self.height = Screen:getHeight()
self.width = Screen:getWidth()
self.region = Geom:new{
x = 0, y = 0,
w = self.width,
h = self.height,
}
self.main_frame = FrameContainer:new{
radius = 0,
bordersize = 0,
padding = 0,
margin = 0,
background = self.background,
width = self.width,
height = self.height,
self.widget,
}
Enable HW dithering in a few key places (#4541) * Enable HW dithering on supported devices (Clara HD, Forma; Oasis 2, PW4) * FileManager and co. (where appropriate, i.e., when covers are shown) * Book Status * Reader, where appropriate: * CRe: on pages whith image content (for over 7.5% of the screen area, should hopefully leave stuff like bullet points or small scene breaks alone). * Other engines: on user-request (in the gear tab of the bottom menu), via the new "Dithering" knob (will only appear on supported devices). * ScreenSaver * ImageViewer * Minimize repaints when flash_ui is enabled (by, almost everywhere, only repainting the flashing element, and not the toplevel window which hosts it). (The first pass of this involved fixing a few Button instances whose show_parent was wrong, in particular, chevrons in the FM & TopMenu). * Hunted down a few redundant repaints (unneeded setDirty("all") calls), either by switching the widget to nil when only a refresh was needed, and not a repaint, or by passing the appropritate widget to setDirty. (Note to self: Enable *verbose* debugging to catch broken setDirty calls via its post guard). There were also a few instances of 'em right behind a widget close. * Don't repaint the underlying widget when initially showing TopMenu & ConfigDialog. We unfortunately do need to do it when switching tabs, because of their variable heights. * On Kobo, disabled the extra and completely useless full refresh before suspend/reboot/poweroff, as well as on resume. No more double refreshes! * Fix another debug guard in Kobo sysfs_light * Switch ImageWidget & ImageViewer mostly to "ui" updates, which will be better suited to image content pretty much everywhere, REAGL or not. PS: (Almost :100: commits! :D)
2019-02-07 00:14:37 +00:00
self.dithered = true
self[1] = self.main_frame
Enable HW dithering in a few key places (#4541) * Enable HW dithering on supported devices (Clara HD, Forma; Oasis 2, PW4) * FileManager and co. (where appropriate, i.e., when covers are shown) * Book Status * Reader, where appropriate: * CRe: on pages whith image content (for over 7.5% of the screen area, should hopefully leave stuff like bullet points or small scene breaks alone). * Other engines: on user-request (in the gear tab of the bottom menu), via the new "Dithering" knob (will only appear on supported devices). * ScreenSaver * ImageViewer * Minimize repaints when flash_ui is enabled (by, almost everywhere, only repainting the flashing element, and not the toplevel window which hosts it). (The first pass of this involved fixing a few Button instances whose show_parent was wrong, in particular, chevrons in the FM & TopMenu). * Hunted down a few redundant repaints (unneeded setDirty("all") calls), either by switching the widget to nil when only a refresh was needed, and not a repaint, or by passing the appropritate widget to setDirty. (Note to self: Enable *verbose* debugging to catch broken setDirty calls via its post guard). There were also a few instances of 'em right behind a widget close. * Don't repaint the underlying widget when initially showing TopMenu & ConfigDialog. We unfortunately do need to do it when switching tabs, because of their variable heights. * On Kobo, disabled the extra and completely useless full refresh before suspend/reboot/poweroff, as well as on resume. No more double refreshes! * Fix another debug guard in Kobo sysfs_light * Switch ImageWidget & ImageViewer mostly to "ui" updates, which will be better suited to image content pretty much everywhere, REAGL or not. PS: (Almost :100: commits! :D)
2019-02-07 00:14:37 +00:00
UIManager:setDirty(self, function()
return "full", self.main_frame.dimen
end)
end
function ScreenSaverWidget:onShow()
UIManager:setDirty(self, function()
return "full", self.main_frame.dimen
end)
return true
end
function ScreenSaverWidget:onTap(_, ges)
if ges.pos:intersectWith(self.main_frame.dimen) then
self:onClose()
end
return true
end
function ScreenSaverWidget:onClose()
-- If we happened to shortcut a delayed close via user input, unschedule it to avoid a spurious refresh.
local Screensaver = require("ui/screensaver")
if Screensaver.delayed_close then
UIManager:unschedule(Screensaver.close_widget)
end
UIManager:close(self)
return true
end
ScreenSaverWidget.onAnyKeyPressed = ScreenSaverWidget.onClose
ScreenSaverWidget.onExitScreensaver = ScreenSaverWidget.onClose
function ScreenSaverWidget:onCloseWidget()
Device.screen_saver_mode = false
Device.screen_saver_lock = false
-- Restore to previous rotation mode, if need be.
if Device.orig_rotation_mode then
Screen:setRotationMode(Device.orig_rotation_mode)
Device.orig_rotation_mode = nil
end
-- Make it full-screen (self.main_frame.dimen might be in a different orientation, and it's already full-screen anyway...)
UIManager:setDirty(nil, function()
return "full"
end)
-- Will come after the Resume event, iff screensaver_delay is set.
-- Comes *before* it otherwise.
UIManager:broadcastEvent(Event:new("OutOfScreenSaver"))
end
function ScreenSaverWidget:onResume()
-- If we actually catch this event, it means screensaver_delay is set.
-- Tell Device about it, so that further power button presses while we're still shown send us back to suspend.
-- NOTE: This only affects devices where we handle Power events ourselves (i.e., rely on Device -> Generic's onPowerEvent),
-- and it *always* implies that Device.screen_saver_mode is true.
Device.screen_saver_lock = true
end
function ScreenSaverWidget:onSuspend()
-- Also flip this back on suspend, in case we suspend again on a delayed screensaver (e.g., via SleepCover or AutoSuspend).
Device.screen_saver_lock = false
end
return ScreenSaverWidget