[Hotkeys] refactor hotkey list and update callbacks (#13219)

Cf.  #13078
This commit is contained in:
David
2025-02-07 12:19:22 +00:00
committed by GitHub
parent 4c431e7566
commit 8e886f5631

View File

@@ -1,13 +1,13 @@
local DataStorage = require("datastorage")
local Device = require("device")
local Dispatcher = require("dispatcher")
local FFIUtil = require("ffi/util")
local InputContainer = require("ui/widget/container/inputcontainer")
local LuaSettings = require("luasettings")
local UIManager = require("ui/uimanager")
local ffiUtil = require("ffi/util")
local logger = require("logger")
local util = require("util")
local T = FFIUtil.template
local T = ffiUtil.template
local _ = require("gettext")
if not (Device:hasScreenKB() or Device:hasKeyboard()) then
@@ -21,83 +21,46 @@ local HotKeys = InputContainer:extend{
defaults = nil,
updated = false,
}
local hotkeys_path = FFIUtil.joinPath(DataStorage:getSettingsDir(), "hotkeys.lua")
local hotkeys_path = ffiUtil.joinPath(DataStorage:getSettingsDir(), "hotkeys.lua")
-- mofifier *here* refers to either screenkb or shift
local hotkeys_list = {
-- cursor keys
modifier_plus_up = Device:hasScreenKB() and _("ScreenKB + Up") or _("Shift + Up"),
modifier_plus_down = Device:hasScreenKB() and _("ScreenKB + Down") or _("Shift + Down"),
modifier_plus_left = Device:hasScreenKB() and _("ScreenKB + Left") or _("Shift + Left"),
modifier_plus_right = Device:hasScreenKB() and _("ScreenKB + Right") or _("Shift + Right"),
-- page turn buttons
modifier_plus_left_page_back = Device:hasScreenKB() and _("ScreenKB + LPgBack") or _("Shift + LPgBack"),
modifier_plus_left_page_forward = Device:hasScreenKB() and _("ScreenKB + LPgFwd") or _("Shift + LPgFwd"),
modifier_plus_right_page_back = Device:hasScreenKB() and _("ScreenKB + RPgBack") or _("Shift + RPgBack"),
modifier_plus_right_page_forward = Device:hasScreenKB() and _("ScreenKB + RPgFwd") or _("Shift + RPgFwd"),
-- function keys
modifier_plus_back = Device:hasScreenKB() and _("ScreenKB + Back") or _("Shift + Back"),
modifier_plus_home = Device:hasScreenKB() and _("ScreenKB + Home") or _("Shift + Home"),
modifier_plus_press = Device:hasScreenKB() and _("ScreenKB + Press") or _("Shift + Press"),
-- modifier_plus_menu (screenkb+menu) is already used globally for screenshots (on k4), don't add it here.
-- Define hotkeys_list
local hotkeys_list = {}
local base_keys = {
up = "Up", down = "Down", left = "Left", right = "Right",
left_page_back = "LPgBack", left_page_forward = "LPgFwd",
right_page_back = "RPgBack", right_page_forward = "RPgFwd",
back = "Back", home = "Home", press = "Press"
}
-- modifier *here* refers to either screenkb or shift
local modifier_one = Device:hasScreenKB() and "ScreenKB + " or "Shift + "
-- screenkb/shift + base_keys
for key, label in pairs(base_keys) do
hotkeys_list["modifier_plus_" .. key] = _(modifier_one .. label)
-- modifier_plus_menu (screenkb+menu) is already used globally for screenshots (on k4), don't add it here.
end
if LuaSettings:open(hotkeys_path).data["press_key_does_hotkeys"] then
local hotkeys_list_press = { press = _("Press") }
util.tableMerge(hotkeys_list, hotkeys_list_press)
util.tableMerge(hotkeys_list, { press = _("Press") })
end
if Device:hasKeyboard() then
local hotkeys_list_haskeyboard = {
modifier_plus_menu = _("Shift + Menu"),
-- NOTE: we will use 'alt' for kindles and 'ctrl' for other devices with keyboards
-- but for simplicity we will use in code 'alt+keys' as the array's key for all.
-- alt+cursor
alt_plus_up = Device:hasSymKey() and _("Alt + Up") or _("Ctrl + Up"),
alt_plus_down = Device:hasSymKey() and _("Alt + Down") or _("Ctrl + Down"),
alt_plus_left = Device:hasSymKey() and _("Alt + Left") or _("Ctrl + Left"),
alt_plus_right = Device:hasSymKey() and _("Alt + Right") or _("Ctrl + Right"),
-- alt+page_turn
alt_plus_left_page_back = Device:hasSymKey() and _("Alt + LPgBack") or _("Ctrl + LPgBack"),
alt_plus_left_page_forward = Device:hasSymKey() and _("Alt + LPgFwd") or _("Ctrl + LPgFwd"),
alt_plus_right_page_back = Device:hasSymKey() and _("Alt + RPgBack") or _("Ctrl + RPgBack"),
alt_plus_right_page_forward = Device:hasSymKey() and _("Alt + RPgFwd") or _("Ctrl + RPgFwd"),
-- alt+fn_keys
alt_plus_back = Device:hasSymKey() and _("Alt + Back") or _("Ctrl + Back"),
alt_plus_home = Device:hasSymKey() and _("Alt + Home") or _("Ctrl + Home"),
alt_plus_press = Device:hasSymKey() and _("Alt + Press") or _("Ctrl + Press"),
alt_plus_menu = Device:hasSymKey() and _("Alt + Menu") or _("Ctrl + Menu"),
-- alt+alphabet
alt_plus_a = Device:hasSymKey() and _("Alt + A") or _("Ctrl + A"),
alt_plus_b = Device:hasSymKey() and _("Alt + B") or _("Ctrl + B"),
alt_plus_c = Device:hasSymKey() and _("Alt + C") or _("Ctrl + C"),
alt_plus_d = Device:hasSymKey() and _("Alt + D") or _("Ctrl + D"),
alt_plus_e = Device:hasSymKey() and _("Alt + E") or _("Ctrl + E"),
alt_plus_f = Device:hasSymKey() and _("Alt + F") or _("Ctrl + F"),
alt_plus_g = Device:hasSymKey() and _("Alt + G") or _("Ctrl + G"),
alt_plus_h = Device:hasSymKey() and _("Alt + H") or _("Ctrl + H"),
alt_plus_i = Device:hasSymKey() and _("Alt + I") or _("Ctrl + I"),
alt_plus_j = Device:hasSymKey() and _("Alt + J") or _("Ctrl + J"),
alt_plus_k = Device:hasSymKey() and _("Alt + K") or _("Ctrl + K"),
alt_plus_l = Device:hasSymKey() and _("Alt + L") or _("Ctrl + L"),
alt_plus_m = Device:hasSymKey() and _("Alt + M") or _("Ctrl + M"),
alt_plus_n = Device:hasSymKey() and _("Alt + N") or _("Ctrl + N"),
alt_plus_o = Device:hasSymKey() and _("Alt + O") or _("Ctrl + O"),
alt_plus_p = Device:hasSymKey() and _("Alt + P") or _("Ctrl + P"),
alt_plus_q = Device:hasSymKey() and _("Alt + Q") or _("Ctrl + Q"),
alt_plus_r = Device:hasSymKey() and _("Alt + R") or _("Ctrl + R"),
alt_plus_s = Device:hasSymKey() and _("Alt + S") or _("Ctrl + S"),
alt_plus_t = Device:hasSymKey() and _("Alt + T") or _("Ctrl + T"),
alt_plus_u = Device:hasSymKey() and _("Alt + U") or _("Ctrl + U"),
alt_plus_v = Device:hasSymKey() and _("Alt + V") or _("Ctrl + V"),
alt_plus_w = Device:hasSymKey() and _("Alt + W") or _("Ctrl + W"),
alt_plus_x = Device:hasSymKey() and _("Alt + X") or _("Ctrl + X"),
alt_plus_y = Device:hasSymKey() and _("Alt + Y") or _("Ctrl + Y"),
alt_plus_z = Device:hasSymKey() and _("Alt + Z") or _("Ctrl + Z"),
}
local hotkeys_list_haskeyboard = { modifier_plus_menu = _("Shift + Menu") }
-- now we can add the "menu" button to base_keys, so we can use it on haskeyboard devices
base_keys.menu = "Menu"
-- NOTE: we will use 'alt' for kindles and 'ctrl' for other devices with keyboards
-- but for simplicity we will use in code 'alt+keys' as the array's key for all.
local modifier_two = Device:hasSymKey() and "Alt + " or "Ctrl + "
-- Alt/Ctrl + base_keys
for key, label in pairs(base_keys) do
hotkeys_list_haskeyboard["alt_plus_" .. key] = _(modifier_two .. label)
end
-- Alt/Ctrl + alphabet keys
for dummy, char in ipairs(Device.input.group.Alphabet) do
hotkeys_list_haskeyboard["alt_plus_" .. char:lower()] = _(modifier_two .. char)
end
util.tableMerge(hotkeys_list, hotkeys_list_haskeyboard)
end
function HotKeys:init()
local defaults_path = FFIUtil.joinPath(self.path, "defaults.lua")
local defaults_path = ffiUtil.joinPath(self.path, "defaults.lua")
self.is_docless = self.ui == nil or self.ui.document == nil
self.hotkey_mode = self.is_docless and "hotkeys_fm" or "hotkeys_reader"
self.defaults = LuaSettings:open(defaults_path).data[self.hotkey_mode]
@@ -106,7 +69,7 @@ function HotKeys:init()
if not next(self.settings_data.data) then
logger.warn("No hotkeys file or invalid hotkeys file found, copying defaults")
self.settings_data:purge()
FFIUtil.copyFile(defaults_path, hotkeys_path)
ffiUtil.copyFile(defaults_path, hotkeys_path)
self.settings_data = LuaSettings:open(hotkeys_path)
end
end
@@ -230,25 +193,43 @@ function HotKeys:genMenu(hotkey)
table.insert(sub_items, {
text = T(_("%1 (default)"), default_text),
keep_menu_open = true,
no_refresh_on_check = true,
checked_func = function()
return util.tableEquals(self.hotkeys[hotkey], self.defaults[hotkey])
end,
callback = function()
self.hotkeys[hotkey] = util.tableDeepCopy(self.defaults[hotkey])
self.updated = true
callback = function(touchmenu_instance)
local function do_remove()
self.hotkeys[hotkey] = util.tableDeepCopy(self.defaults[hotkey])
self.updated = true
touchmenu_instance:updateItems()
end
if self.hotkeys[hotkey] and next(self.hotkeys[hotkey]) then
Dispatcher.removeActions(self.hotkeys[hotkey], do_remove)
else -- If no actions are selected, just update the defaults
do_remove()
end
end,
})
end
table.insert(sub_items, {
text = _("No action"),
keep_menu_open = true,
no_refresh_on_check = true,
separator = true,
checked_func = function()
return self.hotkeys[hotkey] == nil
return self.hotkeys[hotkey] == nil or next(self.hotkeys[hotkey]) == nil
end,
callback = function()
self.hotkeys[hotkey] = nil
self.updated = true
callback = function(touchmenu_instance)
local function do_remove()
self.hotkeys[hotkey] = nil
self.updated = true
touchmenu_instance:updateItems()
end
if self.hotkeys[hotkey] and next(self.hotkeys[hotkey]) then
Dispatcher.removeActions(self.hotkeys[hotkey], do_remove)
else
do_remove()
end
end,
})
Dispatcher:addSubMenu(self, sub_items, self.hotkeys, hotkey)