Dispatcher: allow registering actions at runtime

reviewable/pr6807/r1
yparitcher 4 years ago
parent a73bf1afda
commit 433b82f162

@ -1,3 +1,7 @@
--[[--
Dispatcher module
--]]--
local CreOptions = require("ui/data/creoptions") local CreOptions = require("ui/data/creoptions")
local Device = require("device") local Device = require("device")
local Event = require("ui/event") local Event = require("ui/event")
@ -73,12 +77,6 @@ local settingsList = {
toggle_flash_on_chapter_boundaries = { category="none", event="ToggleFlashOnChapterBoundaries", title=_("Toggle flashing on chapter boundaries"), device=true, condition=Device:hasEinkScreen(),}, toggle_flash_on_chapter_boundaries = { category="none", event="ToggleFlashOnChapterBoundaries", title=_("Toggle flashing on chapter boundaries"), device=true, condition=Device:hasEinkScreen(),},
set_no_flash_on_second_chapter_page = { category="string", event="SetNoFlashOnSecondChapterPage", title=_("Never flash on chapter's 2nd page"), device=true, condition=Device:hasEinkScreen(), args={true, false}, toggle={_("On"), _("Off")},}, set_no_flash_on_second_chapter_page = { category="string", event="SetNoFlashOnSecondChapterPage", title=_("Never flash on chapter's 2nd page"), device=true, condition=Device:hasEinkScreen(), args={true, false}, toggle={_("On"), _("Off")},},
toggle_no_flash_on_second_chapter_page = { category="none", event="ToggleNoFlashOnSecondChapterPage", title=_("Toggle flashing on chapter's 2nd page"), device=true, condition=Device:hasEinkScreen(), separator=true,}, toggle_no_flash_on_second_chapter_page = { category="none", event="ToggleNoFlashOnSecondChapterPage", title=_("Toggle flashing on chapter's 2nd page"), device=true, condition=Device:hasEinkScreen(), separator=true,},
wallabag_download = { category="none", event="SynchronizeWallabag", title=_("Wallabag retrieval"), device=true,},
calibre_search = { category="none", event="CalibreSearch", title=_("Search in calibre metadata"), device=true,},
calibre_browse_tags = { category="none", event="CalibreBrowseTags", title=_("Browse all calibre tags"), device=true,},
calibre_browse_series = { category="none", event="CalibreBrowseSeries", title=_("Browse all calibre series"), device=true, separator=true,},
show_terminal = { category = "none", event = "TerminalStart", title = _("Show terminal"), device = true, },
edit_last_edited_file = { category = "none", event = "OpenLastEditedFile", title = _("Texteditor: open last file"), device = true, separator = true, },
favorites = { category="none", event="ShowColl", arg="favorites", title=_("Favorites"), device=true,}, favorites = { category="none", event="ShowColl", arg="favorites", title=_("Favorites"), device=true,},
-- filemanager settings -- filemanager settings
@ -113,8 +111,6 @@ local settingsList = {
toggle_inverse_reading_order = { category="none", event="ToggleReadingOrder", title=_("Toggle page turn direction"), rolling=true, paging=true,}, toggle_inverse_reading_order = { category="none", event="ToggleReadingOrder", title=_("Toggle page turn direction"), rolling=true, paging=true,},
cycle_highlight_action = { category="none", event="CycleHighlightAction", title=_("Cycle highlight action"), rolling=true, paging=true,}, cycle_highlight_action = { category="none", event="CycleHighlightAction", title=_("Cycle highlight action"), rolling=true, paging=true,},
cycle_highlight_style = { category="none", event="CycleHighlightStyle", title=_("Cycle highlight style"), rolling=true, paging=true,}, cycle_highlight_style = { category="none", event="CycleHighlightStyle", title=_("Cycle highlight style"), rolling=true, paging=true,},
kosync_push_progress = { category="none", event="KOSyncPushProgress", title=_("Push progress from this device"), rolling=true, paging=true,},
kosync_pull_progress = { category="none", event="KOSyncPullProgress", title=_("Pull progress from other devices"), rolling=true, paging=true, separator=true,},
page_jmp = { category="absolutenumber", event="GotoViewRel", min=-100, max=100, title=_("Go %1 pages"), rolling=true, paging=true,}, page_jmp = { category="absolutenumber", event="GotoViewRel", min=-100, max=100, title=_("Go %1 pages"), rolling=true, paging=true,},
panel_zoom_toggle = { category="none", event="TogglePanelZoomSetting", title=_("Toggle panel zoom"), paging=true, separator=true,}, panel_zoom_toggle = { category="none", event="TogglePanelZoomSetting", title=_("Toggle panel zoom"), paging=true, separator=true,},
@ -205,14 +201,6 @@ local dispatcher_menu_order = {
"wifi_off", "wifi_off",
"toggle_wifi", "toggle_wifi",
"wallabag_download",
"calibre_search",
"calibre_browse_tags",
"calibre_browse_series",
"show_terminal",
"edit_last_edited_file",
"rotation_mode", "rotation_mode",
-- filemanager -- filemanager
@ -263,9 +251,6 @@ local dispatcher_menu_order = {
"cycle_highlight_style", "cycle_highlight_style",
"panel_zoom_toggle", "panel_zoom_toggle",
"kosync_push_progress",
"kosync_pull_progress",
"visible_pages", "visible_pages",
"h_page_margins", "h_page_margins",
@ -332,11 +317,29 @@ function Dispatcher:init()
for i=1,#CreOptions do for i=1,#CreOptions do
parseoptions(CreOptions, i) parseoptions(CreOptions, i)
end end
UIManager:broadcastEvent(Event:new("DispatcherRegisterActions"))
Dispatcher.initialized = true Dispatcher.initialized = true
end end
--[[--
add settings at runtime
@param name: the key to use in the table
@param value: a table per settingsList above.
see helloworld plugin for an example.
--]]--
function Dispatcher:registerAction(name, value)
if settingsList[name] == nil then
settingsList[name] = value
table.insert(dispatcher_menu_order, name)
end
return true
end
-- Returns a display name for the item. -- Returns a display name for the item.
function Dispatcher:getNameFromItem(item, location, settings) function Dispatcher:getNameFromItem(item, location, settings)
if settingsList[item] == nil then
return _("Unknown item")
end
local amount local amount
if location[settings] ~= nil and location[settings][item] ~= nil then if location[settings] ~= nil and location[settings][item] ~= nil then
amount = location[settings][item] amount = location[settings][item]
@ -542,7 +545,7 @@ function Dispatcher:addSubMenu(caller, menu, location, settings)
checked_func = function() checked_func = function()
if location[settings] ~= nil then if location[settings] ~= nil then
for k, _ in pairs(location[settings]) do for k, _ in pairs(location[settings]) do
if settingsList[k][section[1]] == true and if settingsList[k] ~= nil and settingsList[k][section[1]] == true and
(settingsList[k].condition == nil or settingsList[k].condition) (settingsList[k].condition == nil or settingsList[k].condition)
then return true end then return true end
end end
@ -551,7 +554,7 @@ function Dispatcher:addSubMenu(caller, menu, location, settings)
hold_callback = function(touchmenu_instance) hold_callback = function(touchmenu_instance)
if location[settings] ~= nil then if location[settings] ~= nil then
for k, _ in pairs(location[settings]) do for k, _ in pairs(location[settings]) do
if settingsList[k][section[1]] == true then if settingsList[k] ~= nil and settingsList[k][section[1]] == true then
location[settings][k] = nil location[settings][k] = nil
caller.updated = true caller.updated = true
end end
@ -573,7 +576,7 @@ arguments are:
--]]-- --]]--
function Dispatcher:execute(ui, settings, gesture) function Dispatcher:execute(ui, settings, gesture)
for k, v in pairs(settings) do for k, v in pairs(settings) do
if settingsList[k].conditions == nil or settingsList[k].conditions == true then if settingsList[k] ~= nil and (settingsList[k].conditions == nil or settingsList[k].conditions == true) then
if settingsList[k].category == "none" then if settingsList[k].category == "none" then
if settingsList[k].arg ~= nil then if settingsList[k].arg ~= nil then
ui:handleEvent(Event:new(settingsList[k].event, settingsList[k].arg)) ui:handleEvent(Event:new(settingsList[k].event, settingsList[k].arg))

@ -10,6 +10,7 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local CalibreSearch = require("search") local CalibreSearch = require("search")
local CalibreWireless = require("wireless") local CalibreWireless = require("wireless")
local Dispatcher = require("dispatcher")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local LuaSettings = require("luasettings") local LuaSettings = require("luasettings")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
@ -57,8 +58,15 @@ function Calibre:closeWirelessConnection()
end end
end end
function Calibre:onDispatcherRegisterActions()
Dispatcher:registerAction("calibre_search", { category="none", event="CalibreSearch", title=_("Search in calibre metadata"), device=true,})
Dispatcher:registerAction("calibre_browse_tags", { category="none", event="CalibreBrowseTags", title=_("Browse all calibre tags"), device=true,})
Dispatcher:registerAction("calibre_browse_series", { category="none", event="CalibreBrowseSeries", title=_("Browse all calibre series"), device=true, separator=true,})
end
function Calibre:init() function Calibre:init()
CalibreWireless:init() CalibreWireless:init()
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
end end

@ -3,7 +3,8 @@ if true then
return { disabled = true, } return { disabled = true, }
end end
local InfoMessage = require("ui/widget/infomessage") -- luacheck:ignore local Dispatcher = require("dispatcher") -- luacheck:ignore
local InfoMessage = require("ui/widget/infomessage")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer") local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext") local _ = require("gettext")
@ -13,7 +14,12 @@ local Hello = WidgetContainer:new{
is_doc_only = false, is_doc_only = false,
} }
function Hello:onDispatcherRegisterActions()
Dispatcher:registerAction("helloworld_action", {category="none", event="HelloWorld", title=_("Hello World"), filemanager=true,})
end
function Hello:init() function Hello:init()
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
end end
@ -21,7 +27,7 @@ function Hello:addToMainMenu(menu_items)
menu_items.hello_world = { menu_items.hello_world = {
text = _("Hello World"), text = _("Hello World"),
-- in which menu this should be appended -- in which menu this should be appended
sorting_hint = "more_plugins", sorting_hint = "more_tools",
-- a callback when tapping -- a callback when tapping
callback = function() callback = function()
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
@ -31,4 +37,11 @@ function Hello:addToMainMenu(menu_items)
} }
end end
function Hello:onHelloWorld()
local popup = InfoMessage:new{
text = _("Hello World"),
}
UIManager:show(popup)
end
return Hello return Hello

