--[[-- This module renders the dictionary widget. ## Dictionary button extension API (for plugins) Plugins can extend dictionary popup buttons via: `ReaderDictionary:addToDictButtons(spec)` Specs are consumed in two places: 1. ReaderDictionary customize menu generation. 2. DictQuickLookup runtime button pool + layout assembly. There is one unified spec model for both persistent and transient actions. ### Registration lifecycle - Register once (typically in plugin init) when `self.ui.dictionary` exists. - Specs are stored by id; ids must be unique. - Popup instances are created later; callbacks run per popup instance. ### Spec fields Required: - `id` (string): unique key used in config/layout/button lookup. Labels: - `menu_text` (string): entry label in "Customize buttons" selector (non-conditional only). - `text` (string): static runtime button label. - `text_func` (function(dict_popup) -> string): dynamic runtime label. Visibility and enabled state: - `show_func` (function(dict_popup) -> bool): runtime visibility gate (default true). - `enabled` (bool): static enabled state. - `enable_func` (function(dict_popup) -> bool): dynamic enabled state (takes precedence over `enabled`). Actions: - `callback` (function(dict_popup)): tap action. - `hold_callback` (function(dict_popup)): long-press action. Layout and style: - `conditional` (bool): runtime-only transient button/row if true. - `row_group` (string): group conditional buttons into same transient row. - `pairs_with` (string|string[]): pairing hint used alongside can_shrink to create compact rows. - `insert_first` (bool): non-conditional auto insertion at top of default layout. - `can_shrink` (bool): allow width shrink in 4-button rows when paired. - `auto_row_style_width_min_row_size` (int): minimum number of buttons per row to apply auto styling. - `auto_row_style_width_ratio` (float): width percentage being given to the button when auto styling is applied. - `vsync` (bool): propagated to button entry. ### Persistent vs transient behavior Non-conditional (`conditional ~= true`): - Can appear in Customize buttons menu when `menu_text` is provided. - Can be toggled/sorted by users through dictionary button customization. - On fresh installs (no `dict_button_config`), can be auto-added to `default_layout`. Conditional or transient (`conditional == true`): - Runtime-only, appended via `extra_layout`. - Not user-toggleable in persistent customization. - Usually used for context-dependent actions (review buttons, link-dependent actions, etc). ### Conditional row grouping rules - If `row_group` is shared, buttons join one transient row. - Else, each conditional button becomes its own transient row. ### Runtime layout pipeline At popup build time (`buildButtonLayout`): 1. Built-in buttons are created (`_getButtonPool`). 2. Base layout is selected (default/config/wiki variants). 3. ReaderDictionary injects plugin buttons into pool/layout. 4. Transient `extra_layout` rows are appended. 5. Row styling/shrink logic is applied. ### Ordering Registered specs are iterated with `ffiUtil.orderedPairs` (alphabetical by key). This gives deterministic order for: - default insertion, - conditional row key discovery, - and final row composition. @usage function MyPlugin:registerDictButtons() if self.ui and self.ui.dictionary then self.ui.dictionary:addToDictButtons({ id = "my_custom_action", menu_text = _("My custom action"), text_func = function(dict_popup) return dict_popup._my_custom_action_state and _("Disable action") or _("Enable action") end, insert_first = true, show_func = function(dict_popup) return true -- or some condition based on dict_popup end, callback = function(dict_popup) local current = dict_popup._my_custom_action_state == true dict_popup._my_custom_action_state = not current end, }) end end ]] local BD = require("ui/bidi") local Blitbuffer = require("ffi/blitbuffer") local ButtonDialog = require("ui/widget/buttondialog") local ButtonTable = require("ui/widget/buttontable") local CenterContainer = require("ui/widget/container/centercontainer") local Device = require("device") local Geom = require("ui/geometry") local Event = require("ui/event") local Font = require("ui/font") local FrameContainer = require("ui/widget/container/framecontainer") local GestureRange = require("ui/gesturerange") local IconButton = require("ui/widget/iconbutton") local InputContainer = require("ui/widget/container/inputcontainer") local InputDialog = require("ui/widget/inputdialog") local Math = require("optmath") local MovableContainer = require("ui/widget/container/movablecontainer") local OverlapGroup = require("ui/widget/overlapgroup") local ScrollHtmlWidget = require("ui/widget/scrollhtmlwidget") local ScrollTextWidget = require("ui/widget/scrolltextwidget") local Size = require("ui/size") local TextWidget = require("ui/widget/textwidget") local TitleBar = require("ui/widget/titlebar") local Translator = require("ui/translator") local Presets = require("ui/presets") local UIManager = require("ui/uimanager") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") local WidgetContainer = require("ui/widget/container/widgetcontainer") local filemanagerutil = require("apps/filemanager/filemanagerutil") local lfs = require("libs/libkoreader-lfs") local logger = require("logger") local util = require("util") local _ = require("gettext") local C_ = _.pgettext local Input = Device.input local Screen = Device.screen local T = require("ffi/util").template local ffiUtil = require("ffi/util") local time = require("ui/time") --[[ Display quick lookup word definition ]] local DictQuickLookup = InputContainer:extend{ results = nil, lookupword = nil, dictionary = nil, dictionary_resource_directory = nil, -- relative path to the dictionary's res folder if it exists definition = nil, displayword = nil, images = nil, is_wiki = false, is_wiki_fullpage = false, is_html = false, 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, -- refresh_callback will be called before we trigger full refresh in onSwipe refresh_callback = nil, html_dictionary_link_tapped_callback = nil, dict_close_callback = nil, -- called when closing DictQuickLookup -- Static class member, holds a ref to the currently opened widgets (in instantiation order). window_list = {}, _is_temporary_large_window = false, } -- Static variable to hold request data for temporary fullscreen DictQuickLookup.temp_large_window_request = nil function DictQuickLookup.getWikiSaveEpubDefaultDir() local dir = filemanagerutil.getHomeFolder() if dir:sub(-1) ~= "/" then dir = dir .. "/" end return dir .. "Wikipedia" end function DictQuickLookup:canSearch() if self.is_wiki then -- In the Wiki variant of this widget, the Search button is coopted to show and select -- between user-defined (or UI+book) languages. if #self.wiki_languages > 1 then return true end else if self:isDocless() then return false end -- This is to prevent an ineffective button when we're launched from the Reader's menu. if self.highlight then return true end end return false end function DictQuickLookup:init() self.dict_font_size = G_reader_settings:readSetting("dict_font_size") or 20 self.content_face = Font:getFace("cfont", self.dict_font_size) local font_size_alt = self.dict_font_size - 4 if font_size_alt < 8 then font_size_alt = 8 end self.image_alt_face = Font:getFace("cfont", font_size_alt) self.allow_key_text_selection = Device:hasDPad() if self.allow_key_text_selection then self.text_selection_started = false self.previous_indicator_pos = nil end self:registerKeyEvents() if Device:isTouchDevice() then local range = Geom:new{ x = 0, y = 0, w = Screen:getWidth(), h = Screen:getHeight(), } local hold_pan_rate = G_reader_settings:readSetting("hold_pan_rate") if not hold_pan_rate then hold_pan_rate = Screen.low_pan_rate and 5.0 or 30.0 end self.ges_events = { Tap = { GestureRange:new{ ges = "tap", range = range, }, }, Swipe = { GestureRange:new{ ges = "swipe", range = range, }, }, -- This was for selection of a single word with simple hold -- HoldWord = { -- GestureRange:new{ -- ges = "hold", -- range = function() -- return self.region -- end, -- }, -- -- callback function when HoldWord is handled as args -- args = function(word) -- self.ui:handleEvent( -- -- don't pass self.highlight to subsequent lookup, we want -- -- the first to be the only one to unhighlight selection -- -- when closed -- Event:new("LookupWord", word, true, {self.word_box})) -- end -- }, -- Allow selection of one or more words (see textboxwidget.lua) : HoldStartText = { GestureRange:new{ ges = "hold", range = range, }, }, HoldPanText = { GestureRange:new{ ges = "hold_pan", range = range, rate = hold_pan_rate, }, }, HoldReleaseText = { GestureRange:new{ ges = "hold_release", range = range, }, -- callback function when HoldReleaseText is handled as args args = function(text, hold_duration) -- short hold: same domain; long hold (>=3s): switch domain self:lookupDictionaryOrWikipedia(text, hold_duration >= time.s(3)) end }, SetTemporaryLargeWindowMode = { GestureRange:new{ ges = "spread", range = range, }, }, -- These will be forwarded to MovableContainer after some checks ForwardingTouch = { GestureRange:new{ ges = "touch", range = range, }, }, ForwardingPan = { GestureRange:new{ ges = "pan", range = range, }, }, ForwardingPanRelease = { GestureRange:new{ ges = "pan_release", range = range, }, }, } end self.temp_large_window = DictQuickLookup.temp_large_window_request and DictQuickLookup.temp_large_window_request.is_large_window == true -- We no longer support setting a default dict with Tap on title. -- self:changeToDefaultDict() if DictQuickLookup.temp_large_window_request and DictQuickLookup.temp_large_window_request.dict_index then self:changeDictionary(DictQuickLookup.temp_large_window_request.dict_index, true) DictQuickLookup.temp_large_window_request.dict_index = nil self._is_temporary_large_window = true else self:changeDictionary(1, true) -- don't call update end -- And here comes the initial widget layout... if self.is_wiki then self.wiki_languages, self.wiki_last_language = self.ui.wikipedia:getWikiLanguages() end -- Bigger window if fullpage Wikipedia article being shown, -- or when large windows for dict requested local is_large_window = self.is_wiki_fullpage or G_reader_settings:isTrue("dict_largewindow") or self.temp_large_window if is_large_window then self.width = Screen:getWidth() - 2*Size.margin.default else self.width = self.width or Screen:getWidth() - Screen:scaleBySize(80) -- user patchable end local frame_bordersize = Size.border.window local inner_width = self.width - 2*frame_bordersize -- Height will be computed below, after we build top an bottom -- components, when we know how much height they are taking. -- Dictionary title self.dict_title = TitleBar:new{ width = inner_width, title = self.displaydictname, with_bottom_line = true, bottom_v_padding = 0, -- padding handled below close_callback = function() if self.in_definition_search then self:endFindInDefinition() return true end self:onClose() end, close_hold_callback = function() self:onHoldClose() end, -- visual hint: title left aligned for dict, centered for Wikipedia align = self.is_wiki and "center" or "left", show_parent = self, lang = self.lang_out, left_icon = "appbar.menu", left_icon_tap_callback = function() if self.is_wiki then if self.is_wiki_fullpage then self:showWikiFullOtherLangsMenu() else self:showWikiResultsMenu() end else self:onShowResultsMenu() end end, left_icon_hold_callback = not self.is_wiki and function() self:showResultsAltMenu() end or nil, } -- Scrollable offsets of the various showResults* menus and submenus, -- so we can reopen them in the same state they were when closed. self.menu_scrolled_offsets = {} -- We'll also need to close any opened such menu when closing this DictQuickLookup -- (needed if closing all DictQuickLookups via long-press on Close on the top one) self.menu_opened = {} -- This padding and the resulting width apply to the content -- below the title: lookup word and definition local content_padding_h = Size.padding.large local content_padding_v = Size.padding.large -- added via VerticalSpan self.content_width = inner_width - 2*content_padding_h -- Spans between components local top_to_word_span = VerticalSpan:new{ width = content_padding_v } local word_to_definition_span = VerticalSpan:new{ width = content_padding_v } local definition_to_bottom_span = VerticalSpan:new{ width = content_padding_v } -- Lookup word local word_font_face = "tfont" -- Ensure this word doesn't get smaller than its definition local word_font_size = math.max(22, self.dict_font_size) -- Get the line height of the normal font size, as a base for sizing this component if not self.word_line_height then local test_widget = TextWidget:new{ text = "z", face = Font:getFace(word_font_face, word_font_size), } self.word_line_height = test_widget:getSize().h test_widget:free() end if self.is_wiki then -- Wikipedia has longer titles, so use a smaller font, word_font_size = math.max(18, self.dict_font_size) end local icon_size = Screen:scaleBySize(32) local lookup_height = math.max(self.word_line_height, icon_size) -- Edit button local lookup_edit_button = IconButton:new{ icon = "edit", width = icon_size, height = icon_size, padding = 0, padding_left = Size.padding.small, callback = function() -- allow adjusting the queried word self:onLookupInputWord(self.word) end, hold_callback = function() -- allow adjusting the current result word self:onLookupInputWord(self.lookupword) end, overlap_align = "right", show_parent = self, } local lookup_edit_button_w = lookup_edit_button:getSize().w -- Nb of results (if set) local lookup_word_nb local lookup_word_nb_w = 0 if self.displaynb then self.displaynb_text = TextWidget:new{ text = self.displaynb, face = Font:getFace("cfont", word_font_size), padding = 0, -- smaller height for better aligmnent with icon } lookup_word_nb = FrameContainer:new{ margin = 0, bordersize = 0, padding = 0, padding_left = Size.padding.small, padding_right = lookup_edit_button_w + Size.padding.default, overlap_align = "right", self.displaynb_text, } lookup_word_nb_w = lookup_word_nb:getSize().w end -- Lookup word self.lookup_word_text = TextWidget:new{ text = self.displayword, face = Font:getFace(word_font_face, word_font_size), bold = true, max_width = self.content_width - math.max(lookup_edit_button_w, lookup_word_nb_w), padding = 0, -- to be aligned with lookup_word_nb lang = self.lang_in } -- Group these 3 widgets local lookup_word = OverlapGroup:new{ dimen = { w = self.content_width, h = lookup_height, }, self.lookup_word_text, lookup_edit_button, lookup_word_nb, -- last, as this might be nil } -- Different sets of buttons whether fullpage or not local buttons = self:buildButtonLayout() -- Bottom buttons get a bit less padding so their line separators -- reach out from the content to the borders a bit more local buttons_padding = Size.padding.default local buttons_width = inner_width - 2*buttons_padding self.button_table = ButtonTable:new{ width = buttons_width, buttons = buttons, zero_sep = true, show_parent = self, } -- Margin from screen edges local margin_top = Size.margin.default local margin_bottom = Size.margin.default if self.ui and self.ui.view and self.ui.view.footer_visible then -- We want to let the footer visible (as it can show time, battery level -- and wifi state, which might be useful when spending time reading -- definitions or wikipedia articles) margin_bottom = margin_bottom + self.ui.view.footer:getHeight() end local avail_height = Screen:getHeight() - margin_top - margin_bottom -- Region in which the window will be aligned center/top/bottom: self.region = Geom:new{ x = 0, y = margin_top, w = Screen:getWidth(), h = avail_height, } self.align = "center" local others_height = frame_bordersize * 2 -- DictQuickLookup border + self.dict_title:getHeight() + top_to_word_span:getSize().h + lookup_word:getSize().h + word_to_definition_span:getSize().h + definition_to_bottom_span:getSize().h + self.button_table:getSize().h -- To properly adjust the definition to the height of text, we need -- the line height a ScrollTextWidget will use for the current font -- size (we'll then use this perfect height for ScrollTextWidget, -- but also for ScrollHtmlWidget, where it doesn't matter). if not self.definition_line_height then local test_widget = ScrollTextWidget:new{ text = "z", face = self.content_face, width = self.content_width, height = self.definition_height, for_measurement_only = true, -- flag it as a dummy, so it won't trigger any bogus repaint/refresh... } self.definition_line_height = test_widget:getLineHeight() test_widget:free(true) end if is_large_window then -- Available height for definition + components self.height = avail_height self.definition_height = self.height - others_height local nb_lines = math.floor(self.definition_height / self.definition_line_height) self.definition_height = nb_lines * self.definition_line_height local pad = self.height - others_height - self.definition_height -- put that unused height on the above span word_to_definition_span.width = word_to_definition_span.width + pad else -- Definition height was previously computed as 0.5*0.7*screen_height, so keep -- it that way. Components will add themselves to that. self.definition_height = math.floor(avail_height * 0.5 * 0.7) -- But we want it to fit to the lines that will show, to avoid -- any extra padding local nb_lines = Math.round(self.definition_height / self.definition_line_height) self.definition_height = nb_lines * self.definition_line_height self.height = self.definition_height + others_height if self.word_boxes and #self.word_boxes > 0 then -- Try to not hide the highlighted word. We don't want to always -- get near it if we can stay center, so that more context around -- the word is still visible with the dict result. -- But if we were to be drawn over the word, move a bit if possible. -- In most cases boxes will be a single sbox, but handle multiple -- sboxes by taking the top and bottom y values. local word_box_top local word_box_bottom for _, box in ipairs(self.word_boxes) do local box_top = box.y local box_bottom = box.y + box.h if not word_box_top or word_box_top > box_top then word_box_top = box_top end if not word_box_bottom or word_box_bottom < box_bottom then word_box_bottom = box_bottom end end -- Don't stick to the box, ensure a minimal padding between box and -- window. word_box_top = word_box_top - Size.padding.small word_box_bottom = word_box_bottom + Size.padding.small local half_visible_height = (avail_height - self.height) / 2 if word_box_bottom > half_visible_height and word_box_top <= half_visible_height + self.height then -- word would be covered by our centered window if word_box_bottom <= avail_height - self.height then -- Window can be moved just below word self.region.y = word_box_bottom self.region.h = self.region.h - word_box_bottom self.align = "top" elseif word_box_top > self.height then -- Window can be moved just above word self.region.y = 0 self.region.h = word_box_top self.align = "bottom" end end end end -- Instantiate self.text_widget self:_instantiateScrollWidget() -- word definition self.definition_widget = FrameContainer:new{ padding = 0, padding_left = content_padding_h, padding_right = content_padding_h, margin = 0, bordersize = 0, self.text_widget, } self.dict_frame = FrameContainer:new{ radius = Size.radius.window, bordersize = frame_bordersize, padding = 0, margin = 0, background = Blitbuffer.COLOR_WHITE, VerticalGroup:new{ align = "left", self.dict_title, top_to_word_span, -- word CenterContainer:new{ dimen = Geom:new{ w = inner_width, h = lookup_word:getSize().h, }, lookup_word, }, word_to_definition_span, -- definition CenterContainer:new{ dimen = Geom:new{ w = inner_width, h = self.definition_widget:getSize().h, }, self.definition_widget, }, definition_to_bottom_span, -- buttons CenterContainer:new{ dimen = Geom:new{ w = inner_width, h = self.button_table:getSize().h, }, self.button_table, } } } self.movable = MovableContainer:new{ -- We'll handle these events ourselves, and call appropriate -- MovableContainer's methods when we didn't process the event ignore_events = { -- These have effects over the definition widget, and may -- or may not be processed by it "swipe", "hold", "hold_release", "hold_pan", -- These do not have direct effect over the definition widget, -- but may happen while selecting text: we need to check -- a few things before forwarding them "touch", "pan", "pan_release", }, is_movable_with_keys = false, self.dict_frame, } self[1] = WidgetContainer:new{ align = self.align, dimen = self.region, self.movable, } -- NT: add dict_title.left_button and lookup_edit_button to FocusManager. -- It is better to add these two buttons into self.movable, but it is not a FocusManager. -- Only self.button_table is a FocusManager, so the workaround is inserting these two buttons into self.button_table.layout. if Device:hasDPad() and not (Device:hasScreenKB() or Device:hasKeyboard()) then table.insert(self.button_table.layout, 1, { self.dict_title.left_button }) table.insert(self.button_table.layout, 2, { lookup_edit_button }) -- Refocus on the updated layout self.button_table:refocusWidget() end -- We're a new window table.insert(DictQuickLookup.window_list, self) end function DictQuickLookup:registerKeyEvents() if Device:hasKeys() then self.key_events.ReadPrevResult = { { Input.group.PgBack } } self.key_events.ReadNextResult = { { Input.group.PgFwd } } 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" self.key_events.ChangeToPrevDict = { { modifier, Input.group.PgBack } } self.key_events.ChangeToNextDict = { { modifier, Input.group.PgFwd } } self.key_events.SetTemporaryLargeWindowMode = { { modifier, "Home" } } 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 = "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.AlphaNumeric }, { "Shift", Input.group.AlphaNumeric }, event = "LookupInputWord" } self.key_events.LookupInputWordEmpty = { { " " }, event = "LookupInputWord" } if G_reader_settings:nilOrFalse("backspace_as_back") then -- We need to concat here so that the 'del' event press, which propagates to inputText (desirable for previous key_event, -- i.e., LookupInputWordClear) does not remove the last char of self.word self.key_events.LookupInputWord = { { Device:hasSymKey() and "Del" or "Backspace" }, args = self.word .." " } end else -- same case as hasKeyboard self.key_events.LookupInputWord = { { "ScreenKB", "Back" }, args = self.word .." " } end end if Device:hasDPad() then self.key_events.TextSelectorPress = { { "Press" } } self.key_events.UpTextSelectorIndicator = { { "Up" }, event = "MoveTextSelectorIndicator", args = { 0, -1 } } self.key_events.DownTextSelectorIndicator = { { "Down" }, event = "MoveTextSelectorIndicator", args = { 0, 1 } } self.key_events.RightTextSelectorIndicator = { { "Right" }, event = "MoveTextSelectorIndicator", args = { 1, 0 } } if not Device:hasFewKeys() then self.key_events.LeftTextSelectorIndicator = { { "Left" }, event = "MoveTextSelectorIndicator", args = { -1, 0 } } end end end end -- Whether currently DictQuickLookup is working without a document. function DictQuickLookup:isDocless() return self.ui == nil or self.ui.highlight == nil end function DictQuickLookup:_getButtonPool() local prev_dict_text = "◁◁" local next_dict_text = "▷▷" if BD.mirroredUILayout() then prev_dict_text, next_dict_text = next_dict_text, prev_dict_text end local pool = { save = { id = "save", text = _("Save as EPUB"), callback = function() local InfoMessage = require("ui/widget/infomessage") local ConfirmBox = require("ui/widget/confirmbox") -- We should have a self.lang set, but let's have a fallback local lang = self.lang or self.wiki_languages[1] -- Find a directory to save file into local dir if G_reader_settings:isTrue("wikipedia_save_in_book_dir") and not self:isDocless() then local last_file = G_reader_settings:readSetting("lastfile") dir = last_file and last_file:match("(.*)/") end dir = dir or G_reader_settings:readSetting("wikipedia_save_dir") or DictQuickLookup.getWikiSaveEpubDefaultDir() if not util.pathExists(dir) then lfs.mkdir(dir) end -- Just to be safe (none of the invalid chars, except ':' for uninteresting -- Portal: or File: wikipedia pages, should be in lookupword) local filename = self.lookupword .. "."..string.upper(lang)..".epub" filename = util.getSafeFilename(filename, dir):gsub("_", " ") local epub_path = dir .. "/" .. filename UIManager:show(ConfirmBox:new{ text = T(_("Save as %1?"), BD.filename(filename)), ok_callback = function() UIManager:scheduleIn(0.1, function() local Wikipedia = require("ui/wikipedia") Wikipedia:createEpubWithUI(epub_path, self.lookupword, lang, function(success) if success then UIManager:show(ConfirmBox:new{ text = T(_("Article saved to:\n%1\n\nWould you like to read the downloaded article now?"), BD.filepath(epub_path)), ok_callback = function() -- close all dict/wiki windows, without scheduleIn(highlight.clear()) self:onHoldClose(true) -- close current ReaderUI in 1 sec, and create a new one UIManager:scheduleIn(1.0, function() UIManager:broadcastEvent(Event:new("SetupShowReader")) if self.ui then -- close Highlight menu if any still shown if self.ui.highlight and self.ui.highlight.highlight_dialog then self.ui.highlight:onClose() end self.ui:onClose() end local ReaderUI = require("apps/reader/readerui") ReaderUI:showReader(epub_path) end) end, }) else UIManager:show(InfoMessage:new{ text = _("Saving Wikipedia article failed or interrupted."), }) end end) end) end }) end, }, close = { id = "close", text = _("Close"), callback = function() self:onClose() end, hold_callback = function() self:onHoldClose() end, }, prev_dict = { id = "prev_dict", text = prev_dict_text, vsync = true, pairs_with = "next_dict", can_shrink = true, enabled = self:isPrevDictAvaiable(), callback = function() self:onChangeToPrevDict() end, hold_callback = function() self:changeToFirstDict() end, }, highlight = { id = "highlight", text = _("Highlight"), enabled = not self:isDocless() and self.highlight ~= nil, callback = function() self.save_highlight = not self.save_highlight -- Just update, repaint and refresh *this* button local this = self.button_table:getButtonById("highlight") this:setText(self.save_highlight and _("Unhighlight") or _("Highlight"), this.width) this:refresh() end, }, next_dict = { id = "next_dict", text = next_dict_text, vsync = true, pairs_with = "prev_dict", can_shrink = true, enabled = self:isNextDictAvaiable(), callback = function() self:onChangeToNextDict() end, hold_callback = function() self:changeToLastDict() end, }, wikipedia = { id = "wikipedia", -- if dictionary result, do the same search on wikipedia -- if already wiki, get the full page for the current result text_func = function() if self.is_wiki then -- @translators Full Wikipedia article. return C_("Wikipedia", "Full article") else return _("Wikipedia") end end, callback = function() UIManager:scheduleIn(0.1, function() self:lookupWikipedia(self.is_wiki) -- will get_fullpage if is_wiki end) end, }, translate = { id = "translate", text = _("Translate"), enabled = not self:isDocless(), callback = function() Translator:showTranslation(self.lookupword, true) end, }, -- Allow selecting a different wikipedia language, or Search in book if dict window search = { id = "search", text = self.is_wiki and C_("Wikipedia", "Language").." \u{2261}" or _("Search"), enabled = self:canSearch(), callback = function() if self.is_wiki then local button_dialog local lang_buttons = {} for idx, lang in ipairs(self.wiki_languages) do local bold = lang == self.lang -- bold the language currently shown local row = { { text = lang, font_bold = bold, align = "left", callback = function() self.ui.wikipedia:setLastSelectedLanguage(lang) self:lookupWikipedia(false, nil, nil, lang) self:onClose(true) end, }, } -- Show them in reverse, so first/prefered ones are at the bottom near the button where the user tap'ed table.insert(lang_buttons, 1, row) end local search_btn = self.button_table:getButtonById("search") button_dialog = ButtonDialog:new{ buttons = lang_buttons, width = search_btn.dimen.w, anchor = function() return Geom:new{ x = search_btn.dimen.x, -- We want its bottom border overlapping the whole DictQuickLookup window bottom border y = self.dict_frame.dimen.y + self.dict_frame.dimen.h, } end, tap_close_callback = function() self.menu_opened[button_dialog] = nil end } self.menu_opened[button_dialog] = true UIManager:show(button_dialog) else self.ui:handleEvent(Event:new("HighlightSearch")) self:onClose(true) -- don't unhighlight (or we might erase a search hit) end end, }, text_selection = { id = "text_selection", text = _("Text selection"), callback = function() self:onStartTextSelectorIndicator() end, }, link = { id = "link", text = _("Follow Link"), callback = function() local link = self.selected_link.link or self.selected_link self.ui.link:onGotoLink(link) self:onClose() end }, } return pool end -- Checks whether a given id is already present inside a layout table. function DictQuickLookup.layoutContainsButtonId(layout, button_id) if not layout then return false end for r = 1, #layout do local r_buttons = layout[r] for b = 1, #r_buttons do if r_buttons[b] == button_id then return true end end end return false end function DictQuickLookup:populatePluginButtons(pool, default_layout, extra_layout) local dict_buttons = self.ui.dictionary._dict_buttons if not dict_buttons then return end local conditional_rows = {} local conditional_row_order = {} local function add_conditional_button(row_key, button_id) local row = conditional_rows[row_key] if not row then row = {} conditional_rows[row_key] = row table.insert(conditional_row_order, row_key) end for _, existing in ipairs(row) do if existing == button_id then return end end table.insert(row, button_id) end for id, spec in ffiUtil.orderedPairs(dict_buttons) do local show = true if spec.show_func then show = spec.show_func(self) end if show then local button = { id = spec.id, text = spec.text, text_func = spec.text_func, font_bold = spec.font_bold, auto_row_style_width_min_row_size = spec.auto_row_style_width_min_row_size, auto_row_style_width_ratio = spec.auto_row_style_width_ratio, width = spec.width, vsync = spec.vsync, pairs_with = spec.pairs_with, can_shrink = spec.can_shrink, callback = spec.callback and function() return spec.callback(self) end, hold_callback = spec.hold_callback and function() return spec.hold_callback(self) end, } if spec.enable_func then button.enabled = spec.enable_func(self) elseif spec.enabled ~= nil then button.enabled = spec.enabled end pool[spec.id] = button if spec.conditional then local row_key = spec.row_group add_conditional_button(row_key or spec.id, spec.id) elseif default_layout and not self.layoutContainsButtonId(default_layout, spec.id) then local i = spec.insert_first and 1 or (#default_layout + 1) table.insert(default_layout, i, { spec.id }) end logger.dbg("DictQuickLookup", id..": populated dict quick button") end end for _, row_key in ipairs(conditional_row_order) do local row = conditional_rows[row_key] if #row > 0 then table.insert(extra_layout, row) end end end function DictQuickLookup:buildButtonLayout() local pool = self:_getButtonPool() if self.is_wiki_fullpage then -- Wiki fullpage has a fixed, non-configurable layout return { { pool.save, pool.close } } end local buttons = {} local default_layout = nil if G_reader_settings:hasNot("dict_button_config") or self.is_wiki then default_layout = self.ui.dictionary.default_layout end local extra_layout = {} -- transient buttons. if not self.is_wiki and self.selected_link ~= nil then -- If selecting a word, which is part of a link (should be rare), -- append a new row with a single button to follow this link. table.insert(extra_layout, { "link" }) end if self.ui and self.ui.dictionary then self:populatePluginButtons(pool, default_layout, extra_layout) end local button_layout -- Wiki has a fixed, non-configurable layout (yet!) if self.is_wiki then button_layout = default_layout else -- We must do util.tableDeepCopy here so we don't accidentally save -- transient buttons into user settings! local config = G_reader_settings:readSetting("dict_button_config") button_layout = util.tableDeepCopy(config and config.layout or default_layout) end local frame_bordersize = Size.border.window local inner_width = self.width - 2 * frame_bordersize local buttons_width = inner_width - 2 * Size.padding.default local function applyAutoRowStyle(row) local row_size = #row if row_size == 0 then return end for _, btn in ipairs(row) do local width_min = btn.auto_row_style_width_min_row_size local width_ratio = btn.auto_row_style_width_ratio if width_min and width_ratio then if row_size >= width_min and not btn.width then btn.width = math.floor(buttons_width * width_ratio) end end end end for _, extra_row in ipairs(extra_layout) do table.insert(button_layout, extra_row) end local has_shrinkable_buttons = false for _, row_ids in ipairs(button_layout) do local new_row = {} for _, btn_id in ipairs(row_ids) do local button = pool[btn_id] if button then if button.can_shrink and button.pairs_with then has_shrinkable_buttons = true end table.insert(new_row, button) end end if #new_row > 0 then applyAutoRowStyle(new_row) table.insert(buttons, new_row) end end -- Make shrinkable paired buttons smaller when they share a 4-button row. if has_shrinkable_buttons then -- 15% * 2 = 30%, thus we allow 70% for the remaing two buttons -- so 35% each, which is roughly the size of buttons in a 3-button row. local fifteen_percent = math.floor(buttons_width * 0.15) local function btn_has_pair(btn, row_button_ids) local pairs_with = btn.pairs_with if type(pairs_with) == "table" then for _, id in ipairs(pairs_with) do if row_button_ids[id] then return true end end return false end return pairs_with and row_button_ids[pairs_with] == true end local function try_shrink_row(row) if #row ~= 4 then return false end local row_has_shrink_candidate = false local has_custom_width = false local row_button_ids = {} for _, btn in ipairs(row) do row_button_ids[btn.id] = true if btn.width then has_custom_width = true end if btn.can_shrink and btn.pairs_with then row_has_shrink_candidate = true end end if not row_has_shrink_candidate then return false end if not has_custom_width then for _, btn in ipairs(row) do if btn.can_shrink and btn_has_pair(btn, row_button_ids) then btn.width = fifteen_percent end end end return true end for _, row in ipairs(buttons) do if try_shrink_row(row) then break end end end -- if has_shrinkable_buttons return buttons end function DictQuickLookup:getHtmlDictionaryCss() -- Using Noto Sans because Nimbus doesn't contain the IPA symbols. -- 'line-height: 1.3' to have it similar to textboxwidget, -- and follow user's choice on justification local css_justify = G_reader_settings:nilOrTrue("dict_justify") and "text-align: justify;" or "" local css = [[ @page { margin: 0; font-family: 'Noto Sans'; } body { margin: 0; line-height: 1.3; ]]..css_justify..[[ } blockquote, dd { margin: 0 1em; } ol, ul, menu { margin: 0; padding: 0 1.7em; } ]] -- For reference, MuPDF declarations with absolute units: -- "blockquote{margin:1em 40px}" -- "dd{margin:0 0 0 40px}" -- "ol,ul,menu {margin:1em 0;padding:0 0 0 30pt}" -- "hr{border-width:1px;}" -- "td,th{padding:1px}" -- -- MuPDF doesn't currently scale CSS pixels, so we have to use a font-size based measurement. -- Unfortunately MuPDF doesn't properly support `rem` either, which it bases on a hard-coded -- value of `16px`, so we have to go with `em` (or `%`). -- -- These `em`-based margins can vary slightly, but it's the best available compromise. -- -- We also keep left and right margin the same so it'll display as expected in RTL. -- Because MuPDF doesn't currently support `margin-start`, this results in a slightly -- unconventional but hopefully barely noticeable right margin for
. -- -- For