From 85591c75ce216cde605ba2fb8abbcc4df4aabfbe Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 10 Oct 2024 18:05:24 +0300 Subject: [PATCH] Profiles: add "on rotation" auto-execute event (#12612) --- frontend/ui/data/creoptions.lua | 6 +- frontend/ui/data/koptoptions.lua | 6 +- frontend/ui/data/optionsutil.lua | 15 ++- .../elements/screen_rotation_menu_table.lua | 9 +- plugins/profiles.koplugin/main.lua | 92 ++++++++++++++++++- 5 files changed, 112 insertions(+), 16 deletions(-) diff --git a/frontend/ui/data/creoptions.lua b/frontend/ui/data/creoptions.lua index f9b773b2a..36f404073 100644 --- a/frontend/ui/data/creoptions.lua +++ b/frontend/ui/data/creoptions.lua @@ -90,11 +90,11 @@ local CreOptions = { end end, -- For Dispatcher & onMakeDefault's sake - labels = {C_("Rotation", "⤹ 90°"), C_("Rotation", "↑ 0°"), C_("Rotation", "⤸ 90°"), C_("Rotation", "↓ 180°")}, + labels = optionsutil.rotation_labels, alternate = false, - values = {Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE, Screen.DEVICE_ROTATED_UPRIGHT, Screen.DEVICE_ROTATED_CLOCKWISE, Screen.DEVICE_ROTATED_UPSIDE_DOWN}, + values = optionsutil.rotation_modes, default_value = Screen.DEVICE_ROTATED_UPRIGHT, - args = {Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE, Screen.DEVICE_ROTATED_UPRIGHT, Screen.DEVICE_ROTATED_CLOCKWISE, Screen.DEVICE_ROTATED_UPSIDE_DOWN}, + args = optionsutil.rotation_modes, current_func = function() return Screen:getRotationMode() end, event = "SetRotationMode", name_text_hold_callback = optionsutil.showValues, diff --git a/frontend/ui/data/koptoptions.lua b/frontend/ui/data/koptoptions.lua index bd08c26f2..ffc5628d1 100644 --- a/frontend/ui/data/koptoptions.lua +++ b/frontend/ui/data/koptoptions.lua @@ -76,11 +76,11 @@ local KoptOptions = { end end, -- For Dispatcher & onMakeDefault's sake - labels = {C_("Rotation", "⤹ 90°"), C_("Rotation", "↑ 0°"), C_("Rotation", "⤸ 90°"), C_("Rotation", "↓ 180°")}, + labels = optionsutil.rotation_labels, alternate = false, - values = {Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE, Screen.DEVICE_ROTATED_UPRIGHT, Screen.DEVICE_ROTATED_CLOCKWISE, Screen.DEVICE_ROTATED_UPSIDE_DOWN}, + values = optionsutil.rotation_modes, default_value = Screen.DEVICE_ROTATED_UPRIGHT, - args = {Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE, Screen.DEVICE_ROTATED_UPRIGHT, Screen.DEVICE_ROTATED_CLOCKWISE, Screen.DEVICE_ROTATED_UPSIDE_DOWN}, + args = optionsutil.rotation_modes, current_func = function() return Screen:getRotationMode() end, event = "SetRotationMode", name_text_hold_callback = optionsutil.showValues, diff --git a/frontend/ui/data/optionsutil.lua b/frontend/ui/data/optionsutil.lua index 715b8348a..22484c08a 100644 --- a/frontend/ui/data/optionsutil.lua +++ b/frontend/ui/data/optionsutil.lua @@ -11,7 +11,20 @@ local T = require("ffi/util").template local logger = require("logger") local Screen = Device.screen -local optionsutil = {} +local optionsutil = { + rotation_labels = { + C_("Rotation", "⤹ 90°"), + C_("Rotation", "↑ 0°"), + C_("Rotation", "⤸ 90°"), + C_("Rotation", "↓ 180°"), + }, + rotation_modes = { + Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE, -- 3 + Screen.DEVICE_ROTATED_UPRIGHT, -- 0 + Screen.DEVICE_ROTATED_CLOCKWISE, -- 1 + Screen.DEVICE_ROTATED_UPSIDE_DOWN, -- 2 + }, +} function optionsutil.enableIfEquals(configurable, option, value) return configurable[option] == value diff --git a/frontend/ui/elements/screen_rotation_menu_table.lua b/frontend/ui/elements/screen_rotation_menu_table.lua index 3a0e444a3..e3b69ae46 100644 --- a/frontend/ui/elements/screen_rotation_menu_table.lua +++ b/frontend/ui/elements/screen_rotation_menu_table.lua @@ -3,7 +3,6 @@ local Event = require("ui/event") local FileManager = require("apps/filemanager/filemanager") local UIManager = require("ui/uimanager") local _ = require("gettext") -local C_ = _.pgettext local Screen = Device.screen local function genMenuItem(text, mode) @@ -78,10 +77,10 @@ When unchecked, the default rotation of the file browser and the default/saved r }) if FileManager.instance then - table.insert(rotation_table, genMenuItem(C_("Rotation", "⤹ 90°"), Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE)) - table.insert(rotation_table, genMenuItem(C_("Rotation", "↑ 0°"), Screen.DEVICE_ROTATED_UPRIGHT)) - table.insert(rotation_table, genMenuItem(C_("Rotation", "⤸ 90°"), Screen.DEVICE_ROTATED_CLOCKWISE)) - table.insert(rotation_table, genMenuItem(C_("Rotation", "↓ 180°"), Screen.DEVICE_ROTATED_UPSIDE_DOWN)) + local optionsutil = require("ui/data/optionsutil") + for i, mode in ipairs(optionsutil.rotation_modes) do + table.insert(rotation_table, genMenuItem(optionsutil.rotation_labels[i], mode)) + end end rotation_table[#rotation_table].separator = true diff --git a/plugins/profiles.koplugin/main.lua b/plugins/profiles.koplugin/main.lua index 7ec059d95..6ee009c38 100644 --- a/plugins/profiles.koplugin/main.lua +++ b/plugins/profiles.koplugin/main.lua @@ -9,6 +9,7 @@ local UIManager = require("ui/uimanager") local WidgetContainer = require("ui/widget/container/widgetcontainer") local _ = require("gettext") local T = FFIUtil.template +local logger = require("logger") local util = require("util") local autostart_done @@ -137,11 +138,30 @@ function Profiles:getSubMenuItems() }, { text = _("Auto-execute"), + checked_func = function() + for _, profiles in pairs(self.autoexec) do + if profiles[k] then + return true + end + end + end, sub_item_table = { self:genAutoExecMenuItem(_("on KOReader start"), "Start", k), self:genAutoExecMenuItem(_("on document opening"), "ReaderReady", k), self:genAutoExecMenuItem(_("on document closing"), "CloseDocument", k), + self:genAutoExecMenuItem(_("on rotation"), "SetRotationMode", k), }, + hold_callback = function(touchmenu_instance) + for event, profiles in pairs(self.autoexec) do + if profiles[k] then + self.autoexec[event][k] = nil + if next(self.autoexec[event]) == nil then + self.autoexec[event] = nil + end + end + end + touchmenu_instance:updateItems() + end, separator = true, }, { @@ -395,6 +415,9 @@ end -- AutoExec function Profiles:genAutoExecMenuItem(text, event, profile_name) + if event == "SetRotationMode" then + return self:genAutoExecSetRotationModeMenuItem(text, event, profile_name) + end return { text = text, checked_func = function() @@ -414,19 +437,63 @@ function Profiles:genAutoExecMenuItem(text, event, profile_name) } end +function Profiles:genAutoExecSetRotationModeMenuItem(text, event, profile_name) + return { + text = text, + checked_func = function() + return self.autoexec[event] and self.autoexec[event][profile_name] and true + end, + sub_item_table_func = function() + local sub_item_table = {} + local optionsutil = require("ui/data/optionsutil") + for i, mode in ipairs(optionsutil.rotation_modes) do + sub_item_table[i] = { + text = optionsutil.rotation_labels[i], + checked_func = function() + return self.autoexec[event] and self.autoexec[event][profile_name] and self.autoexec[event][profile_name][mode] + end, + callback = function() + if self.autoexec[event] and self.autoexec[event][profile_name] and self.autoexec[event][profile_name][mode] then + self.autoexec[event][profile_name][mode] = nil + if next(self.autoexec[event][profile_name]) == nil then + self.autoexec[event][profile_name] = nil + if next(self.autoexec[event]) == nil then + self.autoexec[event] = nil + end + end + else + self.autoexec[event] = self.autoexec[event] or {} + self.autoexec[event][profile_name] = self.autoexec[event][profile_name] or {} + self.autoexec[event][profile_name][mode] = true + end + end, + } + end + return sub_item_table + end, + hold_callback = function(touchmenu_instance) + self.autoexec[event][profile_name] = nil + if next(self.autoexec[event]) == nil then + self.autoexec[event] = nil + end + touchmenu_instance:updateItems() + end, + } +end + function Profiles:updateAutoExec(old_name, new_name) for event, profiles in pairs(self.autoexec) do - local found + local old_value for profile_name in pairs(profiles) do if profile_name == old_name then + old_value = profiles[old_name] profiles[old_name] = nil - found = true break end end - if found then + if old_value then if new_name then - profiles[new_name] = true + profiles[new_name] = old_value else if next(profiles) == nil then self.autoexec[event] = nil @@ -440,6 +507,7 @@ function Profiles:executeAutoExec(event) if self.autoexec[event] then for profile_name in pairs(self.autoexec[event]) do if self.data[profile_name] then + logger.dbg("Profiles - auto executing:", profile_name) UIManager:nextTick(function() Dispatcher:execute(self.data[profile_name]) end) @@ -467,4 +535,20 @@ function Profiles:onCloseDocument() end end +function Profiles:onSetRotationMode(rotation) + if self.autoexec.SetRotationMode and rotation ~= nil then + for profile_name, modes in pairs(self.autoexec.SetRotationMode) do + if modes[rotation] and self.data[profile_name] then + if self.ui.config then -- close bottom menu to let Dispatcher execute profile + self.ui.config:onCloseConfigMenu() + end + logger.dbg("Profiles - auto executing:", profile_name) + UIManager:nextTick(function() + Dispatcher:execute(self.data[profile_name]) + end) + end + end + end +end + return Profiles