@ -1,3 +1,4 @@
local Dispatcher = require("dispatcher")
local InputContainer = require("ui/widget/container/inputcontainer") local InputContainer = require("ui/widget/container/inputcontainer")
local LoginDialog = require("ui/widget/logindialog") local LoginDialog = require("ui/widget/logindialog")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
@ -97,6 +98,11 @@ local function validateUser(user, pass)
end end
end end
function KOSync:onDispatcherRegisterActions()
Dispatcher:registerAction("kosync_push_progress", { category="none", event="KOSyncPushProgress", title=_("Push progress from this device"), rolling=true, paging=true,})
Dispatcher:registerAction("kosync_pull_progress", { category="none", event="KOSyncPullProgress", title=_("Pull progress from other devices"), rolling=true, paging=true, separator=true,})
end
function KOSync:onReaderReady() function KOSync:onReaderReady()
local settings = G_reader_settings:readSetting("kosync") or {} local settings = G_reader_settings:readSetting("kosync") or {}
self.kosync_custom_server = settings.custom_server self.kosync_custom_server = settings.custom_server
@ -112,6 +118,7 @@ function KOSync:onReaderReady()
self:_onResume() self:_onResume()
end end
self:registerEvents() self:registerEvents()
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
-- Make sure checksum has been calculated at the very first time a document has been opened, to -- Make sure checksum has been calculated at the very first time a document has been opened, to
-- avoid document saving feature to impact the checksum, and eventually impact the document -- avoid document saving feature to impact the checksum, and eventually impact the document

