DictQuickLookup: The window_list array needs to be a static class member

Also, make sure references are actually dropped,
no matter how the widget is closed, by relying on onCloseWidget ;).

Enable the "long-press-on-close" trick on the actual Close button,
too, instead of only on the title bar's cross.

Fix: https://github.com/koreader/koreader/pull/9586#issuecomment-1272332275
reviewable/pr9615/r2
NiLuJe 2 years ago
parent 93ae341823
commit a62f076e56

@ -62,7 +62,6 @@ end
local ReaderDictionary = WidgetContainer:extend{ local ReaderDictionary = WidgetContainer:extend{
data_dir = nil, data_dir = nil,
dict_window_list = nil, -- array
lookup_msg = _("Searching dictionary for:\n%1"), lookup_msg = _("Searching dictionary for:\n%1"),
} }
@ -100,7 +99,6 @@ local function getDictionaryFixHtmlFunc(path)
end end
function ReaderDictionary:init() function ReaderDictionary:init()
self.dict_window_list = {}
self.disable_lookup_history = G_reader_settings:isTrue("disable_lookup_history") self.disable_lookup_history = G_reader_settings:isTrue("disable_lookup_history")
self.dicts_order = G_reader_settings:readSetting("dicts_order", {}) self.dicts_order = G_reader_settings:readSetting("dicts_order", {})
self.dicts_disabled = G_reader_settings:readSetting("dicts_disabled", {}) self.dicts_disabled = G_reader_settings:readSetting("dicts_disabled", {})
@ -967,9 +965,8 @@ end
function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_func) function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_func)
if results and results[1] then if results and results[1] then
logger.dbg("showing quick lookup window", #self.dict_window_list+1, ":", word, results) logger.dbg("showing quick lookup window", #DictQuickLookup.window_list+1, ":", word, results)
self.dict_window = DictQuickLookup:new{ self.dict_window = DictQuickLookup:new{
window_list = self.dict_window_list,
ui = self.ui, ui = self.ui,
highlight = self.highlight, highlight = self.highlight,
tweak_buttons_func = tweak_buttons_func, tweak_buttons_func = tweak_buttons_func,
@ -993,7 +990,6 @@ function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_fun
self:onHtmlDictionaryLinkTapped(dictionary, html_link) self:onHtmlDictionaryLinkTapped(dictionary, html_link)
end, end,
} }
table.insert(self.dict_window_list, self.dict_window)
if self.lookup_progress_msg then if self.lookup_progress_msg then
-- If we have a lookup InfoMessage that ended up being displayed, make -- If we have a lookup InfoMessage that ended up being displayed, make
-- it *not* refresh on close if it is hidden by our DictQuickLookup -- it *not* refresh on close if it is hidden by our DictQuickLookup

@ -2,6 +2,7 @@ local BD = require("ui/bidi")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle") local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local DictQuickLookup = require("ui/widget/dictquicklookup")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
local KeyValuePage = require("ui/widget/keyvaluepage") local KeyValuePage = require("ui/widget/keyvaluepage")
@ -553,11 +554,11 @@ function ReaderWikipedia:getWikiLanguages(first_lang)
end end
end end
local update_wiki_languages_on_close = false local update_wiki_languages_on_close = false
if self.dict_window_list.rotated_update_wiki_languages_on_close ~= nil then if DictQuickLookup.rotated_update_wiki_languages_on_close ~= nil then
-- Flag set by DictQuickLookup when rotating, forwarding the flag -- Flag set by DictQuickLookup when rotating, forwarding the flag
-- of the rotated out DictQuickLookup instance: trust it -- of the rotated out DictQuickLookup instance: trust it
update_wiki_languages_on_close = self.dict_window_list.rotated_update_wiki_languages_on_close update_wiki_languages_on_close = DictQuickLookup.rotated_update_wiki_languages_on_close
self.dict_window_list.rotated_update_wiki_languages_on_close = nil DictQuickLookup.rotated_update_wiki_languages_on_close = nil
else else
-- Not a rotation. Only if it's the first request with the current -- Not a rotation. Only if it's the first request with the current
-- first language, we will have it (and any lang rotation from it) -- first language, we will have it (and any lang rotation from it)
@ -567,8 +568,8 @@ function ReaderWikipedia:getWikiLanguages(first_lang)
-- from them) won't update it. -- from them) won't update it.
if is_first_lang then if is_first_lang then
update_wiki_languages_on_close = true update_wiki_languages_on_close = true
for i=1, #self.dict_window_list-1 do -- (ignore the last one, which is the one calling this) for i = #DictQuickLookup.window_list-1, 1, -1 do -- (ignore the last one, which is the one calling this)
if self.dict_window_list[i].is_wiki then if DictQuickLookup.window_list[i].is_wiki then
-- Another upper Wikipedia result: only this one may update it -- Another upper Wikipedia result: only this one may update it
update_wiki_languages_on_close = false update_wiki_languages_on_close = false
break break

