-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcss_menu_sections.lua
More file actions
executable file
·528 lines (504 loc) · 24.3 KB
/
Copy pathcss_menu_sections.lua
File metadata and controls
executable file
·528 lines (504 loc) · 24.3 KB
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
-- Menu items for the Contents section: controls which data each section displays.
local UIManager = require("ui/uimanager")
local _ = require("gettext")
local config = require("css_config")
local USER_CONFIG = config.USER_CONFIG
local SETTINGS = config.SETTINGS
local PluginStore = require("css_settings").plugin()
local h = require("css_menu_helpers")
local getSetting = h.getSetting
local createToggleItem = h.createToggleItem
local createFlipNilOrTrueItem = h.createFlipNilOrTrueItem
local createRadioItem = h.createRadioItem
local createSpinDialog = h.createSpinDialog
local createTextInputDialog = h.createTextInputDialog
local createResetMenuItem = h.createResetMenuItem
local DAILY_GOAL_DEFAULT = USER_CONFIG.DAILY_GOAL
local function buildSectionOrderMenu()
local section_labels = {
book = _("Book info"),
chapter = _("Chapter info"),
goal = _("Daily info"),
battery = _("Battery info"),
message = _("Message info"),
}
local label_to_key = {}
for k, v in pairs(section_labels) do label_to_key[v] = k end
return {
{
text = _("Reorder sections"),
keep_menu_open = true,
callback = function()
local SortWidget = require("ui/widget/sortwidget")
local saved = getSetting("SECTION_ORDER") or {}
local defaults = USER_CONFIG.SECTION_ORDER
local item_table = {}
for i = 1, #defaults do
local key = saved[i] or defaults[i]
item_table[i] = { text = section_labels[key] or key }
end
UIManager:show(SortWidget:new {
title = _("Section order"),
item_table = item_table,
callback = function(self)
local new_order = {}
for i, item in ipairs(self.item_table) do
new_order[i] = label_to_key[item.text] or item.text
end
PluginStore:saveSetting(SETTINGS.SECTION_ORDER, new_order)
end,
})
end,
},
}
end
local function buildVisibilityMenu()
local items = {
{ text = _("Book info"), key = SETTINGS.SHOW_BOOK },
{ text = _("Chapter info"), key = SETTINGS.SHOW_CHAP },
{ text = _("Daily info"), key = SETTINGS.SHOW_GOAL },
{ text = _("Battery info"), key = SETTINGS.SHOW_BATT },
{ text = _("Message info"), key = SETTINGS.SHOW_MSG },
}
local function countVisibleSections()
local count = 0
for i, item in ipairs(items) do
if PluginStore:readSetting(item.key) ~= false then count = count + 1 end
end
return count
end
local sub_menu = {}
for i, item in ipairs(items) do
sub_menu[#sub_menu + 1] = {
text = item.text,
toggle = true,
enabled_func = function()
local is_current_enabled = PluginStore:readSetting(item.key) ~= false
if is_current_enabled then return countVisibleSections() > 1 end
return true
end,
checked_func = function()
return PluginStore:readSetting(item.key) ~= false
end,
callback = function()
local current = PluginStore:readSetting(item.key) ~= false
local visible_count = countVisibleSections()
if current and visible_count <= 1 then
UIManager:show(require("ui/widget/infomessage"):new {
text = _("At least one section must remain visible."),
timeout = 2,
})
return
end
PluginStore:saveSetting(item.key, not current)
end,
}
end
return sub_menu
end
local function buildBookSectionContentMenu()
local function coverEnabled()
return PluginStore:readSetting(SETTINGS.COVER_IN_BOOK) == true
end
local cover_submenu = {
createToggleItem(_("Show cover image above book info"),
_("Display the book's cover image above the title and progress in the book section."),
SETTINGS.COVER_IN_BOOK, false),
{
text = _("Cover image size"),
help_text = _("Height limit for the cover image in pixels."),
enabled_func = coverEnabled,
keep_menu_open = true,
callback = function()
createSpinDialog(
_("Cover image size (pixels)"),
getSetting("COVER_SIZE") or 120,
40, 400, 10,
function(val) PluginStore:saveSetting(SETTINGS.COVER_SIZE, val) end,
_("100–150px fits neatly in most layouts. Go up to 250px+ for a prominent cover.")
)
end,
},
{
text = _("Cover border size"),
help_text = _("Width of the border drawn around the cover image. Set to 0 for no border."),
enabled_func = coverEnabled,
keep_menu_open = true,
callback = function()
createSpinDialog(
_("Cover border size (pixels)"),
getSetting("COVER_BORDER_SIZE") or 0,
0, 20, 1,
function(val) PluginStore:saveSetting(SETTINGS.COVER_BORDER_SIZE, val) end,
_("1–3px for a subtle outline. 0 to disable.")
)
end,
},
createToggleItem(
_("Align cover to text alignment"),
_("When enabled, the cover image follows the text alignment setting instead of always being centered."),
SETTINGS.COVER_ALIGN_TO_TEXT, false
),
}
return {
{
text = _("Cover image"),
help_text = _("Configure the book cover image shown above the book section."),
sub_item_table = cover_submenu,
},
createToggleItem(_("Show book author"),
_("Display the author name below the book title."),
SETTINGS.SHOW_BOOK_AUTHOR, false),
createToggleItem(_("Show series name"),
_("Display the book's series name and index below the title."),
SETTINGS.SHOW_BOOK_SERIES, false),
createToggleItem(_("Show book pages (pg x of x)"),
_("Display total page count for the entire book."),
SETTINGS.SHOW_BOOK_PAGES, false),
createFlipNilOrTrueItem(_("Show book time remaining"),
_("Estimated reading time left to finish the book, based on your average reading speed."),
SETTINGS.SHOW_BOOK_TIME_REMAINING),
}
end
local function buildChapterSectionContentMenu()
return {
createToggleItem(_("Show chapter count (ch x of x)"),
_("Display current chapter number and total chapters (e.g., 'Chapter 5 of 12')."),
SETTINGS.SHOW_CHAP_COUNT, false),
createToggleItem(_("Show chapter pages (pg x of x)"),
_("Display the number of pages in the current chapter."),
SETTINGS.SHOW_CHAP_PAGES, false),
createFlipNilOrTrueItem(_("Show chapter time remaining"),
_("Estimated time to finish the current chapter, based on your reading speed."),
SETTINGS.SHOW_CHAP_TIME_REMAINING),
}
end
local function buildGoalSectionContentMenu()
local function isTimeGoal()
return (getSetting("GOAL_TYPE") or USER_CONFIG.GOAL_TYPE or "pages") == "time"
end
return {
{
text = _("Title Line"),
help_text = _("Choose what the top line of the daily info section displays."),
sub_item_table = {
createRadioItem(
_("Pages read today"),
_("e.g. '35 pages read today'"),
SETTINGS.GOAL_TITLE_TYPE, "pages"
),
createRadioItem(
_("Time read today"),
_("e.g. '42 mins read today'"),
SETTINGS.GOAL_TITLE_TYPE, "time"
),
createRadioItem(
_("Pages and time read today"),
_("e.g. '35 pgs & 42 mins read today'"),
SETTINGS.GOAL_TITLE_TYPE, "both"
),
},
},
{
text = _("Goal type"),
help_text = _("Choose whether your daily goal tracks pages read or time spent reading."),
sub_item_table = {
{
text = _("Page goal"),
help_text = _("Track daily progress by number of pages read."),
checked_func = function()
local val = PluginStore:readSetting(SETTINGS.GOAL_TYPE)
return val == nil or val == "pages"
end,
callback = function()
PluginStore:saveSetting(SETTINGS.GOAL_TYPE, "pages")
end,
radio = true,
},
{
text = _("Time goal"),
help_text = _("Track daily progress by minutes spent reading."),
checked_func = function()
return PluginStore:readSetting(SETTINGS.GOAL_TYPE) == "time"
end,
callback = function()
PluginStore:saveSetting(SETTINGS.GOAL_TYPE, "time")
end,
radio = true,
},
},
},
{
text = _("Daily page goal"),
help_text = _("Set how many pages you aim to read each day."),
enabled_func = function() return not isTimeGoal() end,
keep_menu_open = true,
callback = function()
createSpinDialog(
_("Daily page goal (pages)"),
getSetting("DAILY_GOAL") or DAILY_GOAL_DEFAULT,
1, 500, 5,
function(val) PluginStore:saveSetting(SETTINGS.DAILY_GOAL, val) end,
_("20–50 pages for a daily habit. 50–100 for dedicated reading sessions."), nil, 25
)
end,
},
{
text = _("Daily time goal"),
help_text = _("Set how many minutes you aim to read each day."),
enabled_func = function() return isTimeGoal() end,
keep_menu_open = true,
callback = function()
createSpinDialog(
_("Daily time goal (minutes)"),
getSetting("DAILY_GOAL_MINUTES") or USER_CONFIG.DAILY_GOAL_MINUTES,
5, 480, 5,
function(val) PluginStore:saveSetting(SETTINGS.DAILY_GOAL_MINUTES, val) end,
_("20–30 mins for a daily habit. 60+ mins for dedicated reading sessions."), nil, 15
)
end,
},
createToggleItem(_("Show current reading streak"),
_("Display consecutive days you've met your reading goal."),
SETTINGS.SHOW_GOAL_STREAK, false),
createToggleItem(_("Show weekly progress"),
_("Days this week you have met your reading goal, shown as completed days out of the current weekday (Monday = 1... Sunday = 7). For example: Thursday with goals met on Monday and Tuesday will show 2/4."),
SETTINGS.SHOW_GOAL_ACHIEVEMENT, false),
{
text = _("Show daily goal"),
help_text = _("Target page mode: e.g. 50 page goal. Target time mode: e.g. 1hr 15min time goal."),
checked_func = function()
local val = PluginStore:readSetting(SETTINGS.SHOW_GOAL_PAGES)
return val == nil or val == true
end,
callback = function()
PluginStore:flipNilOrTrue(SETTINGS.SHOW_GOAL_PAGES)
end,
},
}
end
local function buildBatterySectionContentMenu()
return {
{
text = _("Show current time/date on separate line"),
help_text = _("Display time/date on its own line below battery percentage instead of inline."),
help_text_func = function()
local val = getSetting("SHOW_BATT_TIME")
if val == false then return _("Enable 'Show battery time remaining' to use this option") end
return nil
end,
enabled_func = function()
local val = getSetting("SHOW_BATT_TIME")
return val == nil or val == true
end,
checked_func = function() return PluginStore:isTrue(SETTINGS.SHOW_BATT_TIME_SEPARATE) end,
callback = function() PluginStore:flipNilOrFalse(SETTINGS.SHOW_BATT_TIME_SEPARATE) end,
},
{
text = _("Show date instead of time"),
help_text = _("Display current date (e.g. '29th Jan') instead of time in battery section"),
checked_func = function() return getSetting("SHOW_BATT_DATE") end,
callback = function()
PluginStore:saveSetting(SETTINGS.SHOW_BATT_DATE, not getSetting("SHOW_BATT_DATE"))
end,
},
createToggleItem(_("Show battery consumption rate"),
_("Display battery drain percentage per hour based on recent usage or manual input (see advanced menu)"),
SETTINGS.SHOW_BATT_RATE, false),
createFlipNilOrTrueItem(_("Show battery time remaining"),
_("Estimated hours and minutes until battery is depleted, based on current drain rate."),
SETTINGS.SHOW_BATT_TIME, true),
}
end
local function buildMessageSectionContentMenu()
return {
createRadioItem(_("Custom message"),
_("Use a separate custom message just for Customisable Sleep Screen"),
SETTINGS.MESSAGE_SOURCE, "custom"),
createRadioItem(_("Book highlights"),
_("Show a random highlight from the current book"),
SETTINGS.MESSAGE_SOURCE, "highlight"),
createRadioItem(_("Custom quotes"),
_("Show a random quote from the custom_quotes.lua file in the plugin folder."),
SETTINGS.MESSAGE_SOURCE, "custom_quotes"),
createRadioItem(_("KOReader sleep message"),
_("Uses KOReaders own sleep screen message function. Enable 'Add custom message to sleep screen' to use this (Settings → Screen → Sleep screen → Sleep screen message)."),
SETTINGS.MESSAGE_SOURCE, "koreader",
function() return G_reader_settings:isTrue(SETTINGS.SHOW_MSG_GLOBAL) end),
createToggleItem(
_("Show message header"),
_("Show or hide the header label above the message text."),
SETTINGS.SHOW_MSG_HEADER, true
),
{
text = _("Message header"),
help_text = _("Custom header text displayed above the message. Supports variables: %d, %y, %t, %b, %r."),
keep_menu_open = true,
callback = function()
createTextInputDialog(_("Change custom message header"), getSetting("MSG_HEADER"),
function(value) PluginStore:saveSetting(SETTINGS.MSG_HEADER, value) end)
end,
},
{
text = _("Edit custom message"),
help_text = _("Write your custom message text. Only active when 'Custom message' is selected. Supports variables: %d, %y, %t, %b, %r."),
enabled_func = function() return getSetting("MESSAGE_SOURCE") == "custom" end,
keep_menu_open = true,
callback = function()
createTextInputDialog(_("Custom Customisable Sleep Screen message"),
getSetting("CUSTOM_MESSAGE") or "",
function(value) PluginStore:saveSetting(SETTINGS.CUSTOM_MESSAGE, value) end)
end,
},
{
text = _("Book highlight maximum length"),
help_text = _("Maximum characters to display for highlights (0 = no limit)."),
enabled_func = function() return getSetting("MESSAGE_SOURCE") == "highlight" end,
keep_menu_open = true,
callback = function()
local current_value = getSetting("MAX_HIGHLIGHT_LENGTH") or 0
createSpinDialog(
_("Maximum highlight length (characters)"),
current_value > 0 and current_value or USER_CONFIG.MAX_HIGHLIGHT_LENGTH,
0, 1000, 25,
function(val) PluginStore:saveSetting(SETTINGS.MAX_HIGHLIGHT_LENGTH, val) end,
_("100–150 chars keeps highlights readable. Set to 0 to show the full highlight."),
nil, 100
)
end,
},
{
text = _("Add quotation marks to highlights"),
help_text = _("Wraps all highlights in curly double quotes, removing any pre-existing quotation marks."),
enabled_func = function() return getSetting("MESSAGE_SOURCE") == "highlight" end,
checked_func = function()
local setting = getSetting("HIGHLIGHT_ADD_QUOTES")
return setting == nil and USER_CONFIG.HIGHLIGHT_ADD_QUOTES or setting
end,
callback = function()
local current = getSetting("HIGHLIGHT_ADD_QUOTES")
if current == nil then current = USER_CONFIG.HIGHLIGHT_ADD_QUOTES end
PluginStore:saveSetting(SETTINGS.HIGHLIGHT_ADD_QUOTES, not current)
end,
},
{
text = _("Show highlight location"),
help_text = _("Display the chapter title and page number where the highlight is found, shown below the highlight text."),
enabled_func = function() return getSetting("MESSAGE_SOURCE") == "highlight" end,
checked_func = function() return getSetting("SHOW_HIGHLIGHT_LOCATION") end,
callback = function()
PluginStore:saveSetting(SETTINGS.SHOW_HIGHLIGHT_LOCATION,
not getSetting("SHOW_HIGHLIGHT_LOCATION"))
end,
},
{
text = _("Show quote attribution"),
help_text = _("Display the author and book name below the quote, if provided in custom_quotes.lua."),
enabled_func = function() return getSetting("MESSAGE_SOURCE") == "custom_quotes" end,
checked_func = function() return getSetting("SHOW_QUOTE_ATTRIBUTION") end,
callback = function()
PluginStore:saveSetting(SETTINGS.SHOW_QUOTE_ATTRIBUTION,
not getSetting("SHOW_QUOTE_ATTRIBUTION"))
end,
},
}
end
local function buildTitleSubtitleToggles()
return {
{
text = _("Show titles (top line)"),
help_text = _("Display the main heading text in each section. At least one of titles or subtitles must be visible."),
checked_func = function() return getSetting("SHOW_TITLES") ~= false end,
callback = function()
local current_titles = getSetting("SHOW_TITLES")
local current_subtitles = getSetting("SHOW_SUBTITLES")
if current_titles == false then
PluginStore:saveSetting(SETTINGS.SHOW_TITLES, true)
else
if current_subtitles == false then
UIManager:show(require("ui/widget/infomessage"):new {
text = _("Cannot hide both titles and subtitles. At least one must be visible."),
timeout = 3,
})
return
else
PluginStore:saveSetting(SETTINGS.SHOW_TITLES, false)
end
end
end,
},
{
text = _("Show subtitles (bottom lines)"),
help_text = _("Display information below main heading text in each section. At least one of titles or subtitles must be visible."),
checked_func = function() return getSetting("SHOW_SUBTITLES") ~= false end,
callback = function()
local current_titles = getSetting("SHOW_TITLES")
local current_subtitles = getSetting("SHOW_SUBTITLES")
if current_subtitles == false then
PluginStore:saveSetting(SETTINGS.SHOW_SUBTITLES, true)
else
if current_titles == false then
UIManager:show(require("ui/widget/infomessage"):new {
text = _("Cannot hide both titles and subtitles. At least one must be visible."),
timeout = 3,
})
return
else
PluginStore:saveSetting(SETTINGS.SHOW_SUBTITLES, false)
end
end
end,
},
}
end
local function buildContentsMenu()
local menu = {
createResetMenuItem("contents", {
SETTINGS.SHOW_BOOK, SETTINGS.SHOW_CHAP,
SETTINGS.SHOW_GOAL, SETTINGS.SHOW_BATT,
SETTINGS.SHOW_MSG, SETTINGS.SECTION_ORDER,
SETTINGS.SHOW_BOOK_AUTHOR, SETTINGS.SHOW_BOOK_PAGES,
SETTINGS.SHOW_BOOK_TIME_REMAINING, SETTINGS.SHOW_CHAP_COUNT,
SETTINGS.SHOW_CHAP_PAGES, SETTINGS.SHOW_CHAP_TIME_REMAINING,
SETTINGS.DAILY_GOAL, SETTINGS.GOAL_TYPE,
SETTINGS.DAILY_GOAL_MINUTES, SETTINGS.SHOW_GOAL_STREAK,
SETTINGS.SHOW_GOAL_ACHIEVEMENT, SETTINGS.SHOW_GOAL_PAGES,
SETTINGS.GOAL_TITLE_TYPE, SETTINGS.SHOW_MSG_HEADER,
SETTINGS.SHOW_BATT_TIME_SEPARATE, SETTINGS.SHOW_BATT_DATE,
SETTINGS.SHOW_BATT_RATE, SETTINGS.SHOW_BATT_TIME,
SETTINGS.MESSAGE_SOURCE, SETTINGS.MSG_HEADER,
SETTINGS.CUSTOM_MESSAGE, SETTINGS.MAX_HIGHLIGHT_LENGTH,
SETTINGS.HIGHLIGHT_ADD_QUOTES, SETTINGS.SHOW_HIGHLIGHT_LOCATION,
SETTINGS.SHOW_TITLES, SETTINGS.SHOW_SUBTITLES,
SETTINGS.COVER_IN_BOOK, SETTINGS.COVER_SIZE,
SETTINGS.COVER_BORDER_SIZE, SETTINGS.COVER_ALIGN_TO_TEXT,
SETTINGS.SHOW_QUOTE_ATTRIBUTION,
SETTINGS.SHOW_BOOK_SERIES,
}),
{ text = _("Displayed sections"),
sub_item_table = buildVisibilityMenu(),
help_text = _("Toggle which sections show on the sleep screen. At least one must be visible.") },
buildSectionOrderMenu()[1],
{ text = _("[ Section Content ]"), enabled = false },
{ text = _("Book section"), help_text = _("Configure book-specific details."), sub_item_table = buildBookSectionContentMenu() },
{ text = _("Chapter section"), help_text = _("Configure chapter-specific details"), sub_item_table = buildChapterSectionContentMenu() },
{ text = _("Daily goal section"), help_text = _("Configure reading goal details"), sub_item_table = buildGoalSectionContentMenu() },
{ text = _("Battery section"), help_text = _("Configure battery & time/date details"), sub_item_table = buildBatterySectionContentMenu() },
{ text = _("Message section"), help_text = _("Configure message-specific details"), sub_item_table = buildMessageSectionContentMenu() },
}
for i, item in ipairs(buildTitleSubtitleToggles()) do
menu[#menu + 1] = item
end
return menu
end
return {
buildContentsMenu = buildContentsMenu,
buildVisibilityMenu = buildVisibilityMenu,
buildSectionOrderMenu = buildSectionOrderMenu,
buildBookSectionContentMenu = buildBookSectionContentMenu,
buildChapterSectionContentMenu = buildChapterSectionContentMenu,
buildGoalSectionContentMenu = buildGoalSectionContentMenu,
buildBatterySectionContentMenu = buildBatterySectionContentMenu,
buildMessageSectionContentMenu = buildMessageSectionContentMenu,
buildTitleSubtitleToggles = buildTitleSubtitleToggles,
}