From 68f175ecedc495bc7ffc2318b13e780146758052 Mon Sep 17 00:00:00 2001 From: jpirnay Date: Thu, 9 Apr 2026 16:50:13 +0200 Subject: [PATCH] Restructure ClockSettings --- .../settings/ClockSettingsActivity.cpp | 87 ++++++++++++------- .../settings/ClockSettingsActivity.h | 23 ++++- 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/activities/settings/ClockSettingsActivity.cpp b/src/activities/settings/ClockSettingsActivity.cpp index b79a7bc8..95b533cc 100644 --- a/src/activities/settings/ClockSettingsActivity.cpp +++ b/src/activities/settings/ClockSettingsActivity.cpp @@ -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::buildMenuItems() { + std::vector 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 ClockSettingsActivity::buildSelectablePredicate() const { + return [this](int index) { + return index >= 0 && index < static_cast(menuItems.size()) && !menuItems[index].isSeparator; + }; +} + void ClockSettingsActivity::onEnter() { Activity::onEnter(); - selectedIndex = 0; + buttonNavigator.setSelectablePredicate(buildSelectablePredicate(), static_cast(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(renderer, mappedInput), resultHandler); - } else if (selectedIndex == 4) { + } else if (action == Action::DETECT_TIMEZONE) { auto resultHandler = [](const ActivityResult&) { SETTINGS.saveToFile(); }; startActivityForResult(std::make_unique(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(SETTINGS.timeZone); - if (tzIndex < (sizeof(timeZoneNames) / sizeof(timeZoneNames[0]))) { - return std::string(I18N.get(timeZoneNames[tzIndex])); + renderer, Rect{0, contentTop, pageWidth, contentHeight}, static_cast(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(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); diff --git a/src/activities/settings/ClockSettingsActivity.h b/src/activities/settings/ClockSettingsActivity.h index e1f491bc..974259e4 100644 --- a/src/activities/settings/ClockSettingsActivity.h +++ b/src/activities/settings/ClockSettingsActivity.h @@ -1,18 +1,39 @@ #pragma once +#include +#include + #include #include "activities/Activity.h" #include "util/ButtonNavigator.h" 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; int selectedIndex = 0; + const std::vector menuItems; + static std::vector buildMenuItems(); void handleSelection(); + std::function buildSelectablePredicate() const; public: explicit ClockSettingsActivity(GfxRenderer& renderer, MappedInputManager& mappedInput) - : Activity("ClockSettings", renderer, mappedInput) {} + : Activity("ClockSettings", renderer, mappedInput), menuItems(buildMenuItems()) {} void onEnter() override; void onExit() override; void loop() override;