@ -55,6 +55,11 @@ local DictQuickLookup = InputContainer:extend{
-- refresh_callback will be called before we trigger full refresh in onSwipe -- refresh_callback will be called before we trigger full refresh in onSwipe
refresh_callback = nil, refresh_callback = nil,
html_dictionary_link_tapped_callback = nil, html_dictionary_link_tapped_callback = nil,
-- Static class member, holds a ref to the currently opened widgets (in instantiation order).
window_list = {},
-- Static class member, used by ReaderWiktionary to communicate state from a closed widget to the next opened one.
rotated_update_wiki_languages_on_close = nil,
} }
local highlight_strings = { local highlight_strings = {
@ -384,6 +389,9 @@ function DictQuickLookup:init()
callback = function() callback = function()
self:onClose() self:onClose()
end, end,
hold_callback = function()
self:onHoldClose()
end,
}, },
}, },
} }
@ -471,7 +479,7 @@ function DictQuickLookup:init()
if self.is_wiki then if self.is_wiki then
-- We're rotating: forward this flag from the one we're closing so -- We're rotating: forward this flag from the one we're closing so
-- that ReaderWikipedia can give it to the one we'll be showing -- that ReaderWikipedia can give it to the one we'll be showing
self.window_list.rotated_update_wiki_languages_on_close = self.update_wiki_languages_on_close DictQuickLookup.rotated_update_wiki_languages_on_close = self.update_wiki_languages_on_close
self:lookupWikipedia(false, nil, nil, self.wiki_languages[2]) self:lookupWikipedia(false, nil, nil, self.wiki_languages[2])
self:onClose(true) self:onClose(true)
else else
@ -486,6 +494,9 @@ function DictQuickLookup:init()
callback = function() callback = function()
self:onClose() self:onClose()
end, end,
hold_callback = function()
self:onHoldClose()
end,
}, },
}, },
} }
@ -698,6 +709,10 @@ function DictQuickLookup:init()
dimen = self.region, dimen = self.region,
self.movable, self.movable,
} }
-- We're a new window
table.insert(DictQuickLookup.window_list, self)
UIManager:setDirty(self, function() UIManager:setDirty(self, function()
return "partial", self.dict_frame.dimen return "partial", self.dict_frame.dimen
end) end)
@ -900,6 +915,15 @@ function DictQuickLookup:onCloseWidget()
end end
end end
-- Drop our ref from the static class member
for i = #DictQuickLookup.window_list, 1, -1 do
local window = DictQuickLookup.window_list[i]
-- We should only find a single match, but, better safe than sorry...
if window == self then
table.remove(DictQuickLookup.window_list, i)
end
end
-- NOTE: Drop region to make it a full-screen flash -- NOTE: Drop region to make it a full-screen flash
UIManager:setDirty(nil, function() UIManager:setDirty(nil, function()
return "flashui", nil return "flashui", nil
@ -1095,12 +1119,7 @@ end
function DictQuickLookup:onClose(no_clear) function DictQuickLookup:onClose(no_clear)
UIManager:close(self) UIManager:close(self)
for i = #self.window_list, 1, -1 do
local window = self.window_list[i]
if window == self then
table.remove(self.window_list, i)
end
end
if self.update_wiki_languages_on_close then if self.update_wiki_languages_on_close then
-- except if we got no result for current language -- except if we got no result for current language
if not self.results.no_result then if not self.results.no_result then
@ -1119,8 +1138,10 @@ function DictQuickLookup:onClose(no_clear)
end end
function DictQuickLookup:onHoldClose(no_clear) function DictQuickLookup:onHoldClose(no_clear)
while #self.window_list > 0 do -- Pop the windows FILO
self.window_list[#self.window_list]:onClose(no_clear) for i = #DictQuickLookup.window_list, 1, -1 do
local window = DictQuickLookup.window_list[i]
window:onClose(no_clear)
end end
return true return true
end end
@ -1271,8 +1292,8 @@ function DictQuickLookup:lookupWikipedia(get_fullpage, word, is_sane, lang)
if not lang then if not lang then
-- Use the lang of the current or nearest is_wiki DictQuickLookup. -- Use the lang of the current or nearest is_wiki DictQuickLookup.
-- Otherwise, first lang in ReaderWikipedia.wiki_languages will be used. -- Otherwise, first lang in ReaderWikipedia.wiki_languages will be used.
for i = #self.window_list, 1, -1 do for i = #DictQuickLookup.window_list, 1, -1 do
local window = self.window_list[i] local window = DictQuickLookup.window_list[i]
if window.is_wiki and window.lang then if window.is_wiki and window.lang then
lang = window.lang lang = window.lang
break break

Loading…
Cancel
Save