Skip to content

Instantly share code, notes, and snippets.

@IntrovertedMage
Last active February 18, 2025 07:22
Show Gist options
  • Select an option

  • Save IntrovertedMage/34819fcd7870f20e357e5081a1d2f971 to your computer and use it in GitHub Desktop.

Select an option

Save IntrovertedMage/34819fcd7870f20e357e5081a1d2f971 to your computer and use it in GitHub Desktop.
--=====SETTINGS====================================================================
local maxEntries = 1000
local onlyShowPreferred = true
--=================================================================================
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
local ButtonDialog = require("ui/widget/buttondialog")
local Device = require("device")
local DictQuickLookup = require("ui/widget/dictquicklookup")
local Event = require("ui/event")
local UIManager = require("ui/uimanager")
local ffiUtil = require("ffi/util")
local logger = require("logger")
local time = require("ui/time")
local _ = require("gettext")
local Input = Device.input
ReaderDictionary.showDict = function(self, word, results, boxes, link, dict_close_callback)
if results and results[1] then
logger.dbg("showing quick lookup window", #DictQuickLookup.window_list+1, ":", word, results)
results = self:filterOutNonePreferred(results)
self.dict_window = DictQuickLookup:new{
ui = self.ui,
highlight = self.highlight,
dialog = self.dialog,
-- original lookup word
word = word,
-- selected link, if any
selected_link = link,
results = {unpack(results,1,math.min(#results,maxEntries))},
word_boxes = boxes,
preferred_dictionaries = self.preferred_dictionaries,
-- differentiate between dict and wiki
is_wiki = self.is_wiki,
refresh_callback = function()
if self.view then
-- update info in footer (time, battery, etc)
self.view.footer:onUpdateFooter()
end
end,
html_dictionary_link_tapped_callback = function(dictionary, html_link)
self:onHtmlDictionaryLinkTapped(dictionary, html_link)
end,
dict_close_callback = dict_close_callback,
}
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
-- to avoid refreshes competition and possible glitches
local msg_dimen = self.lookup_progress_msg:getVisibleArea()
if msg_dimen then -- not invisible
local dict_dimen = self.dict_window:getInitialVisibleArea()
if dict_dimen and dict_dimen:contains(msg_dimen) then
self.lookup_progress_msg.no_refresh_on_close = true
end
end
end
end
self:dismissLookupInfo()
if results and results[1] then
UIManager:show(self.dict_window)
if not results.lookup_cancelled and self._lookup_start_time
and (time.now() - self._lookup_start_time) > self.quick_dismiss_before_delay then
-- If the search took more than a few seconds to be done, discard
-- queued and upcoming input events to avoid a voluntary dismissal
-- (because the user felt the result would not come) to kill the
-- result that finally came and is about to be displayed
Input:inhibitInputUntil(true)
end
end
end
ReaderDictionary.filterOutNonePreferred = function(self,results)
if not self.preferred_dictionaries or not onlyShowPreferred or #self.preferred_dictionaries == 0 then
return results
end
local filteredResults = {}
for idx, result in ipairs(results) do
if self:isPreferredDictionary(result.dict) then
table.insert(filteredResults,result)
end
end
if #filteredResults ==0 then
return results
end
return filteredResults
end
ReaderDictionary.isPreferredDictionary = function(self,dict)
for idx, name in ipairs(self.preferred_dictionaries) do
if dict == name then
return true
end
end
return false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment