Skip to content

Instantly share code, notes, and snippets.

@IntrovertedMage
Last active March 5, 2025 02:11
Show Gist options
  • Select an option

  • Save IntrovertedMage/65aac4220e380c814af5a97910445191 to your computer and use it in GitHub Desktop.

Select an option

Save IntrovertedMage/65aac4220e380c814af5a97910445191 to your computer and use it in GitHub Desktop.
A KOReader user patch that adds a menu to make it more easy to set the preferred dictionaries for the book you're currently reading
local Menu = require("ui/widget/menu")
local ReaderDictionary = require("apps/reader/modules/readerdictionary")
local Event = require("ui/event")
local UIManager = require("ui/uimanager")
local util = require("util")
local _ = require("gettext")
local addToMainMenu_orig = ReaderDictionary.addToMainMenu
ReaderDictionary.addToMainMenu = function(self,menu_items)
addToMainMenu_orig(self,menu_items)
if self.ui.view then
table.insert(menu_items.dictionary_settings.sub_item_table, 3,
{
text = _("Set preferred dictionaries for this book"),
keep_menu_open = true,
callback = function(touchmenu_instance)
self:showPreferredDictionariesMenu(function()
if touchmenu_instance then touchmenu_instance:updateItems() end
end)
end,
}
)
end
end
local PreferredDictionaryWidget = Menu:extend{
title = _("Manage preferred dictionaries"),
is_popout = false,
is_borderless = true,
}
PreferredDictionaryWidget.init = function(self)
self.item_table = self:generateDictionaryOptions(self.ReaderDictionary.enabled_dict_names)
self.radius = 0
Menu.init(self)
end
PreferredDictionaryWidget.generateDictionaryOptions = function(self,enabled_dictionaries)
local item_table = {}
for _, dictionary in ipairs({unpack(enabled_dictionaries)}) do
local dictionary_name = dictionary
if self.ReaderDictionary.preferred_dictionaries then
-- If current result is from a preferred dictionary, prepend dict name
-- (shown in the window title) with its preference number
for idx, name in ipairs(self.ReaderDictionary.preferred_dictionaries) do
if dictionary == name then
-- Use number in circle symbol (U+2460...2473)
local symbol = util.unicodeCodepointToUtf8(0x245F + (idx < 20 and idx or 20))
dictionary = symbol .. " " .. dictionary
break
end
end
end
table.insert(item_table,{text = dictionary, idx = _, dictionary_name = dictionary_name})
end
return item_table
end
PreferredDictionaryWidget.onMenuSelect = function (self,item)
self.ReaderDictionary.ui:handleEvent(
Event:new("TogglePreferredDict", item.dictionary_name)
)
self.item_table = self:generateDictionaryOptions(self.ReaderDictionary.enabled_dict_names)
self:updateItems(1,true)
end
ReaderDictionary.showPreferredDictionariesMenu = function (self,changed_callback)
local dictionary_widget = PreferredDictionaryWidget:new{
ReaderDictionary = self,
callback = function()
UIManager:setDirty(nil,"ui")
changed_callback()
end
}
UIManager:show(dictionary_widget)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment