From 3def243f9720a6f9c8d7c5bd04bac03d39431fcf Mon Sep 17 00:00:00 2001 From: David <97603719+Commodore64user@users.noreply.github.com> Date: Thu, 10 Apr 2025 22:42:48 +0100 Subject: [PATCH] [FocusManager] correct cursor keys in RTL (#13362) --- frontend/apps/reader/modules/readerpaging.lua | 14 +++++++----- .../apps/reader/modules/readerrolling.lua | 14 +++++++----- frontend/ui/widget/focusmanager.lua | 22 ++++++++++++++++--- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/frontend/apps/reader/modules/readerpaging.lua b/frontend/apps/reader/modules/readerpaging.lua index 9a6ed76de..5ce586583 100644 --- a/frontend/apps/reader/modules/readerpaging.lua +++ b/frontend/apps/reader/modules/readerpaging.lua @@ -51,19 +51,21 @@ end function ReaderPaging:onGesture() end function ReaderPaging:registerKeyEvents() + local nextKey = BD.mirroredUILayout() and "Left" or "Right" + local prevKey = BD.mirroredUILayout() and "Right" or "Left" if Device:hasDPad() and Device:useDPadAsActionKeys() then if G_reader_settings:isTrue("left_right_keys_turn_pages") then - self.key_events.GotoNextPage = { { { "RPgFwd", "LPgFwd", "Right", " " } }, event = "GotoViewRel", args = 1, } - self.key_events.GotoPrevPage = { { { "RPgBack", "LPgBack", "Left" } }, event = "GotoViewRel", args = -1, } + self.key_events.GotoNextPage = { { { "RPgFwd", "LPgFwd", nextKey, " " } }, event = "GotoViewRel", args = 1, } + self.key_events.GotoPrevPage = { { { "RPgBack", "LPgBack", prevKey } }, event = "GotoViewRel", args = -1, } elseif G_reader_settings:nilOrFalse("left_right_keys_turn_pages") then - self.key_events.GotoNextChapter = { { "Right" }, event = "GotoNextChapter", args = 1, } - self.key_events.GotoPrevChapter = { { "Left" }, event = "GotoPrevChapter", args = -1, } + self.key_events.GotoNextChapter = { { nextKey }, event = "GotoNextChapter", args = 1, } + self.key_events.GotoPrevChapter = { { prevKey }, event = "GotoPrevChapter", args = -1, } self.key_events.GotoNextPage = { { { "RPgFwd", "LPgFwd", " " } }, event = "GotoViewRel", args = 1, } self.key_events.GotoPrevPage = { { { "RPgBack", "LPgBack" } }, event = "GotoViewRel", args = -1, } end elseif Device:hasKeys() then - self.key_events.GotoNextPage = { { { "RPgFwd", "LPgFwd", not Device:hasFewKeys() and "Right" } }, event = "GotoViewRel", args = 1, } - self.key_events.GotoPrevPage = { { { "RPgBack", "LPgBack", not Device:hasFewKeys() and "Left" } }, event = "GotoViewRel", args = -1, } + self.key_events.GotoNextPage = { { { "RPgFwd", "LPgFwd", not Device:hasFewKeys() and nextKey } }, event = "GotoViewRel", args = 1, } + self.key_events.GotoPrevPage = { { { "RPgBack", "LPgBack", not Device:hasFewKeys() and prevKey } }, event = "GotoViewRel", args = -1, } self.key_events.GotoNextPos = { { "Down" }, event = "GotoPosRel", args = 1, } self.key_events.GotoPrevPos = { { "Up" }, event = "GotoPosRel", args = -1, } end diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index 0d152375e..5084dec11 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -116,13 +116,15 @@ end function ReaderRolling:onGesture() end function ReaderRolling:registerKeyEvents() + local nextKey = BD.mirroredUILayout() and "Left" or "Right" + local prevKey = BD.mirroredUILayout() and "Right" or "Left" if Device:hasDPad() and Device:useDPadAsActionKeys() then if G_reader_settings:isTrue("left_right_keys_turn_pages") then - self.key_events.GotoNextView = { { { "LPgFwd", "Right" } }, event = "GotoViewRel", args = 1, } - self.key_events.GotoPrevView = { { { "LPgBack", "Left" } }, event = "GotoViewRel", args = -1, } + self.key_events.GotoNextView = { { { "LPgFwd", nextKey } }, event = "GotoViewRel", args = 1, } + self.key_events.GotoPrevView = { { { "LPgBack", prevKey } }, event = "GotoViewRel", args = -1, } elseif G_reader_settings:nilOrFalse("left_right_keys_turn_pages") then - self.key_events.GotoNextChapter = { { "Right" }, event = "GotoNextChapter", args = 1, } - self.key_events.GotoPrevChapter = { { "Left" }, event = "GotoPrevChapter", args = -1, } + self.key_events.GotoNextChapter = { { nextKey }, event = "GotoNextChapter", args = 1, } + self.key_events.GotoPrevChapter = { { prevKey }, event = "GotoPrevChapter", args = -1, } self.key_events.GotoNextView = { { "LPgFwd" }, event = "GotoViewRel", args = 1, } self.key_events.GotoPrevView = { { "LPgBack" }, event = "GotoViewRel", args = -1, } end @@ -133,8 +135,8 @@ function ReaderRolling:registerKeyEvents() self.key_events.MoveDown = { { "Down" }, event = "Panning", args = {0, 1}, } end if (Device:hasDPad() and not Device:useDPadAsActionKeys()) or (Device:hasKeys() and not Device:useDPadAsActionKeys()) then - self.key_events.GotoNextView = { { { "RPgFwd", "LPgFwd", "Right" } }, event = "GotoViewRel", args = 1, } - self.key_events.GotoPrevView = { { { "RPgBack", "LPgBack", "Left" } }, event = "GotoViewRel", args = -1, } + self.key_events.GotoNextView = { { { "RPgFwd", "LPgFwd", nextKey } }, event = "GotoViewRel", args = 1, } + self.key_events.GotoPrevView = { { { "RPgBack", "LPgBack", prevKey } }, event = "GotoViewRel", args = -1, } end if Device:hasKeyboard() and not Device.k3_alt_plus_key_kernel_translated then self.key_events.GotoFirst = { { "1" }, event = "GotoPercent", args = 0, } diff --git a/frontend/ui/widget/focusmanager.lua b/frontend/ui/widget/focusmanager.lua index 2564a2c96..978c53050 100644 --- a/frontend/ui/widget/focusmanager.lua +++ b/frontend/ui/widget/focusmanager.lua @@ -1,9 +1,10 @@ -local bit = require("bit") +local BD = require("ui/bidi") local Device = require("device") local Event = require("ui/event") local InputContainer = require("ui/widget/container/inputcontainer") -local logger = require("logger") local UIManager = require("ui/uimanager") +local bit = require("bit") +local logger = require("logger") local util = require("util") --[[ Wrapper Widget that manages focus for a whole dialog @@ -156,6 +157,7 @@ function FocusManager:onFocusHalfMove(args) end elseif direction == "left" then dx = - math.floor(#row / 2) + if BD.mirroredUILayout() then dx = -dx end if dx == 0 then dx = -1 elseif dx + x <= 0 then @@ -163,12 +165,19 @@ function FocusManager:onFocusHalfMove(args) end elseif direction == "right" then dx = math.floor(#row / 2) + if BD.mirroredUILayout() then dx = -dx end if dx == 0 then dx = 1 elseif dx + x > #row then - dx = #row - y -- last column + dx = #row - x -- last column end end + if dx ~= 0 and BD.mirroredUILayout() then + -- When in RTL we mirror horizontally the elements/buttons on the screen, however we don't mirror self.layout + -- therefore we must account for this when moving the focus. Since we're already inverting dx in the FocusMove + -- method, we need to "unfix" our value here, before calling onFocusMove, where it will be flipped again. + dx = -dx + end return self:onFocusMove({dx, dy}) end @@ -214,6 +223,13 @@ function FocusManager:onFocusMove(args) end local dx, dy = unpack(args) + -- Flip horizontal direction in RTL mode + if dx ~= 0 and BD.mirroredUILayout() then + -- When in RTL we mirror horizontally the elements/buttons on the screen, however we don't mirror self.layout + -- therefore we must account for this when moving the focus. + dx = -dx + end + if (dx ~= 0 and not self.movement_allowed.x) or (dy ~= 0 and not self.movement_allowed.y) then return true