Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace tweak_buttons_func with an event instead #11777

Merged
merged 17 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions frontend/apps/reader/modules/readerdictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ function ReaderDictionary:addToMainMenu(menu_items)
end
end

function ReaderDictionary:onLookupWord(word, is_sane, boxes, highlight, link, tweak_buttons_func)
function ReaderDictionary:onLookupWord(word, is_sane, boxes, highlight, link)
logger.dbg("dict lookup word:", word, boxes)
-- escape quotes and other funny characters in word
word = self:cleanSelection(word, is_sane)
Expand All @@ -440,7 +440,7 @@ function ReaderDictionary:onLookupWord(word, is_sane, boxes, highlight, link, tw

-- Wrapped through Trapper, as we may be using Trapper:dismissablePopen() in it
Trapper:wrap(function()
self:stardictLookup(word, self.enabled_dict_names, not disable_fuzzy_search, boxes, link, tweak_buttons_func)
self:stardictLookup(word, self.enabled_dict_names, not disable_fuzzy_search, boxes, link)
end)
return true
end
Expand Down Expand Up @@ -932,7 +932,7 @@ function ReaderDictionary:startSdcv(word, dict_names, fuzzy_search)
return results
end

function ReaderDictionary:stardictLookup(word, dict_names, fuzzy_search, boxes, link, tweak_buttons_func)
function ReaderDictionary:stardictLookup(word, dict_names, fuzzy_search, boxes, link)
if word == "" then
return
end
Expand Down Expand Up @@ -992,16 +992,15 @@ function ReaderDictionary:stardictLookup(word, dict_names, fuzzy_search, boxes,
return
end

self:showDict(word, tidyMarkup(results), boxes, link, tweak_buttons_func)
self:showDict(word, tidyMarkup(results), boxes, link)
end

function ReaderDictionary:showDict(word, results, boxes, link, tweak_buttons_func)
function ReaderDictionary:showDict(word, results, boxes, link)
if results and results[1] then
logger.dbg("showing quick lookup window", #DictQuickLookup.window_list+1, ":", word, results)
self.dict_window = DictQuickLookup:new{
ui = self.ui,
highlight = self.highlight,
tweak_buttons_func = tweak_buttons_func,
dialog = self.dialog,
-- original lookup word
word = word,
Expand Down
4 changes: 2 additions & 2 deletions frontend/ui/widget/dictquicklookup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ function DictQuickLookup:init()
})
end
end
if self.tweak_buttons_func then
self:tweak_buttons_func(buttons)
if self.ui then
self.ui:handleEvent(Event:new("DictButtonsReady", self, buttons))
end
-- Bottom buttons get a bit less padding so their line separators
-- reach out from the content to the borders a bit more
Expand Down
159 changes: 76 additions & 83 deletions plugins/vocabbuilder.koplugin/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DictQuickLookUp = require("ui/widget/dictquicklookup")
local Dispatcher = require("dispatcher")
local Event = require("ui/event")
local Font = require("ui/font")
Expand Down Expand Up @@ -59,41 +58,6 @@ local subtitle_color = Blitbuffer.COLOR_DARK_GRAY
local dim_color = Blitbuffer.COLOR_GRAY_3
local settings = G_reader_settings:readSetting("vocabulary_builder", {enabled = false, with_context = true})

local function resetButtonOnLookupWindow()
if not settings.enabled then -- auto add words
DictQuickLookUp.tweak_buttons_func = function(obj, buttons)
if obj.is_wiki_fullpage then
return
elseif obj.is_wiki then
-- make wiki window have the same button_tweak as its presenting dictionary window
local widget = UIManager:getNthTopWidget(2)
if widget and widget.tweak_buttons_func then
widget:tweak_buttons_func(buttons)
end
return
end
table.insert(buttons, 1, {{
id = "vocabulary",
text = _("Add to vocabulary builder"),
font_bold = false,
callback = function()
local book_title = (obj.ui.doc_props and obj.ui.doc_props.display_title) or _("Dictionary lookup")
obj.ui:handleEvent(Event:new("WordLookedUp", obj.word, book_title, true)) -- is_manual: true
local button = obj.button_table.button_by_id["vocabulary"]
if button then
button:disable()
UIManager:setDirty(obj, function()
return "ui", button.dimen
end)
end
end
}})
end
else
DictQuickLookUp.tweak_buttons_func = nil
end
end

local function saveSettings()
G_reader_settings:saveSetting("vocabulary_builder", settings)
end
Expand Down Expand Up @@ -507,7 +471,6 @@ end
function MenuDialog:onChangeEnableStatus(args, position)
settings.enabled = position == 2
saveSettings()
resetButtonOnLookupWindow()
end

function MenuDialog:onConfigChoose(values, name, event, args, position)
Expand Down Expand Up @@ -1254,6 +1217,7 @@ end
Container widget. Same as sortwidget
--]]--
local VocabularyBuilderWidget = FocusManager:extend{
id = "vocab_builder_widget",
title = "",
width = nil,
height = nil,
Expand Down Expand Up @@ -1954,6 +1918,78 @@ function VocabBuilder:addToMainMenu(menu_items)
}
end

