-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
main.lua
2062 lines (1922 loc) · 71 KB
/
main.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
--[[--
This plugin processes dictionary word lookups and uses spaced repetition to help you remember new words.
@module koplugin.vocabbuilder
--]]--
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local BottomContainer = require("ui/widget/container/bottomcontainer")
local DB = require("db")
local Button = require("ui/widget/button")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DictQuickLookUp = require("ui/widget/dictquicklookup")
local Dispatcher = require("dispatcher")
local Event = require("ui/event")
local Font = require("ui/font")
local FocusManager = require("ui/widget/focusmanager")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local HorizontalGroup = require("ui/widget/horizontalgroup")
local HorizontalSpan = require("ui/widget/horizontalspan")
local IconButton = require("ui/widget/iconbutton")
local IconWidget = require("ui/widget/iconwidget")
local InfoMessage = require("ui/widget/infomessage")
local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog")
local LeftContainer = require("ui/widget/container/leftcontainer")
local LineWidget = require("ui/widget/linewidget")
local MovableContainer = require("ui/widget/container/movablecontainer")
local Notification = require("ui/widget/notification")
local RightContainer = require("ui/widget/container/rightcontainer")
local OverlapGroup = require("ui/widget/overlapgroup")
local Screen = Device.screen
local Size = require("ui/size")
local SortWidget = require("ui/widget/sortwidget")
local SyncService = require("frontend/apps/cloudstorage/syncservice")
local TextWidget = require("ui/widget/textwidget")
local TextBoxWidget = require("ui/widget/textboxwidget")
local TitleBar = require("ui/widget/titlebar")
local ToggleSwitch = require("ui/widget/toggleswitch")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local T = require("ffi/util").template
local _ = require("gettext")
local C_ = _.pgettext
-------- shared values
local word_face = Font:getFace("x_smallinfofont")
local subtitle_face = Font:getFace("cfont", 12)
local subtitle_italic_face = Font:getFace("NotoSans-Italic.ttf", 12)
local subtitle_color = Blitbuffer.COLOR_DARK_GRAY
local dim_color = Blitbuffer.COLOR_GRAY_3
local settings = G_reader_settings:readSetting("vocabulary_builder", {enabled = false, with_context = true})
local function resetButtonOnLookupWindow()
if not settings.enabled then -- auto add words
DictQuickLookUp.tweak_buttons_func = function(obj, buttons)
if obj.is_wiki_fullpage then
return
elseif obj.is_wiki then
-- make wiki window have the same button_tweak as its presenting dictionary window
local widget = UIManager:getNthTopWidget(2)
if widget and widget.tweak_buttons_func then
widget:tweak_buttons_func(buttons)
end
return
end
table.insert(buttons, 1, {{
id = "vocabulary",
text = _("Add to vocabulary builder"),
font_bold = false,
callback = function()
local book_title = (obj.ui.doc_props and obj.ui.doc_props.display_title) or _("Dictionary lookup")
obj.ui:handleEvent(Event:new("WordLookedUp", obj.word, book_title, true)) -- is_manual: true
local button = obj.button_table.button_by_id["vocabulary"]
if button then
button:disable()
UIManager:setDirty(obj, function()
return "ui", button.dimen
end)
end
end
}})
end
else
DictQuickLookUp.tweak_buttons_func = nil
end
end
local function saveSettings()
G_reader_settings:saveSetting("vocabulary_builder", settings)
end
--[[--
Menu dialogue widget
--]]--
local MenuDialog = FocusManager:extend{
padding = Size.padding.large,
is_edit_mode = false,
edit_callback = nil,
tap_close_callback = nil,
clean_callback = nil,
reset_callback = nil,
}
function MenuDialog:init()
self.layout = {}
if Device:hasKeys() then
self.key_events.Close = { { Device.input.group.Back } }
end
if Device:isTouchDevice() then
self.ges_events.Tap = {
GestureRange:new {
ges = "tap",
range = Geom:new {
x = 0,
y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
}
end
end
function MenuDialog:setupPluginMenu()
local size = Screen:getSize()
local width = math.floor(size.w * 0.9)
-- Switch text translations could be long
local temp_text_widget = TextWidget:new{
text = _("Auto add new words"),
face = Font:getFace("xx_smallinfofont")
}
local switch_guide_width = temp_text_widget:getSize().w
temp_text_widget:setText(_("Save context"))
switch_guide_width = math.max(switch_guide_width, temp_text_widget:getSize().w)
switch_guide_width = math.min(math.max(switch_guide_width, math.ceil(width*0.39)), math.ceil(width*0.61))
temp_text_widget:free()
local switch_width = width - switch_guide_width - Size.padding.fullscreen - Size.padding.default
local switch = ToggleSwitch:new{
width = switch_width,
default_value = 2,
name = "vocabulary_builder",
name_text = nil, --_("Accept new words"),
event = "ChangeEnableStatus",
args = {"off", "on"},
default_arg = "on",
toggle = { _("off"), _("on") },
values = {1, 2},
alternate = false,
enabled = true,
config = self,
readonly = self.readonly,
}
switch:setPosition(settings.enabled and 2 or 1)
self:mergeLayoutInVertical(switch)
self.context_switch = ToggleSwitch:new{
width = switch_width,
default_value = 1,
name_text = nil,
event = "ChangeContextStatus",
args = {"off", "on"},
default_arg = "off",
toggle = { _("off"), _("on") },
values = {1, 2},
alternate = false,
enabled = true,
config = self,
readonly = self.readonly,
}
self.context_switch:setPosition(settings.with_context and 2 or 1)
self:mergeLayoutInVertical(self.context_switch)
local filter_button = {
text = _("Filter books"),
callback = function()
self:onClose()
self.show_parent:onShowFilter()
end
}
local reverse_button = {
text = settings.reverse and _("Reverse order") or _("Reverse order and show only reviewable"),
callback = function()
self:onClose()
settings.reverse = not settings.reverse
saveSettings()
self.show_parent:reloadItems()
end
}
local edit_button = {
text = self.is_edit_mode and _("Resume") or _("Quick deletion"),
callback = function()
self:onClose()
self.edit_callback()
end
}
local reset_button = {
text = _("Reset all progress"),
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Reset progress of all words?"),
ok_text = _("Reset"),
ok_callback = function()
DB:resetProgress()
self:onClose()
self.reset_callback()
end
})
end
}
local clean_button = {
text = _("Clean all words"),
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Clean all words including progress?"),
ok_text = _("Clean"),
ok_callback = function()
DB:purge()
self:onClose()
self.clean_callback()
end
})
end,
}
local show_sync_settings = function()
if not settings.server then
local sync_settings = SyncService:new{}
sync_settings.onClose = function(this)
UIManager:close(this)
end
sync_settings.onConfirm = function(server)
settings.server = server
saveSettings()
DB:batchUpdateItems(self.show_parent.item_table)
SyncService.sync(server, DB.path, DB.onSync, false)
self.show_parent:reloadItems()
end
UIManager:close(self.sync_dialogue)
UIManager:close(self)
UIManager:show(sync_settings)
return
end
local server = settings.server
local buttons = {
{
{
text = _("Delete"),
callback = function()
settings.server = nil
UIManager:close(self.sync_dialogue)
end
},
{
text = _("Edit"),
callback = function()
UIManager:close(self.sync_dialogue)
UIManager:close(self)
local sync_settings = SyncService:new{}
sync_settings.onClose = function(this)
UIManager:close(this)
end
sync_settings.onConfirm = function(chosen_server)
settings.server = chosen_server
end
UIManager:show(sync_settings)
end
},
{
text = _("Synchronize now"),
callback = function()
UIManager:close(self.sync_dialogue)
UIManager:close(self)
DB:batchUpdateItems(self.show_parent.item_table)
SyncService.sync(server, DB.path, DB.onSync, false)
self.show_parent:reloadItems()
end
}
}
}
local type = server.type == "dropbox" and " (DropBox)" or " (WebDAV)"
self.sync_dialogue = ButtonDialogTitle:new{
title = T(_("Cloud storage:\n%1\n\nFolder path:\n%2\n\nSet up the same cloud folder on each device to sync across your devices."),
server.name.." "..type, SyncService.getReadablePath(server)),
info_face = Font:getFace("smallinfofont"),
buttons = buttons,
}
UIManager:show(self.sync_dialogue)
end
local sync_button = {
text = _("Cloud sync"),
callback = function()
show_sync_settings()
end
}
local search_button = {
text = _("Search"),
callback = function()
UIManager:close(self)
self.show_parent:showSearchDialog()
end
}
local buttons = ButtonTable:new{
width = width,
buttons = {
{reverse_button},
{sync_button},
{search_button},
{filter_button, edit_button},
{reset_button, clean_button},
},
show_parent = self
}
self:mergeLayoutInVertical(buttons)
self.covers_fullscreen = true
self[1] = CenterContainer:new{
dimen = size,
FrameContainer:new{
padding = Size.padding.default,
padding_top = Size.padding.large,
padding_bottom = 0,
background = Blitbuffer.COLOR_WHITE,
bordersize = Size.border.window,
radius = Size.radius.window,
VerticalGroup:new{
HorizontalGroup:new{
RightContainer:new{
dimen = Geom:new{w = switch_guide_width, h = switch:getSize().h },
TextWidget:new{
text = _("Auto add new words"),
face = Font:getFace("xx_smallinfofont"),
max_width = switch_guide_width
}
},
HorizontalSpan:new{width = Size.padding.fullscreen},
switch,
},
VerticalSpan:new{ width = Size.padding.default},
HorizontalGroup:new{
RightContainer:new{
dimen = Geom:new{w = switch_guide_width, h = switch:getSize().h},
TextWidget:new{
text = _("Save context"),
face = Font:getFace("xx_smallinfofont"),
max_width = switch_guide_width
}
},
HorizontalSpan:new{width = Size.padding.fullscreen},
self.context_switch,
},
VerticalSpan:new{ width = Size.padding.large},
LineWidget:new{
background = Blitbuffer.COLOR_GRAY,
dimen = Geom:new{
w = width,
h = Screen:scaleBySize(1),
}
},
buttons
}
}
}
end
function MenuDialog:setupBookMenu(sort_item, onSuccess)
local size = Screen:getSize()
local width = math.floor(size.w * 0.9)
local change_title_button = {
text = _("Change book title"),
callback = function()
self:onClose()
-- first show_parent is sortWidget, second is vocabBuilderWidget
self.show_parent.show_parent:showChangeBookTitleDialog(sort_item, onSuccess)
end
}
local select_single_button = {
text = _("Select only this book"),
callback = function()
self:onClose()
for _, item in pairs(self.show_parent.item_table) do
if item == sort_item then
if not item.checked_func() then
item.callback() -- toggle checkmark
end
elseif item.checked_func() then
item.callback()
end
end
self.show_parent:goToPage(self.show_parent.show_page)
end
}
local select_all_button = {
text = _("Select all books"),
callback = function()
self:onClose()
for _, item in pairs(self.show_parent.item_table) do
if not item.checked_func() then
item.callback()
end
end
self.show_parent:goToPage(self.show_parent.show_page)
end
}
local select_page_all_button = {
text = _("Select all books on this page"),
callback = function()
self:onClose()
for _, content in pairs(self.show_parent.main_content) do
if content.item and not content.item.checked_func() then
content.item.callback()
end
end
self.show_parent:goToPage(self.show_parent.show_page)
end
}
local deselect_page_all_button = {
text = _("Deselect all books on this page"),
callback = function()
self:onClose()
for _, content in pairs(self.show_parent.main_content) do
if content.item and content.item.checked_func() then
content.item.callback()
end
end
self.show_parent:goToPage(self.show_parent.show_page)
end
}
local buttons = ButtonTable:new{
width = width,
buttons = {
{change_title_button},
{select_single_button},
{select_all_button},
{select_page_all_button},
{deselect_page_all_button},
},
show_parent = self
}
self.covers_fullscreen = true
self[1] = CenterContainer:new{
dimen = size,
FrameContainer:new{
padding = Size.padding.default,
background = Blitbuffer.COLOR_WHITE,
bordersize = Size.border.window,
radius = Size.radius.window,
buttons
}
}
end
function MenuDialog:onShow()
UIManager:setDirty(self, function()
return "flashui", self[1][1].dimen -- i.e., FrameContainer
end)
end
function MenuDialog:onCloseWidget()
UIManager:setDirty(nil, function()
return "ui", self[1][1].dimen
end)
end
function MenuDialog:onTap(_, ges)
if ges.pos:notIntersectWith(self[1][1].dimen) then
-- Tap outside closes widget
self:onClose()
return true
end
end
function MenuDialog:onClose()
UIManager:close(self)
if self.tap_close_callback then
self.tap_close_callback()
end
return true
end
function MenuDialog:onChangeContextStatus(args, position)
settings.with_context = position == 2
saveSettings()
end
function MenuDialog:onChangeEnableStatus(args, position)
settings.enabled = position == 2
saveSettings()
resetButtonOnLookupWindow()
end
function MenuDialog:onConfigChoose(values, name, event, args, position)
UIManager:tickAfterNext(function()
if values then
if event == "ChangeEnableStatus" then
self:onChangeEnableStatus(args, position)
elseif event == "ChangeContextStatus" then
self:onChangeContextStatus(args, position)
end
end
UIManager:setDirty(nil, "ui")
end)
end
--[[--
Individual word info dialogue widget
--]]--
local WordInfoDialog = InputContainer:extend{
title = nil,
book_title = nil,
dates = nil,
padding = Size.padding.large,
margin = Size.margin.title,
tap_close_callback = nil,
remove_callback = nil,
reset_callback = nil,
dismissable = true, -- set to false if any button callback is required
}
local book_title_triangle = BD.mirroredUILayout() and " ⯇" or " ⯈"
local word_info_dialog_width
function WordInfoDialog:init()
if self.dismissable then
if Device:hasKeys() then
self.key_events.Close = { { Device.input.group.Back } }
end
if Device:isTouchDevice() then
self.ges_events.Tap = {
GestureRange:new {
ges = "tap",
range = Geom:new {
x = 0,
y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
}
}
end
end
if not word_info_dialog_width then
local temp_text = TextWidget:new{
text = self.dates,
padding = Size.padding.fullscreen,
face = Font:getFace("cfont", 14)
}
local dates_width = temp_text:getSize().w
temp_text:free()
local screen_width = math.min(Screen:getWidth(), Screen:getHeight())
word_info_dialog_width = math.floor(math.max(screen_width * 0.6, math.min(screen_width * 0.8, dates_width)))
end
local width = word_info_dialog_width
local reset_button = {
text = _("Reset progress"),
callback = function()
self.reset_callback()
UIManager:close(self)
end
}
local remove_button = {
text = _("Remove word"),
callback = function()
self.remove_callback()
UIManager:close(self)
end
}
local buttons = {{reset_button, remove_button}}
if self.show_parent.item.last_due_time then
table.insert(buttons, {{
text = _("Undo study status"),
callback = function()
self.undo_callback()
UIManager:close(self)
end
}})
end
local focus_button = ButtonTable:new{
width = width,
buttons = buttons,
show_parent = self
}
local copy_button = Button:new{
text = "", -- copy in nerdfont,
callback = function()
Device.input.setClipboardText(self.title)
UIManager:show(Notification:new{
text = _("Word copied to clipboard."),
})
end,
bordersize = 0,
}
self.book_title_button = Button:new{
text = self.book_title .. book_title_triangle,
width = width,
text_font_face = "NotoSans-Italic.ttf",
text_font_size = 14,
text_font_bold = false,
align = self.title_align or "left",
padding = Size.padding.button,
bordersize = 0,
callback = function()
self.show_parent:onShowBookAssignment(function(new_book_title)
self.book_title = new_book_title
self.book_title_button:setText(new_book_title..book_title_triangle, width)
end)
end,
show_parent = self
}
local has_context = self.prev_context or self.next_context
self[1] = CenterContainer:new{
dimen = Screen:getSize(),
MovableContainer:new{
FrameContainer:new{
VerticalGroup:new{
align = "center",
FrameContainer:new{
padding = self.padding,
padding_top = Size.padding.buttontable,
padding_bottom = Size.padding.buttontable,
margin = self.margin,
bordersize = 0,
VerticalGroup:new {
align = "left",
HorizontalGroup:new{
TextWidget:new{
text = self.title,
max_width = width - copy_button:getSize().w - Size.padding.default,
face = word_face,
bold = true,
alignment = self.title_align or "left",
},
HorizontalSpan:new{ width=Size.padding.default },
copy_button,
},
self.book_title_button,
VerticalSpan:new{width= Size.padding.default},
has_context and
TextBoxWidget:new{
text = "..." .. (self.prev_context or ""):gsub("\n", " ") .. "【" ..self.title.."】" .. (self.next_context or ""):gsub("\n", " ") .. "...",
width = width,
face = Font:getFace("smallffont"),
alignment = self.title_align or "left",
}
or VerticalSpan:new{ width = Size.padding.default },
VerticalSpan:new{ width = has_context and Size.padding.default or 0},
TextBoxWidget:new{
text = self.dates,
width = width,
face = Font:getFace("cfont", 14),
alignment = self.title_align or "left",
fgcolor = dim_color
},
}
},
LineWidget:new{
background = Blitbuffer.COLOR_GRAY,
dimen = Geom:new{
w = width + self.padding + self.margin,
h = Screen:scaleBySize(1),
}
},
focus_button
},
background = Blitbuffer.COLOR_WHITE,
bordersize = Size.border.window,
radius = Size.radius.window,
padding = 0
}
}
}
end
function WordInfoDialog:setTitle(title)
self.title = title
self:free()
self:init()
UIManager:setDirty("all", "ui")
end
function WordInfoDialog:onShow()
UIManager:setDirty(self, function()
return "flashui", self[1][1].dimen -- i.e., MovableContainer
end)
end
function WordInfoDialog:onCloseWidget()
UIManager:setDirty(nil, function()
return "ui", self[1][1].dimen
end)
end
function WordInfoDialog:onClose()
UIManager:close(self)
if self.tap_close_callback then
self.tap_close_callback()
end
return true
end
function WordInfoDialog:onTap(_, ges)
if ges.pos:notIntersectWith(self[1][1].dimen) then
-- Tap outside closes widget
self:onClose()
return true
end
end
function WordInfoDialog:paintTo(...)
InputContainer.paintTo(self, ...)
self.dimen = self[1][1].dimen -- FrameContainer
end
-- values useful for item cells
local ellipsis_button_width = Screen:scaleBySize(34)
local star_width = Screen:scaleBySize(25)
local point_widget = TextWidget:new{
text = " • ",
bold = true,
face = Font:getFace("cfont", 24),
fgcolor = dim_color
}
--[[--
Individual word item widget
--]]--
local VocabItemWidget = InputContainer:extend{
face = Font:getFace("smallinfofont"),
width = nil,
height = nil,
review_button_width = nil,
show_parent = nil,
item = nil,
forgot_button = nil,
got_it_button = nil,
more_button = nil,
layout = nil
}
--[[--
item: {
checked_func: Block,
review_count: interger,
word: Text
book_title: TEXT
create_time: Integer
review_time: Integer
due_time: Integer,
got_it_callback: function
remove_callback: function
is_dim: BOOL
}
--]]--
local point_widget_height = point_widget:getSize().h
local point_widget_width = point_widget:getSize().w
local word_height = TextWidget:new{text = " ", face = word_face}:getSize().h
local subtitle_height = TextWidget:new{text = " ", face = subtitle_face}:getSize().h
function VocabItemWidget:init()
self.layout = {}
self.dimen = Geom:new{w = self.width, h = self.height}
self.ges_events.Tap = {
GestureRange:new{
ges = "tap",
range = self.dimen,
}
}
self.ges_events.Hold = {
GestureRange:new{
ges = "hold",
range = self.dimen,
}
}
self.v_spacer = VerticalSpan:new{width = math.floor((self.height - word_height - subtitle_height)/2)}
self.point_v_spacer = VerticalSpan:new{width = (self.v_spacer.width + word_height/2) - point_widget_height/2 }
self.margin_span = HorizontalSpan:new{ width = Size.padding.large }
self:initItemWidget()
end
function VocabItemWidget:initItemWidget()
for i = 1, #self.layout do self.layout[i] = nil end
if not self.show_parent.is_edit_mode then
self.more_button = Button:new{
text = (self.item.prev_context or self.item.next_context) and "⋯" or "⋮",
padding = Size.padding.button,
callback = function() self:showMore() end,
width = ellipsis_button_width,
bordersize = 0,
show_parent = self
}
else
self.more_button = IconButton:new{
icon = "exit",
width = star_width,
height = star_width,
padding = math.floor((ellipsis_button_width - star_width)/2) + Size.padding.button,
callback = function()
self:remover()
end,
}
end
local right_side_width
local right_widget
if not self.show_parent.is_edit_mode and self.item.due_time <= os.time() then
self.has_review_buttons = true
right_side_width = self.review_button_width * 2 + Size.padding.large * 2 + ellipsis_button_width
self.forgot_button = Button:new{
text = _("Forgot"),
width = self.review_button_width,
radius = Size.radius.button,
callback = function()
self:onForgot()
end,
show_parent = self,
}
self.got_it_button = Button:new{
text = _("Got it"),
radius = Size.radius.button,
callback = function()
self:onGotIt()
end,
width = self.review_button_width,
show_parent = self,
}
right_widget = HorizontalGroup:new{
dimen = Geom:new{ w = 0, h = self.height },
self.margin_span,
self.forgot_button,
self.margin_span,
self.got_it_button,
self.more_button,
}
table.insert(self.layout, self.forgot_button)
table.insert(self.layout, self.got_it_button)
table.insert(self.layout, self.more_button)
else
self.has_review_buttons = false
local star = IconWidget:new{
icon = "check",
width = star_width,
height = star_width,
dim = true
}
if self.item.review_count > 6 then
right_side_width = Size.padding.large * 4 + 9 * (star:getSize().w)
right_widget = HorizontalGroup:new {
dimen = Geom:new{w=0, h = self.height}
}
for i=1, 6, 1 do
table.insert(right_widget, star)
end
table.insert(right_widget,
TextWidget:new {
text = " + ",
face = word_face,
fgcolor = Blitbuffer.COLOR_DARK_GRAY
}
)
table.insert(right_widget, star)
table.insert(right_widget,
TextWidget:new {
text = "× " .. self.item.review_count-6,
face = word_face,
fgcolor = Blitbuffer.COLOR_DARK_GRAY
}
)
elseif self.item.review_count > 0 then
right_side_width = Size.padding.large * 4 + self.item.review_count * (star:getSize().w)
right_widget = HorizontalGroup:new {
dimen = Geom:new{w=0, h = self.height}
}
for i=1, self.item.review_count, 1 do
table.insert(right_widget, star)
end
else
star:free()
right_side_width = Size.padding.large * 4
right_widget = HorizontalGroup:new{
dimen = Geom:new{w=0, h = self.height},
HorizontalSpan:new {width = Size.padding.default }
}
end
table.insert(right_widget, self.margin_span)
table.insert(right_widget, self.more_button)
table.insert(self.layout, self.more_button)
end
local text_max_width = self.width - point_widget_width - right_side_width
local subtitle_prefix = TextWidget:new{
text = self:getTimeSinceDue() .. _("From") .. " ",
face = subtitle_face,
fgcolor = subtitle_color
}
local word_widget = Button:new{
text = self.item.word,
bordersize = 0,
callback = function() self.item.callback(self.item) end,
padding = 0,
max_width = math.ceil(math.max(5,text_max_width - Size.padding.fullscreen))
}
word_widget.label_widget.fgcolor = self.item.is_dim and dim_color or Blitbuffer.COLOR_BLACK
table.insert(self.layout, 1, word_widget)
self[1] = FrameContainer:new{
padding = 0,
bordersize = 0,
HorizontalGroup:new{
dimen = Geom:new{
w = self.width,
h = self.height,
},
HorizontalGroup:new{
dimen = Geom:new{
w = self.width - right_side_width,
h = self.height,
},
VerticalGroup:new{
dimen = Geom:new{w = point_widget_width, h = self.height},
self.point_v_spacer,
point_widget,
VerticalSpan:new { width = self.height - point_widget_height - self.point_v_spacer.width}
},
VerticalGroup:new{
dimen = Geom:new{
w = text_max_width,
h = self.height,
},
self.v_spacer,
LeftContainer:new{
dimen = Geom:new{w = text_max_width, h = word_height},
word_widget
},
LeftContainer:new{
dimen = Geom:new{w = text_max_width, h = math.floor(self.height - word_height - self.v_spacer.width*2.2)},
HorizontalGroup:new{
subtitle_prefix,
TextWidget:new{
text = self.item.book_title,
face = subtitle_italic_face,
max_width = math.ceil(math.max(5,text_max_width - subtitle_prefix:getSize().w - Size.padding.fullscreen)),
fgcolor = subtitle_color
}
}
},
self.v_spacer
}
},
RightContainer:new{
dimen = Geom:new{ w = right_side_width+Size.padding.default, h = self.height},
right_widget
}
},
}
end
function VocabItemWidget:getTimeSinceDue()
local elapsed = os.time() - self.item.due_time
local abs = math.abs(elapsed)
local readable_time
local rounding = elapsed > 0 and math.floor or math.ceil