From 178bb823f05e5a2f95ac9d4595acd4c9d1ab4a5f Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 22 May 2025 17:57:36 +0300 Subject: [PATCH] Dispatcher: update action name or value (#13831) --- frontend/apps/reader/modules/readerfooter.lua | 2 + .../apps/reader/modules/readerstyletweak.lua | 16 ++---- plugins/bookshortcuts.koplugin/main.lua | 5 +- plugins/gestures.koplugin/main.lua | 51 +++++++++++++---- plugins/hotkeys.koplugin/main.lua | 51 +++++++++++++---- plugins/profiles.koplugin/main.lua | 55 +++++++++++++------ 6 files changed, 130 insertions(+), 50 deletions(-) diff --git a/frontend/apps/reader/modules/readerfooter.lua b/frontend/apps/reader/modules/readerfooter.lua index 880fb02f3..5aa7c4ea8 100644 --- a/frontend/apps/reader/modules/readerfooter.lua +++ b/frontend/apps/reader/modules/readerfooter.lua @@ -1955,6 +1955,8 @@ function ReaderFooter:genPresetMenuItemTable() choice1_text = _("Delete"), choice1_callback = function() footer_presets[preset_name] = nil + UIManager:broadcastEvent(Event:new("DispatcherActionValueChanged", + { name = "load_footer_preset", old_value = preset_name, new_value = nil })) touchmenu_instance.item_table = self:genPresetMenuItemTable() touchmenu_instance:updateItems() end, diff --git a/frontend/apps/reader/modules/readerstyletweak.lua b/frontend/apps/reader/modules/readerstyletweak.lua index 0afc18ee0..98d7491f5 100644 --- a/frontend/apps/reader/modules/readerstyletweak.lua +++ b/frontend/apps/reader/modules/readerstyletweak.lua @@ -454,11 +454,6 @@ local function dispatcherRegisterStyleTweak(tweak_id, tweak_title) {category="none", event="ToggleStyleTweak", arg=tweak_id, title=T(_("Style tweak '%1' toggle"), tweak_title), rolling=true}) end -local function dispatcherUnregisterStyleTweak(tweak_id) - Dispatcher:removeAction(ReaderStyleTweak.dispatcher_prefix_toggle..tweak_id) - Dispatcher:removeAction(ReaderStyleTweak.dispatcher_prefix_set..tweak_id) -end - function ReaderStyleTweak:init() self.tweaks_in_dispatcher = G_reader_settings:readSetting("style_tweaks_in_dispatcher") or {} self.tweaks_by_id = {} @@ -570,11 +565,12 @@ You can enable individual tweaks on this book with a tap, or view more details a toggle_tweak_in_dispatcher_callback = function() if self.tweaks_in_dispatcher[item.id] then self.tweaks_in_dispatcher[item.id] = nil - dispatcherUnregisterStyleTweak(item.id) - if self.ui.profiles then - self.ui.profiles:updateProfiles(self.dispatcher_prefix_toggle..item.id) - self.ui.profiles:updateProfiles(self.dispatcher_prefix_set..item.id) - end + Dispatcher:removeAction(self.dispatcher_prefix_toggle..item.id) + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", + { old_name = self.dispatcher_prefix_toggle..item.id, new_name = nil })) + Dispatcher:removeAction(self.dispatcher_prefix_set..item.id) + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", + { old_name = self.dispatcher_prefix_set..item.id, new_name = nil })) else self.tweaks_in_dispatcher[item.id] = item.title dispatcherRegisterStyleTweak(item.id, item.title) diff --git a/plugins/bookshortcuts.koplugin/main.lua b/plugins/bookshortcuts.koplugin/main.lua index dac4a1f2e..f728beec9 100644 --- a/plugins/bookshortcuts.koplugin/main.lua +++ b/plugins/bookshortcuts.koplugin/main.lua @@ -2,6 +2,7 @@ local ConfirmBox = require("ui/widget/confirmbox") local DataStorage = require("datastorage") local Device = require("device") local Dispatcher = require("dispatcher") +local Event = require("ui/event") local LuaSettings = require("luasettings") local PathChooser = require("ui/widget/pathchooser") local ReadHistory = require("readhistory") @@ -162,9 +163,7 @@ end function BookShortcuts:deleteShortcut(name) self.shortcuts.data[name] = nil Dispatcher:removeAction(name) - if self.ui.profiles then - self.ui.profiles:updateProfiles(name) - end + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", { old_name = name, new_name = nil })) self.updated = true end diff --git a/plugins/gestures.koplugin/main.lua b/plugins/gestures.koplugin/main.lua index 1036fafe1..4731eec76 100644 --- a/plugins/gestures.koplugin/main.lua +++ b/plugins/gestures.koplugin/main.lua @@ -1222,19 +1222,19 @@ function Gestures:onFlushSettings() end end -function Gestures:updateProfiles(action_old_name, action_new_name) +function Gestures:onDispatcherActionNameChanged(action) for _, section in ipairs({ "gesture_fm", "gesture_reader" }) do local gestures = self.settings_data.data[section] for gesture_name, gesture in pairs(gestures) do - if gesture[action_old_name] then + if gesture[action.old_name] ~= nil then if gesture.settings and gesture.settings.order then - for i, action in ipairs(gesture.settings.order) do - if action == action_old_name then - if action_new_name then - gesture.settings.order[i] = action_new_name + for i, action_in_order in ipairs(gesture.settings.order) do + if action_in_order == action.old_name then + if action.new_name then + gesture.settings.order[i] = action.new_name else table.remove(gesture.settings.order, i) - if #gesture.settings.order == 0 then + if #gesture.settings.order < 2 then gesture.settings.order = nil if next(gesture.settings) == nil then gesture.settings = nil @@ -1245,9 +1245,9 @@ function Gestures:updateProfiles(action_old_name, action_new_name) end end end - gesture[action_old_name] = nil - if action_new_name then - gesture[action_new_name] = true + gesture[action.old_name] = nil + if action.new_name then + gesture[action.new_name] = true else if next(gesture) == nil then self.settings_data.data[section][gesture_name] = nil @@ -1259,4 +1259,35 @@ function Gestures:updateProfiles(action_old_name, action_new_name) end end +function Gestures:onDispatcherActionValueChanged(action) + for _, section in ipairs({ "gesture_fm", "gesture_reader" }) do + local gestures = self.settings_data.data[section] + for gesture_name, gesture in pairs(gestures) do + if gesture[action.name] == action.old_value then + gesture[action.name] = action.new_value + if action.new_value == nil then + if gesture.settings and gesture.settings.order then + for i, action_in_order in ipairs(gesture.settings.order) do + if action_in_order == action.name then + table.remove(gesture.settings.order, i) + if #gesture.settings.order < 2 then + gesture.settings.order = nil + if next(gesture.settings) == nil then + gesture.settings = nil + end + end + break + end + end + end + if next(gesture) == nil then + self.settings_data.data[section][gesture_name] = nil + end + end + self.updated = true + end + end + end +end + return Gestures diff --git a/plugins/hotkeys.koplugin/main.lua b/plugins/hotkeys.koplugin/main.lua index 6f72cf662..738e68fcd 100644 --- a/plugins/hotkeys.koplugin/main.lua +++ b/plugins/hotkeys.koplugin/main.lua @@ -530,19 +530,19 @@ function HotKeys:onFlushSettings() end end -function HotKeys:updateProfiles(action_old_name, action_new_name) +function HotKeys:onDispatcherActionNameChanged(action) for _, section in ipairs({ "hotkeys_fm", "hotkeys_reader" }) do local hotkeys = self.settings_data.data[section] for shortcut_name, shortcut in pairs(hotkeys) do - if shortcut[action_old_name] then + if shortcut[action.old_name] ~= nil then if shortcut.settings and shortcut.settings.order then - for i, action in ipairs(shortcut.settings.order) do - if action == action_old_name then - if action_new_name then - shortcut.settings.order[i] = action_new_name + for i, action_in_order in ipairs(shortcut.settings.order) do + if action_in_order == action.old_name then + if action.new_name then + shortcut.settings.order[i] = action.new_name else table.remove(shortcut.settings.order, i) - if #shortcut.settings.order == 0 then + if #shortcut.settings.order < 2 then shortcut.settings.order = nil if next(shortcut.settings) == nil then shortcut.settings = nil @@ -553,9 +553,9 @@ function HotKeys:updateProfiles(action_old_name, action_new_name) end end end - shortcut[action_old_name] = nil - if action_new_name then - shortcut[action_new_name] = true + shortcut[action.old_name] = nil + if action.new_name then + shortcut[action.new_name] = true else if next(shortcut) == nil then self.settings_data.data[section][shortcut_name] = nil @@ -567,4 +567,35 @@ function HotKeys:updateProfiles(action_old_name, action_new_name) end end +function HotKeys:onDispatcherActionValueChanged(action) + for _, section in ipairs({ "hotkeys_fm", "hotkeys_reader" }) do + local hotkeys = self.settings_data.data[section] + for shortcut_name, shortcut in pairs(hotkeys) do + if shortcut[action.name] == action.old_value then + shortcut[action.name] = action.new_value + if action.new_value == nil then + if shortcut.settings and shortcut.settings.order then + for i, action_in_order in ipairs(shortcut.settings.order) do + if action_in_order == action.name then + table.remove(shortcut.settings.order, i) + if #shortcut.settings.order < 2 then + shortcut.settings.order = nil + if next(shortcut.settings) == nil then + shortcut.settings = nil + end + end + break + end + end + end + if next(shortcut) == nil then + self.settings_data.data[section][shortcut_name] = nil + end + end + self.updated = true + end + end + end +end + return HotKeys diff --git a/plugins/profiles.koplugin/main.lua b/plugins/profiles.koplugin/main.lua index 0e4c84284..f06bb304d 100644 --- a/plugins/profiles.koplugin/main.lua +++ b/plugins/profiles.koplugin/main.lua @@ -3,6 +3,7 @@ local DataStorage = require("datastorage") local DateTimeWidget = require("ui/widget/datetimewidget") local Device = require("device") local Dispatcher = require("dispatcher") +local Event = require("ui/event") local InfoMessage = require("ui/widget/infomessage") local InputDialog = require("ui/widget/inputdialog") local LuaSettings = require("luasettings") @@ -248,7 +249,8 @@ function Profiles:getSubMenuItems() callback = function() if v.settings.registered then dispatcherUnregisterProfile(k) - self:updateProfiles(self.prefix..k) + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", + { old_name = self.prefix..k, new_name = nil })) self.data[k].settings.registered = nil else dispatcherRegisterProfile(k) @@ -279,7 +281,8 @@ function Profiles:getSubMenuItems() if v.settings.registered then dispatcherUnregisterProfile(k) dispatcherRegisterProfile(new_name) - self:updateProfiles(self.prefix..k, self.prefix..new_name) + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", + { old_name = self.prefix..k, new_name = self.prefix..new_name })) end self.data[k] = nil self.updated = true @@ -319,7 +322,8 @@ function Profiles:getSubMenuItems() self:updateAutoExec(k) if v.settings.registered then dispatcherUnregisterProfile(k) - self:updateProfiles(self.prefix..k) + UIManager:broadcastEvent(Event:new("DispatcherActionNameChanged", + { old_name = self.prefix..k, new_name = nil })) end self.data[k] = nil self.updated = true @@ -461,17 +465,17 @@ function Profiles:getProfileFromCurrentBookSettings(new_name) return profile end -function Profiles:updateProfiles(action_old_name, action_new_name) +function Profiles:onDispatcherActionNameChanged(action) for _, profile in pairs(self.data) do - if profile[action_old_name] then + if profile[action.old_name] ~= nil then if profile.settings and profile.settings.order then - for i, action in ipairs(profile.settings.order) do - if action == action_old_name then - if action_new_name then - profile.settings.order[i] = action_new_name + for i, action_in_order in ipairs(profile.settings.order) do + if action_in_order == action.old_name then + if action.new_name then + profile.settings.order[i] = action.new_name else table.remove(profile.settings.order, i) - if #profile.settings.order == 0 then + if #profile.settings.order < 2 then profile.settings.order = nil end end @@ -479,17 +483,34 @@ function Profiles:updateProfiles(action_old_name, action_new_name) end end end - profile[action_old_name] = nil - if action_new_name then - profile[action_new_name] = true + profile[action.old_name] = nil + if action.new_name then + profile[action.new_name] = true end self.updated = true end end - if self.ui.gestures then -- search and update the profile action in assigned gestures - self.ui.gestures:updateProfiles(action_old_name, action_new_name) - elseif self.ui.hotkeys then -- search and update the profile action in assigned keyboard shortcuts - self.ui.hotkeys:updateProfiles(action_old_name, action_new_name) +end + +function Profiles:onDispatcherActionValueChanged(action) + for _, profile in pairs(self.data) do + if profile[action.name] == action.old_value then + profile[action.name] = action.new_value + if action.new_value == nil then + if profile.settings and profile.settings.order then + for i, action_in_order in ipairs(profile.settings.order) do + if action_in_order == action.name then + table.remove(profile.settings.order, i) + if #profile.settings.order < 2 then + profile.settings.order = nil + end + break + end + end + end + end + self.updated = true + end end end