Skip to content

Commit

Permalink
DictQuickLookup: The window_list array needs to be a static class member
Browse files Browse the repository at this point in the history
Also, make references are actually dropped no matter how the widget is
closed by relying on onCloseWidget ;).

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

Fix: koreader#9586 (comment)
  • Loading branch information
NiLuJe committed Oct 8, 2022
1 parent 93ae341 commit ea12574
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
6 changes: 1 addition & 5 deletions frontend/apps/reader/modules/readerdictionary.lua
Expand Up @@ -62,7 +62,6 @@ end

local ReaderDictionary = WidgetContainer:extend{
data_dir = nil,
dict_window_list = nil, -- array
lookup_msg = _("Searching dictionary for:\n%1"),
}

Expand Down Expand Up @@ -100,7 +99,6 @@ local function getDictionaryFixHtmlFunc(path)
end

function ReaderDictionary:init()
self.dict_window_list = {}
self.disable_lookup_history = G_reader_settings:isTrue("disable_lookup_history")
self.dicts_order = G_reader_settings:readSetting("dicts_order", {})
self.dicts_disabled = G_reader_settings:readSetting("dicts_disabled", {})
Expand Down Expand Up @@ -967,9 +965,8 @@ end

function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_func)
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{
window_list = self.dict_window_list,
ui = self.ui,
highlight = self.highlight,
tweak_buttons_func = tweak_buttons_func,
Expand All @@ -993,7 +990,6 @@ function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_fun
self:onHtmlDictionaryLinkTapped(dictionary, html_link)
end,
}
table.insert(self.dict_window_list, self.dict_window)
if self.lookup_progress_msg then
-- If we have a lookup InfoMessage that ended up being displayed, make
-- it *not* refresh on close if it is hidden by our DictQuickLookup
Expand Down
11 changes: 6 additions & 5 deletions frontend/apps/reader/modules/readerwikipedia.lua
Expand Up @@ -2,6 +2,7 @@ local BD = require("ui/bidi")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage")
local DictQuickLookup = require("ui/widget/dictquicklookup")
local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog")
local KeyValuePage = require("ui/widget/keyvaluepage")
Expand Down Expand Up @@ -553,11 +554,11 @@ function ReaderWikipedia:getWikiLanguages(first_lang)
end
end
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
-- of the rotated out DictQuickLookup instance: trust it
update_wiki_languages_on_close = self.dict_window_list.rotated_update_wiki_languages_on_close
self.dict_window_list.rotated_update_wiki_languages_on_close = nil
update_wiki_languages_on_close = DictQuickLookup.rotated_update_wiki_languages_on_close
DictQuickLookup.rotated_update_wiki_languages_on_close = nil
else
-- 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)
Expand All @@ -567,8 +568,8 @@ function ReaderWikipedia:getWikiLanguages(first_lang)
-- from them) won't update it.
if is_first_lang then
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)
if self.dict_window_list[i].is_wiki then
for i = #DictQuickLookup.window_list-1, 1, -1 do -- (ignore the last one, which is the one calling this)
if DictQuickLookup.window_list[i].is_wiki then
-- Another upper Wikipedia result: only this one may update it
update_wiki_languages_on_close = false
break
Expand Down
43 changes: 32 additions & 11 deletions frontend/ui/widget/dictquicklookup.lua
Expand Up @@ -55,6 +55,11 @@ local DictQuickLookup = InputContainer:extend{
-- refresh_callback will be called before we trigger full refresh in onSwipe
refresh_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 = {
Expand Down Expand Up @@ -384,6 +389,9 @@ function DictQuickLookup:init()
callback = function()
self:onClose()
end,
hold_callback = function()
self:onHoldClose()
end,
},
},
}
Expand Down Expand Up @@ -471,7 +479,7 @@ function DictQuickLookup:init()
if self.is_wiki then
-- 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
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:onClose(true)
else
Expand All @@ -486,6 +494,9 @@ function DictQuickLookup:init()
callback = function()
self:onClose()
end,
hold_callback = function()
self:onHoldClose()
end,
},
},
}
Expand Down Expand Up @@ -698,6 +709,10 @@ function DictQuickLookup:init()
dimen = self.region,
self.movable,
}

-- We're a new window
table.insert(DictQuickLookup.window_list, self)

UIManager:setDirty(self, function()
return "partial", self.dict_frame.dimen
end)
Expand Down Expand Up @@ -900,6 +915,15 @@ function DictQuickLookup:onCloseWidget()
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
UIManager:setDirty(nil, function()
return "flashui", nil
Expand Down Expand Up @@ -1095,12 +1119,7 @@ end

function DictQuickLookup:onClose(no_clear)
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
-- except if we got no result for current language
if not self.results.no_result then
Expand All @@ -1119,8 +1138,10 @@ function DictQuickLookup:onClose(no_clear)
end

function DictQuickLookup:onHoldClose(no_clear)
while #self.window_list > 0 do
self.window_list[#self.window_list]:onClose(no_clear)
-- Pop the windows FILO
for i = #DictQuickLookup.window_list, 1, -1 do
local window = DictQuickLookup.window_list[i]
window:onClose(no_clear)
end
return true
end
Expand Down Expand Up @@ -1271,8 +1292,8 @@ function DictQuickLookup:lookupWikipedia(get_fullpage, word, is_sane, lang)
if not lang then
-- Use the lang of the current or nearest is_wiki DictQuickLookup.
-- Otherwise, first lang in ReaderWikipedia.wiki_languages will be used.
for i = #self.window_list, 1, -1 do
local window = self.window_list[i]
for i = #DictQuickLookup.window_list, 1, -1 do
local window = DictQuickLookup.window_list[i]
if window.is_wiki and window.lang then
lang = window.lang
break
Expand Down

0 comments on commit ea12574

Please sign in to comment.