@ -1,6 +1,7 @@
local ButtonDialog = require("ui/widget/buttondialog") local ButtonDialog = require("ui/widget/buttondialog")
local CenterContainer = require("ui/widget/container/centercontainer") local CenterContainer = require("ui/widget/container/centercontainer")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Dispatcher = require("dispatcher")
local Font = require("ui/font") local Font = require("ui/font")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
@ -30,7 +31,12 @@ local Terminal = WidgetContainer:new{
source = "terminal", source = "terminal",
} }
function Terminal:onDispatcherRegisterActions()
Dispatcher:registerAction("show_terminal", { category = "none", event = "TerminalStart", title = _("Show terminal"), device = true, })
end
function Terminal:init() function Terminal:init()
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
self.shortcuts = self.settings:readSetting("shortcuts") or {} self.shortcuts = self.settings:readSetting("shortcuts") or {}
end end

@ -1,6 +1,7 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local ConfirmBox = require("ui/widget/confirmbox") local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Dispatcher = require("dispatcher")
local Font = require("ui/font") local Font = require("ui/font")
local InfoMessage = require("ui/widget/infomessage") local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog") local InputDialog = require("ui/widget/inputdialog")
@ -29,7 +30,12 @@ local TextEditor = WidgetContainer:new{
min_file_size_warn = 200000, -- warn/ask when opening files bigger than this min_file_size_warn = 200000, -- warn/ask when opening files bigger than this
} }
function TextEditor:onDispatcherRegisterActions()
Dispatcher:registerAction("edit_last_edited_file", { category = "none", event = "OpenLastEditedFile", title = _("Texteditor: open last file"), device = true, separator = true, })
end
function TextEditor:init() function TextEditor:init()
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
end end

@ -4,6 +4,7 @@
local BD = require("ui/bidi") local BD = require("ui/bidi")
local DataStorage = require("datastorage") local DataStorage = require("datastorage")
local Dispatcher = require("dispatcher")
local DocSettings = require("docsettings") local DocSettings = require("docsettings")
local Event = require("ui/event") local Event = require("ui/event")
local FFIUtil = require("ffi/util") local FFIUtil = require("ffi/util")
@ -38,6 +39,10 @@ local Wallabag = WidgetContainer:new{
name = "wallabag", name = "wallabag",
} }
function Wallabag:onDispatcherRegisterActions()
Dispatcher:registerAction("wallabag_download", { category="none", event="SynchronizeWallabag", title=_("Wallabag retrieval"), device=true,})
end
function Wallabag:init() function Wallabag:init()
self.token_expiry = 0 self.token_expiry = 0
-- default values so that user doesn't have to explicitely set them -- default values so that user doesn't have to explicitely set them
@ -50,6 +55,7 @@ function Wallabag:init()
self.ignore_tags = "" self.ignore_tags = ""
self.articles_per_sync = 30 self.articles_per_sync = 30
self:onDispatcherRegisterActions()
self.ui.menu:registerToMainMenu(self) self.ui.menu:registerToMainMenu(self)
self.wb_settings = self.readSettings() self.wb_settings = self.readSettings()
self.server_url = self.wb_settings.data.wallabag.server_url self.server_url = self.wb_settings.data.wallabag.server_url

Loading…
Cancel
Save