From 952ebafe2da835b4f7db6be063ad8f64e80f8175 Mon Sep 17 00:00:00 2001 From: Commodore64user <97603719+Commodore64user@users.noreply.github.com> Date: Tue, 26 May 2026 12:19:27 +0100 Subject: [PATCH] KeySelection: add new event handlers and call startIndicator from keyselection instead of readerui --- frontend/apps/reader/modules/readerconfig.lua | 2 +- .../apps/reader/modules/readerhighlight.lua | 4 +- .../reader/modules/readerkeyselection.lua | 55 +++++++++++++++---- frontend/apps/reader/modules/readermenu.lua | 2 +- frontend/apps/reader/readerui.lua | 3 - plugins/hotkeys.koplugin/main.lua | 2 +- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/frontend/apps/reader/modules/readerconfig.lua b/frontend/apps/reader/modules/readerconfig.lua index 83261ce4a..e2d7a445b 100644 --- a/frontend/apps/reader/modules/readerconfig.lua +++ b/frontend/apps/reader/modules/readerconfig.lua @@ -135,7 +135,7 @@ function ReaderConfig:onShowConfigMenu() covers_footer = true, close_callback = function() self:onCloseCallback() end, } - self.ui.keyselection:onStopHighlightIndicator(true) -- stop any text selection in progress, if applicable + self.ui.keyselection:stopHighlightIndicator(true) -- stop any text selection in progress, if applicable self.ui:handleEvent(Event:new("DisableHinting")) -- show last used panel when opening config dialog self.config_dialog:onShowConfigPanel(self.last_panel_index) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index b8dc51837..aa6657e98 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -98,7 +98,7 @@ function ReaderHighlight:init() this:startSelection(index) this:onClose() if not Device:isTouchDevice() then - self:onStartHighlightIndicator() + self.ui.keyselection:startHighlightIndicator() end end, } @@ -1591,7 +1591,7 @@ function ReaderHighlight:onHold(arg, ges) with_title_bar = false, -- more room for image fullscreen = true, }) - self.ui.keyselection:onStopHighlightIndicator() + self.ui.keyselection:stopHighlightIndicator() return true end diff --git a/frontend/apps/reader/modules/readerkeyselection.lua b/frontend/apps/reader/modules/readerkeyselection.lua index 490efbcf3..40193fe71 100644 --- a/frontend/apps/reader/modules/readerkeyselection.lua +++ b/frontend/apps/reader/modules/readerkeyselection.lua @@ -40,8 +40,9 @@ end function ReaderKeySelection:registerKeyEvents() if Device:hasDPad() then self.key_events.StopHighlightIndicator = { { Device.input.group.Back }, args = true } -- true: clear highlight selection - self.key_events.UpHighlightIndicator = { { "Up" }, event = "MoveHighlightIndicator", args = {0, -1} } - self.key_events.DownHighlightIndicator = { { "Down" }, event = "MoveHighlightIndicator", args = {0, 1} } + local event = Device:useDPadAsActionKeys() and "StartOrMoveHighlightIndicator" or "MoveHighlightIndicator" + self.key_events.UpHighlightIndicator = { { "Up" }, event = event, args = {0, -1} } + self.key_events.DownHighlightIndicator = { { "Down" }, event = event, args = {0, 1} } -- let hasFewKeys device move the indicator left self.key_events.LeftHighlightIndicator = { { "Left" }, event = "MoveHighlightIndicator", args = {-1, 0} } self.key_events.RightHighlightIndicator = { { "Right" }, event = "MoveHighlightIndicator", args = {1, 0} } @@ -56,7 +57,7 @@ function ReaderKeySelection:registerKeyEvents() self.key_events.QuickLeftHighlightIndicator = { { modifier, "Left" }, event = "MoveHighlightIndicator", args = {-1, 0, QUICK_INDICATOR_MOVE} } self.key_events.QuickRightHighlightIndicator = { { modifier, "Right" }, event = "MoveHighlightIndicator", args = {1, 0, QUICK_INDICATOR_MOVE} } self.key_events.HighlightModifierPress = { { modifier, "Press" } } - -- onStartHighlightIndicator (H) is handled by hotkeys.koplugin + -- startHighlightIndicator (H) is handled by hotkeys.koplugin end end ReaderKeySelection.onPhysicalKeyboardConnected = ReaderKeySelection.registerKeyEvents @@ -181,23 +182,53 @@ function ReaderKeySelection:addToMainMenu(menu_items) end end +-- for dispatcher and hotkeys +function ReaderKeySelection:onStartHighlightIndicator() + return self:startHighlightIndicator() +end + +function ReaderKeySelection:onStopHighlightIndicator(need_clear_selection) + return self:stopHighlightIndicator(need_clear_selection) +end + function ReaderKeySelection:onHighlightPress(skip_tap_check) + return self:highlightPress(skip_tap_check) +end + +function ReaderKeySelection:onHighlightModifierPress() + return self:highlightModifierPress() +end + +function ReaderKeySelection:onMoveHighlightIndicator(args) + return self:moveHighlightIndicator(args) +end + +function ReaderKeySelection:onStartOrMoveHighlightIndicator(args) + if not self._current_indicator_pos then + self:startHighlightIndicator() + else + self:moveHighlightIndicator(args) + end + return true +end + +function ReaderKeySelection:highlightPress(skip_tap_check) if not self._current_indicator_pos then return false end if self._start_indicator_highlight then self.ui.highlight:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() + self:stopHighlightIndicator() return true end -- Check if we're in select mode (or extending an existing highlight) if self.ui.highlight.select_mode and self.ui.highlight.highlight_idx then self.ui.highlight:onHold(nil, self:_createHighlightGesture("hold")) self.ui.highlight:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() + self:stopHighlightIndicator() return true end -- Attempt to open an existing highlight if not skip_tap_check and self.ui.highlight:onTap(nil, self:_createHighlightGesture("tap")) then - self:onStopHighlightIndicator(true) -- need_clear_selection=true + self:stopHighlightIndicator(true) -- need_clear_selection=true return true end -- no existing highlight at current indicator position: start hold @@ -278,20 +309,20 @@ function ReaderKeySelection:onHighlightPress(skip_tap_check) return true end -function ReaderKeySelection:onHighlightModifierPress() +function ReaderKeySelection:highlightModifierPress() if not self._current_indicator_pos then return false end -- let event propagate to hotkeys if not self._start_indicator_highlight then - self:onHighlightPress(true) + self:highlightPress(true) return true -- don't trigger hotkeys during text selection end -- Simulate very long-long press by setting the long hold flag. This will trigger the long-press dialog. self.ui.highlight.long_hold_reached = true self.ui.highlight:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() + self:stopHighlightIndicator() return true end -function ReaderKeySelection:onStartHighlightIndicator() +function ReaderKeySelection:startHighlightIndicator() -- disable long-press icon (poke-ball), as it is triggered constantly due to NT devices needing a workaround for text selection to work. self.ui.highlight.long_hold_reached_action = function() end if self.view.visible_area and not self._current_indicator_pos then @@ -312,7 +343,7 @@ function ReaderKeySelection:onStartHighlightIndicator() return false end -function ReaderKeySelection:onStopHighlightIndicator(need_clear_selection) +function ReaderKeySelection:stopHighlightIndicator(need_clear_selection) if not self._current_indicator_pos then return false end -- If we're in select mode and user presses back, end the selection if self.ui.highlight.select_mode and self.ui.highlight.highlight_idx then @@ -337,7 +368,7 @@ function ReaderKeySelection:onStopHighlightIndicator(need_clear_selection) return true end -function ReaderKeySelection:onMoveHighlightIndicator(args) +function ReaderKeySelection:moveHighlightIndicator(args) if self.view.visible_area and self._current_indicator_pos then local dx, dy, quick_move = unpack(args) local quick_move_distance_dx = self.view.visible_area.w * (1/5) -- quick move distance: fifth of visible_area diff --git a/frontend/apps/reader/modules/readermenu.lua b/frontend/apps/reader/modules/readermenu.lua index 9480c05b0..484ec7308 100644 --- a/frontend/apps/reader/modules/readermenu.lua +++ b/frontend/apps/reader/modules/readermenu.lua @@ -408,7 +408,7 @@ function ReaderMenu:exitOrRestart(callback, force) end function ReaderMenu:onShowMenu(tab_index, do_not_show) - self.ui.keyselection:onStopHighlightIndicator(true) -- stop any text selection in progress, if applicable + self.ui.keyselection:stopHighlightIndicator(true) -- stop any text selection in progress, if applicable if self.tab_item_table == nil then self:setUpdateItemTable() end diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 7a454bced..dc318eaeb 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -550,9 +550,6 @@ function ReaderUI:registerKeyEvents() if Device:hasKeys() then self.key_events.Home = { { "Home" } } self.key_events.Reload = { { "F5" } } - if Device:hasDPad() and Device:useDPadAsActionKeys() then - self.key_events.StartHighlightIndicator = { { { "Up", "Down" } } } - end end end diff --git a/plugins/hotkeys.koplugin/main.lua b/plugins/hotkeys.koplugin/main.lua index 2747c3d49..35abaa883 100644 --- a/plugins/hotkeys.koplugin/main.lua +++ b/plugins/hotkeys.koplugin/main.lua @@ -94,7 +94,7 @@ end function HotKeys:onHotkeyAction(hotkey) -- Note: we could have started text selection and then trigger a reflow through hotkeys (e.g., increase -- font-size) which will cause pandemonium to ensue (invalid coordinates). - if self.ui.keyselection then self.ui.keyselection:onStopHighlightIndicator(true) end + if self.ui.keyselection then self.ui.keyselection:stopHighlightIndicator(true) end local hotkey_action_list = self.hotkeys[hotkey] local context = self.is_docless and "FileManager" or "Reader" if hotkey_action_list == nil then