From c3ed51adedb76e50f67d4cf9e643a4e05b64ba9b Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Wed, 2 Nov 2022 13:58:12 -0400 Subject: [PATCH] ReaderHighlight: set default long-press action with a gesture (#9723) --- .../apps/reader/modules/readerhighlight.lua | 40 ++++++++++++++----- frontend/dispatcher.lua | 3 ++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index f80652e07..d0226e3f4 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -92,7 +92,7 @@ function ReaderHighlight:init() end, ["04_add_note"] = function(this) return { - text = _("Add Note"), + text = _("Add note"), callback = function() this:addNote() this:onClose() @@ -325,6 +325,7 @@ local long_press_action = { {_("Do nothing"), "nothing"}, {_("Highlight"), "highlight"}, {_("Select and highlight"), "select"}, + {_("Add note"), "note"}, {_("Translate"), "translate"}, {_("Wikipedia"), "wikipedia"}, {_("Dictionary"), "dictionary"}, @@ -497,7 +498,7 @@ The interval value is in seconds and can range from 3 to 20 seconds.]]), }, }, } - for _, v in ipairs(long_press_action) do + for i, v in ipairs(long_press_action) do table.insert(menu_items.long_press.sub_item_table, { text = v[1], checked_func = function() @@ -505,8 +506,7 @@ The interval value is in seconds and can range from 3 to 20 seconds.]]), end, radio = true, callback = function() - G_reader_settings:saveSetting("default_highlight_action", v[2]) - self.view.highlight.disabled = v[2] == "nothing" + self:onSetHighlightAction(i, true) -- no notification end, }) end @@ -821,8 +821,7 @@ function ReaderHighlight:onShowHighlightDialog(page, index, is_auto_text) text = _("Delete"), callback = function() self:deleteHighlight(page, index) - -- other part outside of the dialog may be dirty - UIManager:close(self.edit_highlight_dialog, "ui") + UIManager:close(self.edit_highlight_dialog) self.edit_highlight_dialog = nil end, }, @@ -1522,6 +1521,9 @@ function ReaderHighlight:onHoldRelease() elseif default_highlight_action == "select" then self:startSelection() self:onClose() + elseif default_highlight_action == "note" then + self:addNote() + self:onClose() elseif default_highlight_action == "translate" then self:translate(self.selected_text) elseif default_highlight_action == "wikipedia" then @@ -1540,6 +1542,27 @@ function ReaderHighlight:onHoldRelease() return true end +function ReaderHighlight:getHighlightActions() -- for Dispatcher + local action_nums, action_texts = {}, {} + for i, v in ipairs(long_press_action) do + table.insert(action_nums, i) + table.insert(action_texts, v[1]) + end + return action_nums, action_texts +end + +function ReaderHighlight:onSetHighlightAction(action_num, no_notification) + local v = long_press_action[action_num] + G_reader_settings:saveSetting("default_highlight_action", v[2]) + self.view.highlight.disabled = v[2] == "nothing" + if not no_notification then -- fired with a gesture + UIManager:show(Notification:new{ + text = T(_("Default highlight action changed to '%1'."), v[1]), + }) + end + return true +end + function ReaderHighlight:onCycleHighlightAction() local current_action = G_reader_settings:readSetting("default_highlight_action", "ask") local next_action_num @@ -1552,10 +1575,7 @@ function ReaderHighlight:onCycleHighlightAction() if next_action_num > #long_press_action then next_action_num = 1 end - G_reader_settings:saveSetting("default_highlight_action", long_press_action[next_action_num][2]) - UIManager:show(Notification:new{ - text = T(_("Default highlight action changed to '%1'."), long_press_action[next_action_num][1]), - }) + self:onSetHighlightAction(next_action_num) return true end diff --git a/frontend/dispatcher.lua b/frontend/dispatcher.lua index adbf016bc..13db64c65 100644 --- a/frontend/dispatcher.lua +++ b/frontend/dispatcher.lua @@ -33,6 +33,7 @@ local KoptOptions = require("ui/data/koptoptions") local Device = require("device") local Event = require("ui/event") local Notification = require("ui/widget/notification") +local ReaderHighlight = require("apps/reader/modules/readerhighlight") local ReaderZooming = require("apps/reader/modules/readerzooming") local UIManager = require("ui/uimanager") local util = require("util") @@ -136,6 +137,7 @@ local settingsList = { toggle_page_change_animation = {category="none", event="TogglePageChangeAnimation", title=_("Toggle page turn animations"), reader=true, condition=Device:canDoSwipeAnimation()}, toggle_inverse_reading_order = {category="none", event="ToggleReadingOrder", title=_("Toggle page turn direction"), reader=true, separator=true}, swap_page_turn_buttons = {category="none", event="SwapPageTurnButtons", title=_("Invert page turn buttons"), reader=true, condition=Device:hasKeys(), separator=true}, + set_highlight_action = {category="string", event="SetHighlightAction", title=_("Set highlight action"), args_func=ReaderHighlight.getHighlightActions, reader=true}, cycle_highlight_action = {category="none", event="CycleHighlightAction", title=_("Cycle highlight action"), reader=true}, cycle_highlight_style = {category="none", event="CycleHighlightStyle", title=_("Cycle highlight style"), reader=true}, page_jmp = {category="absolutenumber", event="GotoViewRel", min=-100, max=100, title=_("Go %1 pages"), reader=true}, @@ -324,6 +326,7 @@ local dispatcher_menu_order = { "swap_page_turn_buttons", "zoom", "zoom_factor_change", + "set_highlight_action", "cycle_highlight_action", "cycle_highlight_style", "panel_zoom_toggle",