2019-12-06 22:55:39 +01:00
local BD = require ( " ui/bidi " )
2017-07-28 22:39:54 +02:00
local ButtonDialog = require ( " ui/widget/buttondialog " )
2022-11-18 21:17:25 +02:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2014-10-30 19:42:18 +01:00
local Device = require ( " device " )
2013-10-22 17:11:31 +02:00
local Event = require ( " ui/event " )
2021-10-23 21:12:56 +11:00
local Geom = require ( " ui/geometry " )
2018-03-05 16:38:04 +01:00
local InfoMessage = require ( " ui/widget/infomessage " )
2017-07-28 22:39:54 +02:00
local InputContainer = require ( " ui/widget/container/inputcontainer " )
2019-10-25 11:25:26 -04:00
local Notification = require ( " ui/widget/notification " )
2022-01-02 20:09:53 +02:00
local TextViewer = require ( " ui/widget/textviewer " )
2018-12-17 14:15:13 +01:00
local Translator = require ( " ui/translator " )
2013-10-22 17:11:31 +02:00
local UIManager = require ( " ui/uimanager " )
2021-11-06 18:11:06 +11:00
local dbg = require ( " dbg " )
2016-12-29 00:10:38 -08:00
local logger = require ( " logger " )
2020-06-08 20:47:31 +02:00
local util = require ( " util " )
2022-03-12 19:16:50 +08:00
local Size = require ( " ui/size " )
2020-11-08 13:07:51 +01:00
local ffiUtil = require ( " ffi/util " )
2022-05-05 21:00:22 +02:00
local time = require ( " ui/time " )
2013-10-18 22:38:07 +02:00
local _ = require ( " gettext " )
2019-08-24 13:45:07 +02:00
local C_ = _.pgettext
2023-11-19 09:52:51 +02:00
local T = ffiUtil.template
2019-02-03 10:01:58 +01:00
local Screen = Device.screen
2013-04-24 06:59:52 +08:00
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 02:14:48 +02:00
local ReaderHighlight = InputContainer : extend { }
2020-11-08 13:07:51 +01:00
local function inside_box ( pos , box )
if pos then
local x , y = pos.x , pos.y
if box.x <= x and box.y <= y
and box.x + box.w >= x
and box.y + box.h >= y then
return true
end
end
end
local function cleanupSelectedText ( text )
-- Trim spaces and new lines at start and end
text = text : gsub ( " ^[ \n %s]* " , " " )
text = text : gsub ( " [ \n %s]*$ " , " " )
-- Trim spaces around newlines
text = text : gsub ( " %s* \n %s* " , " \n " )
-- Trim consecutive spaces (that would probably have collapsed
-- in rendered CreDocuments)
text = text : gsub ( " %s%s+ " , " " )
return text
end
2013-04-24 06:59:52 +08:00
function ReaderHighlight : init ( )
2023-11-19 09:52:51 +02:00
self.screen_w = Screen : getWidth ( )
self.screen_h = Screen : getHeight ( )
2021-11-21 19:31:10 +02:00
self.select_mode = false -- extended highlighting
2022-03-12 19:16:50 +08:00
self._start_indicator_highlight = false
self._current_indicator_pos = nil
self._previous_indicator_pos = nil
2022-05-05 21:00:22 +02:00
self._last_indicator_move_args = { dx = 0 , dy = 0 , distance = 0 , time = time : now ( ) }
2022-03-12 19:16:50 +08:00
2022-11-01 23:22:07 +01:00
self : registerKeyEvents ( )
2021-11-21 19:31:10 +02:00
2020-11-08 13:07:51 +01:00
self._highlight_buttons = {
-- highlight and add_note are for the document itself,
-- so we put them first.
2022-06-11 19:06:06 +02:00
[ " 01_select " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
2021-11-21 19:31:10 +02:00
text = _ ( " Select " ) ,
2022-06-11 19:06:06 +02:00
enabled = this.hold_pos ~= nil ,
2020-11-08 13:07:51 +01:00
callback = function ( )
2022-06-11 19:06:06 +02:00
this : startSelection ( )
this : onClose ( )
2020-11-08 13:07:51 +01:00
end ,
}
end ,
2022-06-11 19:06:06 +02:00
[ " 02_highlight " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
2021-11-21 19:31:10 +02:00
text = _ ( " Highlight " ) ,
2020-11-08 13:07:51 +01:00
callback = function ( )
2022-06-11 11:08:40 +02:00
this : saveHighlight ( true )
2022-06-11 19:06:06 +02:00
this : onClose ( )
2020-11-08 13:07:51 +01:00
end ,
2022-06-11 19:06:06 +02:00
enabled = this.hold_pos ~= nil ,
2020-11-08 13:07:51 +01:00
}
end ,
2022-06-11 19:06:06 +02:00
[ " 03_copy " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
text = C_ ( " Text " , " Copy " ) ,
enabled = Device : hasClipboard ( ) ,
callback = function ( )
2022-06-11 19:06:06 +02:00
Device.input . setClipboardText ( cleanupSelectedText ( this.selected_text . text ) )
this : onClose ( )
2021-05-31 21:36:12 +03:00
UIManager : show ( Notification : new {
text = _ ( " Selection copied to clipboard. " ) ,
} )
2020-11-08 13:07:51 +01:00
end ,
}
end ,
2022-06-11 19:06:06 +02:00
[ " 04_add_note " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
2022-11-02 13:58:12 -04:00
text = _ ( " Add note " ) ,
2020-11-08 13:07:51 +01:00
callback = function ( )
2022-06-11 19:06:06 +02:00
this : addNote ( )
this : onClose ( )
2020-11-08 13:07:51 +01:00
end ,
2022-06-11 19:06:06 +02:00
enabled = this.hold_pos ~= nil ,
2020-11-08 13:07:51 +01:00
}
end ,
-- then information lookup functions, putting on the left those that
-- depend on an internet connection.
2022-06-11 19:06:06 +02:00
[ " 05_wikipedia " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
text = _ ( " Wikipedia " ) ,
callback = function ( )
UIManager : scheduleIn ( 0.1 , function ( )
2022-06-11 19:06:06 +02:00
this : lookupWikipedia ( )
-- We don't call this:onClose(), we need the highlight
2020-11-08 13:07:51 +01:00
-- to still be there, as we may Highlight it from the
2021-03-21 13:57:18 +01:00
-- dict lookup widget.
2020-11-08 13:07:51 +01:00
end )
end ,
}
end ,
2022-06-11 19:06:06 +02:00
[ " 06_dictionary " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
text = _ ( " Dictionary " ) ,
callback = function ( )
2022-06-11 19:06:06 +02:00
this : onHighlightDictLookup ( )
-- We don't call this:onClose(), same reason as above
2020-11-08 13:07:51 +01:00
end ,
}
end ,
2022-06-11 19:06:06 +02:00
[ " 07_translate " ] = function ( this , page , index )
2020-11-08 13:07:51 +01:00
return {
text = _ ( " Translate " ) ,
callback = function ( )
2022-06-11 19:06:06 +02:00
this : translate ( this.selected_text , page , index )
-- We don't call this:onClose(), so one can still see
2020-11-08 13:07:51 +01:00
-- the highlighted text when moving the translated
-- text window, and also if NetworkMgr:promptWifiOn()
-- is needed, so the user can just tap again on this
-- button and does not need to select the text again.
end ,
}
end ,
2021-11-21 19:31:10 +02:00
-- buttons 08-11 are conditional ones, so the number of buttons can be even or odd
-- let the Search button be the last, occasionally narrow or wide, less confusing
2022-06-11 19:06:06 +02:00
[ " 12_search " ] = function ( this )
2020-11-08 13:07:51 +01:00
return {
2021-11-21 19:31:10 +02:00
text = _ ( " Search " ) ,
2020-11-08 13:07:51 +01:00
callback = function ( )
2022-06-11 19:06:06 +02:00
this : onHighlightSearch ( )
-- We don't call this:onClose(), crengine will highlight
2021-11-21 19:31:10 +02:00
-- search matches on the current page, and self:clear()
-- would redraw and remove crengine native highlights
2020-11-08 13:07:51 +01:00
end ,
}
2021-11-21 19:31:10 +02:00
end ,
}
2020-11-08 13:07:51 +01:00
2021-11-21 19:31:10 +02:00
-- Android devices
2020-11-08 13:07:51 +01:00
if Device : canShareText ( ) then
2022-06-10 01:53:44 +02:00
local action = _ ( " Share Text " )
2022-06-11 19:06:06 +02:00
self : addToHighlightDialog ( " 08_share_text " , function ( this )
2020-11-08 13:07:51 +01:00
return {
2022-06-10 01:53:44 +02:00
text = action ,
2020-11-08 13:07:51 +01:00
callback = function ( )
2022-06-11 19:06:06 +02:00
local text = cleanupSelectedText ( this.selected_text . text )
2020-11-08 13:07:51 +01:00
-- call self:onClose() before calling the android framework
2022-06-11 19:06:06 +02:00
this : onClose ( )
2022-06-10 01:53:44 +02:00
Device : doShareText ( text , action )
2020-11-08 13:07:51 +01:00
end ,
}
end )
end
2021-11-21 19:31:10 +02:00
-- cre documents only
2021-12-21 14:57:19 +02:00
if not self.document . info.has_pages then
2022-06-11 19:06:06 +02:00
self : addToHighlightDialog ( " 09_view_html " , function ( this )
2021-11-21 19:31:10 +02:00
return {
text = _ ( " View HTML " ) ,
callback = function ( )
2022-06-11 19:06:06 +02:00
this : viewSelectionHTML ( )
2021-11-21 19:31:10 +02:00
end ,
}
end )
end
-- User hyphenation dict
2022-06-11 19:06:06 +02:00
self : addToHighlightDialog ( " 10_user_dict " , function ( this )
2021-02-06 16:59:09 +01:00
return {
2021-11-21 19:31:10 +02:00
text = _ ( " Hyphenate " ) ,
2021-02-06 16:59:09 +01:00
show_in_highlight_dialog_func = function ( )
2022-06-11 19:06:06 +02:00
return this.ui . userhyph and this.ui . userhyph : isAvailable ( )
and not this.selected_text . text : find ( " [ ,;-%. \n ] " )
2021-02-06 16:59:09 +01:00
end ,
callback = function ( )
2022-06-11 19:06:06 +02:00
this.ui . userhyph : modifyUserEntry ( this.selected_text . text )
this : onClose ( )
2021-02-06 16:59:09 +01:00
end ,
}
end )
2020-11-08 13:07:51 +01:00
2021-11-21 19:31:10 +02:00
-- Links
2022-06-11 19:06:06 +02:00
self : addToHighlightDialog ( " 11_follow_link " , function ( this )
2021-06-01 12:06:55 +02:00
return {
2021-11-21 19:31:10 +02:00
text = _ ( " Follow Link " ) ,
2021-06-01 12:06:55 +02:00
show_in_highlight_dialog_func = function ( )
2022-06-11 19:06:06 +02:00
return this.selected_link ~= nil
2021-06-01 12:06:55 +02:00
end ,
callback = function ( )
2022-06-11 19:06:06 +02:00
local link = this.selected_link . link or this.selected_link
this.ui . link : onGotoLink ( link )
this : onClose ( )
2021-06-01 12:06:55 +02:00
end ,
}
end )
2014-10-09 17:41:23 +08:00
self.ui : registerPostInitCallback ( function ( )
self.ui . menu : registerToMainMenu ( self )
end )
2022-10-30 23:05:04 +01:00
-- delegate gesture listener to readerui, NOP our own
self.ges_events = nil
2013-04-24 06:59:52 +08:00
end
2023-11-19 09:52:51 +02:00
function ReaderHighlight : onSetDimensions ( dimen )
self.screen_w , self.screen_h = dimen.w , dimen.h
end
2022-10-30 23:05:04 +01:00
function ReaderHighlight : onGesture ( ) end
2017-01-23 06:54:14 -08:00
2022-11-01 23:22:07 +01:00
function ReaderHighlight : registerKeyEvents ( )
if Device : hasKeys ( ) then
2022-11-01 00:17:25 +01:00
-- Used for text selection with dpad/keys
local QUICK_INDICATOR_MOVE = true
2022-11-01 23:22:07 +01:00
self.key_events . QuickUpHighlightIndicator = { { " Shift " , " Up " } , event = " MoveHighlightIndicator " , args = { 0 , - 1 , QUICK_INDICATOR_MOVE } }
self.key_events . QuickDownHighlightIndicator = { { " Shift " , " Down " } , event = " MoveHighlightIndicator " , args = { 0 , 1 , QUICK_INDICATOR_MOVE } }
self.key_events . QuickLeftHighlightIndicator = { { " Shift " , " Left " } , event = " MoveHighlightIndicator " , args = { - 1 , 0 , QUICK_INDICATOR_MOVE } }
self.key_events . QuickRightHighlightIndicator = { { " Shift " , " Right " } , event = " MoveHighlightIndicator " , args = { 1 , 0 , QUICK_INDICATOR_MOVE } }
self.key_events . StartHighlightIndicator = { { " H " } }
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 " } }
2022-11-01 00:17:25 +01:00
end
end
end
ReaderHighlight.onPhysicalKeyboardConnected = ReaderHighlight.registerKeyEvents
2022-10-30 23:05:04 +01:00
function ReaderHighlight : setupTouchZones ( )
2017-01-23 06:54:14 -08:00
if not Device : isTouchDevice ( ) then return end
2020-08-06 00:13:26 +02:00
local hold_pan_rate = G_reader_settings : readSetting ( " hold_pan_rate " )
if not hold_pan_rate then
hold_pan_rate = Screen.low_pan_rate and 5.0 or 30.0
end
2022-11-18 21:17:25 +02:00
local DTAP_ZONE_TOP_LEFT = G_defaults : readSetting ( " DTAP_ZONE_TOP_LEFT " )
2017-01-23 06:54:14 -08:00
self.ui : registerTouchZones ( {
2022-11-18 21:17:25 +02:00
{
id = " readerhighlight_tap_select_mode " ,
ges = " tap " ,
screen_zone = {
ratio_x = DTAP_ZONE_TOP_LEFT.x , ratio_y = DTAP_ZONE_TOP_LEFT.y ,
ratio_w = DTAP_ZONE_TOP_LEFT.w , ratio_h = DTAP_ZONE_TOP_LEFT.h ,
} ,
overrides = {
" readerhighlight_tap " ,
" tap_top_left_corner " ,
" readermenu_ext_tap " ,
" readermenu_tap " ,
" tap_forward " ,
" tap_backward " ,
} ,
handler = function ( ges ) return self : onTapSelectModeIcon ( ) end
} ,
2017-01-23 06:54:14 -08:00
{
id = " readerhighlight_tap " ,
ges = " tap " ,
screen_zone = {
ratio_x = 0 , ratio_y = 0 , ratio_w = 1 , ratio_h = 1 ,
} ,
2019-03-30 21:05:44 +01:00
overrides = {
2020-01-24 20:05:21 +01:00
-- Tap on existing highlights have priority over
-- everything but tap on links (as links can be
-- part of some highlighted text)
" tap_top_left_corner " ,
" tap_top_right_corner " ,
" tap_left_bottom_corner " ,
" tap_right_bottom_corner " ,
2020-12-03 17:33:54 +01:00
" readerfooter_tap " ,
" readerconfigmenu_ext_tap " ,
" readerconfigmenu_tap " ,
" readermenu_ext_tap " ,
" readermenu_tap " ,
" tap_forward " ,
" tap_backward " ,
2019-03-30 21:05:44 +01:00
} ,
2017-01-23 06:54:14 -08:00
handler = function ( ges ) return self : onTap ( nil , ges ) end
2014-03-13 21:52:43 +08:00
} ,
2017-01-23 06:54:14 -08:00
{
id = " readerhighlight_hold " ,
ges = " hold " ,
screen_zone = {
ratio_x = 0 , ratio_y = 0 , ratio_w = 1 , ratio_h = 1 ,
} ,
handler = function ( ges ) return self : onHold ( nil , ges ) end
2014-03-13 21:52:43 +08:00
} ,
2017-01-23 06:54:14 -08:00
{
id = " readerhighlight_hold_release " ,
ges = " hold_release " ,
screen_zone = {
ratio_x = 0 , ratio_y = 0 , ratio_w = 1 , ratio_h = 1 ,
} ,
handler = function ( ) return self : onHoldRelease ( ) end
2014-03-13 21:52:43 +08:00
} ,
2017-01-23 06:54:14 -08:00
{
id = " readerhighlight_hold_pan " ,
ges = " hold_pan " ,
2020-08-06 00:13:26 +02:00
rate = hold_pan_rate ,
2017-01-23 06:54:14 -08:00
screen_zone = {
ratio_x = 0 , ratio_y = 0 , ratio_w = 1 , ratio_h = 1 ,
} ,
handler = function ( ges ) return self : onHoldPan ( nil , ges ) end
2014-03-13 21:52:43 +08:00
} ,
2017-01-23 06:54:14 -08:00
} )
end
function ReaderHighlight : onReaderReady ( )
self : setupTouchZones ( )
2013-04-24 06:59:52 +08:00
end
2021-10-18 20:17:37 +03:00
local highlight_style = {
{ _ ( " Lighten " ) , " lighten " } ,
{ _ ( " Underline " ) , " underscore " } ,
2022-01-25 22:39:03 +02:00
{ _ ( " Strikeout " ) , " strikeout " } ,
2021-10-18 20:17:37 +03:00
{ _ ( " Invert " ) , " invert " } ,
}
2022-08-03 16:51:57 +03:00
local note_mark = {
{ _ ( " None " ) , " none " } ,
{ _ ( " Underline " ) , " underline " } ,
{ _ ( " Side line " ) , " sideline " } ,
{ _ ( " Side mark " ) , " sidemark " } ,
}
2021-09-11 12:04:48 +03:00
local long_press_action = {
{ _ ( " Ask with popup dialog " ) , " ask " } ,
{ _ ( " Do nothing " ) , " nothing " } ,
{ _ ( " Highlight " ) , " highlight " } ,
2021-11-21 19:31:10 +02:00
{ _ ( " Select and highlight " ) , " select " } ,
2022-11-02 13:58:12 -04:00
{ _ ( " Add note " ) , " note " } ,
2021-09-11 12:04:48 +03:00
{ _ ( " Translate " ) , " translate " } ,
{ _ ( " Wikipedia " ) , " wikipedia " } ,
{ _ ( " Dictionary " ) , " dictionary " } ,
{ _ ( " Fulltext search " ) , " search " } ,
}
2023-11-19 09:52:51 +02:00
local highlight_dialog_position = {
{ _ ( " Top " ) , " top " } ,
{ _ ( " Center " ) , " center " } ,
{ _ ( " Bottom " ) , " bottom " } ,
{ _ ( " Gesture position " ) , " gesture " } ,
}
2017-03-04 14:46:38 +01:00
function ReaderHighlight : addToMainMenu ( menu_items )
2014-03-13 21:52:43 +08:00
-- insert table to main reader menu
2022-03-17 03:00:14 +08:00
if not Device : isTouchDevice ( ) and Device : hasDPad ( ) then
2022-03-12 19:16:50 +08:00
menu_items.start_content_selection = {
text = _ ( " Start content selection " ) ,
callback = function ( )
self : onStartHighlightIndicator ( )
end ,
}
end
2023-05-17 07:34:37 +03:00
-- main menu Typeset
2017-03-04 14:46:38 +01:00
menu_items.highlight_options = {
2021-09-11 12:04:48 +03:00
text = _ ( " Highlight style " ) ,
2021-10-18 20:17:37 +03:00
sub_item_table = { } ,
2017-02-28 22:46:32 +01:00
}
2022-02-01 21:56:28 +02:00
for i , v in ipairs ( highlight_style ) do
2021-10-18 20:17:37 +03:00
table.insert ( menu_items.highlight_options . sub_item_table , {
text_func = function ( )
local text = v [ 1 ]
if v [ 2 ] == G_reader_settings : readSetting ( " highlight_drawing_style " ) then
text = text .. " ★ "
end
return text
end ,
checked_func = function ( )
return self.view . highlight.saved_drawer == v [ 2 ]
end ,
2022-02-01 21:56:28 +02:00
radio = true ,
2021-10-18 20:17:37 +03:00
callback = function ( )
self.view . highlight.saved_drawer = v [ 2 ]
end ,
hold_callback = function ( touchmenu_instance )
G_reader_settings : saveSetting ( " highlight_drawing_style " , v [ 2 ] )
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
end ,
2022-02-01 21:56:28 +02:00
separator = i == # highlight_style ,
2021-10-18 20:17:37 +03:00
} )
end
table.insert ( menu_items.highlight_options . sub_item_table , {
text_func = function ( )
return T ( _ ( " Highlight opacity: %1 " ) , G_reader_settings : readSetting ( " highlight_lighten_factor " , 0.2 ) )
end ,
enabled_func = function ( )
return self.view . highlight.saved_drawer == " lighten "
end ,
callback = function ( touchmenu_instance )
local SpinWidget = require ( " ui/widget/spinwidget " )
local curr_val = G_reader_settings : readSetting ( " highlight_lighten_factor " , 0.2 )
local spin_widget = SpinWidget : new {
value = curr_val ,
value_min = 0 ,
value_max = 1 ,
precision = " %.2f " ,
value_step = 0.1 ,
value_hold_step = 0.25 ,
default_value = 0.2 ,
keep_shown_on_apply = true ,
title_text = _ ( " Highlight opacity " ) ,
info_text = _ ( " The higher the value, the darker the highlight. " ) ,
callback = function ( spin )
G_reader_settings : saveSetting ( " highlight_lighten_factor " , spin.value )
self.view . highlight.lighten_factor = spin.value
UIManager : setDirty ( self.dialog , " ui " )
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
end
}
UIManager : show ( spin_widget )
end ,
} )
2022-08-03 16:51:57 +03:00
table.insert ( menu_items.highlight_options . sub_item_table , {
text_func = function ( )
local notemark = self.view . highlight.note_mark or " none "
for __ , v in ipairs ( note_mark ) do
if v [ 2 ] == notemark then
return T ( _ ( " Note marker: %1 " ) , string.lower ( v [ 1 ] ) )
end
end
end ,
callback = function ( touchmenu_instance )
local notemark = self.view . highlight.note_mark or " none "
local radio_buttons = { }
for _ , v in ipairs ( note_mark ) do
table.insert ( radio_buttons , {
{
text = v [ 1 ] ,
checked = v [ 2 ] == notemark ,
provider = v [ 2 ] ,
} ,
} )
end
UIManager : show ( require ( " ui/widget/radiobuttonwidget " ) : new {
title_text = _ ( " Note marker " ) ,
width_factor = 0.5 ,
keep_shown_on_apply = true ,
radio_buttons = radio_buttons ,
callback = function ( radio )
if radio.provider == " none " then
self.view . highlight.note_mark = nil
G_reader_settings : delSetting ( " highlight_note_marker " )
else
self.view . highlight.note_mark = radio.provider
G_reader_settings : saveSetting ( " highlight_note_marker " , radio.provider )
end
self.view : setupNoteMarkPosition ( )
UIManager : setDirty ( self.dialog , " ui " )
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
end ,
} )
end ,
} )
2020-09-24 15:17:37 +02:00
if self.document . info.has_pages then
menu_items.panel_zoom_options = {
text = _ ( " Panel zoom (manga/comic) " ) ,
sub_item_table = self : genPanelZoomMenu ( ) ,
}
end
2022-01-02 15:01:08 +01:00
2023-05-17 07:34:37 +03:00
-- main menu Settings
2021-09-11 12:04:48 +03:00
menu_items.long_press = {
text = _ ( " Long-press on text " ) ,
sub_item_table = {
{
text = _ ( " Dictionary on single word selection " ) ,
checked_func = function ( )
return not self.view . highlight.disabled and G_reader_settings : nilOrFalse ( " highlight_action_on_single_word " )
end ,
enabled_func = function ( )
return not self.view . highlight.disabled
end ,
callback = function ( )
G_reader_settings : flipNilOrFalse ( " highlight_action_on_single_word " )
end ,
separator = true ,
} ,
} ,
}
2023-11-19 09:52:51 +02:00
-- actions
2022-11-02 13:58:12 -04:00
for i , v in ipairs ( long_press_action ) do
2021-09-11 12:04:48 +03:00
table.insert ( menu_items.long_press . sub_item_table , {
text = v [ 1 ] ,
checked_func = function ( )
return G_reader_settings : readSetting ( " default_highlight_action " , " ask " ) == v [ 2 ]
end ,
2022-02-01 21:56:28 +02:00
radio = true ,
2021-09-11 12:04:48 +03:00
callback = function ( )
2022-11-02 13:58:12 -04:00
self : onSetHighlightAction ( i , true ) -- no notification
2021-09-11 12:04:48 +03:00
end ,
} )
end
2023-11-19 09:52:51 +02:00
-- highlight dialog position
local sub_item_table = { }
for i , v in ipairs ( highlight_dialog_position ) do
table.insert ( sub_item_table , {
text = v [ 1 ] ,
checked_func = function ( )
return G_reader_settings : readSetting ( " highlight_dialog_position " , " center " ) == v [ 2 ]
end ,
callback = function ( )
G_reader_settings : saveSetting ( " highlight_dialog_position " , v [ 2 ] )
end ,
} )
end
table.insert ( menu_items.long_press . sub_item_table , {
text_func = function ( )
local position = G_reader_settings : readSetting ( " highlight_dialog_position " , " center " )
for __ , v in ipairs ( highlight_dialog_position ) do
if v [ 2 ] == position then
return T ( _ ( " Highlight dialog position: %1 " ) , v [ 1 ] : lower ( ) )
end
end
end ,
sub_item_table = sub_item_table ,
} )
-- highlight very-long-press interval
2022-11-18 21:17:25 +02:00
table.insert ( menu_items.long_press . sub_item_table , {
text_func = function ( )
return T ( _ ( " Highlight very-long-press interval: %1 s " ) ,
G_reader_settings : readSetting ( " highlight_long_hold_threshold_s " , 3 ) )
end ,
keep_menu_open = true ,
callback = function ( touchmenu_instance )
local SpinWidget = require ( " ui/widget/spinwidget " )
local items = SpinWidget : new {
title_text = _ ( " Highlight very-long-press interval " ) ,
info_text = _ ( " If a long-press is not released in this interval, it is considered a very-long-press. On document text, single word selection will not be triggered. " ) ,
2023-11-19 09:52:51 +02:00
width = math.floor ( self.screen_w * 0.75 ) ,
2022-11-18 21:17:25 +02:00
value = G_reader_settings : readSetting ( " highlight_long_hold_threshold_s " , 3 ) ,
value_min = 2.5 ,
value_max = 20 ,
value_step = 0.1 ,
value_hold_step = 0.5 ,
unit = C_ ( " Time " , " s " ) ,
precision = " %0.1f " ,
ok_text = _ ( " Set interval " ) ,
default_value = 3 ,
callback = function ( spin )
G_reader_settings : saveSetting ( " highlight_long_hold_threshold_s " , spin.value )
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
end
}
UIManager : show ( items )
end ,
} )
2022-03-12 19:16:50 +08:00
-- long_press menu is under taps_and_gestures menu which is not available for non touch device
-- Clone long_press menu and change label making much meaning for non touch devices
2022-03-17 03:00:14 +08:00
if not Device : isTouchDevice ( ) and Device : hasDPad ( ) then
2022-03-12 19:16:50 +08:00
menu_items.selection_text = util.tableDeepCopy ( menu_items.long_press )
menu_items.selection_text . text = _ ( " Select on text " )
end
2023-05-17 07:34:37 +03:00
-- main menu Search
menu_items.translation_settings = Translator : genSettingsMenu ( )
menu_items.translate_current_page = {
text = _ ( " Translate current page " ) ,
callback = function ( )
self : onTranslateCurrentPage ( )
end ,
}
2014-01-18 03:05:17 +08:00
end
2020-09-24 15:17:37 +02:00
function ReaderHighlight : genPanelZoomMenu ( )
return {
{
text = _ ( " Allow panel zoom " ) ,
checked_func = function ( )
return self.panel_zoom_enabled
end ,
callback = function ( )
self : onTogglePanelZoomSetting ( )
end ,
hold_callback = function ( )
local ext = util.getFileNameSuffix ( self.ui . document.file )
2020-12-24 09:07:27 +01:00
local curr_val = G_reader_settings : getSettingForExt ( " panel_zoom_enabled " , ext )
G_reader_settings : saveSettingForExt ( " panel_zoom_enabled " , not curr_val , ext )
end ,
separator = true ,
} ,
{
text = _ ( " Fall back to text selection " ) ,
checked_func = function ( )
return self.panel_zoom_fallback_to_text_selection
end ,
callback = function ( )
self : onToggleFallbackTextSelection ( )
end ,
hold_callback = function ( )
local ext = util.getFileNameSuffix ( self.ui . document.file )
G_reader_settings : saveSettingForExt ( " panel_zoom_fallback_to_text_selection " , self.panel_zoom_fallback_to_text_selection , ext )
2020-09-24 15:17:37 +02:00
end ,
separator = true ,
} ,
}
end
2017-09-20 17:35:30 +02:00
-- Returns a unique id, that can be provided on delayed call to :clear(id)
-- to ensure current highlight has not already been cleared, and that we
-- are not going to clear a new highlight
function ReaderHighlight : getClearId ( )
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 02:57:59 +02:00
self.clear_id = UIManager : getTime ( ) -- can act as a unique id
2017-09-20 17:35:30 +02:00
return self.clear_id
end
function ReaderHighlight : clear ( clear_id )
if clear_id then -- should be provided by delayed call to clear()
if clear_id ~= self.clear_id then
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 02:57:59 +02:00
-- if clear_id is no longer valid, highlight has already been
2017-09-20 17:35:30 +02:00
-- cleared since this clear_id was given
return
end
end
self.clear_id = nil -- invalidate id
2021-05-19 22:57:46 +02:00
if not self.ui . document then
-- might happen if scheduled and run after document is closed
return
end
2022-12-18 18:04:00 +02:00
if self.ui . paging then
2014-11-05 12:28:11 +08:00
self.view . highlight.temp = { }
else
self.ui . document : clearSelection ( )
end
2019-02-03 10:01:58 +01:00
if self.restore_page_mode_func then
self.restore_page_mode_func ( )
self.restore_page_mode_func = nil
end
2021-10-23 21:12:56 +11:00
self.is_word_selection = false
2019-02-03 10:01:58 +01:00
self.selected_text_start_xpointer = nil
2014-03-13 21:52:43 +08:00
if self.hold_pos then
self.hold_pos = nil
2014-07-02 17:46:17 +08:00
self.selected_text = nil
2017-06-19 00:08:57 +08:00
UIManager : setDirty ( self.dialog , " ui " )
2014-03-13 21:52:43 +08:00
return true
end
2014-08-26 20:29:53 +08:00
end
2014-11-05 12:28:11 +08:00
function ReaderHighlight : onClearHighlight ( )
self : clear ( )
return true
end
2022-11-18 21:17:25 +02:00
function ReaderHighlight : onTapSelectModeIcon ( )
if not self.select_mode then return end
UIManager : show ( ConfirmBox : new {
text = _ ( " You are currently in SELECT mode. \n To finish highlighting, long press where the highlight should end and press the HIGHLIGHT button. \n You can also exit select mode by tapping on the start of the highlight. " ) ,
2023-02-19 21:49:09 +02:00
icon = " texture-box " ,
2022-11-18 21:17:25 +02:00
ok_text = _ ( " Exit select mode " ) ,
cancel_text = _ ( " Close " ) ,
ok_callback = function ( )
self.select_mode = false
self : deleteHighlight ( self.highlight_page , self.highlight_idx )
end
} )
return true
end
2016-03-27 17:18:25 -07:00
function ReaderHighlight : onTap ( _ , ges )
2020-11-08 02:18:50 +01:00
-- We only actually need to clear if we have something to clear in the first place.
-- (We mainly want to avoid CRe's clearSelection,
-- which may incur a redraw as it invalidates the cache, c.f., #6854)
-- ReaderHighlight:clear can only return true if self.hold_pos was set anyway.
local cleared = self.hold_pos and self : clear ( )
-- We only care about potential taps on existing highlights, not on taps that closed a highlight menu.
2022-11-18 21:17:25 +02:00
if not cleared and ges then
2022-12-18 18:04:00 +02:00
if self.ui . paging then
2014-08-26 20:29:53 +08:00
return self : onTapPageSavedHighlight ( ges )
else
return self : onTapXPointerSavedHighlight ( ges )
end
2014-03-13 21:52:43 +08:00
end
2014-01-18 03:05:17 +08:00
end
function ReaderHighlight : onTapPageSavedHighlight ( ges )
2014-03-13 21:52:43 +08:00
local pages = self.view : getCurrentPageList ( )
local pos = self.view : screenToPageTransform ( ges.pos )
2023-05-28 08:05:48 +03:00
local highlights_tapped = { }
2022-12-02 20:22:27 +02:00
for _ , page in ipairs ( pages ) do
local items = self.view : getPageSavedHighlights ( page )
2017-01-23 06:54:14 -08:00
if items then
2022-12-02 20:22:27 +02:00
for i , item in ipairs ( items ) do
local boxes = self.ui . document : getPageBoxesFromPositions ( page , item.pos0 , item.pos1 )
2017-01-23 06:54:14 -08:00
if boxes then
2022-12-02 20:22:27 +02:00
for _ , box in ipairs ( boxes ) do
2017-01-23 06:54:14 -08:00
if inside_box ( pos , box ) then
2017-11-20 21:58:58 +01:00
logger.dbg ( " Tap on highlight " )
2022-12-02 20:22:27 +02:00
local hl_page , hl_i
if item.parent then -- multi-page highlight
hl_page , hl_i = unpack ( item.parent )
else
hl_page , hl_i = page , i
end
2023-05-28 08:05:48 +03:00
if self.select_mode then
if hl_page == self.highlight_page and hl_i == self.highlight_idx then
-- tap on the first fragment: abort select mode, clear highlight
self.select_mode = false
self : deleteHighlight ( hl_page , hl_i )
return true
end
else
table.insert ( highlights_tapped , { hl_page , hl_i } )
break
end
2017-01-23 06:54:14 -08:00
end
2014-03-13 21:52:43 +08:00
end
end
end
end
end
2023-05-28 08:05:48 +03:00
if # highlights_tapped > 0 then
return self : showChooseHighlightDialog ( highlights_tapped )
end
2014-01-18 03:05:17 +08:00
end
function ReaderHighlight : onTapXPointerSavedHighlight ( ges )
2018-10-08 18:57:59 +02:00
-- Getting screen boxes is done for each tap on screen (changing pages,
-- showing menu...). We might want to cache these boxes per page (and
-- clear that cache when page layout change or highlights are added
-- or removed).
2019-03-13 13:05:50 +01:00
local cur_view_top , cur_view_bottom
2014-03-13 21:52:43 +08:00
local pos = self.view : screenToPageTransform ( ges.pos )
2020-11-08 02:18:50 +01:00
-- NOTE: By now, pos.page is set, but if a highlight spans across multiple pages,
-- it's stored under the hash of its *starting* point,
-- so we can't just check the current page, hence the giant hashwalk of death...
-- We can't even limit the walk to page <= pos.page,
-- because pos.page isn't super accurate in continuous mode
-- (it's the page number for what's it the topleft corner of the screen,
-- i.e., often a bit earlier)...
2023-05-28 08:05:48 +03:00
local highlights_tapped = { }
2020-11-08 02:18:50 +01:00
for page , items in pairs ( self.view . highlight.saved ) do
2017-01-23 06:54:14 -08:00
if items then
for i = 1 , # items do
2020-11-08 02:18:50 +01:00
local item = items [ i ]
local pos0 , pos1 = item.pos0 , item.pos1
2017-11-20 21:58:58 +01:00
-- document:getScreenBoxesFromPositions() is expensive, so we
-- first check this item is on current page
2019-03-13 13:05:50 +01:00
if not cur_view_top then
-- Even in page mode, it's safer to use pos and ui.dimen.h
-- than pages' xpointers pos, even if ui.dimen.h is a bit
-- larger than pages' heights
cur_view_top = self.ui . document : getCurrentPos ( )
if self.view . view_mode == " page " and self.ui . document : getVisiblePageCount ( ) > 1 then
cur_view_bottom = cur_view_top + 2 * self.ui . dimen.h
else
cur_view_bottom = cur_view_top + self.ui . dimen.h
2019-02-26 07:16:43 +01:00
end
end
2019-03-13 13:05:50 +01:00
local spos0 = self.ui . document : getPosFromXPointer ( pos0 )
local spos1 = self.ui . document : getPosFromXPointer ( pos1 )
local start_pos = math.min ( spos0 , spos1 )
local end_pos = math.max ( spos0 , spos1 )
if start_pos <= cur_view_bottom and end_pos >= cur_view_top then
2018-10-10 18:50:24 +02:00
local boxes = self.ui . document : getScreenBoxesFromPositions ( pos0 , pos1 , true ) -- get_segments=true
2017-11-20 21:58:58 +01:00
if boxes then
for index , box in pairs ( boxes ) do
if inside_box ( pos , box ) then
logger.dbg ( " Tap on highlight " )
2023-05-28 08:05:48 +03:00
if self.select_mode then
if page == self.highlight_page and i == self.highlight_idx then
-- tap on the first fragment: abort select mode, clear highlight
self.select_mode = false
self : deleteHighlight ( page , i )
return true
end
else
table.insert ( highlights_tapped , { page , i } )
break
end
2017-11-20 21:58:58 +01:00
end
2017-01-23 06:54:14 -08:00
end
2014-03-13 21:52:43 +08:00
end
end
end
end
end
2023-05-28 08:05:48 +03:00
if # highlights_tapped > 0 then
return self : showChooseHighlightDialog ( highlights_tapped )
end
2013-04-24 06:59:52 +08:00
end
2019-02-15 18:42:27 -05:00
function ReaderHighlight : updateHighlight ( page , index , side , direction , move_by_char )
2022-12-18 18:04:00 +02:00
if self.ui . paging then return end
2019-02-15 18:42:27 -05:00
local highlight = self.view . highlight.saved [ page ] [ index ]
local highlight_time = highlight.datetime
local highlight_beginning = highlight.pos0
local highlight_end = highlight.pos1
if side == 0 then -- we move pos0
2019-02-17 16:15:32 +01:00
local updated_highlight_beginning
2019-02-15 18:42:27 -05:00
if direction == 1 then -- move highlight to the right
if move_by_char then
updated_highlight_beginning = self.ui . document : getNextVisibleChar ( highlight_beginning )
else
updated_highlight_beginning = self.ui . document : getNextVisibleWordStart ( highlight_beginning )
end
else -- move highlight to the left
if move_by_char then
updated_highlight_beginning = self.ui . document : getPrevVisibleChar ( highlight_beginning )
else
updated_highlight_beginning = self.ui . document : getPrevVisibleWordStart ( highlight_beginning )
end
2019-02-17 16:15:32 +01:00
end
if updated_highlight_beginning then
local order = self.ui . document : compareXPointers ( updated_highlight_beginning , highlight_end )
if order and order > 0 then -- only if beginning did not go past end
2019-02-15 18:42:27 -05:00
self.view . highlight.saved [ page ] [ index ] . pos0 = updated_highlight_beginning
end
end
else -- we move pos1
2019-02-17 16:15:32 +01:00
local updated_highlight_end
2019-02-15 18:42:27 -05:00
if direction == 1 then -- move highlight to the right
if move_by_char then
updated_highlight_end = self.ui . document : getNextVisibleChar ( highlight_end )
else
updated_highlight_end = self.ui . document : getNextVisibleWordEnd ( highlight_end )
end
else -- move highlight to the left
if move_by_char then
updated_highlight_end = self.ui . document : getPrevVisibleChar ( highlight_end )
else
updated_highlight_end = self.ui . document : getPrevVisibleWordEnd ( highlight_end )
end
2019-02-17 16:15:32 +01:00
end
if updated_highlight_end then
local order = self.ui . document : compareXPointers ( highlight_beginning , updated_highlight_end )
if order and order > 0 then -- only if end did not go back past beginning
2019-02-15 18:42:27 -05:00
self.view . highlight.saved [ page ] [ index ] . pos1 = updated_highlight_end
end
end
end
local new_beginning = self.view . highlight.saved [ page ] [ index ] . pos0
local new_end = self.view . highlight.saved [ page ] [ index ] . pos1
local new_text = self.ui . document : getTextFromXPointers ( new_beginning , new_end )
2020-06-17 23:51:02 +02:00
local new_chapter = self.ui . toc : getTocTitleByPage ( new_beginning )
2020-07-22 00:14:57 +02:00
self.view . highlight.saved [ page ] [ index ] . text = cleanupSelectedText ( new_text )
2020-06-17 23:51:02 +02:00
self.view . highlight.saved [ page ] [ index ] . chapter = new_chapter
2019-02-15 18:42:27 -05:00
local new_highlight = self.view . highlight.saved [ page ] [ index ]
self.ui . bookmark : updateBookmark ( {
page = highlight_beginning ,
datetime = highlight_time ,
updated_highlight = new_highlight
2021-10-18 20:17:37 +03:00
} )
2020-03-20 21:58:58 +01:00
if side == 0 then
-- Ensure we show the page with the new beginning of highlight
if not self.ui . document : isXPointerInCurrentPage ( new_beginning ) then
self.ui : handleEvent ( Event : new ( " GotoXPointer " , new_beginning ) )
end
else
-- Ensure we show the page with the new end of highlight
if not self.ui . document : isXPointerInCurrentPage ( new_end ) then
if self.view . view_mode == " page " then
self.ui : handleEvent ( Event : new ( " GotoXPointer " , new_end ) )
else
-- Not easy to get the y that would show the whole line
-- containing new_end. So, we scroll so that new_end
-- is at 2/3 of the screen.
local end_y = self.ui . document : getPosFromXPointer ( new_end )
2023-11-19 09:52:51 +02:00
local top_y = end_y - math.floor ( self.screen_h * 2 / 3 )
2020-03-20 21:58:58 +01:00
self.ui . rolling : _gotoPos ( top_y )
end
end
end
2019-02-15 18:42:27 -05:00
UIManager : setDirty ( self.dialog , " ui " )
end
2023-05-28 08:05:48 +03:00
function ReaderHighlight : showChooseHighlightDialog ( highlights )
if # highlights == 1 then
local page , index = unpack ( highlights [ 1 ] )
local item = self.view . highlight.saved [ page ] [ index ]
local bookmark_note = self.ui . bookmark : getBookmarkNote ( { datetime = item.datetime } )
self : showHighlightNoteOrDialog ( page , index , bookmark_note )
else -- overlapped highlights
local dialog
local buttons = { }
for i , v in ipairs ( highlights ) do
local page , index = unpack ( v )
local item = self.view . highlight.saved [ page ] [ index ]
local bookmark_note = self.ui . bookmark : getBookmarkNote ( { datetime = item.datetime } )
buttons [ i ] = { {
text = ( bookmark_note and self.ui . bookmark.display_prefix [ " note " ]
or self.ui . bookmark.display_prefix [ " highlight " ] ) .. item.text ,
align = " left " ,
avoid_text_truncation = false ,
font_face = " smallinfofont " ,
font_size = 22 ,
font_bold = false ,
callback = function ( )
UIManager : close ( dialog )
self : showHighlightNoteOrDialog ( page , index , bookmark_note )
end ,
} }
end
dialog = ButtonDialog : new {
buttons = buttons ,
}
UIManager : show ( dialog )
2022-11-18 21:17:25 +02:00
end
2023-05-28 08:05:48 +03:00
return true
end
function ReaderHighlight : showHighlightNoteOrDialog ( page , index , bookmark_note )
2022-01-02 20:09:53 +02:00
if bookmark_note then
local textviewer
textviewer = TextViewer : new {
title = _ ( " Note " ) ,
text = bookmark_note ,
2023-11-19 09:52:51 +02:00
width = math.floor ( math.min ( self.screen_w , self.screen_h ) * 0.8 ) ,
height = math.floor ( math.max ( self.screen_w , self.screen_h ) * 0.4 ) ,
2022-01-02 20:09:53 +02:00
buttons_table = {
{
{
2023-10-29 12:33:29 +02:00
text = _ ( " Delete highlight " ) ,
2022-01-02 20:09:53 +02:00
callback = function ( )
UIManager : close ( textviewer )
2023-10-29 12:33:29 +02:00
self : deleteHighlight ( page , index )
2022-01-02 20:09:53 +02:00
end ,
} ,
{
text = _ ( " Edit highlight " ) ,
callback = function ( )
UIManager : close ( textviewer )
self : onShowHighlightDialog ( page , index , false )
end ,
} ,
} ,
} ,
}
UIManager : show ( textviewer )
else
self : onShowHighlightDialog ( page , index , true )
end
end
function ReaderHighlight : onShowHighlightDialog ( page , index , is_auto_text )
2019-02-15 18:42:27 -05:00
local buttons = {
{
2014-03-13 21:52:43 +08:00
{
2019-02-15 18:42:27 -05:00
text = _ ( " Delete " ) ,
callback = function ( )
self : deleteHighlight ( page , index )
2022-11-02 13:58:12 -04:00
UIManager : close ( self.edit_highlight_dialog )
2021-03-21 13:57:18 +01:00
self.edit_highlight_dialog = nil
2019-02-15 18:42:27 -05:00
end ,
2014-03-13 21:52:43 +08:00
} ,
2019-02-15 18:42:27 -05:00
{
2021-12-04 12:51:38 +01:00
text = C_ ( " Highlight " , " Style " ) ,
2021-10-18 20:17:37 +03:00
callback = function ( )
self : editHighlightStyle ( page , index )
UIManager : close ( self.edit_highlight_dialog )
self.edit_highlight_dialog = nil
end ,
} ,
{
text = is_auto_text and _ ( " Add note " ) or _ ( " Edit note " ) ,
2019-02-15 18:42:27 -05:00
callback = function ( )
self : editHighlight ( page , index )
UIManager : close ( self.edit_highlight_dialog )
2021-03-21 13:57:18 +01:00
self.edit_highlight_dialog = nil
2019-02-15 18:42:27 -05:00
end ,
} ,
2019-10-06 23:47:53 +02:00
{
2022-05-29 22:32:16 +02:00
text = " … " ,
2019-10-06 23:47:53 +02:00
callback = function ( )
self.selected_text = self.view . highlight.saved [ page ] [ index ]
2022-01-16 21:54:08 +02:00
self : onShowHighlightMenu ( page , index )
2019-10-06 23:47:53 +02:00
UIManager : close ( self.edit_highlight_dialog )
2021-03-21 13:57:18 +01:00
self.edit_highlight_dialog = nil
2019-10-06 23:47:53 +02:00
end ,
} ,
2019-02-15 18:42:27 -05:00
}
}
2021-12-10 11:06:16 +02:00
if self.ui . rolling then
2021-05-20 23:04:57 +02:00
local start_prev = " ◁▒▒ "
local start_next = " ▷▒▒ "
local end_prev = " ▒▒◁ "
local end_next = " ▒▒▷ "
2019-12-06 22:55:39 +01:00
if BD.mirroredUILayout ( ) then
2021-05-20 23:04:57 +02:00
-- BiDi will mirror the arrows, and this just works
start_prev , start_next = start_next , start_prev
end_prev , end_next = end_next , end_prev
2019-12-06 22:55:39 +01:00
end
2019-02-15 18:42:27 -05:00
table.insert ( buttons , {
{
2019-12-06 22:55:39 +01:00
text = start_prev ,
2019-02-15 18:42:27 -05:00
callback = function ( )
self : updateHighlight ( page , index , 0 , - 1 , false )
end ,
hold_callback = function ( )
self : updateHighlight ( page , index , 0 , - 1 , true )
return true
end
} ,
{
2019-12-06 22:55:39 +01:00
text = start_next ,
2019-02-15 18:42:27 -05:00
callback = function ( )
self : updateHighlight ( page , index , 0 , 1 , false )
end ,
hold_callback = function ( )
self : updateHighlight ( page , index , 0 , 1 , true )
return true
end
} ,
{
2019-12-06 22:55:39 +01:00
text = end_prev ,
2019-02-15 18:42:27 -05:00
callback = function ( )
self : updateHighlight ( page , index , 1 , - 1 , false )
end ,
hold_callback = function ( )
self : updateHighlight ( page , index , 1 , - 1 , true )
end
} ,
{
2019-12-06 22:55:39 +01:00
text = end_next ,
2019-02-15 18:42:27 -05:00
callback = function ( )
self : updateHighlight ( page , index , 1 , 1 , false )
end ,
hold_callback = function ( )
self : updateHighlight ( page , index , 1 , 1 , true )
end
}
} )
end
self.edit_highlight_dialog = ButtonDialog : new {
buttons = buttons
2014-03-13 21:52:43 +08:00
}
UIManager : show ( self.edit_highlight_dialog )
return true
2014-01-18 03:05:17 +08:00
end
2020-11-08 13:07:51 +01:00
function ReaderHighlight : addToHighlightDialog ( idx , fn_button )
-- fn_button is a function that takes the ReaderHighlight instance
-- as argument, and returns a table describing the button to be added.
self._highlight_buttons [ idx ] = fn_button
end
function ReaderHighlight : removeFromHighlightDialog ( idx )
local button = self._highlight_buttons [ idx ]
self._highlight_buttons [ idx ] = nil
return button
end
2022-01-16 21:54:08 +02:00
function ReaderHighlight : onShowHighlightMenu ( page , index )
2021-10-31 17:36:00 +01:00
if not self.selected_text then
return
end
2020-11-08 13:07:51 +01:00
local highlight_buttons = { { } }
local columns = 2
for idx , fn_button in ffiUtil.orderedPairs ( self._highlight_buttons ) do
2022-01-16 21:54:08 +02:00
local button = fn_button ( self , page , index )
2021-02-06 16:59:09 +01:00
if not button.show_in_highlight_dialog_func or button.show_in_highlight_dialog_func ( ) then
if # highlight_buttons [ # highlight_buttons ] >= columns then
table.insert ( highlight_buttons , { } )
end
table.insert ( highlight_buttons [ # highlight_buttons ] , button )
logger.dbg ( " ReaderHighlight " , idx .. " : line " .. # highlight_buttons .. " , col " .. # highlight_buttons [ # highlight_buttons ] )
2020-11-08 13:07:51 +01:00
end
2020-01-05 12:56:01 +01:00
end
2019-10-06 23:47:53 +02:00
self.highlight_dialog = ButtonDialog : new {
buttons = highlight_buttons ,
2023-11-19 09:52:51 +02:00
anchor = function ( ) return self : _getHighlightMenuAnchor ( ) end ,
2019-10-06 23:47:53 +02:00
tap_close_callback = function ( ) self : handleEvent ( Event : new ( " Tap " ) ) end ,
}
Assorted bag'o tweaks & fixes (#9569)
* UIManager: Support more specialized update modes for corner-cases:
* A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor).
* NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*).
* UIManager: Assorted code cleanups & simplifications.
* Logger & dbg: Unify logging style, and code cleanups.
* SDL: Unbreak suspend/resume outside of the emulator (fix #9567).
* NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay).
* OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573)
* WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!).
* Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit.
* UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo).
* UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes).
* Kindle: ID the upcoming Scribe.
* Bump base (https://github.com/koreader/koreader-base/pull/1524)
2022-10-02 03:01:49 +02:00
-- NOTE: Disable merging for this update,
-- or the buggy Sage kernel may alpha-blend it into the page (with a bogus alpha value, to boot)...
UIManager : show ( self.highlight_dialog , " [ui] " )
2019-10-06 23:47:53 +02:00
end
2021-11-06 18:11:06 +11:00
dbg : guard ( ReaderHighlight , " onShowHighlightMenu " ,
function ( self )
assert ( self.selected_text ~= nil ,
" onShowHighlightMenu must not be called with nil self.selected_text! " )
end )
2019-10-06 23:47:53 +02:00
2023-11-19 09:52:51 +02:00
function ReaderHighlight : _getHighlightMenuAnchor ( )
local position = G_reader_settings : readSetting ( " highlight_dialog_position " , " center " )
if position == " center " or not self.gest_pos then return end
local dialog_box = self.highlight_dialog : getContentSize ( )
local anchor_x = math.floor ( ( self.screen_w - dialog_box.w ) / 2 ) -- center by width
local anchor_y , prefers_pop_down
if position == " top " then
anchor_y = Size.padding . small -- do not stick to the edge
prefers_pop_down = true
elseif position == " bottom " then
anchor_y = self.screen_h - Size.padding . small
else -- "gesture"
local text_box = self.ui . document : getWordFromPosition ( self.gest_pos ) . sbox
if self.ui . paging then
text_box = self.view : pageToScreenTransform ( self.ui . paging.current_page , text_box )
end
anchor_y = text_box.y + text_box.h + Size.padding . small -- do not stick to the box
if anchor_y + dialog_box.h <= self.screen_h - Size.padding . small then -- enough room below box with gest_pos
prefers_pop_down = true
else -- above box with gest_pos
anchor_y = text_box.y - Size.padding . small
end
end
self.gest_pos = nil
return { x = anchor_x , y = anchor_y , h = 0 , w = 0 } , prefers_pop_down
end
2020-05-05 18:56:30 +02:00
function ReaderHighlight : _resetHoldTimer ( clear )
if clear then
2022-05-05 21:00:22 +02:00
self.hold_last_time = nil
2020-05-05 18:56:30 +02:00
else
2022-05-05 21:00:22 +02:00
self.hold_last_time = UIManager : getTime ( )
2020-05-05 18:56:30 +02:00
end
end
2020-09-24 15:17:37 +02:00
function ReaderHighlight : onTogglePanelZoomSetting ( arg , ges )
if not self.document . info.has_pages then return end
self.panel_zoom_enabled = not self.panel_zoom_enabled
end
2020-12-24 09:07:27 +01:00
function ReaderHighlight : onToggleFallbackTextSelection ( arg , ges )
if not self.document . info.has_pages then return end
self.panel_zoom_fallback_to_text_selection = not self.panel_zoom_fallback_to_text_selection
end
2020-09-24 15:17:37 +02:00
function ReaderHighlight : onPanelZoom ( arg , ges )
self : clear ( )
local hold_pos = self.view : screenToPageTransform ( ges.pos )
if not hold_pos then return false end -- outside page boundary
local rect = self.ui . document : getPanelFromPage ( hold_pos.page , hold_pos )
if not rect then return false end -- panel not found, return
local image = self.ui . document : getPagePart ( hold_pos.page , rect , 0 )
if image then
local ImageViewer = require ( " ui/widget/imageviewer " )
local imgviewer = ImageViewer : new {
image = image ,
with_title_bar = false ,
fullscreen = true ,
}
UIManager : show ( imgviewer )
2020-12-24 09:07:27 +01:00
return true
2020-09-24 15:17:37 +02:00
end
2020-12-24 09:07:27 +01:00
return false
2020-09-24 15:17:37 +02:00
end
2017-01-15 21:51:43 +01:00
function ReaderHighlight : onHold ( arg , ges )
2020-09-24 15:17:37 +02:00
if self.document . info.has_pages and self.panel_zoom_enabled then
2020-12-24 09:07:27 +01:00
local res = self : onPanelZoom ( arg , ges )
if res or not self.panel_zoom_fallback_to_text_selection then
return res
end
2020-09-24 15:17:37 +02:00
end
2017-09-20 17:35:30 +02:00
self : clear ( ) -- clear previous highlight (delayed clear may not have done it yet)
2014-03-13 21:52:43 +08:00
self.hold_pos = self.view : screenToPageTransform ( ges.pos )
2016-12-29 00:10:38 -08:00
logger.dbg ( " hold position in page " , self.hold_pos )
2014-03-13 21:52:43 +08:00
if not self.hold_pos then
2016-12-29 00:10:38 -08:00
logger.dbg ( " not inside page area " )
2020-01-24 20:05:21 +01:00
return false
2014-03-13 21:52:43 +08:00
end
2023-11-19 09:52:51 +02:00
self.gest_pos = self.hold_pos
2013-10-12 23:07:13 +08:00
2017-01-15 21:51:43 +01:00
-- check if we were holding on an image
2018-04-21 22:03:04 +02:00
-- we provide want_frames=true, so we get a list of images for
-- animated GIFs (supported by ImageViewer)
2022-09-11 19:55:28 +02:00
-- We provide accept_cre_scalable_image=true to get, if the image is a SVG image,
-- a function that ImageViewer can use to get a perfect bb at any scale factor.
local image = self.ui . document : getImageFromPosition ( self.hold_pos , true , true )
2017-01-15 21:51:43 +01:00
if image then
logger.dbg ( " hold on image " )
local ImageViewer = require ( " ui/widget/imageviewer " )
local imgviewer = ImageViewer : new {
image = image ,
-- title_text = _("Document embedded image"),
-- No title, more room for image
with_title_bar = false ,
fullscreen = true ,
}
UIManager : show ( imgviewer )
2022-03-12 19:16:50 +08:00
self : onStopHighlightIndicator ( )
2017-01-15 21:51:43 +01:00
return true
end
-- otherwise, we must be holding on text
2021-09-11 12:04:48 +03:00
if self.view . highlight.disabled then return false end -- Long-press action "Do nothing" checked
2014-03-13 21:52:43 +08:00
local ok , word = pcall ( self.ui . document.getWordFromPosition , self.ui . document , self.hold_pos )
if ok and word then
2016-12-29 00:10:38 -08:00
logger.dbg ( " selected word: " , word )
2021-10-23 21:12:56 +11:00
-- Convert "word selection" table to "text selection" table because we
2021-10-23 21:13:09 +11:00
-- use text selections throughout readerhighlight in order to allow the
-- highlight to be corrected by language-specific plugins more easily.
2021-10-23 21:12:56 +11:00
self.is_word_selection = true
self.selected_text = {
text = word.word or " " ,
pos0 = word.pos0 or word.pos ,
pos1 = word.pos1 or word.pos ,
sboxes = word.sbox and { word.sbox } ,
pboxes = word.pbox and { word.pbox } ,
}
2017-09-10 14:26:49 +02:00
local link = self.ui . link : getLinkFromGes ( ges )
2017-09-09 18:30:00 +02:00
self.selected_link = nil
if link then
logger.dbg ( " link: " , link )
self.selected_link = link
end
2021-10-23 21:13:09 +11:00
2021-10-25 17:45:26 +11:00
if self.ui . languagesupport and self.ui . languagesupport : hasActiveLanguagePlugins ( ) then
2021-10-23 21:13:09 +11:00
-- If this is a language where pan-less word selection needs some
-- extra work above and beyond what the document engine gives us
-- from getWordFromPosition, call the relevant language-specific
-- plugin.
local new_selected_text = self.ui . languagesupport : improveWordSelection ( self.selected_text )
if new_selected_text then
self.selected_text = new_selected_text
end
end
2022-12-18 18:04:00 +02:00
if self.ui . paging then
2021-10-23 21:12:56 +11:00
self.view . highlight.temp [ self.hold_pos . page ] = self.selected_text . sboxes
2021-01-09 21:59:29 +01:00
-- Unfortunately, getWordFromPosition() may not return good coordinates,
-- so refresh the whole page
UIManager : setDirty ( self.dialog , " ui " )
else
2021-10-23 21:12:56 +11:00
-- With crengine, getWordFromPosition() does return good coordinates.
UIManager : setDirty ( self.dialog , " ui " , Geom.boundingBox ( self.selected_text . sboxes ) )
2014-03-13 21:52:43 +08:00
end
2020-05-05 18:56:30 +02:00
self : _resetHoldTimer ( )
2019-03-13 13:10:32 +01:00
if word.pos0 then
-- Remember original highlight start position, so we can show
-- a marker when back from across-pages text selection, which
-- is handled in onHoldPan()
self.selected_text_start_xpointer = word.pos0
end
2020-01-24 20:05:21 +01:00
return true
2014-03-13 21:52:43 +08:00
end
2020-01-24 20:05:21 +01:00
return false
2013-06-15 23:13:19 +08:00
end
2016-03-27 17:18:25 -07:00
function ReaderHighlight : onHoldPan ( _ , ges )
2021-12-10 11:06:16 +02:00
if self.view . highlight.disabled then return false end -- Long-press action "Do nothing" checked
2014-03-13 21:52:43 +08:00
if self.hold_pos == nil then
2016-12-29 00:10:38 -08:00
logger.dbg ( " no previous hold position " )
2020-05-05 18:56:30 +02:00
self : _resetHoldTimer ( true )
2014-03-13 21:52:43 +08:00
return true
end
2014-07-02 16:38:09 +08:00
local page_area = self.view : getScreenPageArea ( self.hold_pos . page )
if ges.pos : notIntersectWith ( page_area ) then
2016-12-29 00:10:38 -08:00
logger.dbg ( " not inside page area " , ges , page_area )
2020-05-05 18:56:30 +02:00
self : _resetHoldTimer ( )
2014-07-02 16:38:09 +08:00
return true
end
2014-03-13 21:52:43 +08:00
self.holdpan_pos = self.view : screenToPageTransform ( ges.pos )
2016-12-29 00:10:38 -08:00
logger.dbg ( " holdpan position in page " , self.holdpan_pos )
2019-02-03 10:01:58 +01:00
2021-12-10 11:06:16 +02:00
if self.ui . rolling and self.selected_text_start_xpointer then
2019-02-03 10:01:58 +01:00
-- With CreDocuments, allow text selection across multiple pages
-- by (temporarily) switching to scroll mode when panning to the
-- top left or bottom right corners.
2020-01-06 21:09:00 +01:00
local mirrored_reading = BD.mirroredUILayout ( )
2021-11-21 19:33:51 +02:00
if self.view . inverse_reading_order then
2020-01-06 21:09:00 +01:00
mirrored_reading = not mirrored_reading
end
local is_in_prev_page_corner , is_in_next_page_corner
if mirrored_reading then
2019-12-06 22:55:39 +01:00
-- Note: this might not be really usable, as crengine native selection
-- is not adapted to RTL text
2020-01-06 21:09:00 +01:00
-- top right corner
2023-11-19 09:52:51 +02:00
is_in_prev_page_corner = self.holdpan_pos . y < 1 / 8 * self.screen_h
and self.holdpan_pos . x > 7 / 8 * self.screen_w
2020-01-06 21:09:00 +01:00
-- bottom left corner
2023-11-19 09:52:51 +02:00
is_in_next_page_corner = self.holdpan_pos . y > 7 / 8 * self.screen_h
and self.holdpan_pos . x < 1 / 8 * self.screen_w
2020-01-06 21:09:00 +01:00
else -- default in LTR UI with no inverse_reading_order
-- top left corner
2023-11-19 09:52:51 +02:00
is_in_prev_page_corner = self.holdpan_pos . y < 1 / 8 * self.screen_h
and self.holdpan_pos . x < 1 / 8 * self.screen_w
2020-01-06 21:09:00 +01:00
-- bottom right corner
2023-11-19 09:52:51 +02:00
is_in_next_page_corner = self.holdpan_pos . y > 7 / 8 * self.screen_h
and self.holdpan_pos . x > 7 / 8 * self.screen_w
2019-12-06 22:55:39 +01:00
end
2020-01-06 21:09:00 +01:00
if is_in_prev_page_corner or is_in_next_page_corner then
2020-05-05 18:56:30 +02:00
self : _resetHoldTimer ( )
2019-02-03 10:01:58 +01:00
if self.was_in_some_corner then
-- Do nothing, wait for the user to move his finger out of that corner
return true
end
self.was_in_some_corner = true
2019-03-13 13:10:32 +01:00
if self.ui . document : getVisiblePageCount ( ) == 1 then -- single page mode
-- We'll adjust hold_pos.y after the mode switch and the scroll
-- so it's accurate in the new screen coordinates
local orig_y = self.ui . document : getScreenPositionFromXPointer ( self.selected_text_start_xpointer )
if self.view . view_mode ~= " scroll " then
-- Switch from page mode to scroll mode
local restore_page_mode_xpointer = self.ui . document : getXPointer ( ) -- top of current page
self.restore_page_mode_func = function ( )
self.ui : handleEvent ( Event : new ( " SetViewMode " , " page " ) )
self.ui . rolling : onGotoXPointer ( restore_page_mode_xpointer , self.selected_text_start_xpointer )
end
self.ui : handleEvent ( Event : new ( " SetViewMode " , " scroll " ) )
end
-- (using rolling:onGotoViewRel(1/3) has some strange side effects)
2023-11-19 09:52:51 +02:00
local scroll_distance = math.floor ( self.screen_h * 1 / 3 )
2020-01-06 21:09:00 +01:00
local move_y = is_in_next_page_corner and scroll_distance or - scroll_distance
2019-03-13 13:10:32 +01:00
self.ui . rolling : _gotoPos ( self.ui . document : getCurrentPos ( ) + move_y )
local new_y = self.ui . document : getScreenPositionFromXPointer ( self.selected_text_start_xpointer )
self.hold_pos . y = self.hold_pos . y - orig_y + new_y
UIManager : setDirty ( self.dialog , " ui " )
return true
else -- two pages mode
-- We don't switch to scroll mode: we just turn 1 page to
-- allow continuing the selection.
-- Unlike in 1-page mode, we have a limitation here: we can't adjust
-- the selection to further than current page and prev/next one.
-- So don't handle another corner if we already handled one:
2020-01-06 21:09:00 +01:00
-- Note that this feature won't work well with the RTL UI or
-- if inverse_reading_order as crengine currently always displays
-- the first page on the left and the second on the right in
-- dual page mode.
2019-03-13 13:10:32 +01:00
if self.restore_page_mode_func then
return true
end
-- Also, we are not able to move hold_pos.x out of screen,
-- so if we started on the right page, ignore top left corner,
-- and if we started on the left page, ignore bottom right corner.
2023-11-19 09:52:51 +02:00
local screen_half_width = math.floor ( self.screen_w * 0.5 )
2020-01-06 21:09:00 +01:00
if self.hold_pos . x >= screen_half_width and is_in_prev_page_corner then
2019-03-13 13:10:32 +01:00
return true
2020-01-06 21:09:00 +01:00
elseif self.hold_pos . x <= screen_half_width and is_in_next_page_corner then
2019-03-13 13:10:32 +01:00
return true
end
2021-01-29 01:02:19 +01:00
-- To be able to browse half-page when 2 visible pages as 1 page number,
-- we must work with internal page numbers
local cur_page = self.ui . document : getCurrentPage ( true )
2019-02-03 10:01:58 +01:00
local restore_page_mode_xpointer = self.ui . document : getXPointer ( ) -- top of current page
2021-01-29 01:02:19 +01:00
self.ui . document.no_page_sync = true -- avoid CreDocument:drawCurrentViewByPage() to resync page
2019-02-03 10:01:58 +01:00
self.restore_page_mode_func = function ( )
2021-01-29 01:02:19 +01:00
self.ui . document.no_page_sync = nil
2019-02-03 10:01:58 +01:00
self.ui . rolling : onGotoXPointer ( restore_page_mode_xpointer , self.selected_text_start_xpointer )
end
2020-01-06 21:09:00 +01:00
if is_in_next_page_corner then -- bottom right corner in LTR UI
2021-01-29 01:02:19 +01:00
self.ui . rolling : _gotoPage ( cur_page + 1 , true , true ) -- no odd left page enforcement
2019-03-13 13:10:32 +01:00
self.hold_pos . x = self.hold_pos . x - screen_half_width
2020-01-06 21:09:00 +01:00
else -- top left corner in RTL UI
2021-01-29 01:02:19 +01:00
self.ui . rolling : _gotoPage ( cur_page - 1 , true , true ) -- no odd left page enforcement
2019-03-13 13:10:32 +01:00
self.hold_pos . x = self.hold_pos . x + screen_half_width
end
UIManager : setDirty ( self.dialog , " ui " )
return true
2019-02-03 10:01:58 +01:00
end
else
self.was_in_some_corner = nil
end
end
2014-11-30 00:12:00 +00:00
local old_text = self.selected_text and self.selected_text . text
2014-03-13 21:52:43 +08:00
self.selected_text = self.ui . document : getTextFromPositions ( self.hold_pos , self.holdpan_pos )
2023-11-19 09:52:51 +02:00
self.gest_pos = self.holdpan_pos
2021-10-23 21:12:56 +11:00
self.is_word_selection = false
2019-02-03 10:01:58 +01:00
if self.selected_text and self.selected_text . pos0 then
if not self.selected_text_start_xpointer then
2019-03-13 13:10:32 +01:00
-- This should have been set in onHold(), where we would get
-- a precise pos0 on the first word selected.
-- Do it here too in case onHold() missed it, but it could be
-- less precise (getTextFromPositions() does order pos0 and pos1,
-- so it's not certain pos0 is where we started from; we get
-- the ones from the first pan, and if it is not small enough
-- and spans quite some height, the marker could point away
-- from the start position)
2019-02-03 10:01:58 +01:00
self.selected_text_start_xpointer = self.selected_text . pos0
end
end
2014-11-30 00:12:00 +00:00
if self.selected_text and old_text and old_text == self.selected_text . text then
-- no modification
return
end
2020-05-05 18:56:30 +02:00
self : _resetHoldTimer ( ) -- selection updated
2016-12-29 00:10:38 -08:00
logger.dbg ( " selected text: " , self.selected_text )
2014-03-13 21:52:43 +08:00
if self.selected_text then
self.view . highlight.temp [ self.hold_pos . page ] = self.selected_text . sboxes
end
2014-12-03 14:06:43 +08:00
UIManager : setDirty ( self.dialog , " ui " )
2013-06-15 23:13:19 +08:00
end
2019-08-22 17:11:47 +02:00
local info_message_ocr_text = _ ( [ [
No OCR results or no language data .
2018-03-05 16:38:04 +01:00
KOReader has a build - in OCR engine for recognizing words in scanned PDF and DjVu documents . In order to use OCR in scanned pages , you need to install tesseract trained data for your document language .
2020-07-30 22:15:02 +02:00
You can download language data files for version 3.04 from https : // tesseract - ocr.github . io / tessdoc / Data - Files
2018-03-05 16:38:04 +01:00
2018-03-17 18:13:04 +01:00
Copy the language data files for Tesseract 3.04 ( e.g . , eng.traineddata for English and spa.traineddata for Spanish ) into koreader / data / tessdata ] ] )
2018-03-05 16:38:04 +01:00
2021-10-23 21:12:56 +11:00
function ReaderHighlight : lookup ( selected_text , selected_link )
-- convert sboxes to word boxes
local word_boxes = { }
for i , sbox in ipairs ( selected_text.sboxes ) do
word_boxes [ i ] = self.view : pageToScreenTransform ( self.hold_pos . page , sbox )
end
2014-03-13 21:52:43 +08:00
-- if we extracted text directly
2023-10-03 20:19:23 +00:00
if # selected_text.text > 0 and self.hold_pos then
2021-10-23 21:12:56 +11:00
self.ui : handleEvent ( Event : new ( " LookupWord " , selected_text.text , false , word_boxes , self , selected_link ) )
2014-03-13 21:52:43 +08:00
-- or we will do OCR
2021-10-23 21:12:56 +11:00
elseif selected_text.sboxes and self.hold_pos then
local text = self.ui . document : getOCRText ( self.hold_pos . page , selected_text.sboxes )
if not text then
-- getOCRText is not implemented in some document backends, but
-- getOCRWord is implemented everywhere. As such, fall back to
-- getOCRWord.
text = " "
for _ , sbox in ipairs ( selected_text.sboxes ) do
local word = self.ui . document : getOCRWord ( self.hold_pos . page , { sbox = sbox } )
logger.dbg ( " OCRed word: " , word )
2021-10-23 16:29:00 +02:00
--- @fixme This might produce incorrect results on RTL text.
2021-10-23 21:12:56 +11:00
if word and word ~= " " then
text = text .. word
end
end
end
logger.dbg ( " OCRed text: " , text )
if text and text ~= " " then
self.ui : handleEvent ( Event : new ( " LookupWord " , text , false , word_boxes , self , selected_link ) )
2018-03-05 16:38:04 +01:00
else
UIManager : show ( InfoMessage : new {
text = info_message_ocr_text ,
} )
end
2014-03-13 21:52:43 +08:00
end
2013-07-20 02:49:03 +08:00
end
2021-11-06 18:11:06 +11:00
dbg : guard ( ReaderHighlight , " lookup " ,
function ( self , selected_text , selected_link )
assert ( selected_text ~= nil ,
" lookup must not be called with nil selected_text! " )
end )
2013-07-20 02:49:03 +08:00
2022-06-13 03:34:17 +08:00
function ReaderHighlight : getSelectedWordContext ( nb_words )
2022-10-25 18:23:18 +08:00
if not self.selected_text then return end
local ok , prev_context , next_context = pcall ( self.ui . document.getSelectedWordContext , self.ui . document ,
self.selected_text . text , nb_words , self.selected_text . pos0 , self.selected_text . pos1 )
if ok then
return prev_context , next_context
2022-06-13 03:34:17 +08:00
end
end
2021-09-01 14:17:35 +02:00
function ReaderHighlight : viewSelectionHTML ( debug_view , no_css_files_buttons )
2022-12-18 18:04:00 +02:00
if self.ui . paging then return end
2018-10-08 18:58:43 +02:00
if self.selected_text and self.selected_text . pos0 and self.selected_text . pos1 then
2023-03-05 13:54:25 +01:00
local ViewHtml = require ( " ui/viewhtml " )
ViewHtml : viewSelectionHTML ( self.ui . document , self.selected_text )
2018-10-08 18:58:43 +02:00
end
end
2022-01-16 21:54:08 +02:00
function ReaderHighlight : translate ( selected_text , page , index )
2022-06-11 11:08:40 +02:00
if self.ui . rolling then
-- Extend the selected text to include any punctuation at start or end,
-- which may give a better translation with the added context.
local extended_text = self.ui . document : extendXPointersToSentenceSegment ( selected_text.pos0 , selected_text.pos1 )
if extended_text then
selected_text = extended_text
end
end
2023-10-03 20:19:23 +00:00
if # selected_text.text > 0 then
2022-01-16 21:54:08 +02:00
self : onTranslateText ( selected_text.text , page , index )
2014-03-13 21:52:43 +08:00
-- or we will do OCR
2021-05-16 19:54:46 +02:00
elseif self.hold_pos then
2014-03-13 21:52:43 +08:00
local text = self.ui . document : getOCRText ( self.hold_pos . page , selected_text )
2016-12-29 00:10:38 -08:00
logger.dbg ( " OCRed text: " , text )
2018-03-05 16:38:04 +01:00
if text and text ~= " " then
2018-12-16 18:02:38 +01:00
self : onTranslateText ( text )
2018-03-05 16:38:04 +01:00
else
UIManager : show ( InfoMessage : new {
text = info_message_ocr_text ,
} )
end
2014-03-13 21:52:43 +08:00
end
2013-07-20 02:49:03 +08:00
end
2021-11-06 18:11:06 +11:00
dbg : guard ( ReaderHighlight , " translate " ,
function ( self , selected_text )
assert ( selected_text ~= nil ,
" translate must not be called with nil selected_text! " )
end )
2013-07-20 02:49:03 +08:00
2023-05-18 22:57:16 +03:00
function ReaderHighlight : onTranslateText ( text , page , index )
2023-05-20 15:41:50 +03:00
Translator : showTranslation ( text , true , nil , nil , true , page , index )
2023-05-17 07:34:37 +03:00
end
function ReaderHighlight : onTranslateCurrentPage ( )
local x0 , y0 , x1 , y1 , page , is_reflow
if self.ui . rolling then
x0 = 0
y0 = 0
2023-11-19 09:52:51 +02:00
x1 = self.screen_w
y1 = self.screen_h
2023-05-17 07:34:37 +03:00
else
page = self.ui : getCurrentPage ( )
is_reflow = self.ui . document.configurable . text_wrap
self.ui . document.configurable . text_wrap = 0
local page_boxes = self.ui . document : getTextBoxes ( page )
if page_boxes and page_boxes [ 1 ] [ 1 ] . word then
x0 = page_boxes [ 1 ] [ 1 ] . x0
y0 = page_boxes [ 1 ] [ 1 ] . y0
x1 = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . x1
y1 = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . y1
end
end
local res = x0 and self.ui . document : getTextFromPositions ( { x = x0 , y = y0 , page = page } , { x = x1 , y = y1 } , true )
if self.ui . paging then
self.ui . document.configurable . text_wrap = is_reflow
end
if res and res.text then
2023-08-30 07:53:59 +03:00
Translator : showTranslation ( res.text , false , self.ui . doc_props.language )
2023-05-17 07:34:37 +03:00
end
2018-12-16 18:02:38 +01:00
end
2014-08-11 21:49:42 +08:00
function ReaderHighlight : onHoldRelease ( )
2022-01-08 16:58:32 +01:00
if self.clear_id then
-- Something has requested a clear id and is about to clear
-- the highlight: it may be a onHoldClose() that handled
-- "hold" and was closed, and can't handle "hold_release":
-- ignore this "hold_release" event.
return true
end
2021-11-21 19:31:10 +02:00
local default_highlight_action = G_reader_settings : readSetting ( " default_highlight_action " , " ask " )
if self.select_mode then -- extended highlighting, ending fragment
if self.selected_text then
2022-12-02 20:22:27 +02:00
self.select_mode = false
self : extendSelection ( )
if default_highlight_action == " select " then
self : saveHighlight ( true )
self : clear ( )
2021-11-21 19:31:10 +02:00
else
2022-12-02 20:22:27 +02:00
self : onShowHighlightMenu ( )
2021-11-21 19:31:10 +02:00
end
end
return true
end
2021-11-21 18:41:07 +01:00
2020-05-05 18:56:30 +02:00
local long_final_hold = false
2022-05-05 21:00:22 +02:00
if self.hold_last_time then
local hold_duration = time.now ( ) - self.hold_last_time
2022-05-24 00:25:50 +02:00
local long_hold_threshold_s = G_reader_settings : readSetting ( " highlight_long_hold_threshold_s " , 3 ) -- seconds
2022-05-05 21:00:22 +02:00
if hold_duration > time.s ( long_hold_threshold_s ) then
2020-05-05 18:56:30 +02:00
-- We stayed 3 seconds before release without updating selection
long_final_hold = true
end
2022-05-05 21:00:22 +02:00
self.hold_last_time = nil
2020-05-05 18:56:30 +02:00
end
2021-10-23 21:12:56 +11:00
if self.is_word_selection then -- single-word selection
2020-05-05 18:56:30 +02:00
if long_final_hold or G_reader_settings : isTrue ( " highlight_action_on_single_word " ) then
2021-10-23 21:12:56 +11:00
self.is_word_selection = false
2017-09-10 20:35:27 +02:00
end
end
2019-01-17 22:12:38 +01:00
2021-11-06 18:11:06 +11:00
if self.selected_text then
if self.is_word_selection then
self : lookup ( self.selected_text , self.selected_link )
else
if long_final_hold or default_highlight_action == " ask " then
-- bypass default action and show popup if long final hold
self : onShowHighlightMenu ( )
elseif default_highlight_action == " highlight " then
2022-06-11 11:08:40 +02:00
self : saveHighlight ( true )
2021-11-06 18:11:06 +11:00
self : onClose ( )
2021-11-21 19:31:10 +02:00
elseif default_highlight_action == " select " then
self : startSelection ( )
self : onClose ( )
2022-11-02 13:58:12 -04:00
elseif default_highlight_action == " note " then
self : addNote ( )
self : onClose ( )
2021-11-06 18:11:06 +11:00
elseif default_highlight_action == " translate " then
self : translate ( self.selected_text )
elseif default_highlight_action == " wikipedia " then
self : lookupWikipedia ( )
self : onClose ( )
elseif default_highlight_action == " dictionary " then
self : onHighlightDictLookup ( )
self : onClose ( )
elseif default_highlight_action == " search " then
self : onHighlightSearch ( )
-- No self:onClose() to not remove the selected text
-- which will have been the first search result
end
2018-12-16 18:02:38 +01:00
end
2014-03-13 21:52:43 +08:00
end
return true
2013-04-24 06:59:52 +08:00
end
2022-11-02 13:58:12 -04:00
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
2019-03-14 15:33:04 +01:00
function ReaderHighlight : onCycleHighlightAction ( )
2021-09-11 12:04:48 +03:00
local current_action = G_reader_settings : readSetting ( " default_highlight_action " , " ask " )
local next_action_num
for i , v in ipairs ( long_press_action ) do
if v [ 2 ] == current_action then
next_action_num = i + 1
break
end
end
if next_action_num > # long_press_action then
next_action_num = 1
2019-03-14 15:33:04 +01:00
end
2022-11-02 13:58:12 -04:00
self : onSetHighlightAction ( next_action_num )
2019-04-18 06:12:38 -04:00
return true
end
function ReaderHighlight : onCycleHighlightStyle ( )
2021-10-18 20:17:37 +03:00
local current_style = self.view . highlight.saved_drawer
local next_style_num
for i , v in ipairs ( highlight_style ) do
if v [ 2 ] == current_style then
next_style_num = i + 1
break
end
end
if next_style_num > # highlight_style then
next_style_num = 1
end
self.view . highlight.saved_drawer = highlight_style [ next_style_num ] [ 2 ]
2019-04-18 06:12:38 -04:00
self.ui . doc_settings : saveSetting ( " highlight_drawer " , self.view . highlight.saved_drawer )
UIManager : show ( Notification : new {
2021-10-18 20:17:37 +03:00
text = T ( _ ( " Default highlight style changed to '%1'. " ) , highlight_style [ next_style_num ] [ 1 ] ) ,
2019-04-18 06:12:38 -04:00
} )
return true
2019-03-14 15:33:04 +01:00
end
2014-11-05 12:28:11 +08:00
function ReaderHighlight : highlightFromHoldPos ( )
2014-08-11 21:49:42 +08:00
if self.hold_pos then
if not self.selected_text then
self.selected_text = self.ui . document : getTextFromPositions ( self.hold_pos , self.hold_pos )
2021-10-25 17:45:26 +11:00
if self.ui . languagesupport and self.ui . languagesupport : hasActiveLanguagePlugins ( ) then
2021-10-23 21:13:09 +11:00
-- Match language-specific expansion you'd get from self:onHold().
local new_selected_text = self.ui . languagesupport : improveWordSelection ( self.selected_text )
if new_selected_text then
self.selected_text = new_selected_text
end
end
2016-12-29 00:10:38 -08:00
logger.dbg ( " selected text: " , self.selected_text )
2014-08-11 21:49:42 +08:00
end
end
end
2014-11-05 12:28:11 +08:00
function ReaderHighlight : onHighlight ( )
self : saveHighlight ( )
end
2017-10-18 17:19:06 +02:00
function ReaderHighlight : onUnhighlight ( bookmark_item )
2017-07-28 22:39:54 +02:00
local page
local sel_text
local sel_pos0
2017-10-18 17:19:06 +02:00
local datetime
2017-07-28 22:39:54 +02:00
local idx
2017-10-18 17:19:06 +02:00
if bookmark_item then -- called from Bookmarks menu onHold
page = bookmark_item.page
sel_text = bookmark_item.notes
sel_pos0 = bookmark_item.pos0
datetime = bookmark_item.datetime
else -- called from DictQuickLookup Unhighlight button
2021-05-16 19:54:46 +02:00
--- @fixme: is this self.hold_pos access safe?
2017-07-28 22:39:54 +02:00
page = self.hold_pos . page
2020-07-22 00:14:57 +02:00
sel_text = cleanupSelectedText ( self.selected_text . text )
2017-07-28 22:39:54 +02:00
sel_pos0 = self.selected_text . pos0
end
2022-12-18 18:04:00 +02:00
if self.ui . paging then -- We can safely use page
2021-07-18 19:56:17 +02:00
-- As we may have changed spaces and hyphens handling in the extracted
-- text over the years, check text identities with them removed
2023-08-01 10:09:29 +02:00
local sel_text_cleaned = sel_text : gsub ( " [ -] " , " " ) : gsub ( " \u{00AD} " , " " )
2017-10-18 17:19:06 +02:00
for index = 1 , # self.view . highlight.saved [ page ] do
local highlight = self.view . highlight.saved [ page ] [ index ]
-- pos0 are tables and can't be compared directly, except when from
-- DictQuickLookup where these are the same object.
-- If bookmark_item provided, just check datetime
2021-07-18 19:56:17 +02:00
if ( ( datetime == nil and highlight.pos0 == sel_pos0 ) or
( datetime ~= nil and highlight.datetime == datetime ) ) then
2023-08-01 10:09:29 +02:00
if highlight.text : gsub ( " [ -] " , " " ) : gsub ( " \u{00AD} " , " " ) == sel_text_cleaned then
2021-07-18 19:56:17 +02:00
idx = index
break
end
2017-10-18 17:19:06 +02:00
end
end
else -- page is a xpointer
-- The original page could be found in bookmark_item.text, but
-- no more if it has been renamed: we need to loop through all
-- highlights on all page slots
for p , highlights in pairs ( self.view . highlight.saved ) do
for index = 1 , # highlights do
local highlight = highlights [ index ]
-- pos0 are strings and can be compared directly
if highlight.text == sel_text and (
( datetime == nil and highlight.pos0 == sel_pos0 ) or
( datetime ~= nil and highlight.datetime == datetime ) ) then
page = p -- this is the original page slot
idx = index
break
end
end
if idx then
break
end
2017-07-28 22:39:54 +02:00
end
end
2022-12-09 10:08:14 +02:00
if idx then
logger.dbg ( " found highlight to delete on page " , page , idx )
self : deleteHighlight ( page , idx , bookmark_item )
return true
2017-10-18 17:19:06 +02:00
end
2017-07-28 22:39:54 +02:00
end
2015-03-12 18:50:57 +08:00
function ReaderHighlight : getHighlightBookmarkItem ( )
if self.hold_pos and not self.selected_text then
self : highlightFromHoldPos ( )
end
if self.selected_text and self.selected_text . pos0 and self.selected_text . pos1 then
return {
2022-12-02 20:22:27 +02:00
page = self.ui . paging and self.selected_text . pos0.page or self.selected_text . pos0 ,
2015-03-12 18:50:57 +08:00
pos0 = self.selected_text . pos0 ,
pos1 = self.selected_text . pos1 ,
2020-07-22 00:14:57 +02:00
notes = cleanupSelectedText ( self.selected_text . text ) ,
2015-03-12 18:50:57 +08:00
highlighted = true ,
}
end
end
2022-06-11 11:08:40 +02:00
function ReaderHighlight : saveHighlight ( extend_to_sentence )
2017-07-27 19:00:37 +02:00
self.ui : handleEvent ( Event : new ( " AddHighlight " ) )
2016-12-29 00:10:38 -08:00
logger.dbg ( " save highlight " )
2022-12-02 20:22:27 +02:00
if self.selected_text and self.selected_text . pos0 and self.selected_text . pos1 then
2022-06-11 11:08:40 +02:00
if extend_to_sentence and self.ui . rolling then
local extended_text = self.ui . document : extendXPointersToSentenceSegment ( self.selected_text . pos0 , self.selected_text . pos1 )
if extended_text then
self.selected_text = extended_text
end
end
2022-12-02 20:22:27 +02:00
local page = self.ui . paging and self.selected_text . pos0.page or self.ui . document : getPageFromXPointer ( self.selected_text . pos0 )
2014-03-13 21:52:43 +08:00
if not self.view . highlight.saved [ page ] then
self.view . highlight.saved [ page ] = { }
end
2014-11-27 21:59:27 +08:00
local datetime = os.date ( " %Y-%m-%d %H:%M:%S " )
2022-01-21 17:56:08 +02:00
local pg_or_xp = self.ui . paging and page or self.selected_text . pos0
2020-06-17 23:51:02 +02:00
local chapter_name = self.ui . toc : getTocTitleByPage ( pg_or_xp )
2014-11-27 21:59:27 +08:00
local hl_item = {
datetime = datetime ,
2020-07-22 00:14:57 +02:00
text = cleanupSelectedText ( self.selected_text . text ) ,
2014-11-27 21:59:27 +08:00
pos0 = self.selected_text . pos0 ,
pos1 = self.selected_text . pos1 ,
pboxes = self.selected_text . pboxes ,
2022-12-02 20:22:27 +02:00
ext = self.selected_text . ext ,
2014-11-27 21:59:27 +08:00
drawer = self.view . highlight.saved_drawer ,
2022-01-21 17:56:08 +02:00
chapter = chapter_name ,
2014-11-27 21:59:27 +08:00
}
2014-03-13 21:52:43 +08:00
table.insert ( self.view . highlight.saved [ page ] , hl_item )
2015-03-12 18:50:57 +08:00
local bookmark_item = self : getHighlightBookmarkItem ( )
if bookmark_item then
2022-01-21 17:56:08 +02:00
bookmark_item.datetime = datetime
bookmark_item.chapter = chapter_name
2015-03-12 18:50:57 +08:00
self.ui . bookmark : addBookmark ( bookmark_item )
end
2022-12-02 20:22:27 +02:00
self : writePdfAnnotation ( " save " , page , hl_item )
2019-03-12 20:14:34 +01:00
return page , # self.view . highlight.saved [ page ]
2014-03-13 21:52:43 +08:00
end
2013-06-15 23:13:19 +08:00
end
2022-12-02 20:22:27 +02:00
function ReaderHighlight : writePdfAnnotation ( action , page , item , content )
if self.ui . rolling or G_reader_settings : readSetting ( " save_document " ) == " disable " then
return
end
logger.dbg ( " write to pdf document " , action , item )
2022-12-12 00:47:34 +01:00
local function doAction ( action_ , page_ , item_ , content_ )
if action_ == " save " then
return self.ui . document : saveHighlight ( page_ , item_ )
elseif action_ == " delete " then
return self.ui . document : deleteHighlight ( page_ , item_ )
elseif action_ == " content " then
return self.ui . document : updateHighlightContents ( page_ , item_ , content_ )
2022-12-02 20:22:27 +02:00
end
end
local can_write
if item.pos0 . page == item.pos1 . page then -- single-page highlight
local item_
if item.pboxes then
item_ = item
else -- called from bookmarks to write bookmark note to annotation
for _ , hl in ipairs ( self.view . highlight.saved [ page ] ) do
if hl.datetime == item.datetime then
item_ = { pboxes = hl.pboxes }
break
end
end
end
can_write = doAction ( action , page , item_ , content )
else -- multi-page highlight
local is_reflow = self.ui . document.configurable . text_wrap
for hl_page = item.pos0 . page , item.pos1 . page do
self.ui . document.configurable . text_wrap = 0
local hl_part = self : getSavedExtendedHighlightPage ( item , hl_page )
self.ui . document.configurable . text_wrap = is_reflow
can_write = doAction ( action , hl_page , hl_part , content )
if can_write == false then break end
if action == " save " then -- update pboxes from quadpoints
item.ext [ hl_page ] . pboxes = hl_part.pboxes
end
end
end
2020-02-25 21:26:19 +01:00
if can_write == false and not self.warned_once then
self.warned_once = true
UIManager : show ( InfoMessage : new {
text = _ ( [ [
Highlights in this document will be saved in the settings file , but they won ' t be written in the document itself because the file is in a read-only location.
If you wish your highlights to be saved in the document , just move it to a writable directory first . ] ] ) ,
timeout = 5 ,
} )
end
2014-02-02 00:16:51 +08:00
end
2022-01-16 21:54:08 +02:00
function ReaderHighlight : addNote ( text )
2022-06-11 11:08:40 +02:00
local page , index = self : saveHighlight ( true )
2022-01-16 21:54:08 +02:00
if text then self : clear ( ) end
self : editHighlight ( page , index , true , text )
2019-03-12 20:14:34 +01:00
UIManager : close ( self.edit_highlight_dialog )
2021-03-21 13:57:18 +01:00
self.edit_highlight_dialog = nil
2013-06-15 23:13:19 +08:00
end
2014-08-20 14:41:45 +08:00
function ReaderHighlight : lookupWikipedia ( )
if self.selected_text then
2020-07-22 00:14:57 +02:00
self.ui : handleEvent ( Event : new ( " LookupWikipedia " , cleanupSelectedText ( self.selected_text . text ) ) )
2014-08-20 14:41:45 +08:00
end
end
2014-11-05 12:28:11 +08:00
function ReaderHighlight : onHighlightSearch ( )
2016-12-29 00:10:38 -08:00
logger.dbg ( " search highlight " )
2021-03-21 13:57:18 +01:00
-- First, if our dialog is still shown, close it.
if self.highlight_dialog then
UIManager : close ( self.highlight_dialog )
self.highlight_dialog = nil
end
2014-11-05 12:28:11 +08:00
self : highlightFromHoldPos ( )
if self.selected_text then
2020-07-22 00:14:57 +02:00
local text = util.stripPunctuation ( cleanupSelectedText ( self.selected_text . text ) )
2015-03-12 17:41:20 +08:00
self.ui : handleEvent ( Event : new ( " ShowSearchDialog " , text ) )
2014-11-05 12:28:11 +08:00
end
end
2015-06-15 18:11:42 +08:00
function ReaderHighlight : onHighlightDictLookup ( )
2016-12-29 00:10:38 -08:00
logger.dbg ( " dictionary lookup highlight " )
2015-06-15 18:11:42 +08:00
self : highlightFromHoldPos ( )
if self.selected_text then
2020-07-22 00:14:57 +02:00
self.ui : handleEvent ( Event : new ( " LookupWord " , cleanupSelectedText ( self.selected_text . text ) ) )
2015-06-15 18:11:42 +08:00
end
end
2017-10-18 17:19:06 +02:00
function ReaderHighlight : deleteHighlight ( page , i , bookmark_item )
logger.dbg ( " delete highlight " , page , i )
2020-11-08 02:18:50 +01:00
-- The per-page table is a pure array
2014-11-27 21:59:27 +08:00
local removed = table.remove ( self.view . highlight.saved [ page ] , i )
2020-11-08 02:18:50 +01:00
-- But the main, outer table is a hash, so clear the table for this page if there are no longer any highlights on it
if # self.view . highlight.saved [ page ] == 0 then
self.view . highlight.saved [ page ] = nil
end
2017-10-18 17:19:06 +02:00
if bookmark_item then
self.ui . bookmark : removeBookmark ( bookmark_item )
else
self.ui . bookmark : removeBookmark ( {
2022-12-18 18:04:00 +02:00
page = self.ui . paging and page or removed.pos0 ,
2017-10-18 17:19:06 +02:00
datetime = removed.datetime ,
} )
end
2022-12-02 20:22:27 +02:00
self : writePdfAnnotation ( " delete " , page , removed )
2022-01-16 21:54:08 +02:00
UIManager : setDirty ( self.dialog , " ui " )
2013-06-15 23:13:19 +08:00
end
2022-01-16 21:54:08 +02:00
function ReaderHighlight : editHighlight ( page , i , is_new_note , text )
2017-10-18 17:19:06 +02:00
local item = self.view . highlight.saved [ page ] [ i ]
2022-12-09 10:08:14 +02:00
self.ui . bookmark : setBookmarkNote ( {
2022-12-18 18:04:00 +02:00
page = self.ui . paging and page or item.pos0 ,
2017-10-18 17:19:06 +02:00
datetime = item.datetime ,
2022-01-16 21:54:08 +02:00
} , true , is_new_note , text )
2013-06-15 23:13:19 +08:00
end
2013-10-18 22:38:07 +02:00
2021-10-18 20:17:37 +03:00
function ReaderHighlight : editHighlightStyle ( page , i )
local item = self.view . highlight.saved [ page ] [ i ]
2023-06-08 08:28:43 +03:00
local apply_drawer = function ( drawer )
self : writePdfAnnotation ( " delete " , page , item )
item.drawer = drawer
if self.ui . paging then
self : writePdfAnnotation ( " save " , page , item )
local bm_note = self.ui . bookmark : getBookmarkNote ( item )
if bm_note then
self : writePdfAnnotation ( " content " , page , item , bm_note )
end
end
UIManager : setDirty ( self.dialog , " ui " )
self.ui : handleEvent ( Event : new ( " BookmarkUpdated " ,
self.ui . bookmark : getBookmarkForHighlight ( {
page = self.ui . paging and page or item.pos0 ,
datetime = item.datetime ,
} ) ) )
end
self : showHighlightStyleDialog ( apply_drawer , item.drawer )
end
function ReaderHighlight : showHighlightStyleDialog ( caller_callback , item_drawer )
local default_drawer , keep_shown_on_apply
if item_drawer then -- called from editHighlightStyle
default_drawer = self.view . highlight.saved_drawer or
G_reader_settings : readSetting ( " highlight_drawing_style " , " lighten " )
keep_shown_on_apply = true
end
2021-10-18 20:17:37 +03:00
local radio_buttons = { }
for _ , v in ipairs ( highlight_style ) do
table.insert ( radio_buttons , {
{
2022-01-25 22:39:03 +02:00
text = v [ 1 ] ,
2023-06-08 08:28:43 +03:00
checked = item_drawer == v [ 2 ] ,
2022-01-25 22:39:03 +02:00
provider = v [ 2 ] ,
2021-10-18 20:17:37 +03:00
} ,
} )
end
2023-06-08 08:28:43 +03:00
local RadioButtonWidget = require ( " ui/widget/radiobuttonwidget " )
UIManager : show ( RadioButtonWidget : new {
2021-10-18 20:17:37 +03:00
title_text = _ ( " Highlight style " ) ,
width_factor = 0.5 ,
2023-06-08 08:28:43 +03:00
keep_shown_on_apply = keep_shown_on_apply ,
2021-10-18 20:17:37 +03:00
radio_buttons = radio_buttons ,
2023-06-08 08:28:43 +03:00
default_provider = default_drawer ,
2021-10-18 20:17:37 +03:00
callback = function ( radio )
2023-06-08 08:28:43 +03:00
caller_callback ( radio.provider )
2021-10-18 20:17:37 +03:00
end ,
} )
end
2021-11-21 19:31:10 +02:00
function ReaderHighlight : startSelection ( )
self.highlight_page , self.highlight_idx = self : saveHighlight ( )
self.select_mode = true
end
function ReaderHighlight : extendSelection ( )
-- item1 - starting fragment (saved), item2 - ending fragment (currently selected)
-- new extended highlight includes item1, item2 and the text between them
local item1 = self.view . highlight.saved [ self.highlight_page ] [ self.highlight_idx ]
local item2_pos0 , item2_pos1 = self.selected_text . pos0 , self.selected_text . pos1
-- getting starting and ending positions, text and pboxes of extended highlight
2022-12-02 20:22:27 +02:00
local new_pos0 , new_pos1 , new_text , new_pboxes , ext
if self.ui . paging then
local cur_page = self.hold_pos . page
2022-01-06 21:54:33 +02:00
local is_reflow = self.ui . document.configurable . text_wrap
2021-11-21 19:31:10 +02:00
-- pos0 and pos1 are not in order within highlights, hence sorting all
local function comparePositions ( pos1 , pos2 )
2022-01-06 21:54:33 +02:00
return self.ui . document : comparePositions ( pos1 , pos2 ) == 1
2021-11-21 19:31:10 +02:00
end
local positions = { item1.pos0 , item1.pos1 , item2_pos0 , item2_pos1 }
self.ui . document.configurable . text_wrap = 0 -- native positions
table.sort ( positions , comparePositions )
new_pos0 = positions [ 1 ]
new_pos1 = positions [ 4 ]
2022-12-02 20:22:27 +02:00
local temp_pos0 , temp_pos1
if new_pos0.page == new_pos1.page then -- single-page highlight
local text_boxes = self.ui . document : getTextFromPositions ( new_pos0 , new_pos1 )
new_text = text_boxes.text
new_pboxes = text_boxes.pboxes
temp_pos0 , temp_pos1 = new_pos0 , new_pos1
else -- multi-page highlight
new_text = " "
ext = { }
for page = new_pos0.page , new_pos1.page do
local item = self : getExtendedHighlightPage ( new_pos0 , new_pos1 , page )
new_text = new_text .. item.text
ext [ page ] = { -- for every page of multi-page highlight
pos0 = item.pos0 ,
pos1 = item.pos1 ,
pboxes = item.pboxes ,
}
if page == cur_page then
temp_pos0 , temp_pos1 = item.pos0 , item.pos1
end
end
end
2022-01-06 21:54:33 +02:00
self.ui . document.configurable . text_wrap = is_reflow -- restore reflow
2021-11-21 19:31:10 +02:00
-- draw
2022-12-02 20:22:27 +02:00
self.view . highlight.temp [ cur_page ] = self.ui . document : getPageBoxesFromPositions ( cur_page , temp_pos0 , temp_pos1 )
2021-11-21 19:31:10 +02:00
else
-- pos0 and pos1 are in order within highlights
new_pos0 = self.ui . document : compareXPointers ( item1.pos0 , item2_pos0 ) == 1 and item1.pos0 or item2_pos0
new_pos1 = self.ui . document : compareXPointers ( item1.pos1 , item2_pos1 ) == 1 and item2_pos1 or item1.pos1
-- true to draw
new_text = self.ui . document : getTextFromXPointers ( new_pos0 , new_pos1 , true )
end
self : deleteHighlight ( self.highlight_page , self.highlight_idx ) -- starting fragment
self.selected_text = {
text = new_text ,
pos0 = new_pos0 ,
pos1 = new_pos1 ,
pboxes = new_pboxes ,
2022-12-02 20:22:27 +02:00
ext = ext ,
2021-11-21 19:31:10 +02:00
}
UIManager : setDirty ( self.dialog , " ui " )
end
2022-12-02 20:22:27 +02:00
-- Calculates positions, text, pboxes of one page of selected multi-page highlight
-- (For pdf documents only, reflow mode must be off)
function ReaderHighlight : getExtendedHighlightPage ( pos0 , pos1 , cur_page )
local item = { }
for page = pos0.page , pos1.page do
if page == cur_page then
local page_boxes = self.ui . document : getTextBoxes ( page )
if page == pos0.page then
-- first page (from the start of highlight to the end of the page)
item.pos0 = pos0
item.pos1 = {
x = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . x1 ,
y = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . y1 ,
}
elseif page ~= pos1.page then
-- middle pages (full pages)
item.pos0 = {
x = page_boxes [ 1 ] [ 1 ] . x0 ,
y = page_boxes [ 1 ] [ 1 ] . y0 ,
}
item.pos1 = {
x = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . x1 ,
y = page_boxes [ # page_boxes ] [ # page_boxes [ # page_boxes ] ] . y1 ,
}
else
-- last page (from the start of the page to the end of highlight)
item.pos0 = {
x = page_boxes [ 1 ] [ 1 ] . x0 ,
y = page_boxes [ 1 ] [ 1 ] . y0 ,
}
item.pos1 = pos1
end
item.pos0 . page = page
item.pos1 . page = page
local text_boxes = self.ui . document : getTextFromPositions ( item.pos0 , item.pos1 )
item.text = text_boxes.text
item.pboxes = text_boxes.pboxes
end
end
return item
end
-- Returns one page of saved multi-page highlight
-- (For pdf documents only)
function ReaderHighlight : getSavedExtendedHighlightPage ( hl_or_bm , page , index )
local highlight
if hl_or_bm.ext then
highlight = hl_or_bm
else -- called from bookmark, need to find the corresponding highlight
for _ , hl in ipairs ( self.view . highlight.saved [ hl_or_bm.page ] ) do
if hl.datetime == hl_or_bm.datetime then
highlight = hl
break
end
end
end
local item = { }
item.datetime = highlight.datetime
item.drawer = highlight.drawer
item.pos0 = highlight.ext [ page ] . pos0
item.pos0 . zoom = highlight.pos0 . zoom
item.pos0 . rotation = highlight.pos0 . rotation
item.pos1 = highlight.ext [ page ] . pos1
item.pos1 . zoom = highlight.pos0 . zoom
item.pos1 . rotation = highlight.pos0 . rotation
item.pboxes = highlight.ext [ page ] . pboxes
item.parent = { highlight.pos0 . page , index }
return item
end
2014-01-18 03:05:17 +08:00
function ReaderHighlight : onReadSettings ( config )
2021-09-02 23:46:27 +03:00
self.view . highlight.saved_drawer = config : readSetting ( " highlight_drawer " )
or G_reader_settings : readSetting ( " highlight_drawing_style " ) or self.view . highlight.saved_drawer
2021-09-11 12:04:48 +03:00
self.view . highlight.disabled = G_reader_settings : has ( " default_highlight_action " )
and G_reader_settings : readSetting ( " default_highlight_action " ) == " nothing "
2020-09-24 15:17:37 +02:00
-- panel zoom settings isn't supported in EPUB
if self.document . info.has_pages then
2020-12-24 09:07:27 +01:00
local ext = util.getFileNameSuffix ( self.ui . document.file )
G_reader_settings : initializeExtSettings ( " panel_zoom_enabled " , { cbz = true , cbt = true } )
G_reader_settings : initializeExtSettings ( " panel_zoom_fallback_to_text_selection " , { pdf = true } )
2021-03-06 22:44:18 +01:00
if config : has ( " panel_zoom_enabled " ) then
self.panel_zoom_enabled = config : isTrue ( " panel_zoom_enabled " )
else
2020-12-24 09:07:27 +01:00
self.panel_zoom_enabled = G_reader_settings : getSettingForExt ( " panel_zoom_enabled " , ext ) or false
end
2021-03-06 22:44:18 +01:00
if config : has ( " panel_zoom_fallback_to_text_selection " ) then
self.panel_zoom_fallback_to_text_selection = config : isTrue ( " panel_zoom_fallback_to_text_selection " )
else
2020-12-24 09:07:27 +01:00
self.panel_zoom_fallback_to_text_selection = G_reader_settings : getSettingForExt ( " panel_zoom_fallback_to_text_selection " , ext ) or false
2020-09-24 15:17:37 +02:00
end
end
2014-01-18 03:05:17 +08:00
end
2020-08-06 00:13:26 +02:00
function ReaderHighlight : onUpdateHoldPanRate ( )
self : setupTouchZones ( )
end
2014-01-18 03:05:17 +08:00
function ReaderHighlight : onSaveSettings ( )
2014-03-13 21:52:43 +08:00
self.ui . doc_settings : saveSetting ( " highlight_drawer " , self.view . highlight.saved_drawer )
2020-09-24 15:17:37 +02:00
self.ui . doc_settings : saveSetting ( " panel_zoom_enabled " , self.panel_zoom_enabled )
2014-01-18 03:05:17 +08:00
end
2014-08-20 14:41:45 +08:00
function ReaderHighlight : onClose ( )
UIManager : close ( self.highlight_dialog )
2021-03-21 13:57:18 +01:00
self.highlight_dialog = nil
2014-08-20 14:41:45 +08:00
-- clear highlighted text
2014-08-26 20:29:53 +08:00
self : clear ( )
2014-08-20 14:41:45 +08:00
end
2022-03-12 19:16:50 +08:00
function ReaderHighlight : onHighlightPress ( )
if self._current_indicator_pos then
if not self._start_indicator_highlight then
-- try a tap at current indicator position to open any existing highlight
if not self : onTap ( nil , self : _createHighlightGesture ( " tap " ) ) then
-- no existing highlight at current indicator position: start hold
self._start_indicator_highlight = true
self : onHold ( nil , self : _createHighlightGesture ( " hold " ) )
2022-03-17 03:00:14 +08:00
-- With crengine, selected_text.sboxes does return good coordinates.
if self.ui . rolling and self.selected_text and self.selected_text . sboxes and # self.selected_text . sboxes > 0 then
2022-03-12 19:16:50 +08:00
local pos = self.selected_text . sboxes [ 1 ]
-- set hold_pos to center of selected_test to make center selection more stable, not jitted at edge
self.hold_pos = self.view : screenToPageTransform ( {
x = pos.x + pos.w / 2 ,
y = pos.y + pos.h / 2
} )
-- move indicator to center selected text making succeed same row selection much accurate.
UIManager : setDirty ( self.dialog , " ui " , self._current_indicator_pos )
self._current_indicator_pos . x = pos.x + pos.w / 2 - self._current_indicator_pos . w / 2
self._current_indicator_pos . y = pos.y + pos.h / 2 - self._current_indicator_pos . h / 2
UIManager : setDirty ( self.dialog , " ui " , self._current_indicator_pos )
end
else
self : onStopHighlightIndicator ( true ) -- need_clear_selection=true
end
else
self : onHoldRelease ( nil , self : _createHighlightGesture ( " hold_release " ) )
self : onStopHighlightIndicator ( )
end
return true
end
return false
end
function ReaderHighlight : onStartHighlightIndicator ( )
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 self._current_indicator_pos then
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
return false
end
function ReaderHighlight : onMoveHighlightIndicator ( args )
if self.view . visible_area and self._current_indicator_pos then
local dx , dy , quick_move = unpack ( args )
2022-10-10 22:21:27 +02:00
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 )
2022-04-14 14:59:36 +08:00
-- single move distance, small and capable to move on word with small font size and narrow line height
local move_distance = Size.item . height_default / 4
2022-03-12 19:16:50 +08:00
local rect = self._current_indicator_pos : copy ( )
2022-04-14 14:59:36 +08:00
if quick_move then
rect.x = rect.x + quick_move_distance_dx * dx
rect.y = rect.y + quick_move_distance_dy * dy
else
2022-05-05 21:00:22 +02:00
local now = time : now ( )
2022-04-14 14:59:36 +08:00
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 press same arrow key in 1 second, 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: 54 single distances, almost move to screen edge
2022-05-05 21:00:22 +02:00
if diff < time.s ( 1 ) then
2022-04-14 14:59:36 +08:00
move_distance = self._last_indicator_move_args . distance * 4
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
2022-03-12 19:16:50 +08:00
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
if rect.y < 0 then
rect.y = 0
end
if rect.y + rect.h > self.view . visible_area.h then
rect.y = self.view . visible_area.h - 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 ,
2022-05-05 21:00:22 +02:00
time = time.realtime ( ) ,
2022-03-12 19:16:50 +08:00
}
end
2013-10-18 22:38:07 +02:00
return ReaderHighlight