function VocabItemWidget:onDictButtonsReady(popup_dict, buttons)
if self.item.word ~= popup_dict.word then
return
end
if self.item.due_time > os.time() then
return true
end
local tweaked_button_count = 0
local early_break
for j = 1, #buttons do
for k = 1, #buttons[j] do
if buttons[j][k].id == "highlight" and not buttons[j][k].enabled then
buttons[j][k] = {
id = "got_it",
text = _("Got it"),
callback = function()
self.show_parent:gotItFromDict(self.item.word)
UIManager:sendEvent(Event:new("Close"))
end
}
if tweaked_button_count == 1 then
early_break = true
break
end
tweaked_button_count = tweaked_button_count + 1
elseif buttons[j][k].id == "search" and not buttons[j][k].enabled then
buttons[j][k] = {
id = "forgot",
text = _("Forgot"),
callback = function()
self.show_parent:forgotFromDict(self.item.word)
UIManager:sendEvent(Event:new("Close"))
end
}
if tweaked_button_count == 1 then
early_break = true
break
end
tweaked_button_count = tweaked_button_count + 1
end
end
if early_break then break end
end
return true -- we consume the event here!
end

function VocabBuilder:onDictButtonsReady(obj, buttons)
if settings.enabled then
-- words are added automatically, no need to add the button
return
end
if obj.is_wiki_fullpage then
return
end
table.insert(buttons, 1, {{
id = "vocabulary",
text = _("Add to vocabulary builder"),
font_bold = false,
callback = function()
local book_title = (obj.ui.doc_props and obj.ui.doc_props.display_title) or _("Dictionary lookup")
obj.ui:handleEvent(Event:new("WordLookedUp", obj.word, book_title, true)) -- is_manual: true
local button = obj.button_table.button_by_id["vocabulary"]
if button then
button:disable()
UIManager:setDirty(obj, function()
return "ui", button.dimen
end)
end
end
}})
end

function VocabBuilder:setupWidget()
if self.widget then
self.widget:reloadItems()
Expand All @@ -1963,54 +1999,11 @@ function VocabBuilder:setupWidget()
local reload_items = function(widget)
widget.reload_time = os.time()
local vocab_items = {}
for i = 1, DB:selectCount(widget) do
for _ = 1, DB:selectCount(widget) do
table.insert(vocab_items, {
callback = function(item)
-- custom button table
local tweak_buttons_func = function() end
if item.due_time <= os.time() then
tweak_buttons_func = function(obj, buttons)
local tweaked_button_count = 0
local early_break
for j = 1, #buttons do
for k = 1, #buttons[j] do
if buttons[j][k].id == "highlight" and not buttons[j][k].enabled then
buttons[j][k] = {
id = "got_it",
text = _("Got it"),
callback = function()
self.widget:gotItFromDict(item.word)
UIManager:sendEvent(Event:new("Close"))
end
}
if tweaked_button_count == 1 then
early_break = true
break
end
tweaked_button_count = tweaked_button_count + 1
elseif buttons[j][k].id == "search" and not buttons[j][k].enabled then
buttons[j][k] = {
id = "forgot",
text = _("Forgot"),
callback = function()
self.widget:forgotFromDict(item.word)
UIManager:sendEvent(Event:new("Close"))
end
}
if tweaked_button_count == 1 then
early_break = true
break
end
tweaked_button_count = tweaked_button_count + 1
end
end
if early_break then break end
end
end
end

widget.current_lookup_word = item.word
self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil, tweak_buttons_func))
self.ui:handleEvent(Event:new("LookupWord", item.word, true, nil, nil, nil))
end
})
end
Expand All @@ -2025,6 +2018,7 @@ function VocabBuilder:setupWidget()
reload_items_callback = reload_items
}
end
self[1] = self.widget
end

function VocabBuilder:onDispatcherRegisterActions()
Expand Down Expand Up @@ -2057,6 +2051,5 @@ function VocabBuilder:onWordLookedUp(word, title, is_manual)
end

-- register button in readerdictionary
nairyosangha marked this conversation as resolved.
Show resolved Hide resolved
resetButtonOnLookupWindow()

return VocabBuilder