From adf60de7317c79f412144af6b76150dd43d85692 Mon Sep 17 00:00:00 2001 From: Commodore64user <97603719+Commodore64user@users.noreply.github.com> Date: Tue, 26 May 2026 02:38:28 +0100 Subject: [PATCH] KeySelection: add registerToMainMenu in readerkeyselection, remove old creation from readerhighlight --- .../apps/reader/modules/readerhighlight.lua | 124 +---------------- .../reader/modules/readerkeyselection.lua | 130 ++++++++++++++++++ 2 files changed, 133 insertions(+), 121 deletions(-) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index ab86d1cd8..b8dc51837 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -4,7 +4,6 @@ local ButtonDialog = require("ui/widget/buttondialog") local ButtonSelector = require("ui/widget/buttonselector") local ConfirmBox = require("ui/widget/confirmbox") local Device = require("device") -local DoubleSpinWidget = require("ui/widget/doublespinwidget") local Event = require("ui/event") local Geom = require("ui/geometry") local GestureDetector = require("device/gesturedetector") @@ -19,7 +18,6 @@ local ffiUtil = require("ffi/util") local logger = require("logger") local util = require("util") local Size = require("ui/size") -local time = require("ui/time") local _ = require("gettext") local C_ = _.pgettext local N_ = _.ngettext @@ -376,6 +374,9 @@ local long_press_action = { {_("Dictionary"), "dictionary"}, {_("Fulltext search"), "search"}, } +-- we need to expose this table to readerkeyselection +-- as here it is hidden under a isTouchDevice cap +ReaderHighlight.long_press_action = long_press_action local highlight_dialog_position = { {_("Top"), "top"}, @@ -385,16 +386,6 @@ local highlight_dialog_position = { } function ReaderHighlight:addToMainMenu(menu_items) - -- insert table to main reader menu - if not Device:isTouchDevice() and Device:hasDPad() and not Device:useDPadAsActionKeys() then - menu_items.start_content_selection = { - text = _("Start content selection"), - callback = function() - self:onStartHighlightIndicator() - end, - } - end - -- main menu Typeset local star = " ★" local hl_sub_item_table = {} @@ -814,113 +805,6 @@ Except when in two columns mode, where this is limited to showing only the previ end, }) - -- we allow user to select the rate at which the content selection tool moves through screen - if not Device:isTouchDevice() and Device:hasDPad() then - table.insert(menu_items.long_press.sub_item_table, { - text_func = function() - local reader_speed = G_reader_settings:readSetting("highlight_non_touch_factor") or 4 - local dict_speed = G_reader_settings:readSetting("highlight_non_touch_factor_dict") or 3 - return T(_("Crosshairs speed (reader/dict): %1 / %2"), reader_speed, dict_speed) - end, - callback = function(touchmenu_instance) - local reader_speed = G_reader_settings:readSetting("highlight_non_touch_factor") or 4 - local dict_speed = G_reader_settings:readSetting("highlight_non_touch_factor_dict") or 3 - local double_spin_widget = DoubleSpinWidget:new{ - left_text = _("Reader"), - left_value = reader_speed, - left_min = 0.25, - left_max = 5, - left_default = 4, - left_precision = "%.2f", - left_step = 0.25, - left_hold_step = 0.05, - right_text = _("Dictionary"), - right_value = dict_speed, - right_min = 0.25, - right_max = 5, - right_default = 3, - right_precision = "%.2f", - right_step = 0.25, - right_hold_step = 0.05, - title_text = _("Crosshairs speed"), - info_text = _("Select a decimal value from 0.25 to 5. A smaller value increases the travel distance of the crosshairs per keystroke. Font size and this value are inversely correlated, meaning a smaller font size requires a larger value and vice versa."), - callback = function(left_value, right_value) - G_reader_settings:saveSetting("highlight_non_touch_factor", left_value) - G_reader_settings:saveSetting("highlight_non_touch_factor_dict", right_value) - if touchmenu_instance then touchmenu_instance:updateItems() end - end - } - UIManager:show(double_spin_widget) - end, - }) - table.insert(menu_items.long_press.sub_item_table, { - text = _("Increase crosshairs speed on consecutive keystrokes"), - checked_func = function() - return G_reader_settings:nilOrTrue("highlight_non_touch_spedup") - end, - enabled_func = function() - return not self.view.highlight.disabled - end, - callback = function() - G_reader_settings:flipNilOrTrue("highlight_non_touch_spedup") - end, - }) - table.insert(menu_items.long_press.sub_item_table, { - text_func = function() - local highlight_non_touch_interval = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 - return T(N_("Interval for crosshairs speed increase: 1 second", "Interval for crosshairs speed increase: %1 seconds", highlight_non_touch_interval), highlight_non_touch_interval) - end, - separator = true, -- needed as this is not the last item, readerlink adds another one - enabled_func = function() - return not self.view.highlight.disabled and G_reader_settings:nilOrTrue("highlight_non_touch_spedup") - end, - callback = function(touchmenu_instance) - local curr_val = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 - local spin_widget = SpinWidget:new{ - value = curr_val, - value_min = 0.1, - value_max = 1, - precision = "%.1f", - value_step = 0.1, - default_value = 1, - title_text = _("Time interval"), - info_text = _("Select a decimal value up to 1 second. This defines the time period within which multiple keystrokes will trigger an increase in the crosshairs speed."), - callback = function(spin) - G_reader_settings:saveSetting("highlight_non_touch_interval", spin.value) - if touchmenu_instance then touchmenu_instance:updateItems() end - end - } - UIManager:show(spin_widget) - end, - }) - - -- long_press settings are under the taps_and_gestures menu, which is not available for non-touch devices - -- Clone long_press settings, and change its label, making it much more meaningful for non-touch device users. - menu_items.selection_text = { - text = _("Text selection tools"), - sub_item_table = { - menu_items.long_press.sub_item_table[1], -- Dictionary on single word selection - { - text_func = function() - local multi_word = G_reader_settings:readSetting("default_highlight_action") - for __, v in ipairs(long_press_action) do - if v[2] == multi_word then - return T(_("Multi-word selection: %1"), v[1]:lower()) - end - end - end, - sub_item_table = { table.unpack(menu_items.long_press.sub_item_table, 2, #long_press_action + 1) } - } - } - } - local post_long_press_action_index = #menu_items.selection_text.sub_item_table + #long_press_action -- index after long_press_action - -- Copy remaining items (anything after long_press_action) directly to selection_text's sub_item_table - for i = post_long_press_action_index, #menu_items.long_press.sub_item_table do - table.insert(menu_items.selection_text.sub_item_table, menu_items.long_press.sub_item_table[i]) - end - menu_items.long_press = nil - end - -- main menu Search menu_items.translation_settings = Translator:genSettingsMenu() menu_items.translate_current_page = { @@ -2712,6 +2596,4 @@ function ReaderHighlight:onClose(keep_highlight) end end - - return ReaderHighlight diff --git a/frontend/apps/reader/modules/readerkeyselection.lua b/frontend/apps/reader/modules/readerkeyselection.lua index 15e030d1b..490efbcf3 100644 --- a/frontend/apps/reader/modules/readerkeyselection.lua +++ b/frontend/apps/reader/modules/readerkeyselection.lua @@ -1,12 +1,19 @@ local BD = require("ui/bidi") local Device = require("device") +local DoubleSpinWidget = require("ui/widget/doublespinwidget") local Geom = require("ui/geometry") local InputContainer = require("ui/widget/container/inputcontainer") +local ReaderHighlight = require("apps/reader/modules/readerhighlight") +local SpinWidget = require("ui/widget/spinwidget") local UIManager = require("ui/uimanager") local logger = require("logger") local Size = require("ui/size") local time = require("ui/time") local Screen = Device.screen +local ffiUtil = require("ffi/util") +local _ = require("gettext") +local N_ = _.ngettext +local T = ffiUtil.template local ReaderKeySelection = InputContainer:extend{} @@ -21,6 +28,9 @@ function ReaderKeySelection:init() 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.ui:registerPostInitCallback(function() + self.ui.menu:registerToMainMenu(self) + end) end function ReaderKeySelection:onSetDimensions(dimen) @@ -51,6 +61,126 @@ function ReaderKeySelection:registerKeyEvents() end ReaderKeySelection.onPhysicalKeyboardConnected = ReaderKeySelection.registerKeyEvents +function ReaderKeySelection:addToMainMenu(menu_items) + if Device:isTouchDevice() or not Device:hasDPad() then return end + -- insert table to main reader menu + if not Device:useDPadAsActionKeys() then + menu_items.start_content_selection = { + text = _("Start text selection"), + callback = function() + self:onStartHighlightIndicator() + end, + } + end + -- we allow user to select the rate at which the content selection tool moves through screen + table.insert(menu_items.long_press.sub_item_table, { + text_func = function() + local reader_speed = G_reader_settings:readSetting("highlight_non_touch_factor") or 4 + local dict_speed = G_reader_settings:readSetting("highlight_non_touch_factor_dict") or 3 + return T(_("Crosshairs speed (reader/dict): %1 / %2"), reader_speed, dict_speed) + end, + callback = function(touchmenu_instance) + local reader_speed = G_reader_settings:readSetting("highlight_non_touch_factor") or 4 + local dict_speed = G_reader_settings:readSetting("highlight_non_touch_factor_dict") or 3 + local double_spin_widget = DoubleSpinWidget:new{ + left_text = _("Reader") .. " PDF/DjVu", + left_value = reader_speed, + left_min = 0.25, + left_max = 5, + left_default = 4, + left_precision = "%.2f", + left_step = 0.25, + left_hold_step = 0.05, + right_text = _("Dictionary"), + right_value = dict_speed, + right_min = 0.25, + right_max = 5, + right_default = 3, + right_precision = "%.2f", + right_step = 0.25, + right_hold_step = 0.05, + title_text = _("Crosshairs speed"), + info_text = _("Select a decimal value from 0.25 to 5. A smaller value increases the travel distance of the crosshairs per keystroke. Font size and this value are inversely correlated, meaning a smaller font size requires a larger value and vice versa."), + callback = function(left_value, right_value) + G_reader_settings:saveSetting("highlight_non_touch_factor", left_value) + G_reader_settings:saveSetting("highlight_non_touch_factor_dict", right_value) + if touchmenu_instance then touchmenu_instance:updateItems() end + end + } + UIManager:show(double_spin_widget) + end, + }) + table.insert(menu_items.long_press.sub_item_table, { + text = _("Increase crosshairs speed on consecutive keystrokes"), + checked_func = function() + return G_reader_settings:nilOrTrue("highlight_non_touch_spedup") + end, + enabled_func = function() + return not self.view.highlight.disabled + end, + callback = function() + G_reader_settings:flipNilOrTrue("highlight_non_touch_spedup") + end, + }) + table.insert(menu_items.long_press.sub_item_table, { + text_func = function() + local highlight_non_touch_interval = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 + return T(N_("Interval for crosshairs speed increase: 1 second", "Interval for crosshairs speed increase: %1 seconds", highlight_non_touch_interval), highlight_non_touch_interval) + end, + separator = true, -- needed as this is not the last item, readerlink adds another one + enabled_func = function() + return not self.view.highlight.disabled and G_reader_settings:nilOrTrue("highlight_non_touch_spedup") + end, + callback = function(touchmenu_instance) + local curr_val = G_reader_settings:readSetting("highlight_non_touch_interval") or 1 + local spin_widget = SpinWidget:new{ + value = curr_val, + value_min = 0.1, + value_max = 1, + precision = "%.1f", + value_step = 0.1, + default_value = 1, + title_text = _("Time interval"), + info_text = _("Select a decimal value up to 1 second. This defines the time period within which multiple keystrokes will trigger an increase in the crosshairs speed."), + callback = function(spin) + G_reader_settings:saveSetting("highlight_non_touch_interval", spin.value) + if touchmenu_instance then touchmenu_instance:updateItems() end + end + } + UIManager:show(spin_widget) + end, + }) + + if menu_items.long_press then + local long_press_action = ReaderHighlight.long_press_action + -- long_press settings are under the taps_and_gestures menu, which is not available for non-touch devices + -- Clone long_press settings, and change its label, making it much more meaningful for non-touch device users. + menu_items.selection_text = { + text = _("Text selection tools"), + sub_item_table = { + menu_items.long_press.sub_item_table[1], -- Dictionary on single word selection + { + text_func = function() + local multi_word = G_reader_settings:readSetting("default_highlight_action") + for __, v in ipairs(long_press_action) do + if v[2] == multi_word then + return T(_("Multi-word selection: %1"), v[1]:lower()) + end + end + end, + sub_item_table = { table.unpack(menu_items.long_press.sub_item_table, 2, #long_press_action + 1) } + } + } + } + local post_long_press_action_index = #menu_items.selection_text.sub_item_table + #long_press_action -- index after long_press_action + -- Copy remaining items (anything after long_press_action) directly to selection_text's sub_item_table + for i = post_long_press_action_index, #menu_items.long_press.sub_item_table do + table.insert(menu_items.selection_text.sub_item_table, menu_items.long_press.sub_item_table[i]) + end + menu_items.long_press = nil + end +end + function ReaderKeySelection:onHighlightPress(skip_tap_check) if not self._current_indicator_pos then return false end if self._start_indicator_highlight then