-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
readerhighlight.lua
2516 lines (2394 loc) · 103 KB
/
readerhighlight.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local BD = require("ui/bidi")
local BlitBuffer = require("ffi/blitbuffer")
local ButtonDialog = require("ui/widget/buttondialog")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local Event = require("ui/event")
local Geom = require("ui/geometry")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local Notification = require("ui/widget/notification")
local TextViewer = require("ui/widget/textviewer")
local Translator = require("ui/translator")
local UIManager = require("ui/uimanager")
local dbg = require("dbg")
local logger = require("logger")
local util = require("util")
local Size = require("ui/size")
local ffiUtil = require("ffi/util")
local time = require("ui/time")
local _ = require("gettext")
local C_ = _.pgettext
local N_ = _.ngettext
local T = ffiUtil.template
local Screen = Device.screen
local ReaderHighlight = InputContainer:extend{
highlight_colors = {
{_("Red"), "red"},
{_("Orange"), "orange"},
{_("Yellow"), "yellow"},
{_("Green"), "green"},
{_("Cyan"), "cyan"},
{_("Blue"), "blue"},
{_("Purple"), "purple"},
{_("Gray"), "gray"},
}
}
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
function ReaderHighlight:init()
self.screen_w = Screen:getWidth()
self.screen_h = Screen:getHeight()
self.select_mode = false -- extended highlighting
self._start_indicator_highlight = false
self._current_indicator_pos = nil
self._previous_indicator_pos = nil
self._last_indicator_move_args = {dx = 0, dy = 0, distance = 0, time = time:now()}
self:registerKeyEvents()
self._highlight_buttons = {
-- highlight and add_note are for the document itself,
-- so we put them first.
["01_select"] = function(this)
return {
text = _("Select"),
enabled = this.hold_pos ~= nil,
callback = function()
this:startSelection()
this:onClose()
end,
}
end,
["02_highlight"] = function(this)
return {
text = _("Highlight"),
callback = function()
this:saveHighlight(true)
this:onClose()
end,
enabled = this.hold_pos ~= nil,
}
end,
["03_copy"] = function(this)
return {
text = C_("Text", "Copy"),
enabled = Device:hasClipboard(),
callback = function()
Device.input.setClipboardText(cleanupSelectedText(this.selected_text.text))
this:onClose()
UIManager:show(Notification:new{
text = _("Selection copied to clipboard."),
})
end,
}
end,
["04_add_note"] = function(this)
return {
text = _("Add note"),
callback = function()
this:addNote()
this:onClose()
end,
enabled = this.hold_pos ~= nil,
}
end,
-- then information lookup functions, putting on the left those that
-- depend on an internet connection.
["05_wikipedia"] = function(this)
return {
text = _("Wikipedia"),
callback = function()
UIManager:scheduleIn(0.1, function()
this:lookupWikipedia()
-- We don't call this:onClose(), we need the highlight
-- to still be there, as we may Highlight it from the
-- dict lookup widget.
end)
end,
}
end,
["06_dictionary"] = function(this)
return {
text = _("Dictionary"),
callback = function()
this:onHighlightDictLookup()
-- We don't call this:onClose(), same reason as above
end,
}
end,
["07_translate"] = function(this, page, index)
return {
text = _("Translate"),
callback = function()
this:translate(this.selected_text, page, index)
-- We don't call this:onClose(), so one can still see
-- 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,
-- 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
["12_search"] = function(this)
return {
text = _("Search"),
callback = function()
this:onHighlightSearch()
-- We don't call this:onClose(), crengine will highlight
-- search matches on the current page, and self:clear()
-- would redraw and remove crengine native highlights
end,
}
end,
}
-- Android devices
if Device:canShareText() then
local action = _("Share Text")
self:addToHighlightDialog("08_share_text", function(this)
return {
text = action,
callback = function()
local text = cleanupSelectedText(this.selected_text.text)
-- call self:onClose() before calling the android framework
this:onClose()
Device:doShareText(text, action)
end,
}
end)
end
-- cre documents only
if not self.document.info.has_pages then
self:addToHighlightDialog("09_view_html", function(this)
return {
text = _("View HTML"),
callback = function()
this:viewSelectionHTML()
end,
}
end)
end
-- User hyphenation dict
self:addToHighlightDialog("10_user_dict", function(this)
return {
text= _("Hyphenate"),
show_in_highlight_dialog_func = function()
return this.ui.userhyph and this.ui.userhyph:isAvailable()
and not this.selected_text.text:find("[ ,;-%.\n]")
end,
callback = function()
this.ui.userhyph:modifyUserEntry(this.selected_text.text)
this:onClose()
end,
}
end)
-- Links
self:addToHighlightDialog("11_follow_link", function(this)
return {
text = _("Follow Link"),
show_in_highlight_dialog_func = function()
return this.selected_link ~= nil
end,
callback = function()
local link = this.selected_link.link or this.selected_link
this.ui.link:onGotoLink(link)
this:onClose()
end,
}
end)
self.ui:registerPostInitCallback(function()
self.ui.menu:registerToMainMenu(self)
end)
-- delegate gesture listener to readerui, NOP our own
self.ges_events = nil
end
function ReaderHighlight:onSetDimensions(dimen)
self.screen_w, self.screen_h = dimen.w, dimen.h
end
function ReaderHighlight:onGesture() end
function ReaderHighlight:registerKeyEvents()
if Device:hasKeys() then
-- Used for text selection with dpad/keys
local QUICK_INDICATOR_MOVE = true
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" } }
end
end
end
ReaderHighlight.onPhysicalKeyboardConnected = ReaderHighlight.registerKeyEvents
function ReaderHighlight:setupTouchZones()
if not Device:isTouchDevice() then return end
local hold_pan_rate = G_reader_settings:readSetting("hold_pan_rate")
if not hold_pan_rate then
hold_pan_rate = Screen.low_pan_rate and 5.0 or 30.0
end
local DTAP_ZONE_TOP_LEFT = G_defaults:readSetting("DTAP_ZONE_TOP_LEFT")
self.ui:registerTouchZones({
{
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
},
{
id = "readerhighlight_tap",
ges = "tap",
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
overrides = {
-- 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",
"readerfooter_tap",
"readerconfigmenu_ext_tap",
"readerconfigmenu_tap",
"readermenu_ext_tap",
"readermenu_tap",
"tap_forward",
"tap_backward",
},
handler = function(ges) return self:onTap(nil, ges) end
},
{
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
},
{
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
},
{
id = "readerhighlight_hold_pan",
ges = "hold_pan",
rate = hold_pan_rate,
screen_zone = {
ratio_x = 0, ratio_y = 0, ratio_w = 1, ratio_h = 1,
},
handler = function(ges) return self:onHoldPan(nil, ges) end
},
})
end
function ReaderHighlight:onReaderReady()
self:setupTouchZones()
end
local highlight_style = {
{_("Lighten"), "lighten"},
{_("Underline"), "underscore"},
{_("Strikeout"), "strikeout"},
{_("Invert"), "invert"},
}
local note_mark = {
{_("None"), "none"},
{_("Underline"), "underline"},
{_("Side line"), "sideline"},
{_("Side mark"), "sidemark"},
}
local long_press_action = {
{_("Ask with popup dialog"), "ask"},
{_("Do nothing"), "nothing"},
{_("Highlight"), "highlight"},
{_("Select and highlight"), "select"},
{_("Add note"), "note"},
{_("Translate"), "translate"},
{_("Wikipedia"), "wikipedia"},
{_("Dictionary"), "dictionary"},
{_("Fulltext search"), "search"},
}
local highlight_dialog_position = {
{_("Top"), "top"},
{_("Center"), "center"},
{_("Bottom"), "bottom"},
{_("Gesture position"), "gesture"},
}
function ReaderHighlight:addToMainMenu(menu_items)
-- insert table to main reader menu
if not Device:isTouchDevice() and Device:hasDPad() then
menu_items.start_content_selection = {
text = _("Start content selection"),
callback = function()
self:onStartHighlightIndicator()
end,
}
end
-- main menu Typeset
menu_items.highlight_options = {
text = _("Highlight style"),
sub_item_table = {},
}
for i, v in ipairs(highlight_style) do
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,
radio = true,
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,
separator = i == #highlight_style,
})
end
table.insert(menu_items.highlight_options.sub_item_table, {
text_func = function()
local saved_color = self.view.highlight.saved_color or "yellow"
for __, v in ipairs(self.highlight_colors) do
if v[2] == saved_color then
return T(_("Highlight color: %1"), string.lower(v[1]))
end
end
return T(_("Highlight color: %1"), saved_color)
end,
enabled_func = function()
return self.view.highlight.saved_drawer ~= "invert"
end,
callback = function(touchmenu_instance)
local default_color = G_reader_settings:readSetting("highlight_color", "yellow")
local saved_color = self.view.highlight.saved_color or "yellow"
local radio_buttons = {}
for _, v in ipairs(self.highlight_colors) do
table.insert(radio_buttons, {
{
text = v[1],
checked = v[2] == saved_color,
provider = v[2],
},
})
end
UIManager:show(require("ui/widget/radiobuttonwidget"):new{
title_text = _("Highlight color"),
width_factor = 0.5,
keep_shown_on_apply = false,
radio_buttons = radio_buttons,
default_provider = default_color,
callback = function(radio)
self.view.highlight.saved_color = radio.provider
UIManager:setDirty(self.dialog, "ui")
if touchmenu_instance then touchmenu_instance:updateItems() end
end,
})
end,
hold_callback = function(touchmenu_instance)
G_reader_settings:saveSetting("highlight_color", self.view.highlight.saved_color)
UIManager:show(Notification:new{
text = T(_("Default highlight color changed to '%1'."), self.view.highlight.saved_color),
})
if touchmenu_instance then touchmenu_instance:updateItems() end
end,
})
table.insert(menu_items.highlight_options.sub_item_table, {
text_func = function()
return T(_("Gray 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 = _("Gray highlight opacity"),
info_text = _("The higher the value, the darker the gray."),
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,
})
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,
})
table.insert(menu_items.highlight_options.sub_item_table, {
text = _("Apply default style to all highlights"),
callback = function(touchmenu_instance)
UIManager:show(ConfirmBox:new{
text = _("Are you sure you want to edit all highlights."),
icon = "texture-box",
ok_callback = function()
local count = 0
for _, items in pairs(self.view.highlight.saved) do
if items then
count = count + #items
for i = 1, #items do
local item = items[i]
item.drawer = self.view.highlight.saved_drawer
item.color = self.view.highlight.saved_color
end
end
end
if count > 0 then
UIManager:setDirty(self.dialog, "ui")
UIManager:show(Notification:new{
text = T(N_("Applied default style to 1 highlight",
"Applied default style to %1 highlights", count), count),
})
end
end
})
end,
})
if self.document.info.has_pages then
menu_items.panel_zoom_options = {
text = _("Panel zoom (manga/comic)"),
sub_item_table = self:genPanelZoomMenu(),
}
end
-- main menu Settings
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,
},
},
}
-- actions
for i, v in ipairs(long_press_action) do
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,
radio = true,
callback = function()
self:onSetHighlightAction(i, true) -- no notification
end,
})
end
-- 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
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."),
width = math.floor(self.screen_w * 0.75),
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,
})
table.insert(menu_items.long_press.sub_item_table, {
text = _("Auto-scroll when selection reaches a corner"),
help_text = _([[
Auto-scroll to show part of the previous page when your text selection reaches the top left corner, or of the next page when it reaches the bottom right corner.
Except when in two columns mode, where this is limited to showing only the previous or next column.]]),
checked_func = function()
if self.ui.paging then return false end
return not self.view.highlight.disabled and G_reader_settings:nilOrTrue("highlight_corner_scroll")
end,
enabled_func = function()
if self.ui.paging then return false end
return not self.view.highlight.disabled
end,
callback = function()
G_reader_settings:flipNilOrTrue("highlight_corner_scroll")
self.allow_corner_scroll = G_reader_settings:nilOrTrue("highlight_corner_scroll")
end,
})
-- 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
if not Device:isTouchDevice() and Device:hasDPad() then
menu_items.selection_text = util.tableDeepCopy(menu_items.long_press)
menu_items.selection_text.text = _("Select on text")
end
-- main menu Search
menu_items.translation_settings = Translator:genSettingsMenu()
menu_items.translate_current_page = {
text = _("Translate current page"),
callback = function()
self:onTranslateCurrentPage()
end,
}
end
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)
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)
end,
separator = true,
},
}
end
-- 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()
self.clear_id = UIManager:getTime() -- can act as a unique id
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
-- if clear_id is no longer valid, highlight has already been
-- cleared since this clear_id was given
return
end
end
self.clear_id = nil -- invalidate id
if not self.ui.document then
-- might happen if scheduled and run after document is closed
return
end
if self.ui.paging then
self.view.highlight.temp = {}
else
self.ui.document:clearSelection()
end
if self.restore_page_mode_func then
self.restore_page_mode_func()
self.restore_page_mode_func = nil
end
self.is_word_selection = false
self.selected_text_start_xpointer = nil
if self.hold_pos then
self.hold_pos = nil
self.selected_text = nil
UIManager:setDirty(self.dialog, "ui")
return true
end
end
function ReaderHighlight:onClearHighlight()
self:clear()
return true
end
function ReaderHighlight:onTapSelectModeIcon()
if not self.select_mode then return end
UIManager:show(ConfirmBox:new{
text = _("You are currently in SELECT mode.\nTo finish highlighting, long press where the highlight should end and press the HIGHLIGHT button.\nYou can also exit select mode by tapping on the start of the highlight."),
icon = "texture-box",
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
function ReaderHighlight:onTap(_, ges)
-- 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.
if not cleared and ges then
if self.ui.paging then
return self:onTapPageSavedHighlight(ges)
else
return self:onTapXPointerSavedHighlight(ges)
end
end
end
function ReaderHighlight:onTapPageSavedHighlight(ges)
local pages = self.view:getCurrentPageList()
local pos = self.view:screenToPageTransform(ges.pos)
local highlights_tapped = {}
for _, page in ipairs(pages) do
local items = self.view:getPageSavedHighlights(page)
if items then
for i, item in ipairs(items) do
local boxes = self.ui.document:getPageBoxesFromPositions(page, item.pos0, item.pos1)
if boxes then
for _, box in ipairs(boxes) do
if inside_box(pos, box) then
logger.dbg("Tap on highlight")
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
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
end
end
end
end
end
end
if #highlights_tapped > 0 then
return self:showChooseHighlightDialog(highlights_tapped)
end
end
function ReaderHighlight:onTapXPointerSavedHighlight(ges)
-- 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).
local cur_view_top, cur_view_bottom
local pos = self.view:screenToPageTransform(ges.pos)
-- 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)...
local highlights_tapped = {}
for page, items in pairs(self.view.highlight.saved) do
if items then
for i = 1, #items do
local item = items[i]
local pos0, pos1 = item.pos0, item.pos1
-- document:getScreenBoxesFromPositions() is expensive, so we
-- first check this item is on current page
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
end
end
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
local boxes = self.ui.document:getScreenBoxesFromPositions(pos0, pos1, true) -- get_segments=true
if boxes then
for index, box in pairs(boxes) do
if inside_box(pos, box) then
logger.dbg("Tap on highlight")
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
end
end
end
end
end
end
end
if #highlights_tapped > 0 then
return self:showChooseHighlightDialog(highlights_tapped)
end
end
function ReaderHighlight:updateHighlight(page, index, side, direction, move_by_char)
if self.ui.paging then return end
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
local updated_highlight_beginning
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
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
self.view.highlight.saved[page][index].pos0 = updated_highlight_beginning
end
end
else -- we move pos1
local updated_highlight_end
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
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
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)
local new_chapter = self.ui.toc:getTocTitleByPage(new_beginning)
self.view.highlight.saved[page][index].text = cleanupSelectedText(new_text)
self.view.highlight.saved[page][index].chapter = new_chapter
local new_highlight = self.view.highlight.saved[page][index]
self.ui.bookmark:updateBookmark({
page = highlight_beginning,
datetime = highlight_time,
updated_highlight = new_highlight
})
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)
local top_y = end_y - math.floor(self.screen_h * 2/3)
self.ui.rolling:_gotoPos(top_y)
end
end
end
UIManager:setDirty(self.dialog, "ui")
end
function ReaderHighlight:showChooseHighlightDialog(highlights)
if #highlights == 1 then