Restructure ClockSettings

This commit is contained in:
jpirnay
2026-04-09 16:50:13 +02:00
parent 5f4c5550e8
commit 68f175eced
2 changed files with 80 additions and 30 deletions
@@ -12,10 +12,6 @@
#include "fontIds.h"
namespace {
constexpr int MENU_ITEMS = 5;
const StrId menuNames[MENU_ITEMS] = {StrId::STR_USE_CLOCK, StrId::STR_CLOCK_FORMAT, StrId::STR_TIMEZONE,
StrId::STR_SYNC_TIME, StrId::STR_DETECT_TIMEZONE};
const StrId timeZoneNames[CrossPointSettings::TIMEZONE_COUNT] = {
StrId::STR_TZ_UTC, StrId::STR_TZ_CET, StrId::STR_TZ_EET, StrId::STR_TZ_MSK,
StrId::STR_TZ_UTC_PLUS4, StrId::STR_TZ_IST, StrId::STR_TZ_UTC_PLUS7, StrId::STR_TZ_UTC_PLUS8,
@@ -23,9 +19,34 @@ const StrId timeZoneNames[CrossPointSettings::TIMEZONE_COUNT] = {
StrId::STR_TZ_EST, StrId::STR_TZ_CST, StrId::STR_TZ_MST, StrId::STR_TZ_PST};
} // namespace
std::vector<ClockSettingsActivity::MenuItem> ClockSettingsActivity::buildMenuItems() {
std::vector<MenuItem> items;
items.reserve(7);
// Settings
items.push_back({Action::NONE, StrId::STR_SETTINGS_TITLE, true});
items.push_back({Action::USE_CLOCK, StrId::STR_USE_CLOCK});
items.push_back({Action::CLOCK_FORMAT, StrId::STR_CLOCK_FORMAT});
items.push_back({Action::TIMEZONE, StrId::STR_TIMEZONE});
// Tools
items.push_back({Action::NONE, StrId::STR_READER_TOOLS, true});
items.push_back({Action::DETECT_TIMEZONE, StrId::STR_DETECT_TIMEZONE});
items.push_back({Action::SYNC_TIME, StrId::STR_SYNC_TIME});
return items;
}
std::function<bool(int)> ClockSettingsActivity::buildSelectablePredicate() const {
return [this](int index) {
return index >= 0 && index < static_cast<int>(menuItems.size()) && !menuItems[index].isSeparator;
};
}
void ClockSettingsActivity::onEnter() {
Activity::onEnter();
selectedIndex = 0;
buttonNavigator.setSelectablePredicate(buildSelectablePredicate(), static_cast<int>(menuItems.size()));
if (!buildSelectablePredicate()(selectedIndex)) {
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
}
requestUpdate();
}
@@ -44,44 +65,45 @@ void ClockSettingsActivity::loop() {
}
buttonNavigator.onNextRelease([this] {
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, MENU_ITEMS);
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
requestUpdate();
});
buttonNavigator.onPreviousRelease([this] {
selectedIndex = ButtonNavigator::previousIndex(selectedIndex, MENU_ITEMS);
selectedIndex = buttonNavigator.previousIndex(selectedIndex);
requestUpdate();
});
buttonNavigator.onNextContinuous([this] {
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, MENU_ITEMS);
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
requestUpdate();
});
buttonNavigator.onPreviousContinuous([this] {
selectedIndex = ButtonNavigator::previousIndex(selectedIndex, MENU_ITEMS);
selectedIndex = buttonNavigator.previousIndex(selectedIndex);
requestUpdate();
});
}
void ClockSettingsActivity::handleSelection() {
if (selectedIndex == 0) {
const auto action = menuItems[selectedIndex].action;
if (action == Action::USE_CLOCK) {
SETTINGS.useClock = (SETTINGS.useClock + 1) % 2;
if (!SETTINGS.useClock) {
SETTINGS.statusBarClock = 0;
}
SETTINGS.saveToFile();
} else if (selectedIndex == 1) {
} else if (action == Action::CLOCK_FORMAT) {
SETTINGS.clockFormat12h = (SETTINGS.clockFormat12h + 1) % 2;
SETTINGS.saveToFile();
} else if (selectedIndex == 2) {
} else if (action == Action::TIMEZONE) {
SETTINGS.timeZone = (SETTINGS.timeZone + 1) % CrossPointSettings::TIMEZONE_COUNT;
HalClock::applyTimezone(SETTINGS.timeZone);
SETTINGS.saveToFile();
} else if (selectedIndex == 3) {
} else if (action == Action::SYNC_TIME) {
auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); };
startActivityForResult(std::make_unique<SyncTimeActivity>(renderer, mappedInput), resultHandler);
} else if (selectedIndex == 4) {
} else if (action == Action::DETECT_TIMEZONE) {
auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); };
startActivityForResult(std::make_unique<DetectTimezoneActivity>(renderer, mappedInput), resultHandler);
}
@@ -102,23 +124,30 @@ void ClockSettingsActivity::render(RenderLock&&) {
const int contentHeight = pageHeight - contentTop - metrics.buttonHintsHeight - metrics.verticalSpacing * 2;
GUI.drawList(
renderer, Rect{0, contentTop, pageWidth, contentHeight}, MENU_ITEMS, selectedIndex,
[](int index) { return std::string(I18N.get(menuNames[index])); }, nullptr, nullptr,
[](int index) {
if (index == 0) {
return std::string(SETTINGS.useClock ? tr(STR_STATE_ON) : tr(STR_STATE_OFF));
}
if (index == 1) {
return std::string(SETTINGS.clockFormat12h ? tr(STR_12H) : tr(STR_24H));
}
if (index == 2) {
const auto tzIndex = static_cast<size_t>(SETTINGS.timeZone);
if (tzIndex < (sizeof(timeZoneNames) / sizeof(timeZoneNames[0]))) {
return std::string(I18N.get(timeZoneNames[tzIndex]));
renderer, Rect{0, contentTop, pageWidth, contentHeight}, static_cast<int>(menuItems.size()),
selectedIndex,
[this](int index) {
const auto title = I18N.get(menuItems[index].labelId);
return menuItems[index].isSeparator ? UITheme::makeSeparatorTitle(title) : title;
},
nullptr, nullptr,
[this](int index) {
const auto action = menuItems[index].action;
switch (action) {
case Action::USE_CLOCK:
return std::string(SETTINGS.useClock ? tr(STR_STATE_ON) : tr(STR_STATE_OFF));
case Action::CLOCK_FORMAT:
return std::string(SETTINGS.clockFormat12h ? tr(STR_12H) : tr(STR_24H));
case Action::TIMEZONE: {
const auto tzIndex = static_cast<size_t>(SETTINGS.timeZone);
if (tzIndex < (sizeof(timeZoneNames) / sizeof(timeZoneNames[0]))) {
return std::string(I18N.get(timeZoneNames[tzIndex]));
}
return std::string(tr(STR_TZ_UTC));
}
return std::string(tr(STR_TZ_UTC));
default:
return std::string("");
}
return std::string("");
},
true);