Repository navigation
Expand file tree
/
Copy pathSettingsActivity.cpp
More file actions
671 lines (625 loc) · 30.5 KB
/
Copy pathSettingsActivity.cpp
File metadata and controls
671 lines (625 loc) · 30.5 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
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
#include "SettingsActivity.h"
#include <BlePageTurner.h>
#include <BoardConfig.h>
#include <GfxRenderer.h>
#include <HalClock.h>
#include <HalDisplay.h>
#include <LibraryBuilder.h>
#include <Logging.h>
#include <Memory.h>
#include <WiFi.h>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include "AboutActivity.h"
#include "BleSettingsActivity.h"
#include "ButtonRemapActivity.h"
#include "ClearCacheActivity.h"
#include "ClockSettingsActivity.h"
#include "CrossPointSettings.h"
#include "FontDownloadActivity.h"
#include "HomeButtonSettingsActivity.h"
#include "KOReaderSettingsActivity.h"
#include "KeyboardLayoutsActivity.h"
#include "LanguageSelectActivity.h"
#include "MappedInputManager.h"
#include "OpdsServerListActivity.h"
#include "OtaUpdateActivity.h"
#include "SdCardFontSystem.h"
#include "SdFirmwareUpdateActivity.h"
#include "SettingsList.h"
#include "SilentRestart.h"
#include "StatusBarSettingsActivity.h"
#include "TextSettingsActivity.h"
#include "activities/network/WifiSelectionActivity.h"
#include "activities/util/IntervalSelectionActivity.h"
#include "components/UITheme.h"
#include "components/UIThemeTokens.h"
#include "components/UiAppHelpers.h"
#include "fontIds.h"
#include "util/PluginEvents.h"
namespace fui = freeink::ui;
namespace {
// Shared-list filtering for this board; false drops the row from the device UI
// (it stays in the shared list for the web settings API).
SettingsSection sectionFor(const SettingInfo& setting);
bool showOnDevice(const SettingInfo& setting) {
if (sectionFor(setting) == SettingsSection::End || home_button::isSetting(setting.valuePtr)) return false;
if (setting.valuePtr == &CrossPointSettings::fadingFix && !CrossPointSettings::fadingFixSupported()) return false;
// Merged into the Text Settings screen.
if (setting.category == StrId::STR_CAT_READER && setting.inTextSettings) return false;
if (setting.valuePtr == &CrossPointSettings::pwrBtnFootnoteBack &&
SETTINGS.shortPwrBtn != CrossPointSettings::SHORT_PWRBTN::FOOTNOTES &&
!(CrossPointSettings::hasPowerDoubleClick() && SETTINGS.doublePwrBtn == CrossPointSettings::FOOTNOTES)) {
return false;
}
return true;
}
// End = not a row here: other categories (KOReader Sync, Status Bar, ...)
// only group the web UI; their rows live on their own sub-screens.
SettingsSection sectionFor(const SettingInfo& setting) {
if (setting.valuePtr == &CrossPointSettings::libraryUseMetadata ||
setting.valuePtr == &CrossPointSettings::libraryRecentOpenedOnly ||
setting.valuePtr == &CrossPointSettings::removeReadBooksFromRecents ||
setting.valuePtr == &CrossPointSettings::moveFinishedToReadFolder) {
return SettingsSection::Library;
}
if (setting.valuePtr == &CrossPointSettings::sleepTimeoutMinutes) return SettingsSection::Display;
if (setting.category == StrId::STR_CAT_DISPLAY) return SettingsSection::Display;
if (setting.category == StrId::STR_CAT_READER) return SettingsSection::Reader;
if (setting.category == StrId::STR_CAT_CONTROLS) return SettingsSection::Controls;
if (setting.category == StrId::STR_CAT_SYSTEM) return SettingsSection::General;
return SettingsSection::End;
}
// The shared long-press setting deliberately appears in both button groups.
bool belongsToControlsGroup(const SettingInfo& setting, ControlsGroup group) {
const auto field = setting.valuePtr;
const bool power =
setting.key && (std::strcmp(setting.key, "shortPwrBtn") == 0 || std::strcmp(setting.key, "doublePwrBtn") == 0);
const bool menu = setting.key && std::strcmp(setting.key, "longPressMenuFunction") == 0;
const bool shared = field == &CrossPointSettings::longPressButtonBehavior;
switch (group) {
case ControlsGroup::Power:
return power || field == &CrossPointSettings::pwrBtnFootnoteBack;
case ControlsGroup::Front:
return shared || menu || field == &CrossPointSettings::frontButtonFollowOrientation ||
field == &CrossPointSettings::backShortToFileBrowser;
case ControlsGroup::Side:
return shared || field == &CrossPointSettings::sideButtonLayout ||
field == &CrossPointSettings::sideButtonFollowOrientation;
case ControlsGroup::Touch:
return field == &CrossPointSettings::touchReaderControls || field == &CrossPointSettings::pageTurnGesture ||
field == &CrossPointSettings::previousPageGesture || field == &CrossPointSettings::showReaderMenu;
case ControlsGroup::Root:
return !belongsToControlsGroup(setting, ControlsGroup::Power) &&
!belongsToControlsGroup(setting, ControlsGroup::Front) &&
!belongsToControlsGroup(setting, ControlsGroup::Side) &&
!belongsToControlsGroup(setting, ControlsGroup::Touch);
}
return false;
}
// Device-only action rows added per section, beyond the shared list.
constexpr size_t EXTRA_ROWS_PER_SECTION = 6;
} // namespace
void SettingsActivity::rebuildSettings() {
// Pick up any fonts uploaded/deleted over the web server since the last
// reader activity ran — otherwise the font-family picker shows stale list.
sdFontSystem.refreshIfDirty();
// Rescan /dictionaries on every rebuild to pick up dictionaries copied to the
// SD card since the last visit. Only the Reader section shows the row.
std::vector<DictionaryEntry> dictionaries;
if (section == SettingsSection::Reader) DictionaryRegistry::discover(dictionaries);
auto all = getSettingsList(&sdFontSystem.registry(), &dictionaries);
const auto include = [this](const SettingInfo& setting) {
return sectionFor(setting) == section && showOnDevice(setting) &&
(section != SettingsSection::Controls || belongsToControlsGroup(setting, controlsGroup));
};
const size_t count = std::count_if(all.begin(), all.end(), include);
settings.clear();
settings.reserve(count + EXTRA_ROWS_PER_SECTION);
const auto addAction = [this](SettingsSection target, StrId name, SettingAction action) {
if (target == section) settings.push_back(SettingInfo::Action(name, action));
};
// Language leads so a user stuck in an unfamiliar language finds it first.
addAction(SettingsSection::General, StrId::STR_LANGUAGE, SettingAction::Language);
addAction(SettingsSection::General, StrId::STR_KEYBOARD_LAYOUTS, SettingAction::KeyboardLayouts);
addAction(SettingsSection::Reader, StrId::STR_TEXT_SETTINGS, SettingAction::TextSettings);
addAction(SettingsSection::Reader, StrId::STR_MANAGE_FONTS, SettingAction::DownloadFonts);
if (section == SettingsSection::Controls) {
if (controlsGroup == ControlsGroup::Root) {
if (BoardConfig::hasHomeKey()) {
addAction(section, StrId::STR_HOME_BUTTON, SettingAction::HomeButton);
}
const auto addGroup = [&](ControlsGroup group, StrId title, SettingAction action) {
if (std::any_of(all.begin(), all.end(),
[&](const SettingInfo& setting) {
return sectionFor(setting) == section && showOnDevice(setting) &&
belongsToControlsGroup(setting, group);
}) ||
(group == ControlsGroup::Front && !BoardConfig::hasTouch())) {
addAction(section, title, action);
}
};
addGroup(ControlsGroup::Power, StrId::STR_POWER_BUTTON, SettingAction::ControlsPower);
if (!BoardConfig::hasTouch()) {
addGroup(ControlsGroup::Front, StrId::STR_FRONT_BUTTONS, SettingAction::ControlsFront);
}
addGroup(ControlsGroup::Side, StrId::STR_SIDE_BUTTONS, SettingAction::ControlsSide);
addGroup(ControlsGroup::Touch, StrId::STR_TAPS_AND_GESTURES, SettingAction::ControlsTouch);
} else if (controlsGroup == ControlsGroup::Front && !BoardConfig::hasTouch()) {
addAction(section, StrId::STR_REMAP_FRONT_BUTTONS, SettingAction::RemapFrontButtons);
}
}
for (auto& setting : all) {
if (!include(setting)) continue;
if (section == SettingsSection::Controls && setting.valuePtr == &CrossPointSettings::longPressButtonBehavior) {
setting.nameId = StrId::STR_PAGE_TURN_LONG_PRESS;
}
settings.push_back(std::move(setting));
}
if (section == SettingsSection::Controls && controlsGroup == ControlsGroup::Front) {
for (const char* key : {"longPressButtonBehavior", "longPressMenuFunction"}) {
const auto row = std::find_if(settings.begin(), settings.end(), [key](const SettingInfo& setting) {
return setting.key && std::strcmp(setting.key, key) == 0;
});
if (row != settings.end()) std::rotate(row, row + 1, settings.end());
}
}
// Clock configuration only exists where the RTC probe found hardware.
if (halClock.isAvailable()) {
addAction(SettingsSection::General, StrId::STR_CLOCK, SettingAction::ClockSettings);
}
addAction(SettingsSection::Reader, StrId::STR_CUSTOMISE_STATUS_BAR, SettingAction::CustomiseStatusBar);
addAction(SettingsSection::Reader, StrId::STR_CLEAR_READING_CACHE, SettingAction::ClearCache);
addAction(SettingsSection::Library, StrId::STR_LIBRARY_REBUILD, SettingAction::RefreshLibrary);
addAction(SettingsSection::Network, StrId::STR_WIFI_NETWORKS, SettingAction::Network);
if (bleturner::status().compiledIn) {
addAction(SettingsSection::Network, StrId::STR_BT_PAGE_TURNER, SettingAction::Bluetooth);
}
addAction(SettingsSection::Network, StrId::STR_KOREADER_SYNC, SettingAction::KOReaderSync);
addAction(SettingsSection::Network, StrId::STR_OPDS_SERVERS, SettingAction::OPDSBrowser);
addAction(SettingsSection::Network, StrId::STR_PLUGINS, SettingAction::Plugins);
// OTA fetches this board's own release asset (see OtaUpdater); boards whose
// asset isn't published yet just report no update available. The Picco's
// stock layout has one app slot (partitions_picco.csv), so it cannot update
// itself either way.
if (!BoardConfig::isPicco()) {
addAction(SettingsSection::General, StrId::STR_CHECK_UPDATES, SettingAction::CheckForUpdates);
addAction(SettingsSection::General, StrId::STR_SD_FIRMWARE_UPDATE, SettingAction::SdFirmwareUpdate);
}
addAction(SettingsSection::General, StrId::STR_ABOUT, SettingAction::About);
rebuildRowItems();
}
SettingsActivity::SettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const SettingsSection section, const ControlsGroup controlsGroup)
: UiListActivity("Settings", renderer, mappedInput), section(section), controlsGroup(controlsGroup) {}
void SettingsActivity::onEnter() {
UiListActivity::onEnter();
preserveQuickResumeTimeoutOn =
SETTINGS.quickResumeSleepScreen == CrossPointSettings::QUICK_RESUME_SLEEP_SCREEN::QUICK_RESUME_AFTER_TIMEOUT;
quickResumeTimeoutAutoEnabled = false;
syncQuickResumeTimeoutForSleepScreen(/*sleepScreenChanged=*/true, /*quickResumeTimeoutChanged=*/false);
rebuildSettings();
}
// Rebuilds rowValues_/rowItems_ (label + actionValue) for settings.
// Structural — call only when the setting list changes, never from
// buildScreen(), which only refreshes rowValues_ content and rowItems_[].value
// pointers in place.
void SettingsActivity::rebuildRowItems() {
rowValues_.assign(settings.size(), std::string());
rowItems_.clear();
rowItems_.reserve(settings.size());
for (size_t i = 0; i < settings.size(); i++) {
fui::ListItem item;
item.label = I18N.get(settings[i].nameId);
item.actionValue = static_cast<int16_t>(i);
rowItems_.push_back(item);
}
}
const char* SettingsActivity::headerTitle() const {
switch (controlsGroup) {
case ControlsGroup::Power:
return tr(STR_POWER_BUTTON);
case ControlsGroup::Front:
return tr(STR_FRONT_BUTTONS);
case ControlsGroup::Side:
return tr(STR_SIDE_BUTTONS);
case ControlsGroup::Touch:
return tr(STR_TAPS_AND_GESTURES);
case ControlsGroup::Root:
break;
}
return I18N.get(SETTINGS_SECTION_TITLES[static_cast<size_t>(section)]);
}
void SettingsActivity::activateIndex(const int index) {
if (optionPopup.isActive()) return;
// Most rows repaint a different surface (popup, sub-activity, new value);
// a lingering tap flash would gray an unrelated element.
app.clearTapFlash();
nav.selected = index;
toggleSelectedSetting();
requestUpdate();
}
void SettingsActivity::applyUiSettingChange(uint8_t CrossPointSettings::* valuePtr) {
// Theme changes take effect immediately, on this screen — reload the theme
// and re-derive the app's tokens so the very next repaint is in the new look.
if (valuePtr != &CrossPointSettings::uiTheme) {
return;
}
UITheme::getInstance().reload();
// Re-derive the shared tokens for the new look; the gate stays closed until
// the repaint that rebuilds the interaction table in the new layout.
resetUi();
}
bool SettingsActivity::handleCustomInput() {
return optionPopup.handleInput(mappedInput, [this] { requestUpdate(); });
}
void SettingsActivity::toggleSelectedSetting() {
mappedInput.resetHomeButtonInput();
const int selectedSetting = nav.selected;
if (selectedSetting < 0 || selectedSetting >= static_cast<int>(settings.size())) {
return;
}
const auto& setting = settings[selectedSetting];
const bool sleepScreenChanged = setting.valuePtr == &CrossPointSettings::sleepScreen;
const bool quickResumeTimeoutChanged = setting.valuePtr == &CrossPointSettings::quickResumeSleepScreen;
if (setting.nameId == StrId::STR_TIME_TO_SLEEP) {
openSleepTimeoutPicker();
return;
}
if (setting.type == SettingType::TOGGLE && setting.valuePtr != nullptr) {
// Toggle the boolean value using the member pointer
const bool currentValue = SETTINGS.*(setting.valuePtr);
SETTINGS.*(setting.valuePtr) = !currentValue;
} else if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
const uint8_t currentValue = SETTINGS.*(setting.valuePtr);
const auto enumLabels = setting.enumLabels();
if (enumLabels.size() > 2) {
const auto valuePtr = setting.valuePtr;
optionPopup.show(setting.nameId, enumLabels.data(), static_cast<int>(enumLabels.size()), currentValue,
[this, valuePtr, sleepScreenChanged, quickResumeTimeoutChanged](int idx) {
SETTINGS.*valuePtr = idx;
syncQuickResumeTimeoutForSleepScreen(sleepScreenChanged, quickResumeTimeoutChanged);
SETTINGS.saveToFile();
rebuildSettings();
applyUiSettingChange(valuePtr);
});
requestUpdate();
return;
}
SETTINGS.*(setting.valuePtr) = (currentValue + 1) % static_cast<uint8_t>(enumLabels.size());
} else if (setting.type == SettingType::ENUM && setting.valueGetter && setting.valueSetter) {
const uint8_t totalValues = setting.enumStringValues.empty()
? static_cast<uint8_t>(setting.enumLabels().size())
: static_cast<uint8_t>(setting.enumStringValues.size());
const uint8_t cur = setting.valueGetter();
if (totalValues > 2) {
const auto valueSetter = setting.valueSetter;
auto onSelect = [this, valueSetter, sleepScreenChanged, quickResumeTimeoutChanged](int idx) {
valueSetter(idx);
syncQuickResumeTimeoutForSleepScreen(sleepScreenChanged, quickResumeTimeoutChanged);
SETTINGS.saveToFile();
rebuildSettings();
};
if (!setting.enumStringValues.empty()) {
optionPopup.show(setting.nameId, setting.enumStringValues, cur, std::move(onSelect));
} else {
const auto enumLabels = setting.enumLabels();
optionPopup.show(setting.nameId, enumLabels.data(), static_cast<int>(enumLabels.size()), cur,
std::move(onSelect));
}
requestUpdate();
return;
}
setting.valueSetter((cur + 1) % totalValues);
} else if (setting.type == SettingType::VALUE && setting.valuePtr != nullptr) {
const int8_t currentValue = SETTINGS.*(setting.valuePtr);
if (currentValue + setting.valueRange.step > setting.valueRange.max) {
SETTINGS.*(setting.valuePtr) = setting.valueRange.min;
} else {
SETTINGS.*(setting.valuePtr) = currentValue + setting.valueRange.step;
}
} else if (setting.type == SettingType::ACTION) {
auto resultHandler = [this](const ActivityResult&) { SETTINGS.saveToFile(); };
switch (setting.action) {
case SettingAction::ControlsPower:
case SettingAction::ControlsFront:
case SettingAction::ControlsSide:
case SettingAction::ControlsTouch: {
ControlsGroup group = ControlsGroup::Root;
switch (setting.action) {
case SettingAction::ControlsPower:
group = ControlsGroup::Power;
break;
case SettingAction::ControlsFront:
group = ControlsGroup::Front;
break;
case SettingAction::ControlsSide:
group = ControlsGroup::Side;
break;
case SettingAction::ControlsTouch:
group = ControlsGroup::Touch;
break;
default:
break; // Only the four group actions reach this branch.
}
// The activity stack owns the child until Back returns to this menu.
auto activity = makeUniqueNoThrow<SettingsActivity>(renderer, mappedInput, SettingsSection::Controls, group);
if (!activity) {
LOG_ERR("SET", "OOM: Controls settings (%u bytes)", static_cast<unsigned>(sizeof(SettingsActivity)));
return;
}
startActivityForResult(std::move(activity), [this](const ActivityResult&) { requestUpdate(); });
return;
}
case SettingAction::HomeButton: {
// Activities must outlive this call and are owned by the activity stack.
auto activity = makeUniqueNoThrow<HomeButtonSettingsActivity>(renderer, mappedInput);
if (!activity) {
LOG_ERR("SET", "OOM: Home button settings");
return;
}
startActivityForResult(std::move(activity), [this](const ActivityResult&) { requestUpdate(); });
return;
}
case SettingAction::RemapFrontButtons:
startActivityForResult(std::make_unique<ButtonRemapActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::CustomiseStatusBar:
startActivityForResult(std::make_unique<StatusBarSettingsActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::ClockSettings:
if (auto activity = makeUniqueNoThrow<ClockSettingsActivity>(renderer, mappedInput)) {
startActivityForResult(std::move(activity), resultHandler);
} else {
LOG_ERR("SETTINGS", "OOM: ClockSettingsActivity");
}
break;
case SettingAction::KOReaderSync:
startActivityForResult(std::make_unique<KOReaderSettingsActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::OPDSBrowser:
startActivityForResult(std::make_unique<OpdsServerListActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::Network: {
auto activity = makeUniqueNoThrow<WifiSelectionActivity>(renderer, mappedInput, false);
if (!activity) {
LOG_ERR("SETTINGS", "OOM: WifiSelectionActivity");
return;
}
startActivityForResult(std::move(activity), [](const ActivityResult&) {
SETTINGS.saveToFile();
// Every other WiFi consumer hands the radio to a session it owns;
// these rows only save credentials, so nothing here would ever
// release the driver's heap. The scan alone brings it up, so tear
// down whether or not the user joined a network.
if (WiFi.getMode() == WIFI_MODE_NULL) return;
WiFi.disconnect(false);
delay(30);
// Unlike the onExit() teardowns, this runs from the loop task with
// no lock held; the restart popup paints straight to the panel.
RenderLock lock;
silentRestartToSettings();
});
break;
}
case SettingAction::Bluetooth: {
auto activity = makeUniqueNoThrow<BleSettingsActivity>(renderer, mappedInput);
if (!activity) {
LOG_ERR("SETTINGS", "OOM: BleSettingsActivity");
return;
}
startActivityForResult(std::move(activity), resultHandler);
break;
}
case SettingAction::ClearCache:
startActivityForResult(std::make_unique<ClearCacheActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::RefreshLibrary: {
// Blocking, like the Library screen's own rebuild: the walk needs the
// card to itself, so the render task stays locked out.
RenderLock lock;
GUI.drawPopup(renderer, tr(STR_LIBRARY_REBUILDING));
library::BuildStats stats;
const bool ok = library::buildLibraryIndex("/", stats, SETTINGS.libraryUseMetadata != 0);
if (!ok) LOG_ERR("SET", "library index build failed");
GUI.drawPopup(renderer, ok ? tr(STR_LIBRARY_REBUILD_DONE) : tr(STR_LIBRARY_REBUILD_FAILED));
delay(1000);
requestUpdate();
break;
}
case SettingAction::CheckForUpdates:
startActivityForResult(std::make_unique<OtaUpdateActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::SdFirmwareUpdate:
startActivityForResult(std::make_unique<SdFirmwareUpdateActivity>(renderer, mappedInput), resultHandler);
break;
case SettingAction::DownloadFonts:
startActivityForResult(std::make_unique<FontDownloadActivity>(renderer, mappedInput),
[this](const ActivityResult&) {
SETTINGS.saveToFile();
rebuildSettings();
});
break;
case SettingAction::TextSettings:
startActivityForResult(std::make_unique<TextSettingsActivity>(renderer, mappedInput, &sdFontSystem.registry(),
TextSettingsActivity::Tab::Family),
[this](const ActivityResult&) {
// TextSettingsActivity saves on each change; no save needed here.
rebuildSettings();
});
break;
case SettingAction::Language:
// Row labels are translated once in rebuildRowItems() and don't
// re-run on Pop (see ActivityManager::loop()), so a language switch
// needs an explicit rebuild here rather than the generic resultHandler.
startActivityForResult(std::make_unique<LanguageSelectActivity>(renderer, mappedInput),
[this](const ActivityResult&) {
SETTINGS.saveToFile();
rebuildSettings();
});
break;
case SettingAction::Plugins: {
static constexpr StrId OPTIONS[] = {StrId::STR_ENABLED, StrId::STR_DISABLED, StrId::STR_CANCEL};
optionPopup.show(StrId::STR_PLUGINS, OPTIONS, 3, SETTINGS.pluginsEnabled ? 0 : 1, [this](const int idx) {
if (idx == 2) return;
SETTINGS.pluginsEnabled = idx == 0 ? 1 : 0;
// Turning plugins on brings back a dismissed Plugin Hub install row.
if (SETTINGS.pluginsEnabled) SETTINGS.pluginHubPromptHidden = 0;
SETTINGS.saveToFile();
// Applies to event delivery now, not after a reboot.
pluginevents::refreshSubscriptions();
rebuildSettings();
});
requestUpdate();
return;
}
case SettingAction::KeyboardLayouts:
if (auto activity = makeUniqueNoThrow<KeyboardLayoutsActivity>(renderer, mappedInput)) {
startActivityForResult(std::move(activity), nullptr);
} else {
LOG_ERR("SETTINGS", "OOM: KeyboardLayoutsActivity");
}
break;
case SettingAction::About:
if (auto activity = makeUniqueNoThrow<AboutActivity>(renderer, mappedInput)) {
startActivityForResult(std::move(activity), nullptr);
} else {
LOG_ERR("SETTINGS", "OOM: AboutActivity");
}
break;
case SettingAction::None:
// Do nothing
break;
}
return; // Results will be handled in the result handler, so we can return early here
} else {
return;
}
syncQuickResumeTimeoutForSleepScreen(sleepScreenChanged, quickResumeTimeoutChanged);
SETTINGS.saveToFile();
const auto valuePtr = setting.valuePtr; // rebuildSettings() replaces `setting`
rebuildSettings();
applyUiSettingChange(valuePtr);
// Rows can appear/disappear with a change (e.g. the footnote-back toggle).
nav.selected = std::min(selectedSetting, static_cast<int>(settings.size()) - 1);
}
void SettingsActivity::syncQuickResumeTimeoutForSleepScreen(bool sleepScreenChanged, bool quickResumeTimeoutChanged) {
if (quickResumeTimeoutChanged) {
preserveQuickResumeTimeoutOn =
SETTINGS.quickResumeSleepScreen == CrossPointSettings::QUICK_RESUME_SLEEP_SCREEN::QUICK_RESUME_AFTER_TIMEOUT;
quickResumeTimeoutAutoEnabled = false;
}
if (SETTINGS.sleepScreen == CrossPointSettings::SLEEP_SCREEN_MODE::QUICK_RESUME) {
if (SETTINGS.quickResumeSleepScreen != CrossPointSettings::QUICK_RESUME_SLEEP_SCREEN::QUICK_RESUME_AFTER_TIMEOUT) {
SETTINGS.quickResumeSleepScreen = CrossPointSettings::QUICK_RESUME_SLEEP_SCREEN::QUICK_RESUME_AFTER_TIMEOUT;
quickResumeTimeoutAutoEnabled = !preserveQuickResumeTimeoutOn;
} else if (sleepScreenChanged && !preserveQuickResumeTimeoutOn) {
quickResumeTimeoutAutoEnabled = true;
}
return;
}
if (sleepScreenChanged && quickResumeTimeoutAutoEnabled && !preserveQuickResumeTimeoutOn) {
SETTINGS.quickResumeSleepScreen = CrossPointSettings::QUICK_RESUME_SLEEP_SCREEN::QUICK_RESUME_NEVER;
quickResumeTimeoutAutoEnabled = false;
}
}
void SettingsActivity::openSleepTimeoutPicker() {
startActivityForResult(
std::make_unique<IntervalSelectionActivity>(
renderer, mappedInput, "SleepTimeoutInterval", StrId::STR_TIME_TO_SLEEP, SETTINGS.sleepTimeoutMinutes,
CrossPointSettings::MIN_SLEEP_TIMEOUT_MINUTES, CrossPointSettings::MAX_SLEEP_TIMEOUT_MINUTES, 1, 5,
StrId::STR_SLEEP_TIMER_VALUE_FORMAT, false, StrId::STR_SLEEP_NEVER),
[this](const ActivityResult& result) {
if (!result.isCancelled) {
SETTINGS.sleepTimeoutMinutes = static_cast<uint8_t>(std::get<IntervalResult>(result.data).value);
SETTINGS.saveToFile();
}
requestUpdate();
});
}
std::string SettingsActivity::settingValueText(const SettingInfo& setting) {
if (setting.action == SettingAction::Plugins) return SETTINGS.pluginsEnabled ? tr(STR_ENABLED) : tr(STR_DISABLED);
if (setting.type == SettingType::ENUM && setting.valuePtr != nullptr) {
// Guard like the valueGetter branch below: a corrupt/migrated settings
// byte must not index past the enum table.
const uint8_t value = SETTINGS.*(setting.valuePtr);
const auto enumLabels = setting.enumLabels();
if (value >= enumLabels.size()) return "";
return I18N.get(enumLabels[value]);
}
if (setting.type == SettingType::ENUM && setting.valueGetter) {
const uint8_t value = setting.valueGetter();
if (!setting.enumStringValues.empty() && value < setting.enumStringValues.size()) {
return setting.enumStringValues[value];
}
const auto enumLabels = setting.enumLabels();
if (value < enumLabels.size()) {
return I18N.get(enumLabels[value]);
}
return "";
}
if (setting.type == SettingType::VALUE && setting.valuePtr != nullptr) {
if (setting.nameId == StrId::STR_TIME_TO_SLEEP) {
if (SETTINGS.sleepTimeoutMinutes >= CrossPointSettings::SLEEP_TIMEOUT_NEVER_MINUTES) {
return tr(STR_SLEEP_NEVER);
}
char valueBuffer[32];
snprintf(valueBuffer, sizeof(valueBuffer), tr(STR_SLEEP_TIMER_VALUE_FORMAT),
static_cast<unsigned int>(SETTINGS.*(setting.valuePtr)));
return valueBuffer;
}
return std::to_string(SETTINGS.*(setting.valuePtr));
}
return "";
}
void SettingsActivity::buildScreen(UiScreen& screen) {
const auto& metrics = UITheme::getInstance().getMetrics();
// Content below the GUI.drawHeader band, above the button hints.
screen.setContentMarginFromScreen(fui::Insets{static_cast<int16_t>(metrics.topPadding + metrics.headerHeight), 0,
static_cast<int16_t>(metrics.buttonHintsHeight), 0});
screen.spacer(static_cast<int16_t>(metrics.verticalSpacing));
// rowItems_ (label/actionValue) was built by rebuildRowItems(); only the
// live value text needs refreshing here, by assigning into the existing
// rowValues_ strings (no vector growth).
for (size_t i = 0; i < settings.size(); i++) {
const auto& setting = settings[i];
const auto labels = setting.enumLabels();
const bool checkbox = setting.type == SettingType::TOGGLE ||
(setting.type == SettingType::ENUM && setting.enumStringValues.empty() &&
labels.size() == 2 && labels[0] == StrId::STR_STATE_OFF && labels[1] == StrId::STR_STATE_ON);
if (checkbox && (setting.valuePtr || setting.valueGetter)) {
const bool checked = setting.valuePtr ? SETTINGS.*(setting.valuePtr) != 0 : setting.valueGetter() != 0;
rowValues_[i].clear();
GUI.setCheckboxRow(rowItems_[i], checked);
} else {
rowValues_[i] = settingValueText(setting);
rowItems_[i].value = rowValues_[i].empty() ? nullptr : rowValues_[i].c_str();
}
}
fui::ListProps props;
props.items = rowItems_.data();
props.count = static_cast<uint16_t>(rowItems_.size());
props.action = ACTION_ROW;
props.inputMask = fui::InputTouch; // physical buttons stay in loop()
props.valueInset = 8; // air between the value and the row edge
// Touch uses the standard list sizing, as in Bluetooth; buttons keep compact labels.
// maxLines also marks smallText as explicit so the list does not replace it with bodyText.
props.labelText = mappedInput.hasTouch() ? screen.theme().bodyText : screen.theme().smallText;
props.labelText.maxLines = 2;
syncListViewport(screen, props);
screen.list(props);
}
void SettingsActivity::drawFooter() {
const int selected = nav.selected;
const bool picker =
selected >= 0 && selected < static_cast<int>(settings.size()) &&
(settings[selected].type == SettingType::ACTION || settings[selected].nameId == StrId::STR_TIME_TO_SLEEP);
const auto labels =
mappedInput.mapLabels(tr(STR_BACK), picker ? tr(STR_SELECT) : tr(STR_TOGGLE), tr(STR_DIR_UP), tr(STR_DIR_DOWN));
GUI.drawButtonHints(renderer, labels.btn1, labels.btn2, labels.btn3, labels.btn4);
}
void SettingsActivity::render(RenderLock&& lock) {
if (optionPopup.processRender(renderer, mappedInput)) return;
UiListActivity::render(std::move(lock));
}