diff --git a/frontend/apps/reader/modules/readerconfig.lua b/frontend/apps/reader/modules/readerconfig.lua index 563375b3f..83261ce4a 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.highlight:onStopHighlightIndicator(true) -- stop any text selection in progress, if applicable + self.ui.keyselection:onStopHighlightIndicator(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 91b45e94f..ab86d1cd8 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -86,15 +86,9 @@ function ReaderHighlight:init() self.screen_w = Screen:getWidth() self.screen_h = Screen:getHeight() self.select_mode = false -- extended highlighting - self._start_indicator_highlight = false - self._current_indicator_pos = nil - self._previous_indicator_pos = nil - self._last_indicator_move_args = {dx = 0, dy = 0, distance = 0, time = time:now()} self._fallback_drawer = self.view.highlight.saved_drawer -- "lighten" self._fallback_color = self.view.highlight.saved_color -- "yellow" or "gray" - self:registerKeyEvents() - self._highlight_buttons = { -- highlight and add_note are for the document itself, -- so we put them first. @@ -271,31 +265,6 @@ end function ReaderHighlight:onGesture() end -function ReaderHighlight: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} } - -- 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} } - self.key_events.HighlightPress = { { "Press" } } - end - if Device:hasScreenKB() or Device:hasKeyboard() then - local modifier = Device:hasScreenKB() and "ScreenKB" or "Shift" - -- Used for text selection with dpad/keys - local QUICK_INDICATOR_MOVE = true - self.key_events.QuickUpHighlightIndicator = { { modifier, "Up" }, event = "MoveHighlightIndicator", args = {0, -1, QUICK_INDICATOR_MOVE} } - self.key_events.QuickDownHighlightIndicator = { { modifier, "Down" }, event = "MoveHighlightIndicator", args = {0, 1, QUICK_INDICATOR_MOVE} } - 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 - end -end - -ReaderHighlight.onPhysicalKeyboardConnected = ReaderHighlight.registerKeyEvents - function ReaderHighlight:setupTouchZones() if not Device:isTouchDevice() then return end local hold_pan_rate = G_reader_settings:readSetting("hold_pan_rate") @@ -1738,7 +1707,7 @@ function ReaderHighlight:onHold(arg, ges) with_title_bar = false, -- more room for image fullscreen = true, }) - self:onStopHighlightIndicator() + self.ui.keyselection:onStopHighlightIndicator() return true end @@ -2743,240 +2712,6 @@ function ReaderHighlight:onClose(keep_highlight) end end --- dpad/keys support -function ReaderHighlight:onHighlightPress(skip_tap_check) - if not self._current_indicator_pos then return false end - if self._start_indicator_highlight then - self:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() - return true - end - -- Check if we're in select mode (or extending an existing highlight) - if self.select_mode and self.highlight_idx then - self:onHold(nil, self:_createHighlightGesture("hold")) - self:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() - return true - end - -- Attempt to open an existing highlight - if not skip_tap_check and self:onTap(nil, self:_createHighlightGesture("tap")) then - self:onStopHighlightIndicator(true) -- need_clear_selection=true - return true - end - -- no existing highlight at current indicator position: start hold - self._start_indicator_highlight = true - self:onHold(nil, self:_createHighlightGesture("hold")) - - if not (self.ui.rolling and self.selected_text and self.selected_text.sboxes and #self.selected_text.sboxes > 0) then - return true - end - -- With crengine, selected_text.sboxes have good coordinates, so we'll borrow them. - local pos = self.selected_text.sboxes[1] - local margins = self.ui.document.configurable.h_page_margins[1] + self.ui.document.configurable.h_page_margins[2] - local two_column_mode = self.ui.document.configurable.visible_pages == 2 - local effective_width = two_column_mode and (self.screen_w - margins) / 2 or self.screen_w - margins - -- When words are split (and hyphenated) due to line breaks, they create selection boxes that are almost as wide as the - -- effective_width, so we need to check if that is the case, in order to handle those cases properly. We cannot precisely - -- and easily recognise hyphenated words in the front end, so a heuristic approach is used, it goes in two steps. - -- Step one: check if our box is a 'big boy'. We must allow some room for unknown variables like publisher-embedded padding, etc. - local is_word_split = pos.w > 0.7 * effective_width - -- Step two: weed out false positives (i.e long words) by comparing words found at different box coordinates. - if is_word_split then - -- In the case of a split (and hyphenated) word, we should get distinct words at different coordinates inside the box, - -- false positives on the other hand, should return the same word at different coordinates. - local word_at_pos1 = self.ui.document:getWordFromPosition({ - x = BD.mirroredUILayout() and pos.x + pos.w or pos.x, - y = pos.y + pos.h * 1/4 -- puts us at a potential line 1 of 2 - }) - local word_at_pos2 = self.ui.document:getWordFromPosition({ - x = BD.mirroredUILayout() and pos.x or pos.x + pos.w, - y = pos.y + pos.h * 3/4 -- puts us at a potential line 2 of 2 - }) - local does_word_at_pos1_match = word_at_pos1 and word_at_pos1.word == self.selected_text.text - local does_word_at_pos2_match = word_at_pos2 and word_at_pos2.word == self.selected_text.text - -- If all 3 words are a match, then we're likely not a split word, just a very long one, something worthy of floccinaucinihilipilification. - if does_word_at_pos1_match and does_word_at_pos2_match then - is_word_split = false -- check mate - else -- We're reasonably sure the word was split (and hyphenated). Re-select the original word to ensure the correct word is highlighted. - self.ui.document:getWordFromPosition({ - x = BD.mirroredUILayout() and pos.x + pos.w or pos.x, - y = pos.y + pos.h * 3/4 - }) - end - end - - -- helper function to update crosshairs positioning and self.hold_pos - local function updatePositions(hold_x, hold_y, indicator_x, indicator_y) - self.hold_pos = self.view:screenToPageTransform({ x = hold_x, y = hold_y }) - UIManager:setDirty(self.dialog, "ui", self._current_indicator_pos) - self._current_indicator_pos.x = indicator_x - self._current_indicator_pos.y = indicator_y - end - -- Determine positions based on word type and layout. - if is_word_split then - if BD.mirroredUILayout() then -- RTL - updatePositions( - pos.x + pos.w, -- rightmost point - pos.y + pos.h * 3 / 4, -- adjusted vertical position - pos.x + pos.w, - pos.y + pos.h * 3 / 4 - self._current_indicator_pos.h / 2 - ) - else - updatePositions( - pos.x, -- leftmost point - pos.y + pos.h * 3 / 4, -- adjusted vertical position - pos.x, - pos.y + pos.h * 3 / 4 - self._current_indicator_pos.h / 2 - ) - end - else - updatePositions( - -- set hold_pos to center of selected_text to make center selection more stable, not JITted at edge - pos.x + pos.w / 2, -- center of word horizontally - pos.y + pos.h / 2, -- center of word vertically - pos.x + pos.w / 2 - self._current_indicator_pos.w / 2, - pos.y + pos.h / 2 - self._current_indicator_pos.h / 2 - ) - end - return true -end - -function ReaderHighlight:onHighlightModifierPress() - 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) - 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.long_hold_reached = true - self:onHoldRelease(nil, self:_createHighlightGesture("hold_release")) - self:onStopHighlightIndicator() - return true -end - -function ReaderHighlight:onStartHighlightIndicator() - -- disable long-press icon (poke-ball), as it is triggered constantly due to NT devices needing a workaround for text selection to work. - self.long_hold_reached_action = function() end - if self.view.visible_area and not self._current_indicator_pos then - -- set start position to centor of page - local rect = self._previous_indicator_pos - if not rect then - rect = Geom:new() - rect.x = self.view.visible_area.w / 2 - rect.y = self.view.visible_area.h / 2 - rect.w = Size.item.height_default - rect.h = rect.w - end - self._current_indicator_pos = rect - self.view.highlight.indicator = rect - UIManager:setDirty(self.dialog, "ui", rect) - return true - end - return false -end - -function ReaderHighlight:onStopHighlightIndicator(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.select_mode and self.highlight_idx then - self.select_mode = false - if self.ui.annotation.annotations[self.highlight_idx].is_tmp then - self:deleteHighlight(self.highlight_idx) -- temporary highlight, delete it - else - UIManager:setDirty(self.dialog, "ui", self.view.flipping:getRefreshRegion()) - end - self.highlight_idx = nil - end - - local rect = self._current_indicator_pos - self._previous_indicator_pos = rect - self._start_indicator_highlight = false - self._current_indicator_pos = nil - self.view.highlight.indicator = nil - UIManager:setDirty(self.dialog, "ui", rect) - if need_clear_selection then - self:clear() - end - return true -end - -function ReaderHighlight:onMoveHighlightIndicator(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 - local quick_move_distance_dy = self.view.visible_area.h * (1/5) - -- single move distance, user adjustable, default value (4) capable to move on word with small font size and narrow line height - local move_distance = Size.item.height_default / (G_reader_settings:readSetting("highlight_non_touch_factor") or 4) - local rect = self._current_indicator_pos:copy() - if quick_move then - rect.x = rect.x + quick_move_distance_dx * dx - rect.y = rect.y + quick_move_distance_dy * dy - else - local now = time:now() - if dx == self._last_indicator_move_args.dx and dy == self._last_indicator_move_args.dy then - local diff = now - self._last_indicator_move_args.time - -- if user presses same arrow key within 1 second (default, user adjustable), speed up - -- double press: 4 single move distances, usually move to next word or line - -- triple press: 16 single distances, usually skip several words or lines - -- quadruple press: 64 single distances, almost move to screen edge - if G_reader_settings:nilOrTrue("highlight_non_touch_spedup") then - -- user selects whether to use 'constant' or [this] 'sped up' rate (speed-up on by default) - local t_inter = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 - if diff < time.s( t_inter ) then - move_distance = self._last_indicator_move_args.distance * 4 - end - end - end - rect.x = rect.x + move_distance * dx - rect.y = rect.y + move_distance * dy - self._last_indicator_move_args.distance = move_distance - self._last_indicator_move_args.dx = dx - self._last_indicator_move_args.dy = dy - self._last_indicator_move_args.time = now - end - if rect.x < 0 then - rect.x = 0 - end - if rect.x + rect.w > self.view.visible_area.w then - rect.x = self.view.visible_area.w - rect.w - end - -- make sure we account for both the status bar and alt status bar so we don't overlap them with the indicator - local alt_status_bar_height = 0 - if self.ui.rolling and self.ui.document.configurable.status_line == 0 then - alt_status_bar_height = self.ui.document:getHeaderHeight() - end - if rect.y < alt_status_bar_height then - rect.y = alt_status_bar_height - end - local footer_height = self.view.footer_visible and self.view.footer:getHeight() or 0 - local status_bar_height = self.ui.rolling and footer_height or 0 -- for PDFs, status bar is already accounted for - if rect.y + rect.h > self.view.visible_area.h - status_bar_height then - rect.y = self.view.visible_area.h - status_bar_height - rect.h - end - UIManager:setDirty(self.dialog, "ui", self._current_indicator_pos) - self._current_indicator_pos = rect - self.view.highlight.indicator = rect - UIManager:setDirty(self.dialog, "ui", rect) - if self._start_indicator_highlight then - self:onHoldPan(nil, self:_createHighlightGesture("hold_pan")) - end - return true - end - return false -end - -function ReaderHighlight:_createHighlightGesture(gesture) - local point = self._current_indicator_pos:copy() - point.x = point.x + point.w / 2 - point.y = point.y + point.h / 2 - point.w = 0 - point.h = 0 - return { - ges = gesture, - pos = point, - time = time.realtime(), - } -end return ReaderHighlight diff --git a/frontend/apps/reader/modules/readerkeyselection.lua b/frontend/apps/reader/modules/readerkeyselection.lua new file mode 100644 index 000000000..15e030d1b --- /dev/null +++ b/frontend/apps/reader/modules/readerkeyselection.lua @@ -0,0 +1,288 @@ +local BD = require("ui/bidi") +local Device = require("device") +local Geom = require("ui/geometry") +local InputContainer = require("ui/widget/container/inputcontainer") +local UIManager = require("ui/uimanager") +local logger = require("logger") +local Size = require("ui/size") +local time = require("ui/time") +local Screen = Device.screen + +local ReaderKeySelection = InputContainer:extend{} + +function ReaderKeySelection:init() + if Device:isTouchDevice() and not Device:hasDPad() then + return + end + self:registerKeyEvents() + self.screen_w = Screen:getWidth() + self.screen_h = Screen:getHeight() + self._start_indicator_highlight = false + self._current_indicator_pos = nil + self._previous_indicator_pos = nil + self._last_indicator_move_args = {dx = 0, dy = 0, distance = 0, time = time:now()} +end + +function ReaderKeySelection:onSetDimensions(dimen) + self.screen_w, self.screen_h = dimen.w, dimen.h +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} } + -- 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} } + self.key_events.HighlightPress = { { "Press" } } + end + if Device:hasScreenKB() or Device:hasKeyboard() then + local modifier = Device:hasScreenKB() and "ScreenKB" or "Shift" + -- Used for text selection with dpad/keys + local QUICK_INDICATOR_MOVE = true + self.key_events.QuickUpHighlightIndicator = { { modifier, "Up" }, event = "MoveHighlightIndicator", args = {0, -1, QUICK_INDICATOR_MOVE} } + self.key_events.QuickDownHighlightIndicator = { { modifier, "Down" }, event = "MoveHighlightIndicator", args = {0, 1, QUICK_INDICATOR_MOVE} } + 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 + end +end +ReaderKeySelection.onPhysicalKeyboardConnected = ReaderKeySelection.registerKeyEvents + +function ReaderKeySelection:onHighlightPress(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() + 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() + 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 + return true + end + -- no existing highlight at current indicator position: start hold + self._start_indicator_highlight = true + self.ui.highlight:onHold(nil, self:_createHighlightGesture("hold")) + + if not (self.ui.rolling and self.ui.highlight.selected_text and self.ui.highlight.selected_text.sboxes and #self.ui.highlight.selected_text.sboxes > 0) then + return true + end + -- With crengine, selected_text.sboxes have good coordinates, so we'll borrow them. + local pos = self.ui.highlight.selected_text.sboxes[1] + local margins = self.ui.document.configurable.h_page_margins[1] + self.ui.document.configurable.h_page_margins[2] + local two_column_mode = self.ui.document.configurable.visible_pages == 2 + local effective_width = two_column_mode and (self.screen_w - margins) / 2 or self.screen_w - margins + -- When words are split (and hyphenated) due to line breaks, they create selection boxes that are almost as wide as the + -- effective_width, so we need to check if that is the case, in order to handle those cases properly. We cannot precisely + -- and easily recognise hyphenated words in the front end, so a heuristic approach is used, it goes in two steps. + -- Step one: check if our box is a 'big boy'. We must allow some room for unknown variables like publisher-embedded padding, etc. + local is_word_split = pos.w > 0.7 * effective_width + -- Step two: weed out false positives (i.e long words) by comparing words found at different box coordinates. + if is_word_split then + -- In the case of a split (and hyphenated) word, we should get distinct words at different coordinates inside the box, + -- false positives on the other hand, should return the same word at different coordinates. + local word_at_pos1 = self.ui.document:getWordFromPosition({ + x = BD.mirroredUILayout() and pos.x + pos.w or pos.x, + y = pos.y + pos.h * 1/4 -- puts us at a potential line 1 of 2 + }) + local word_at_pos2 = self.ui.document:getWordFromPosition({ + x = BD.mirroredUILayout() and pos.x or pos.x + pos.w, + y = pos.y + pos.h * 3/4 -- puts us at a potential line 2 of 2 + }) + local does_word_at_pos1_match = word_at_pos1 and word_at_pos1.word == self.ui.highlight.selected_text.text + local does_word_at_pos2_match = word_at_pos2 and word_at_pos2.word == self.ui.highlight.selected_text.text + -- If all 3 words are a match, then we're likely not a split word, just a very long one, something worthy of floccinaucinihilipilification. + if does_word_at_pos1_match and does_word_at_pos2_match then + is_word_split = false -- check mate + else -- We're reasonably sure the word was split (and hyphenated). Re-select the original word to ensure the correct word is highlighted. + self.ui.document:getWordFromPosition({ + x = BD.mirroredUILayout() and pos.x + pos.w or pos.x, + y = pos.y + pos.h * 3/4 + }) + end + end + + -- helper function to update crosshairs positioning and self.hold_pos + local function updatePositions(hold_x, hold_y, indicator_x, indicator_y) + self.hold_pos = self.view:screenToPageTransform({ x = hold_x, y = hold_y }) + UIManager:setDirty(self.dialog, "ui", self._current_indicator_pos) + self._current_indicator_pos.x = indicator_x + self._current_indicator_pos.y = indicator_y + end + -- Determine positions based on word type and layout. + if is_word_split then + if BD.mirroredUILayout() then -- RTL + updatePositions( + pos.x + pos.w, -- rightmost point + pos.y + pos.h * 3 / 4, -- adjusted vertical position + pos.x + pos.w, + pos.y + pos.h * 3 / 4 - self._current_indicator_pos.h / 2 + ) + else + updatePositions( + pos.x, -- leftmost point + pos.y + pos.h * 3 / 4, -- adjusted vertical position + pos.x, + pos.y + pos.h * 3 / 4 - self._current_indicator_pos.h / 2 + ) + end + else + updatePositions( + -- set hold_pos to center of selected_text to make center selection more stable, not JITted at edge + pos.x + pos.w / 2, -- center of word horizontally + pos.y + pos.h / 2, -- center of word vertically + pos.x + pos.w / 2 - self._current_indicator_pos.w / 2, + pos.y + pos.h / 2 - self._current_indicator_pos.h / 2 + ) + end + return true +end + +function ReaderKeySelection:onHighlightModifierPress() + 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) + 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() + return true +end + +function ReaderKeySelection:onStartHighlightIndicator() + -- 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 + -- set start position to centor of page + local rect = self._previous_indicator_pos + if not rect then + rect = Geom:new() + rect.x = self.view.visible_area.w / 2 + rect.y = self.view.visible_area.h / 2 + rect.w = Size.item.height_default + rect.h = rect.w + end + self._current_indicator_pos = rect + self.view.highlight.indicator = rect + UIManager:setDirty(self.dialog, "ui", rect) + return true + end + return false +end + +function ReaderKeySelection:onStopHighlightIndicator(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 + self.ui.highlight.select_mode = false + if self.ui.annotation.annotations[self.ui.highlight.highlight_idx].is_tmp then + self.ui.highlight:deleteHighlight(self.ui.highlight.highlight_idx) -- temporary highlight, delete it + else + UIManager:setDirty(self.dialog, "ui", self.view.flipping:getRefreshRegion()) + end + self.ui.highlight.highlight_idx = nil + end + + local rect = self._current_indicator_pos + self._previous_indicator_pos = rect + self._start_indicator_highlight = false + self._current_indicator_pos = nil + self.view.highlight.indicator = nil + UIManager:setDirty(self.dialog, "ui", rect) + if need_clear_selection then + self.ui.highlight:clear() + end + return true +end + +function ReaderKeySelection:onMoveHighlightIndicator(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 + local quick_move_distance_dy = self.view.visible_area.h * (1/5) + -- single move distance, user adjustable, default value (4) capable to move on word with small font size and narrow line height + local move_distance = Size.item.height_default / (G_reader_settings:readSetting("highlight_non_touch_factor") or 4) + local rect = self._current_indicator_pos:copy() + if quick_move then + rect.x = rect.x + quick_move_distance_dx * dx + rect.y = rect.y + quick_move_distance_dy * dy + else + local now = time:now() + if dx == self._last_indicator_move_args.dx and dy == self._last_indicator_move_args.dy then + local diff = now - self._last_indicator_move_args.time + -- if user presses same arrow key within 1 second (default, user adjustable), speed up + -- double press: 4 single move distances, usually move to next word or line + -- triple press: 16 single distances, usually skip several words or lines + -- quadruple press: 64 single distances, almost move to screen edge + if G_reader_settings:nilOrTrue("highlight_non_touch_spedup") then + -- user selects whether to use 'constant' or [this] 'sped up' rate (speed-up on by default) + local t_inter = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 + if diff < time.s( t_inter ) then + move_distance = self._last_indicator_move_args.distance * 4 + end + end + end + rect.x = rect.x + move_distance * dx + rect.y = rect.y + move_distance * dy + self._last_indicator_move_args.distance = move_distance + self._last_indicator_move_args.dx = dx + self._last_indicator_move_args.dy = dy + self._last_indicator_move_args.time = now + end + if rect.x < 0 then + rect.x = 0 + end + if rect.x + rect.w > self.view.visible_area.w then + rect.x = self.view.visible_area.w - rect.w + end + -- make sure we account for both the status bar and alt status bar so we don't overlap them with the indicator + local alt_status_bar_height = 0 + if self.ui.rolling and self.ui.document.configurable.status_line == 0 then + alt_status_bar_height = self.ui.document:getHeaderHeight() + end + if rect.y < alt_status_bar_height then + rect.y = alt_status_bar_height + end + local footer_height = self.view.footer_visible and self.view.footer:getHeight() or 0 + local status_bar_height = self.ui.rolling and footer_height or 0 -- for PDFs, status bar is already accounted for + if rect.y + rect.h > self.view.visible_area.h - status_bar_height then + rect.y = self.view.visible_area.h - status_bar_height - rect.h + end + UIManager:setDirty(self.dialog, "ui", self._current_indicator_pos) + self._current_indicator_pos = rect + self.view.highlight.indicator = rect + UIManager:setDirty(self.dialog, "ui", rect) + if self._start_indicator_highlight then + self.ui.highlight:onHoldPan(nil, self:_createHighlightGesture("hold_pan")) + end + return true + end + return false +end + +function ReaderKeySelection:_createHighlightGesture(gesture) + local point = self._current_indicator_pos:copy() + point.x = point.x + point.w / 2 + point.y = point.y + point.h / 2 + point.w = 0 + point.h = 0 + return { + ges = gesture, + pos = point, + time = time.realtime(), + } +end + +return ReaderKeySelection diff --git a/frontend/apps/reader/modules/readermenu.lua b/frontend/apps/reader/modules/readermenu.lua index 81ce9ef04..9480c05b0 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.highlight:onStopHighlightIndicator(true) -- stop any text selection in progress, if applicable + self.ui.keyselection:onStopHighlightIndicator(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 db10be04f..7a454bced 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -41,6 +41,7 @@ local ReaderHandMade = require("apps/reader/modules/readerhandmade") local ReaderHinting = require("apps/reader/modules/readerhinting") local ReaderHighlight = require("apps/reader/modules/readerhighlight") local ReaderScrolling = require("apps/reader/modules/readerscrolling") +local ReaderKeySelection = require("apps/reader/modules/readerkeyselection") local ReaderKoptListener = require("apps/reader/modules/readerkoptlistener") local ReaderLink = require("apps/reader/modules/readerlink") local ReaderMenu = require("apps/reader/modules/readermenu") @@ -228,6 +229,13 @@ function ReaderUI:init() ui = self, document = self.document, }) + -- text selection with cursor keys + self:registerModule("keyselection", ReaderKeySelection:new{ + dialog = self.dialog, + view = self.view, + ui = self, + document = self.document, + }) -- screenshot controller self:registerModule("screenshot", Screenshoter:new{ prefix = 'Reader', diff --git a/plugins/hotkeys.koplugin/main.lua b/plugins/hotkeys.koplugin/main.lua index 745ec7f6c..2747c3d49 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.highlight then self.ui.highlight:onStopHighlightIndicator(true) end + if self.ui.keyselection then self.ui.keyselection:onStopHighlightIndicator(true) end local hotkey_action_list = self.hotkeys[hotkey] local context = self.is_docless and "FileManager" or "Reader" if hotkey_action_list == nil then