From dee1c902ec25cfea19c44836816b40261c408404 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Mon, 3 Jul 2023 17:43:13 +0300 Subject: [PATCH] ReaderUI: minor optimization (#10634) --- frontend/apps/filemanager/filemanager.lua | 1 - frontend/apps/reader/readerui.lua | 19 +++---------------- frontend/ui/screensaver.lua | 8 ++++---- frontend/ui/widget/menu.lua | 4 ++-- frontend/ui/widget/touchmenu.lua | 4 ++-- spec/unit/device_spec.lua | 8 ++++---- spec/unit/readerui_spec.lua | 2 +- 7 files changed, 16 insertions(+), 30 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index d2bef827b..939f181d8 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -451,7 +451,6 @@ function FileManager:init() self:initGesListener() self:handleEvent(Event:new("SetDimensions", self.dimen)) - -- NOTE: ReaderUI has a _getRunningInstance method for this, because it used to store the instance reference in a private module variable. if FileManager.instance == nil then logger.dbg("Spinning up new FileManager instance", tostring(self)) else diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 4d5bbac70..b3a131d9e 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -656,7 +656,7 @@ function ReaderUI:doShowReader(file, provider, seamless) document = document, } - local title = reader.document:getProps().title + local title = reader.doc_settings:readSetting("doc_props").title if title ~= "" then Screen:setWindowTitle(title) @@ -678,13 +678,6 @@ function ReaderUI:doShowReader(file, provider, seamless) UIManager:show(reader, seamless and "ui" or "full") end --- NOTE: The instance reference used to be stored in a private module variable, hence the getter method. --- We've since aligned behavior with FileManager, which uses a class member instead, --- but kept the function to avoid changing existing code. -function ReaderUI:_getRunningInstance() - return ReaderUI.instance -end - function ReaderUI:unlockDocumentWithPassword(document, try_again) logger.dbg("show input password dialog") self.password_dialog = InputDialog:new{ @@ -695,7 +688,6 @@ function ReaderUI:unlockDocumentWithPassword(document, try_again) { text = _("Cancel"), id = "close", - enabled = true, callback = function() self:closeDialog() coroutine.resume(self._coroutine) @@ -703,7 +695,6 @@ function ReaderUI:unlockDocumentWithPassword(document, try_again) }, { text = _("OK"), - enabled = true, callback = function() local success = self:onVerifyPassword(document) self:closeDialog() @@ -767,7 +758,7 @@ function ReaderUI:notifyCloseDocument() self:closeDocument() else UIManager:show(ConfirmBox:new{ - text = _("Write highlights into this PDF??"), + text = _("Write highlights into this PDF?"), ok_text = _("Write"), dismissable = false, ok_callback = function() @@ -889,11 +880,7 @@ function ReaderUI:onOpenLastDoc() end function ReaderUI:getCurrentPage() - if self.document.info.has_pages then - return self.paging.current_page - else - return self.document:getCurrentPage() - end + return self.paging and self.paging.current_page or self.document:getCurrentPage() end return ReaderUI diff --git a/frontend/ui/screensaver.lua b/frontend/ui/screensaver.lua index 069cb0463..bfa63d885 100644 --- a/frontend/ui/screensaver.lua +++ b/frontend/ui/screensaver.lua @@ -156,7 +156,7 @@ function Screensaver:expandSpecial(message, fallback) local batt_lvl = _("N/A") local ReaderUI = require("apps/reader/readerui") - local ui = ReaderUI:_getRunningInstance() + local ui = ReaderUI.instance if ui and ui.document then -- If we have a ReaderUI instance, use it. local doc = ui.document @@ -372,7 +372,7 @@ end function Screensaver:isExcluded() local ReaderUI = require("apps/reader/readerui") - local ui = ReaderUI:_getRunningInstance() + local ui = ReaderUI.instance if ui and ui.doc_settings then local doc_settings = ui.doc_settings return doc_settings:isTrue("exclude_screensaver") @@ -508,7 +508,7 @@ function Screensaver:setup(event, event_message) -- Check lastfile and setup the requested mode's resources, or a fallback mode if the required resources are unavailable. local ReaderUI = require("apps/reader/readerui") - local ui = ReaderUI:_getRunningInstance() + local ui = ReaderUI.instance local lastfile = G_reader_settings:readSetting("lastfile") if self.screensaver_type == "document_cover" then -- Set lastfile to the document of which we want to show the cover. @@ -639,7 +639,7 @@ function Screensaver:show() } elseif self.screensaver_type == "bookstatus" then local ReaderUI = require("apps/reader/readerui") - local ui = ReaderUI:_getRunningInstance() + local ui = ReaderUI.instance local doc = ui.document local doc_settings = ui.doc_settings widget = BookStatusWidget:new{ diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index b8722c6f4..14b34e814 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -960,8 +960,8 @@ function Menu:onCloseWidget() -- Don't do anything if we're in the process of tearing down FM or RD, or if we don't actually have a live instance of 'em... local FileManager = require("apps/filemanager/filemanager") local ReaderUI = require("apps/reader/readerui") - local reader_ui = ReaderUI:_getRunningInstance() - if (FileManager.instance and not FileManager.instance.tearing_down) or (reader_ui and not reader_ui.tearing_down) then + if (FileManager.instance and not FileManager.instance.tearing_down) + or (ReaderUI.instance and not ReaderUI.instance.tearing_down) then UIManager:setDirty(nil, "ui") end end diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index b16265d19..dea9bb39c 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -649,8 +649,8 @@ function TouchMenu:onCloseWidget() -- Don't do anything when we're switching between the two, or if we don't actually have a live instance of 'em... local FileManager = require("apps/filemanager/filemanager") local ReaderUI = require("apps/reader/readerui") - local reader_ui = ReaderUI:_getRunningInstance() - if (FileManager.instance and not FileManager.instance.tearing_down) or (reader_ui and not reader_ui.tearing_down) then + if (FileManager.instance and not FileManager.instance.tearing_down) + or (ReaderUI.instance and not ReaderUI.instance.tearing_down) then UIManager:setDirty(nil, "flashui") end end diff --git a/spec/unit/device_spec.lua b/spec/unit/device_spec.lua index 020728a2f..babe2db43 100644 --- a/spec/unit/device_spec.lua +++ b/spec/unit/device_spec.lua @@ -367,7 +367,7 @@ describe("device module", function() local sample_pdf = "spec/front/unit/data/tall.pdf" local ReaderUI = require("apps/reader/readerui") ReaderUI:doShowReader(sample_pdf) - local readerui = ReaderUI._getRunningInstance() + local readerui = ReaderUI.instance stub(readerui, "onFlushSettings") UIManager.event_handlers.PowerPress() UIManager.event_handlers.PowerRelease() @@ -409,7 +409,7 @@ describe("device module", function() local sample_pdf = "spec/front/unit/data/tall.pdf" local ReaderUI = require("apps/reader/readerui") ReaderUI:doShowReader(sample_pdf) - local readerui = ReaderUI._getRunningInstance() + local readerui = ReaderUI.instance stub(readerui, "onFlushSettings") UIManager.event_handlers.PowerPress() UIManager.event_handlers.PowerRelease() @@ -457,7 +457,7 @@ describe("device module", function() local sample_pdf = "spec/front/unit/data/tall.pdf" local ReaderUI = require("apps/reader/readerui") ReaderUI:doShowReader(sample_pdf) - local readerui = ReaderUI._getRunningInstance() + local readerui = ReaderUI.instance stub(readerui, "onFlushSettings") UIManager.event_handlers.PowerPress() UIManager.event_handlers.PowerRelease() @@ -484,7 +484,7 @@ describe("device module", function() local sample_pdf = "spec/front/unit/data/tall.pdf" local ReaderUI = require("apps/reader/readerui") ReaderUI:doShowReader(sample_pdf) - local readerui = ReaderUI._getRunningInstance() + local readerui = ReaderUI.instance stub(readerui, "onFlushSettings") -- UIManager.event_handlers.PowerPress() -- We only fake a Release event on the Emu UIManager.event_handlers.PowerRelease() diff --git a/spec/unit/readerui_spec.lua b/spec/unit/readerui_spec.lua index f88c7794d..ccb7e0077 100644 --- a/spec/unit/readerui_spec.lua +++ b/spec/unit/readerui_spec.lua @@ -44,7 +44,7 @@ describe("Readerui module", function() end) it("should not reset ReaderUI.instance by mistake", function() ReaderUI:doShowReader(sample_epub) -- spins up a new, sane instance - local new_readerui = ReaderUI:_getRunningInstance() + local new_readerui = ReaderUI.instance assert.is.truthy(new_readerui.document) -- This *will* trip: -- * A pair of ReaderUI instance mimsatch warnings (on open/close) because it bypasses the safety of doShowReader!