diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index f1c19e7e5..76e66ca36 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -825,6 +825,23 @@ function UIManager:unsetRunForeverMode() self._gated_quit = self.quit end +-- Ignore an empty window stack *once*; for startup w/ a missing last_file shenanigans... +function UIManager:runOnce() + -- We don't actually want to call self.quit, and we need to deal with a bit of trickery in there anyway... + self._gated_quit = function() + -- We need this set to break the loop in UIManager:run() + self._exit_code = 0 + -- And this is to break the loop in UIManager:handleInput() + return true + end + -- The idea being that we want to *return* from this run call, but *without* quitting. + -- NOTE: This implies that calling run multiple times across a single session *needs* to be safe. + self:run() + -- Restore standard behavior + self:unsetRunForeverMode() + self._exit_code = nil +end + --[[-- Transmits an @{ui.event.Event|Event} to active widgets, top to bottom. Stops at the first handler that returns `true`. diff --git a/reader.lua b/reader.lua index 39614b841..d7b0c4dc3 100755 --- a/reader.lua +++ b/reader.lua @@ -271,15 +271,11 @@ end local exit_code if file then local ReaderUI = require("apps/reader/readerui") - UIManager:nextTick(function() - ReaderUI:showReader(file) - end) + ReaderUI:showReader(file) exit_code = UIManager:run() elseif directory then local FileManager = require("apps/filemanager/filemanager") - UIManager:nextTick(function() - FileManager:showFiles(directory) - end) + FileManager:showFiles(directory) exit_code = UIManager:run() else local QuickStart = require("ui/quickstart") @@ -290,47 +286,38 @@ else if start_with == "last" and last_file and lfs.attributes(last_file, "mode") ~= "file" then UIManager:show(retryLastFile()) - -- no exit code as something else will be run after this. - UIManager:run() + -- We'll want to return from this without actually quitting, + -- so this is a slightly mangled UIManager:run() call to coerce the main loop into submission... + -- We'll call :run properly in either of the following branches once returning. + UIManager:runOnce() end if start_with == "last" and last_file then local ReaderUI = require("apps/reader/readerui") - UIManager:nextTick(function() - -- Instantiate RD - ReaderUI:showReader(last_file) - end) + -- Instantiate RD + ReaderUI:showReader(last_file) exit_code = UIManager:run() else local FileManager = require("apps/filemanager/filemanager") - local home_dir = - G_reader_settings:readSetting("home_dir") or Device.home_dir or lfs.currentdir() - UIManager:nextTick(function() - -- Instantiate FM - FileManager:showFiles(home_dir) - end) + local home_dir = G_reader_settings:readSetting("home_dir") or Device.home_dir or lfs.currentdir() + -- Instantiate FM + FileManager:showFiles(home_dir) -- Always open FM modules on top of filemanager, so closing 'em doesn't result in an exit -- because of an empty widget stack, and so they can interact with the FM instance as expected. if start_with == "history" then local FileManagerHistory = require("apps/filemanager/filemanagerhistory") - UIManager:nextTick(function() - FileManagerHistory:new{ - ui = FileManager.instance, - }:onShowHist() - end) + FileManagerHistory:new{ + ui = FileManager.instance, + }:onShowHist() elseif start_with == "favorites" then local FileManagerCollection = require("apps/filemanager/filemanagercollection") - UIManager:nextTick(function() - FileManagerCollection:new{ - ui = FileManager.instance, - }:onShowColl("favorites") - end) + FileManagerCollection:new{ + ui = FileManager.instance, + }:onShowColl("favorites") elseif start_with == "folder_shortcuts" then local FileManagerShortcuts = require("apps/filemanager/filemanagershortcuts") - UIManager:nextTick(function() - FileManagerShortcuts:new{ - ui = FileManager.instance, - }:onShowFolderShortcutsDialog() - end) + FileManagerShortcuts:new{ + ui = FileManager.instance, + }:onShowFolderShortcutsDialog() end exit_code = UIManager:run() end @@ -342,8 +329,10 @@ local function exitReader() local ko_exit = os.getenv("KO_EXIT_CODE") if ko_exit then local fo = io.open(ko_exit, "w+") - fo:write(tostring(exit_code)) - fo:close() + if fo then + fo:write(tostring(exit_code)) + fo:close() + end end local ReaderActivityIndicator =