forked from crosspoint-reader/crosspoint-reader
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLanguageSelectActivity.cpp
More file actions
83 lines (68 loc) · 3.11 KB
/
Copy pathLanguageSelectActivity.cpp
File metadata and controls
83 lines (68 loc) · 3.11 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
#include "LanguageSelectActivity.h"
#include <GfxRenderer.h>
#include <I18n.h>
#include <algorithm>
#include <iterator>
#include "CrossPointSettings.h"
#include "I18nKeys.h"
#include "MappedInputManager.h"
#include "components/UITheme.h"
namespace fui = freeink::ui;
LanguageSelectActivity::LanguageSelectActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: UiListActivity("LanguageSelect", renderer, mappedInput) {}
void LanguageSelectActivity::onEnter() {
UiListActivity::onEnter();
// Open on the current language, which may sit past the first page; the first
// screen build pulls the viewport to it (ListNav follow-on-build).
const auto currentLang = static_cast<uint8_t>(I18N.getLanguage());
const auto* begin = std::begin(SORTED_LANGUAGE_INDICES);
const auto* end = std::end(SORTED_LANGUAGE_INDICES);
const auto* it = std::find(begin, end, currentLang);
nav.selected = (it != end) ? static_cast<int>(std::distance(begin, it)) : 0;
// Built once here rather than every buildScreen() call: labels are static,
// and the "Selected" marker can't go stale mid-visit since activateIndex()
// finishes the activity immediately on selection.
for (int i = 0; i < totalItems; ++i) {
fui::ListItem item;
item.label = I18N.getLanguageName(static_cast<Language>(SORTED_LANGUAGE_INDICES[i]));
if (SORTED_LANGUAGE_INDICES[i] == currentLang) {
item.value = tr(STR_SELECTED);
}
item.actionValue = static_cast<int16_t>(i);
rowItems[i] = item;
}
}
const char* LanguageSelectActivity::headerTitle() const { return tr(STR_LANGUAGE); }
void LanguageSelectActivity::activateIndex(const int index) {
// The activated row leaves this screen; a lingering flash would gray an
// unrelated element on the next render.
app.clearTapFlash();
nav.selected = index;
const uint8_t langIndex = SORTED_LANGUAGE_INDICES[index];
{
RenderLock lock(*this);
I18N.setLanguage(static_cast<Language>(langIndex));
}
SETTINGS.language = langIndex;
SETTINGS.saveToFile();
// Return to previous page
finish();
}
void LanguageSelectActivity::buildScreen(UiScreen& screen) {
const auto& metrics = UITheme::getInstance().getMetrics();
const Rect safe = UITheme::getInstance().getScreenSafeArea(renderer, true, false);
// Content: the safe area minus the header band GUI.drawHeader paints.
screen.setContentMargin(fui::Insets{static_cast<int16_t>(safe.y + metrics.topPadding + metrics.headerHeight),
static_cast<int16_t>(renderer.getScreenWidth() - (safe.x + safe.width)),
static_cast<int16_t>(renderer.getScreenHeight() - (safe.y + safe.height)),
static_cast<int16_t>(safe.x)});
screen.spacer(static_cast<int16_t>(metrics.verticalSpacing));
// rowItems was built once in onEnter() and is reused here on every repaint.
fui::ListProps props;
props.items = rowItems;
props.count = static_cast<uint16_t>(totalItems);
props.action = ACTION_ROW;
props.inputMask = fui::InputTouch; // physical buttons stay in loop()
syncListViewport(screen, props);
screen.list(props);
}