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" #include "fontIds.h"
namespace { 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] = { 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, 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, 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}; StrId::STR_TZ_EST, StrId::STR_TZ_CST, StrId::STR_TZ_MST, StrId::STR_TZ_PST};
} // namespace } // 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() { void ClockSettingsActivity::onEnter() {
Activity::onEnter(); Activity::onEnter();
selectedIndex = 0; buttonNavigator.setSelectablePredicate(buildSelectablePredicate(), static_cast<int>(menuItems.size()));
if (!buildSelectablePredicate()(selectedIndex)) {
selectedIndex = buttonNavigator.nextIndex(selectedIndex);
}
requestUpdate(); requestUpdate();
} }
@@ -44,44 +65,45 @@ void ClockSettingsActivity::loop() {
} }
buttonNavigator.onNextRelease([this] { buttonNavigator.onNextRelease([this] {
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, MENU_ITEMS); selectedIndex = buttonNavigator.nextIndex(selectedIndex);
requestUpdate(); requestUpdate();
}); });
buttonNavigator.onPreviousRelease([this] { buttonNavigator.onPreviousRelease([this] {
selectedIndex = ButtonNavigator::previousIndex(selectedIndex, MENU_ITEMS); selectedIndex = buttonNavigator.previousIndex(selectedIndex);
requestUpdate(); requestUpdate();
}); });
buttonNavigator.onNextContinuous([this] { buttonNavigator.onNextContinuous([this] {
selectedIndex = ButtonNavigator::nextIndex(selectedIndex, MENU_ITEMS); selectedIndex = buttonNavigator.nextIndex(selectedIndex);
requestUpdate(); requestUpdate();
}); });
buttonNavigator.onPreviousContinuous([this] { buttonNavigator.onPreviousContinuous([this] {
selectedIndex = ButtonNavigator::previousIndex(selectedIndex, MENU_ITEMS); selectedIndex = buttonNavigator.previousIndex(selectedIndex);
requestUpdate(); requestUpdate();
}); });
} }
void ClockSettingsActivity::handleSelection() { void ClockSettingsActivity::handleSelection() {
if (selectedIndex == 0) { const auto action = menuItems[selectedIndex].action;
if (action == Action::USE_CLOCK) {
SETTINGS.useClock = (SETTINGS.useClock + 1) % 2; SETTINGS.useClock = (SETTINGS.useClock + 1) % 2;
if (!SETTINGS.useClock) { if (!SETTINGS.useClock) {
SETTINGS.statusBarClock = 0; SETTINGS.statusBarClock = 0;
} }
SETTINGS.saveToFile(); SETTINGS.saveToFile();
} else if (selectedIndex == 1) { } else if (action == Action::CLOCK_FORMAT) {
SETTINGS.clockFormat12h = (SETTINGS.clockFormat12h + 1) % 2; SETTINGS.clockFormat12h = (SETTINGS.clockFormat12h + 1) % 2;
SETTINGS.saveToFile(); SETTINGS.saveToFile();
} else if (selectedIndex == 2) { } else if (action == Action::TIMEZONE) {
SETTINGS.timeZone = (SETTINGS.timeZone + 1) % CrossPointSettings::TIMEZONE_COUNT; SETTINGS.timeZone = (SETTINGS.timeZone + 1) % CrossPointSettings::TIMEZONE_COUNT;
HalClock::applyTimezone(SETTINGS.timeZone); HalClock::applyTimezone(SETTINGS.timeZone);
SETTINGS.saveToFile(); SETTINGS.saveToFile();
} else if (selectedIndex == 3) { } else if (action == Action::SYNC_TIME) {
auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); }; auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); };
startActivityForResult(std::make_unique<SyncTimeActivity>(renderer, mappedInput), resultHandler); startActivityForResult(std::make_unique<SyncTimeActivity>(renderer, mappedInput), resultHandler);
} else if (selectedIndex == 4) { } else if (action == Action::DETECT_TIMEZONE) {
auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); }; auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); };
startActivityForResult(std::make_unique<DetectTimezoneActivity>(renderer, mappedInput), resultHandler); 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; const int contentHeight = pageHeight - contentTop - metrics.buttonHintsHeight - metrics.verticalSpacing * 2;
GUI.drawList( GUI.drawList(
renderer, Rect{0, contentTop, pageWidth, contentHeight}, MENU_ITEMS, selectedIndex, renderer, Rect{0, contentTop, pageWidth, contentHeight}, static_cast<int>(menuItems.size()),
[](int index) { return std::string(I18N.get(menuNames[index])); }, nullptr, nullptr, selectedIndex,
[](int index) { [this](int index) {
if (index == 0) { const auto title = I18N.get(menuItems[index].labelId);
return std::string(SETTINGS.useClock ? tr(STR_STATE_ON) : tr(STR_STATE_OFF)); return menuItems[index].isSeparator ? UITheme::makeSeparatorTitle(title) : title;
} },
if (index == 1) { nullptr, nullptr,
return std::string(SETTINGS.clockFormat12h ? tr(STR_12H) : tr(STR_24H)); [this](int index) {
} const auto action = menuItems[index].action;
if (index == 2) { switch (action) {
const auto tzIndex = static_cast<size_t>(SETTINGS.timeZone); case Action::USE_CLOCK:
if (tzIndex < (sizeof(timeZoneNames) / sizeof(timeZoneNames[0]))) { return std::string(SETTINGS.useClock ? tr(STR_STATE_ON) : tr(STR_STATE_OFF));
return std::string(I18N.get(timeZoneNames[tzIndex])); 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); true);
@@ -1,18 +1,39 @@
#pragma once #pragma once
#include <functional>
#include <vector>
#include <I18n.h> #include <I18n.h>
#include "activities/Activity.h" #include "activities/Activity.h"
#include "util/ButtonNavigator.h" #include "util/ButtonNavigator.h"
class ClockSettingsActivity final : public Activity { class ClockSettingsActivity final : public Activity {
enum class Action {
USE_CLOCK,
CLOCK_FORMAT,
TIMEZONE,
SYNC_TIME,
DETECT_TIMEZONE,
NONE
};
struct MenuItem {
Action action;
StrId labelId;
bool isSeparator = false;
};
ButtonNavigator buttonNavigator; ButtonNavigator buttonNavigator;
int selectedIndex = 0; int selectedIndex = 0;
const std::vector<MenuItem> menuItems;
static std::vector<MenuItem> buildMenuItems();
void handleSelection(); void handleSelection();
std::function<bool(int)> buildSelectablePredicate() const;
public: public:
explicit ClockSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput) explicit ClockSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput)
: Activity("ClockSettings", renderer, mappedInput) {} : Activity("ClockSettings", renderer, mappedInput), menuItems(buildMenuItems()) {}
void onEnter() override; void onEnter() override;
void onExit() override; void onExit() override;
void loop() override; void loop() override;