simplify Dispatcher (#6435)

there is no need to pass the caller rather just the table where the setting is located. passing the setting alone is not enough as sometimes it is nil and then you do not get a reference to its location
reviewable/pr6445/r1
yparitcher 4 years ago committed by GitHub
parent 93eadbaf88
commit 87b1f0c1f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -302,7 +302,7 @@ function Dispatcher:init()
Dispatcher.initialized = true Dispatcher.initialized = true
end end
function Dispatcher.addItem(caller, menu, location, settings, section) function Dispatcher:addItem(menu, location, settings, section)
for _, k in ipairs(dispatcher_menu_order) do for _, k in ipairs(dispatcher_menu_order) do
if settingsList[k][section] == true and if settingsList[k][section] == true and
(settingsList[k].condition == nil or settingsList[k].condition) (settingsList[k].condition == nil or settingsList[k].condition)
@ -311,15 +311,15 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
table.insert(menu, { table.insert(menu, {
text = settingsList[k].title, text = settingsList[k].title,
checked_func = function() checked_func = function()
return caller[location][settings] ~= nil and caller[location][settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
if caller[location][settings] ~= nil if location[settings] ~= nil
and caller[location][settings][k] and location[settings][k]
then then
caller[location][settings][k] = nil location[settings][k] = nil
else else
caller[location][settings][k] = true location[settings][k] = true
end end
if touchmenu_instance then touchmenu_instance:updateItems() end if touchmenu_instance then touchmenu_instance:updateItems() end
end, end,
@ -328,24 +328,24 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
elseif settingsList[k].category == "absolutenumber" then elseif settingsList[k].category == "absolutenumber" then
table.insert(menu, { table.insert(menu, {
text_func = function() text_func = function()
return T(settingsList[k].title, caller[location][settings][k] or "") return T(settingsList[k].title, location[settings][k] or "")
end, end,
checked_func = function() checked_func = function()
return caller[location][settings] ~= nil and caller[location][settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
local SpinWidget = require("ui/widget/spinwidget") local SpinWidget = require("ui/widget/spinwidget")
local items = SpinWidget:new{ local items = SpinWidget:new{
width = Screen:getWidth() * 0.6, width = Screen:getWidth() * 0.6,
value = caller[location][settings][k] or settingsList[k].default or 0, value = location[settings][k] or settingsList[k].default or 0,
value_min = settingsList[k].min, value_min = settingsList[k].min,
value_step = 1, value_step = 1,
value_hold_step = 2, value_hold_step = 2,
value_max = settingsList[k].max, value_max = settingsList[k].max,
default_value = 0, default_value = 0,
title_text = T(settingsList[k].title, caller[location][settings][k] or ""), title_text = T(settingsList[k].title, location[settings][k] or ""),
callback = function(spin) callback = function(spin)
caller[location][settings][k] = spin.value location[settings][k] = spin.value
if touchmenu_instance then if touchmenu_instance then
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
end end
@ -354,7 +354,7 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
UIManager:show(items) UIManager:show(items)
end, end,
hold_callback = function(touchmenu_instance) hold_callback = function(touchmenu_instance)
caller[location][settings][k] = nil location[settings][k] = nil
if touchmenu_instance then touchmenu_instance:updateItems() end if touchmenu_instance then touchmenu_instance:updateItems() end
end, end,
separator = settingsList[k].separator, separator = settingsList[k].separator,
@ -362,26 +362,26 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
elseif settingsList[k].category == "incrementalnumber" then elseif settingsList[k].category == "incrementalnumber" then
table.insert(menu, { table.insert(menu, {
text_func = function() text_func = function()
return T(settingsList[k].title, caller[location][settings][k] or "") return T(settingsList[k].title, location[settings][k] or "")
end, end,
checked_func = function() checked_func = function()
return caller[location][settings] ~= nil and caller[location][settings][k] ~= nil return location[settings] ~= nil and location[settings][k] ~= nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
local _ = require("gettext") local _ = require("gettext")
local SpinWidget = require("ui/widget/spinwidget") local SpinWidget = require("ui/widget/spinwidget")
local items = SpinWidget:new{ local items = SpinWidget:new{
width = Screen:getWidth() * 0.6, width = Screen:getWidth() * 0.6,
value = caller[location][settings][k] or 0, value = location[settings][k] or 0,
value_min = settingsList[k].min, value_min = settingsList[k].min,
value_step = 1, value_step = 1,
value_hold_step = 2, value_hold_step = 2,
value_max = settingsList[k].max, value_max = settingsList[k].max,
default_value = 0, default_value = 0,
title_text = T(settingsList[k].title, caller[location][settings][k] or ""), title_text = T(settingsList[k].title, location[settings][k] or ""),
info_text = _([[If called by a gesture the amount of the gesture will be used]]), info_text = _([[If called by a gesture the amount of the gesture will be used]]),
callback = function(spin) callback = function(spin)
caller[location][settings][k] = spin.value location[settings][k] = spin.value
if touchmenu_instance then if touchmenu_instance then
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
end end
@ -390,7 +390,7 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
UIManager:show(items) UIManager:show(items)
end, end,
hold_callback = function(touchmenu_instance) hold_callback = function(touchmenu_instance)
caller[location][settings][k] = nil location[settings][k] = nil
if touchmenu_instance then if touchmenu_instance then
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
end end
@ -403,27 +403,27 @@ function Dispatcher.addItem(caller, menu, location, settings, section)
table.insert(sub_item_table, { table.insert(sub_item_table, {
text = tostring(settingsList[k].toggle[i]), text = tostring(settingsList[k].toggle[i]),
checked_func = function() checked_func = function()
return caller[location][settings] ~= nil return location[settings] ~= nil
and caller[location][settings][k] ~= nil and location[settings][k] ~= nil
and caller[location][settings][k] == settingsList[k].args[i] and location[settings][k] == settingsList[k].args[i]
end, end,
callback = function() callback = function()
caller[location][settings][k] = settingsList[k].args[i] location[settings][k] = settingsList[k].args[i]
end, end,
}) })
end end
table.insert(menu, { table.insert(menu, {
text_func = function() text_func = function()
return T(settingsList[k].title, caller[location][settings][k]) return T(settingsList[k].title, location[settings][k])
end, end,
checked_func = function() checked_func = function()
return caller[location][settings] ~= nil return location[settings] ~= nil
and caller[location][settings][k] ~= nil and location[settings][k] ~= nil
end, end,
sub_item_table = sub_item_table, sub_item_table = sub_item_table,
keep_menu_open = true, keep_menu_open = true,
hold_callback = function(touchmenu_instance) hold_callback = function(touchmenu_instance)
caller[location][settings][k] = nil location[settings][k] = nil
if touchmenu_instance then if touchmenu_instance then
touchmenu_instance:updateItems() touchmenu_instance:updateItems()
end end
@ -438,27 +438,22 @@ end
--[[-- --[[--
Add a submenu to edit which items are dispatched Add a submenu to edit which items are dispatched
arguments are: arguments are:
1) the caller 1) the table representing the submenu (can be empty)
2) the table representing the submenu (can be empty) 2) the object (table) in which the settings table is found
3) the name of the parent of the settings table (must be a child of self) 3) the name of the settings table
4) the name of the settings table
example usage: example usage:
Dispatcher.addSubMenu(self, sub_items, "profiles", "profile1") Dispatcher.addSubMenu(sub_items, self.data, "profile1")
Must be executed in the context of the caller to add items to the callers menu.
therefore declared as Dispatcher.addSubMenu(caller, menu, location, settings)
but should be called as Dispatcher.addSubMenu(self, menu, location, settings)
--]]-- --]]--
function Dispatcher.addSubMenu(caller, menu, location, settings) function Dispatcher:addSubMenu(menu, location, settings)
if not Dispatcher.initialized then Dispatcher:init() end if not Dispatcher.initialized then Dispatcher:init() end
table.insert(menu, { table.insert(menu, {
text = _("None"), text = _("None"),
separator = true, separator = true,
checked_func = function() checked_func = function()
return next(caller[location][settings]) == nil return next(location[settings]) == nil
end, end,
callback = function(touchmenu_instance) callback = function(touchmenu_instance)
caller[location][settings] = {} location[settings] = {}
if touchmenu_instance then touchmenu_instance:updateItems() end if touchmenu_instance then touchmenu_instance:updateItems() end
end, end,
}) })
@ -471,7 +466,7 @@ function Dispatcher.addSubMenu(caller, menu, location, settings)
for _, section in ipairs(section_list) do for _, section in ipairs(section_list) do
local submenu = {} local submenu = {}
-- pass caller's context -- pass caller's context
Dispatcher.addItem(caller, submenu, location, settings, section[1]) Dispatcher:addItem(submenu, location, settings, section[1])
table.insert(menu, { table.insert(menu, {
text = section[2], text = section[2],
sub_item_table = submenu, sub_item_table = submenu,
@ -479,26 +474,33 @@ function Dispatcher.addSubMenu(caller, menu, location, settings)
end end
end end
function Dispatcher:execute(settings, gesture) --[[--
Calls the events in a settings list
arguments are:
1) a reference to the uimanager
2) the settings table
3) optionally a `gestures`object
--]]--
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].conditions == nil or settingsList[k].conditions == true then
if settingsList[k].category == "none" then if settingsList[k].category == "none" then
self.ui:handleEvent(Event:new(settingsList[k].event)) ui:handleEvent(Event:new(settingsList[k].event))
end end
if settingsList[k].category == "absolutenumber" if settingsList[k].category == "absolutenumber"
or settingsList[k].category == "string" or settingsList[k].category == "string"
then then
self.ui:handleEvent(Event:new(settingsList[k].event, v)) ui:handleEvent(Event:new(settingsList[k].event, v))
end end
-- the event can accept a gesture object or an argument -- the event can accept a gesture object or an argument
if settingsList[k].category == "arg" then if settingsList[k].category == "arg" then
local arg = gesture or settingsList[k].arg local arg = gesture or settingsList[k].arg
self.ui:handleEvent(Event:new(settingsList[k].event, arg)) ui:handleEvent(Event:new(settingsList[k].event, arg))
end end
-- the event can accept a gesture object or a number -- the event can accept a gesture object or a number
if settingsList[k].category == "incrementalnumber" then if settingsList[k].category == "incrementalnumber" then
local arg = gesture or v local arg = gesture or v
self.ui:handleEvent(Event:new(settingsList[k].event, arg)) ui:handleEvent(Event:new(settingsList[k].event, arg))
end end
end end
end end

@ -104,13 +104,13 @@ function Profiles:getSubMenuItems()
end, end,
} }
} }
Dispatcher.addSubMenu(self, sub_items, "data", k) Dispatcher:addSubMenu(sub_items, self.data, k)
table.insert(sub_item_table, { table.insert(sub_item_table, {
text = k, text = k,
hold_keep_menu_open = false, hold_keep_menu_open = false,
sub_item_table = sub_items, sub_item_table = sub_items,
hold_callback = function() hold_callback = function()
Dispatcher.execute(self, self.data[k]) Dispatcher:execute(self.ui, self.data[k])
end, end,
}) })
end end

Loading…
Cancel
Save