mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
UIManager: get rid of self._running and self._run_forever (#9669)
This commit is contained in:
@@ -29,7 +29,6 @@ local UIManager = {
|
|||||||
|
|
||||||
event_handlers = nil,
|
event_handlers = nil,
|
||||||
|
|
||||||
_running = true,
|
|
||||||
_now = time.now(),
|
_now = time.now(),
|
||||||
_window_stack = {},
|
_window_stack = {},
|
||||||
_task_queue = {},
|
_task_queue = {},
|
||||||
@@ -68,7 +67,11 @@ function UIManager:init()
|
|||||||
self:nextTick(function()
|
self:nextTick(function()
|
||||||
Device:saveSettings()
|
Device:saveSettings()
|
||||||
Device:powerOff()
|
Device:powerOff()
|
||||||
self:quit(Device:isKobo() and 88)
|
if Device:isKobo() then
|
||||||
|
self:quit(88)
|
||||||
|
else
|
||||||
|
self:quit()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
self.reboot_action = function()
|
self.reboot_action = function()
|
||||||
@@ -81,11 +84,19 @@ function UIManager:init()
|
|||||||
self:nextTick(function()
|
self:nextTick(function()
|
||||||
Device:saveSettings()
|
Device:saveSettings()
|
||||||
Device:reboot()
|
Device:reboot()
|
||||||
self:quit(Device:isKobo() and 88)
|
if Device:isKobo() then
|
||||||
|
self:quit(88)
|
||||||
|
else
|
||||||
|
self:quit()
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
Device:_setEventHandlers(self)
|
Device:_setEventHandlers(self)
|
||||||
|
|
||||||
|
-- A simple wrapper for UIManager:quit()
|
||||||
|
-- This may be overwritten by setRunForeverMode(); for testing purposes
|
||||||
|
self._gated_quit = self.quit
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
@@ -116,7 +127,6 @@ function UIManager:show(widget, refreshtype, refreshregion, x, y, refreshdither)
|
|||||||
end
|
end
|
||||||
logger.dbg("show widget:", widget.id or widget.name or tostring(widget))
|
logger.dbg("show widget:", widget.id or widget.name or tostring(widget))
|
||||||
|
|
||||||
self._running = true
|
|
||||||
local window = {x = x or 0, y = y or 0, widget = widget}
|
local window = {x = x or 0, y = y or 0, widget = widget}
|
||||||
-- put this window on top of the topmost non-modal window
|
-- put this window on top of the topmost non-modal window
|
||||||
for i = #self._window_stack, 0, -1 do
|
for i = #self._window_stack, 0, -1 do
|
||||||
@@ -728,13 +738,16 @@ function UIManager:isWidgetShown(widget)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Signals to quit.
|
--- Signals to quit.
|
||||||
|
-- An exit_code of false is not allowed.
|
||||||
function UIManager:quit(exit_code)
|
function UIManager:quit(exit_code)
|
||||||
if not self._running then return end
|
if exit_code == false then
|
||||||
logger.info("quitting uimanager with exit code:", exit_code or 0)
|
logger.err("UIManager:quit() called with false")
|
||||||
self._exit_code = exit_code
|
return
|
||||||
|
end
|
||||||
|
-- Also honor older exit codes; default to 0
|
||||||
|
self._exit_code = exit_code or self._exit_code or 0
|
||||||
|
logger.info("quitting uimanager with exit code:", self._exit_code)
|
||||||
self._task_queue_dirty = false
|
self._task_queue_dirty = false
|
||||||
self._running = false
|
|
||||||
self._run_forever = nil
|
|
||||||
for i = #self._window_stack, 1, -1 do
|
for i = #self._window_stack, 1, -1 do
|
||||||
table.remove(self._window_stack, i)
|
table.remove(self._window_stack, i)
|
||||||
end
|
end
|
||||||
@@ -749,6 +762,21 @@ function UIManager:quit(exit_code)
|
|||||||
self.looper:close()
|
self.looper:close()
|
||||||
self.looper = nil
|
self.looper = nil
|
||||||
end
|
end
|
||||||
|
return self._exit_code
|
||||||
|
end
|
||||||
|
dbg:guard(UIManager, 'quit',
|
||||||
|
function(self, exit_code)
|
||||||
|
assert(exit_code ~= false, "exit_code == false is not supported")
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- Disable automatic UIManager quit; for testing purposes
|
||||||
|
function UIManager:setRunForeverMode()
|
||||||
|
self._gated_quit = function() return false end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Enable automatic UIManager quit; for testing purposes
|
||||||
|
function UIManager:unsetRunForeverMode()
|
||||||
|
self._gated_quit = self.quit
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
@@ -1345,10 +1373,11 @@ function UIManager:handleInput()
|
|||||||
--]]
|
--]]
|
||||||
|
|
||||||
-- stop when we have no window to show
|
-- stop when we have no window to show
|
||||||
if not self._window_stack[1] and not self._run_forever then
|
if not self._window_stack[1] then
|
||||||
logger.info("no dialog left to show")
|
logger.info("no dialog left to show")
|
||||||
self:quit()
|
if self:_gated_quit() ~= false then
|
||||||
return nil
|
return nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
self:_repaint()
|
self:_repaint()
|
||||||
@@ -1446,8 +1475,6 @@ This is the main loop of the UI controller.
|
|||||||
It is intended to manage input events and delegate them to dialogs.
|
It is intended to manage input events and delegate them to dialogs.
|
||||||
--]]
|
--]]
|
||||||
function UIManager:run()
|
function UIManager:run()
|
||||||
self._running = true
|
|
||||||
|
|
||||||
-- Tell PowerD that we're ready
|
-- Tell PowerD that we're ready
|
||||||
Device:getPowerDevice():readyUI()
|
Device:getPowerDevice():readyUI()
|
||||||
|
|
||||||
@@ -1455,9 +1482,9 @@ function UIManager:run()
|
|||||||
-- currently there is no Turbo support for Windows
|
-- currently there is no Turbo support for Windows
|
||||||
-- use our own main loop
|
-- use our own main loop
|
||||||
if not self.looper then
|
if not self.looper then
|
||||||
while self._running do
|
repeat
|
||||||
self:handleInput()
|
self:handleInput()
|
||||||
end
|
until self._exit_code
|
||||||
else
|
else
|
||||||
self.looper:add_callback(function() self:handleInput() end)
|
self.looper:add_callback(function() self:handleInput() end)
|
||||||
self.looper:start()
|
self.looper:start()
|
||||||
@@ -1466,12 +1493,6 @@ function UIManager:run()
|
|||||||
return self._exit_code
|
return self._exit_code
|
||||||
end
|
end
|
||||||
|
|
||||||
-- run uimanager forever for testing purpose
|
|
||||||
function UIManager:runForever()
|
|
||||||
self._run_forever = true
|
|
||||||
return self:run()
|
|
||||||
end
|
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Executes all the operations of a suspension (i.e., sleep) request.
|
Executes all the operations of a suspension (i.e., sleep) request.
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ describe("AutoFrontlight widget tests", function()
|
|||||||
close()
|
close()
|
||||||
|
|
||||||
UIManager = require("ui/uimanager")
|
UIManager = require("ui/uimanager")
|
||||||
UIManager._run_forever = true
|
UIManager:setRunForeverMode()
|
||||||
|
|
||||||
requireBackgroundRunner()
|
requireBackgroundRunner()
|
||||||
class = dofile("plugins/autofrontlight.koplugin/main.lua")
|
class = dofile("plugins/autofrontlight.koplugin/main.lua")
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ describe("AutoSuspend", function()
|
|||||||
Device.input.waitEvent = function() end
|
Device.input.waitEvent = function() end
|
||||||
local UIManager = require("ui/uimanager")
|
local UIManager = require("ui/uimanager")
|
||||||
stub(UIManager, "suspend")
|
stub(UIManager, "suspend")
|
||||||
UIManager._run_forever = true
|
UIManager:setRunForeverMode()
|
||||||
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 10)
|
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 10)
|
||||||
require("mock_time"):install()
|
require("mock_time"):install()
|
||||||
-- Reset UIManager:getTime()
|
-- Reset UIManager:getTime()
|
||||||
@@ -71,7 +71,7 @@ describe("AutoSuspend", function()
|
|||||||
Device.input.waitEvent = function() end
|
Device.input.waitEvent = function() end
|
||||||
local UIManager = require("ui/uimanager")
|
local UIManager = require("ui/uimanager")
|
||||||
stub(UIManager, "poweroff_action")
|
stub(UIManager, "poweroff_action")
|
||||||
UIManager._run_forever = true
|
UIManager:setRunForeverMode()
|
||||||
G_reader_settings:saveSetting("autoshutdown_timeout_seconds", 10)
|
G_reader_settings:saveSetting("autoshutdown_timeout_seconds", 10)
|
||||||
require("mock_time"):install()
|
require("mock_time"):install()
|
||||||
-- Reset UIManager:getTime()
|
-- Reset UIManager:getTime()
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ describe("BackgroundRunner widget tests", function()
|
|||||||
MockTime = require("mock_time")
|
MockTime = require("mock_time")
|
||||||
MockTime:install()
|
MockTime:install()
|
||||||
UIManager = require("ui/uimanager")
|
UIManager = require("ui/uimanager")
|
||||||
UIManager._run_forever = true
|
UIManager:setRunForeverMode()
|
||||||
requireBackgroundRunner()
|
requireBackgroundRunner()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ describe("BackgroundTaskPlugin", function()
|
|||||||
MockTime:install()
|
MockTime:install()
|
||||||
local Device = require("device")
|
local Device = require("device")
|
||||||
Device.input.waitEvent = function() end
|
Device.input.waitEvent = function() end
|
||||||
UIManager._run_forever = true
|
UIManager:setRunForeverMode()
|
||||||
requireBackgroundRunner()
|
requireBackgroundRunner()
|
||||||
-- Monkey patch this method to notify BackgroundRunner
|
-- Monkey patch this method to notify BackgroundRunner
|
||||||
-- as it is not accessible to UIManager in these tests
|
-- as it is not accessible to UIManager in these tests
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ describe("HTTP client module #notest #nocov", function()
|
|||||||
url = url,
|
url = url,
|
||||||
}, response_callback)
|
}, response_callback)
|
||||||
end
|
end
|
||||||
UIManager:runForever()
|
UIManager:setRunForeverMode()
|
||||||
|
UIManager:run()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ describe("ReaderLink module", function()
|
|||||||
|
|
||||||
it("should jump to links in pdf page mode", function()
|
it("should jump to links in pdf page mode", function()
|
||||||
UIManager:quit()
|
UIManager:quit()
|
||||||
|
UIManager._exit_code = nil
|
||||||
local readerui = ReaderUI:new{
|
local readerui = ReaderUI:new{
|
||||||
dimen = Screen:getSize(),
|
dimen = Screen:getSize(),
|
||||||
document = DocumentRegistry:openDocument(sample_pdf),
|
document = DocumentRegistry:openDocument(sample_pdf),
|
||||||
@@ -44,6 +45,7 @@ describe("ReaderLink module", function()
|
|||||||
|
|
||||||
it("should jump to links in pdf scroll mode", function()
|
it("should jump to links in pdf scroll mode", function()
|
||||||
UIManager:quit()
|
UIManager:quit()
|
||||||
|
UIManager._exit_code = nil
|
||||||
local readerui = ReaderUI:new{
|
local readerui = ReaderUI:new{
|
||||||
dimen = Screen:getSize(),
|
dimen = Screen:getSize(),
|
||||||
document = DocumentRegistry:openDocument(sample_pdf),
|
document = DocumentRegistry:openDocument(sample_pdf),
|
||||||
@@ -78,6 +80,7 @@ describe("ReaderLink module", function()
|
|||||||
|
|
||||||
it("should be able to go back after link jump in pdf page mode", function()
|
it("should be able to go back after link jump in pdf page mode", function()
|
||||||
UIManager:quit()
|
UIManager:quit()
|
||||||
|
UIManager._exit_code = nil
|
||||||
local readerui = ReaderUI:new{
|
local readerui = ReaderUI:new{
|
||||||
dimen = Screen:getSize(),
|
dimen = Screen:getSize(),
|
||||||
document = DocumentRegistry:openDocument(sample_pdf),
|
document = DocumentRegistry:openDocument(sample_pdf),
|
||||||
@@ -96,6 +99,7 @@ describe("ReaderLink module", function()
|
|||||||
|
|
||||||
it("should be able to go back after link jump in pdf scroll mode", function()
|
it("should be able to go back after link jump in pdf scroll mode", function()
|
||||||
UIManager:quit()
|
UIManager:quit()
|
||||||
|
UIManager._exit_code = nil
|
||||||
local readerui = ReaderUI:new{
|
local readerui = ReaderUI:new{
|
||||||
dimen = Screen:getSize(),
|
dimen = Screen:getSize(),
|
||||||
document = DocumentRegistry:openDocument(sample_pdf),
|
document = DocumentRegistry:openDocument(sample_pdf),
|
||||||
@@ -116,6 +120,7 @@ describe("ReaderLink module", function()
|
|||||||
|
|
||||||
it("should be able to go back to the same position after link jump in pdf scroll mode", function()
|
it("should be able to go back to the same position after link jump in pdf scroll mode", function()
|
||||||
UIManager:quit()
|
UIManager:quit()
|
||||||
|
UIManager._exit_code = nil
|
||||||
local expected_page_states = {
|
local expected_page_states = {
|
||||||
{
|
{
|
||||||
gamma = 1,
|
gamma = 1,
|
||||||
|
|||||||
@@ -102,7 +102,8 @@ describe("Lua Spore modules with async http request #notest #nocov", function()
|
|||||||
client:enable("Format.JSON")
|
client:enable("Format.JSON")
|
||||||
client:enable("AsyncHTTP", {thread = co})
|
client:enable("AsyncHTTP", {thread = co})
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
UIManager:runForever()
|
UIManager:setRunForeverMode()
|
||||||
|
UIManager:run()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("should complete POST request", function()
|
it("should complete POST request", function()
|
||||||
@@ -117,6 +118,7 @@ describe("Lua Spore modules with async http request #notest #nocov", function()
|
|||||||
client:enable("Format.JSON")
|
client:enable("Format.JSON")
|
||||||
client:enable("AsyncHTTP", {thread = co})
|
client:enable("AsyncHTTP", {thread = co})
|
||||||
coroutine.resume(co)
|
coroutine.resume(co)
|
||||||
UIManager:runForever()
|
UIManager:setRunForeverMode()
|
||||||
|
UIManager:run()
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user