ReaderUI: Saner FM/RD lifecycle

* Ensure that going from one to the other tears down the former and
    its plugins before instantiating the latter and its plugins.

UIManager: Unify Event sending & broadcasting
  * Make the two behave the same way (walk the widget stack from top to
    bottom), and properly handle the window stack shrinking shrinking
    *and* growing.
    Previously, broadcasting happened bottom-to-top and didn't really
    handle the list shrinking/growing, while sending only handled the list
    shrinking by a single element, and hopefully that element being the one
    the event was just sent to.

These two items combined allowed us to optimize suboptimal
refresh behavior with Menu and other Menu classes when
opening/closing a document.
e.g., the "opening document" Notification is now properly regional,
and the "open last doc" option no longer flashes like a crazy person
anymore.

Plugins: Allow optimizing Menu refresh with custom menus, too.

Requires moving Menu's close_callback *after* onMenuSelect, which, eh,
probably makes sense, and is probably harmless in the grand scheme of
things.
This commit is contained in:
NiLuJe
2021-05-01 18:53:04 +02:00
parent 47cc7bb1a6
commit e7acec1526
23 changed files with 245 additions and 76 deletions

View File

@@ -480,6 +480,21 @@ function ReaderUI:showFileManager(file)
end
end
function ReaderUI:onShowingReader()
-- Allows us to optimize out a few useless refreshes in various CloseWidgets handlers...
self.tearing_down = true
self.dithered = nil
self:onClose()
end
-- Same as above, except we don't close it yet. Useful for plugins that need to close custom Menus before calling showReader.
function ReaderUI:onSetupShowReader()
self.tearing_down = true
self.dithered = nil
end
--- @note: Will sanely close existing FileManager/ReaderUI instance for you!
function ReaderUI:showReader(file, provider)
logger.dbg("show reader ui")
@@ -497,6 +512,10 @@ function ReaderUI:showReader(file, provider)
self:showFileManager(file)
return
end
-- We can now signal the existing ReaderUI/FileManager instances that it's time to go bye-bye...
UIManager:broadcastEvent(Event:new("ShowingReader"))
-- prevent crash due to incompatible bookmarks
--- @todo Split bookmarks from metadata and do per-engine in conversion.
provider = provider or DocumentRegistry:getProvider(file)
@@ -550,7 +569,7 @@ end
local _running_instance = nil
function ReaderUI:doShowReader(file, provider)
logger.info("opening file", file)
-- keep only one instance running
-- Keep only one instance running
if _running_instance then
_running_instance:onClose()
end
@@ -591,12 +610,17 @@ function ReaderUI:doShowReader(file, provider)
end
Device:notifyBookState(title, document)
UIManager:show(reader)
_running_instance = reader
-- This is mostly for the few callers that bypass the coroutine shenanigans and call doShowReader directly,
-- instead of showReader...
-- Otherwise, showReader will have taken care of that *before* instantiating a new RD,
-- in order to ensure a sane ordering of plugins teardown -> instantiation.
local FileManager = require("apps/filemanager/filemanager")
if FileManager.instance then
FileManager.instance:onClose()
end
UIManager:show(reader)
_running_instance = reader
end
function ReaderUI:_getRunningInstance()
@@ -756,6 +780,11 @@ end
function ReaderUI:reloadDocument(after_close_callback)
local file = self.document.file
local provider = getmetatable(self.document).__index
-- Mimic onShowingReader's refresh optimizations
self.tearing_down = true
self.dithered = nil
self:handleEvent(Event:new("CloseReaderMenu"))
self:handleEvent(Event:new("CloseConfigMenu"))
self.highlight:onClose() -- close highlight dialog if any
@@ -764,15 +793,22 @@ function ReaderUI:reloadDocument(after_close_callback)
-- allow caller to do stuff between close an re-open
after_close_callback(file, provider)
end
self:showReader(file, provider)
end
function ReaderUI:switchDocument(new_file)
if not new_file then return end
-- Mimic onShowingReader's refresh optimizations
self.tearing_down = true
self.dithered = nil
self:handleEvent(Event:new("CloseReaderMenu"))
self:handleEvent(Event:new("CloseConfigMenu"))
self.highlight:onClose() -- close highlight dialog if any
self:onClose(false)
self:showReader(new_file)
end