[HtmlBoxWidget, DictQuickLookup] add capability to search within dictionary definitions (#15001)

Adds in-definition search to DictQuickLookup relaying on MuPDF's word-matching implementation, uses tap handling for navigation by allowing temporary disable/restore of Tap events.
This commit is contained in:
David authored and GitHub committed 2026-03-21 13:29:45 +00:00
1 parent 9e44aa5cf4
commit 80cb27dbf5
3 files changed
+318 -55

No files matched your search

+167 -50
View File
@@ -54,6 +54,7 @@ local DictQuickLookup = InputContainer:extend{
dict_index = 1,
width = nil,
height = nil,
in_definition_search = false, -- search on an entry's definition
nt_text_selector_indicator = nil, -- crosshairs for text selection on non-touch devices
-- sboxes containing highlighted text, quick lookup window tries to not hide the word
word_boxes = nil,
@@ -792,7 +793,7 @@ function DictQuickLookup:registerKeyEvents()
if Device:hasKeys() then
self.key_events.ReadPrevResult = { { Input.group.PgBack } }
self.key_events.ReadNextResult = { { Input.group.PgFwd } }
self.key_events.Close = { { Input.group.Back } }
self.key_events.CloseWithKeys = { { Input.group.Back } }
self.key_events.MenuKeyPress = { { "Menu" } }
if Device:hasScreenKB() or Device:hasKeyboard() then
local modifier = Device:hasScreenKB() and "ScreenKB" or "Shift"
@@ -802,8 +803,8 @@ function DictQuickLookup:registerKeyEvents()
self.key_events.TextSelectorModifierPress = { { modifier, "Press" } }
self.key_events.StartOrUpTextSelectorIndicator = { { modifier, "Up" }, event = "StartOrMoveTextSelectorIndicator", args = { 0, -1, true } }
self.key_events.StartOrDownTextSelectorIndicator = { { modifier, "Down" }, event = "StartOrMoveTextSelectorIndicator", args = { 0, 1, true } }
self.key_events.FastLeftTextSelectorIndicator = { { modifier, "Left" }, event = "MoveTextSelectorIndicator", args = { -1, 0, true } }
self.key_events.FastRightTextSelectorIndicator = { { modifier, "Right" }, event = "MoveTextSelectorIndicator", args = { 1, 0, true } }
self.key_events.FastLeftTextSelectorIndicator = { { modifier, "Left" }, event = "FindInTextOrMoveSelectorIndicator", args = { -1, 0, true } }
self.key_events.FastRightTextSelectorIndicator = { { modifier, "Right" }, event = "FindInTextOrMoveSelectorIndicator", args = { 1, 0, true } }
if Device:hasKeyboard() then
self.key_events.LookupInputWordClear = { { Input.group.Alphabet }, event = "LookupInputWord" }
-- We need to concat here so that the 'del' event press, which propagates to inputText (desirable for previous key_event,
@@ -899,6 +900,10 @@ function DictQuickLookup:_instantiateScrollWidget()
html_link_tapped_callback = function(link)
self.html_dictionary_link_tapped_callback(self.dictionary, link)
end,
on_clear_search = function()
self.in_definition_search = false
self.shw_widget:setTapScrollEnabled(true)
end,
-- We need to override the widget's paintTo method to draw our indicator
paintTo = self.allow_key_text_selection and function(widget, bb, x, y)
-- Call original paintTo from ScrollHtmlWidget
@@ -927,6 +932,10 @@ function DictQuickLookup:_instantiateScrollWidget()
image_alt_face = self.image_alt_face,
images = self.images,
highlight_text_selection = true,
-- on_clear_search = function()
-- self.in_definition_search = false
-- self.stw_widget:setTapScrollEnabled(true)
-- end,
-- We need to override the widget's paintTo method to draw our indicator
paintTo = self.allow_key_text_selection and function(widget, bb, x, y)
-- Call original paintTo from ScrollTextWidget
@@ -1161,6 +1170,12 @@ function DictQuickLookup:changeDictionary(index, skip_update)
-- when we're done.
self.images_cleanup_needed = true
end
if self.in_definition_search then
local _, content_widget = self:_getScrollAndContentWidgets()
if content_widget and content_widget.clearSearch then
content_widget:clearSearch() -- self.in_definition_search gets set false here
end
end
if self.is_wiki_fullpage then
self.displayword = self.lookupword
self.displaynb = nil
@@ -1270,10 +1285,20 @@ function DictQuickLookup:onTap(arg, ges_ev)
-- Allow for changing dict with tap (tap event will be first
-- processed for scrolling definition by ScrollTextWidget, which
-- will pop it up for us here when it can't scroll anymore).
-- This allow for continuous reading of results' definitions with tap.
-- This allows for continuous reading of results' definitions with a tap.
-- Note: During in_definition_search, ScrollWidget returns false, allowing
-- to use tap to navigate between matches in the current definition.
if BD.flipIfMirroredUILayout(ges_ev.pos.x < Screen:getWidth()/2) then
if self.in_definition_search then
self:findInDefinitionNextOrPreviousPage(-1)
return true
end
self:onReadPrevResult()
else
if self.in_definition_search then
self:findInDefinitionNextOrPreviousPage(1)
return true
end
self:onReadNextResult()
end
end
@@ -1281,11 +1306,6 @@ function DictQuickLookup:onTap(arg, ges_ev)
end
function DictQuickLookup:onClose(no_clear)
if self.allow_key_text_selection and self.nt_text_selector_indicator then
-- If we're in text selection mode, stop it
self:onStopTextSelectorIndicator(true)
return true
end
for menu, _ in pairs(self.menu_opened) do
UIManager:close(menu)
end
@@ -1318,6 +1338,23 @@ function DictQuickLookup:onClose(no_clear)
return true
end
function DictQuickLookup:onCloseWithKeys(no_clear)
if self.in_definition_search then
local _, content_widget = self:_getScrollAndContentWidgets()
if content_widget and content_widget.clearSearch then
content_widget:clearSearch(true)
end
return true
end
if self.allow_key_text_selection and self.nt_text_selector_indicator then
-- If we're in text selection mode, stop it
self:onStopTextSelectorIndicator(true)
return true
end
self:onClose(no_clear)
return true
end
function DictQuickLookup:onHoldClose(no_clear)
-- Pop the windows FILO
for i = #DictQuickLookup.window_list, 1, -1 do
@@ -1413,7 +1450,56 @@ function DictQuickLookup:onForwardingPanRelease(arg, ges)
return self.movable:onMovablePanRelease(arg, ges)
end
function DictQuickLookup:findInDefinitionNextOrPreviousPage(direction)
if not self.in_definition_search then return false end
local scroll_widget, content_widget = self:_getScrollAndContentWidgets()
if content_widget and content_widget.findTextNextPage then
content_widget:findTextNextPage(direction)
if self.is_html then
scroll_widget:_updateScrollBar()
UIManager:setDirty(scroll_widget, function() return "partial", scroll_widget.dimen end)
else
scroll_widget:updateScrollBar()
end
return true
end
return false
end
function DictQuickLookup:searchInDefinition(text)
if not text then return end
local scroll_widget, content_widget = self:_getScrollAndContentWidgets()
local found = content_widget:findText(text)
if found then
self.in_definition_search = true
scroll_widget:setTapScrollEnabled(false)
if self.is_html then
scroll_widget:_updateScrollBar()
UIManager:setDirty(scroll_widget, function() return "partial", scroll_widget.dimen end)
else
scroll_widget:updateScrollBar()
end
else
local InfoMessage = require("ui/widget/infomessage")
UIManager:show(InfoMessage:new{
text = T(_("No matches for '%1' were found."), text),
timeout = 2,
})
if self.in_definition_search then
content_widget:clearSearch(true)
end
end
end
function DictQuickLookup:onLookupInputWord(hint)
if self.allow_key_text_selection and self.nt_text_selector_indicator then
self:onStopTextSelectorIndicator(true)
end
self:lookupInputWord(hint)
end
function DictQuickLookup:lookupInputWord(hint)
local buttons = {
{
{
@@ -1439,6 +1525,22 @@ function DictQuickLookup:onLookupInputWord(hint)
end,
},
},
{
{
text = _("Find in definition"),
enabled_func = function()
return self.is_html and self.shw_widget
end,
callback = function()
local text = self.input_dialog:getInputText()
if text ~= "" and not text:match("^%s*$") then
UIManager:close(self.input_dialog)
self:searchInDefinition(text)
end
end,
},
-- 'Search with preset' will be inserted here
},
{
{
text = _("Cancel"),
@@ -1463,41 +1565,39 @@ function DictQuickLookup:onLookupInputWord(hint)
}
local preset_names = Presets.getPresets(self.ui.dictionary.preset_obj)
if preset_names and #preset_names > 0 then
table.insert(buttons, 2, {
{
text = _("Search with preset"),
callback = function()
local text = self.input_dialog:getInputText()
if text == "" or text:match("^%s*$") then return end
local current_dict_state = self.ui.dictionary:buildPreset()
local button_dialog, dialog_buttons = nil, {} -- CI won't like it if we call it buttons :( so dialog_buttons
for _, preset_name in ipairs(preset_names) do
table.insert(dialog_buttons, {
{
align = "left",
text = preset_name,
callback = function()
self.ui.dictionary:loadPreset(self.ui.dictionary.preset_obj.presets[preset_name], true)
UIManager:close(button_dialog)
UIManager:close(self.input_dialog)
self.ui:handleEvent(Event:new("LookupWord", text, true, nil, nil, nil,
function()
-- Restore original preset _after_ lookup is complete
self.ui.dictionary:loadPreset(current_dict_state, true)
end
))
end
}
})
end
button_dialog = ButtonDialog:new{
buttons = dialog_buttons,
shrink_unneeded_width = true,
}
self.input_dialog:onCloseKeyboard()
UIManager:show(button_dialog)
end,
}
table.insert(buttons[2], {
text = _("Search with preset"),
callback = function()
local text = self.input_dialog:getInputText()
if text == "" or text:match("^%s*$") then return end
local current_dict_state = self.ui.dictionary:buildPreset()
local button_dialog, dialog_buttons = nil, {} -- CI won't like it if we call it buttons :( so dialog_buttons
for _, preset_name in ipairs(preset_names) do
table.insert(dialog_buttons, {
{
align = "left",
text = preset_name,
callback = function()
self.ui.dictionary:loadPreset(self.ui.dictionary.preset_obj.presets[preset_name], true)
UIManager:close(button_dialog)
UIManager:close(self.input_dialog)
self.ui:handleEvent(Event:new("LookupWord", text, true, nil, nil, nil,
function()
-- Restore original preset _after_ lookup is complete
self.ui.dictionary:loadPreset(current_dict_state, true)
end
))
end
}
})
end
button_dialog = ButtonDialog:new{
buttons = dialog_buttons,
shrink_unneeded_width = true,
}
self.input_dialog:onCloseKeyboard()
UIManager:show(button_dialog)
end,
})
end
self.input_dialog = InputDialog:new{
@@ -1952,7 +2052,7 @@ function DictQuickLookup:onMoveTextSelectorIndicator(args)
-- Update widget state
self.nt_text_selector_indicator = rect
if self._text_selection_started then
local selection_widget = self:_getSelectionWidget(self)
local _, selection_widget = self:_getScrollAndContentWidgets()
if selection_widget then
selection_widget:onHoldPanText(nil, self:_createTextSelectionGesture("hold_pan"))
end
@@ -1971,7 +2071,7 @@ end
]]
function DictQuickLookup:onTextSelectorPress()
if not self.nt_text_selector_indicator then return false end
local selection_widget = self:_getSelectionWidget(self)
local _, selection_widget = self:_getScrollAndContentWidgets()
if not selection_widget then self:onStopTextSelectorIndicator() return end
if not self._text_selection_started then
-- start text selection on first press
@@ -2001,7 +2101,7 @@ end
function DictQuickLookup:onTextSelectorModifierPress()
if not self.nt_text_selector_indicator then return false end
local selection_widget = self:_getSelectionWidget(self)
local _, selection_widget = self:_getScrollAndContentWidgets()
if not selection_widget then return false end
-- If we have an active text selection (started via onTextSelectorPress), Mod+Press switches domain (Dict <-> Wiki)
if self._text_selection_started then
@@ -2027,6 +2127,20 @@ end
function DictQuickLookup:onStartOrMoveTextSelectorIndicator(args)
if not self.nt_text_selector_indicator then
self:onStartTextSelectorIndicator()
if self.in_definition_search then
local _, content_widget = self:_getScrollAndContentWidgets()
content_widget:clearSearch(true)
end
else
self:onMoveTextSelectorIndicator(args)
end
return true
end
function DictQuickLookup:onFindInTextOrMoveSelectorIndicator(args)
if self.in_definition_search then
local direction = unpack(args)
self:findInDefinitionNextOrPreviousPage(direction)
else
self:onMoveTextSelectorIndicator(args)
end
@@ -2046,9 +2160,12 @@ function DictQuickLookup:lookupDictionaryOrWikipedia(selected_text, switch_domai
return true
end
-- helper function to get the actual widget that handles text selection
function DictQuickLookup:_getSelectionWidget(instance)
return instance.is_html and instance.text_widget.htmlbox_widget or instance.text_widget.text_widget
-- helper function to get scroll widget and inner widget (htmlbox or text) based on current dictionary type
function DictQuickLookup:_getScrollAndContentWidgets()
local scroll_widget = self.is_html and self.shw_widget or self.stw_widget
if not scroll_widget then return nil, nil end
local content_widget = scroll_widget.htmlbox_widget or scroll_widget.text_widget
return scroll_widget, content_widget
end
function DictQuickLookup:_createTextSelectionGesture(gesture)
+144 -5
View File
@@ -194,6 +194,9 @@ local HtmlBoxWidget = InputContainer:extend{
highlight_rects = nil,
highlight_text = nil,
highlight_clear_and_redraw_action = nil,
search_term = nil, -- string (from InputText) to search within the HTML dictionary content.
on_clear_search = nil, -- callback, set by parent if needed
}
function HtmlBoxWidget:init()
@@ -376,6 +379,7 @@ function HtmlBoxWidget:onHoldStartText(_, ges)
self.hold_end_pos = self.hold_start_pos
self.highlight_rects = nil
self.highlight_text = nil
self:clearSearch()
if not self.hold_start_pos then
return false -- let event be processed by other widgets
@@ -469,12 +473,17 @@ function HtmlBoxWidget:onTapText(arg, ges)
end
function HtmlBoxWidget:setPageNumber(page_number)
if page_number == self.page_number then
return
if page_number ~= self.page_number then
self.page_number = page_number
self.page_boxes = nil
if not self._search_navigating then
self._match_page_index = nil -- resync index on next navigation
end
self:clearHighlight()
end
if self.search_term then
self:_highlightSearchOnCurrentPage()
end
self.page_number = page_number
self.page_boxes = nil
self:clearHighlight()
end
-- Returns true if the highlight has changed.
@@ -562,4 +571,134 @@ function HtmlBoxWidget:unscheduleClearHighlightAndRedraw()
end
end
function HtmlBoxWidget:findText(text)
if not self.document or not text then return false end
self.search_term = text
local start_page = self.page_number
local num_pages = self.page_count
self._search_index = {}
local first_match_page = nil
-- Search from current page onwards, wrapping around
for i = 0, num_pages - 1 do
local page_idx = start_page + i
if page_idx > num_pages then
page_idx = page_idx - num_pages
end
local match_rects = self:_findMatchesOnPage(page_idx, text)
if match_rects then
self._search_index[page_idx] = match_rects
if not first_match_page then
first_match_page = page_idx
end
end
end
-- Build sorted list from index keys for navigation
self._match_page_list = {}
for page_num in pairs(self._search_index) do
table.insert(self._match_page_list, page_num)
end
table.sort(self._match_page_list)
if first_match_page then
self:setPageNumber(first_match_page)
return true
end
return false
end
function HtmlBoxWidget:_findMatchesOnPage(page_idx, search_text)
local page = self.document:openPage(page_idx)
if not page then return nil end
local quads = page:searchPageText(search_text)
page:close()
if not quads or #quads == 0 then return nil end
local match_rects = {}
for _, quad in ipairs(quads) do
-- Compute bounding box from the 4 quad corners
local x0 = math.min(quad.ul_x, quad.ll_x)
local y0 = math.min(quad.ul_y, quad.ur_y)
local x1 = math.max(quad.ur_x, quad.lr_x)
local y1 = math.max(quad.ll_y, quad.lr_y)
local rect = Geom:new{
x = x0,
y = y0,
w = x1 - x0,
h = y1 - y0,
}
table.insert(match_rects, rect)
end
return match_rects
end
-- Navigate to next/previous page with search matches
function HtmlBoxWidget:findTextNextPage(direction)
local list = self._match_page_list
local count = list and #list or 0
if count == 0 then return false end
if count == 1 and self.page_number == list[1] then return true end
-- Resolve index if missing or desynced
if not self._match_page_index then
if not self._match_page_index then
if direction > 0 then
local idx = util.bsearch_right(list, self.page_number) - 1
if idx < 1 then idx = count end
self._match_page_index = idx
else
local idx = util.bsearch_left(list, self.page_number)
if idx > count then idx = 1 end
self._match_page_index = idx
end
end
if not self._match_page_index then return false end
end
local idx = self._match_page_index
if direction > 0 then -- Next (wrap around)
idx = idx + 1
if idx > count then idx = 1 end
else -- Previous (wrap around)
idx = idx - 1
if idx < 1 then idx = count end
end
self._match_page_index = idx
self._search_navigating = true
self:setPageNumber(list[idx])
self._search_navigating = nil
return true
end
function HtmlBoxWidget:_highlightSearchOnCurrentPage()
if not self.document or not self.search_term then return false end
-- Use cached search index for instant lookup
if self._search_index and self._search_index[self.page_number] then
self.highlight_rects = self._search_index[self.page_number]
self:freeBb()
UIManager:setDirty(self.dialog, function()
return "partial", self.dimen
end)
return true
end
return false
end
function HtmlBoxWidget:clearSearch(redraw)
self.search_term = nil
self._search_index = nil
self._match_page_list = nil
self._match_page_index = nil
if self.on_clear_search then
self.on_clear_search()
end
-- Clear the visual aspect too
if redraw and self:clearHighlight() then
self:redrawHighlight()
end
end
return HtmlBoxWidget
+7
View File
@@ -30,6 +30,7 @@ local ScrollHtmlWidget = InputContainer:extend{
height = 0,
scroll_bar_width = Screen:scaleBySize(6),
text_scroll_span = Screen:scaleBySize(12),
on_clear_search = nil,
}
function ScrollHtmlWidget:init()
@@ -41,6 +42,7 @@ function ScrollHtmlWidget:init()
dialog = self.dialog,
highlight_text_selection = self.highlight_text_selection,
html_link_tapped_callback = self.html_link_tapped_callback,
on_clear_search = self.on_clear_search,
}
self.htmlbox_widget:setContent(self.html_body, self.css, self.default_font_size, self.is_xhtml, nil, self.html_resource_directory)
@@ -187,6 +189,7 @@ function ScrollHtmlWidget:onScrollText(arg, ges)
end
function ScrollHtmlWidget:onTapScrollText(arg, ges)
if self.ignore_taps then return false end
if BD.flipIfMirroredUILayout(ges.pos.x < Screen:getWidth()/2) then
return self:onScrollUp()
else
@@ -194,6 +197,10 @@ function ScrollHtmlWidget:onTapScrollText(arg, ges)
end
end
function ScrollHtmlWidget:setTapScrollEnabled(enabled)
self.ignore_taps = not enabled
end
function ScrollHtmlWidget:onScrollUp()
if self.htmlbox_widget.page_number > 1 then
self:scrollText(-1)