From 22f6d4a40b6715b436f5eb3bf8524fa955eccd20 Mon Sep 17 00:00:00 2001 From: Jean-François Milants Date: Sun, 24 Dec 2023 11:44:37 +0100 Subject: Watch face selection using CMake The list of watch face to build into the firmware is now set by CMake (-DENABLE_WATCHFACES). Fix SettingWatchFace : convert to index to/from WatchFace when needed. --- .../screens/settings/SettingWatchFace.cpp | 42 +++++++++++++++++++--- src/displayapp/screens/settings/SettingWatchFace.h | 10 ++++-- 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'src/displayapp/screens') diff --git a/src/displayapp/screens/settings/SettingWatchFace.cpp b/src/displayapp/screens/settings/SettingWatchFace.cpp index f052573c..e01e9f84 100644 --- a/src/displayapp/screens/settings/SettingWatchFace.cpp +++ b/src/displayapp/screens/settings/SettingWatchFace.cpp @@ -9,6 +9,37 @@ using namespace Pinetime::Applications::Screens; constexpr const char* SettingWatchFace::title; constexpr const char* SettingWatchFace::symbol; +namespace { + uint32_t IndexOf(const std::array& watchfaces, + Pinetime::Applications::WatchFace watchface) { + size_t index = 0; + auto found = std::find_if(watchfaces.begin(), + watchfaces.end(), + [&index, &watchface](const Pinetime::Applications::Screens::SettingWatchFace::Item& item) { + const bool result = item.watchface == watchface; + if (!result) { + index++; + } + return result; + }); + if (found == watchfaces.end()) { + index = 0; + } + + return index; + } + + Pinetime::Applications::WatchFace IndexToWatchFace(const std::array& watchfaces, + size_t index) { + if (index >= watchfaces.size()) { + return watchfaces[0].watchface; + } + return watchfaces[index].watchface; + } +} + auto SettingWatchFace::CreateScreenList() const { std::array()>, nScreens> screens; for (size_t i = 0; i < screens.size(); i++) { @@ -20,7 +51,7 @@ auto SettingWatchFace::CreateScreenList() const { } SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app, - std::array&& watchfaceItems, + std::array&& watchfaceItems, Pinetime::Controllers::Settings& settingsController, Pinetime::Controllers::FS& filesystem) : app {app}, @@ -44,7 +75,8 @@ std::unique_ptr SettingWatchFace::CreateScreen(unsigned int screenNum) c if (i + (screenNum * settingsPerScreen) >= watchfaceItems.size()) { watchfacesOnThisScreen[i] = {"", false}; } else { - watchfacesOnThisScreen[i] = watchfaceItems[i + (screenNum * settingsPerScreen)]; + auto& item = watchfaceItems[i + (screenNum * settingsPerScreen)]; + watchfacesOnThisScreen[i] = Screens::CheckboxList::Item {item.name, item.enabled}; } } @@ -53,9 +85,9 @@ std::unique_ptr SettingWatchFace::CreateScreen(unsigned int screenNum) c nScreens, title, symbol, - static_cast(settingsController.GetWatchFace()), - [&settings = settingsController](uint32_t index) { - settings.SetWatchFace(static_cast(index)); + static_cast(IndexOf(watchfaceItems, settingsController.GetWatchFace())), + [this, &settings = settingsController](uint32_t index) { + settings.SetWatchFace(IndexToWatchFace(watchfaceItems, index)); settings.SaveSettings(); }, watchfacesOnThisScreen); diff --git a/src/displayapp/screens/settings/SettingWatchFace.h b/src/displayapp/screens/settings/SettingWatchFace.h index 66559c73..4c75b0ab 100644 --- a/src/displayapp/screens/settings/SettingWatchFace.h +++ b/src/displayapp/screens/settings/SettingWatchFace.h @@ -19,8 +19,14 @@ namespace Pinetime { class SettingWatchFace : public Screen { public: + struct Item { + const char* name; + WatchFace watchface; + bool enabled; + }; + SettingWatchFace(DisplayApp* app, - std::array&& watchfaceItems, + std::array&& watchfaceItems, Pinetime::Controllers::Settings& settingsController, Pinetime::Controllers::FS& filesystem); ~SettingWatchFace() override; @@ -33,7 +39,7 @@ namespace Pinetime { std::unique_ptr CreateScreen(unsigned int screenNum) const; static constexpr int settingsPerScreen = 4; - std::array watchfaceItems; + std::array watchfaceItems; static constexpr int nScreens = UserWatchFaceTypes::Count > 0 ? (UserWatchFaceTypes ::Count - 1) / settingsPerScreen + 1 : 1; Controllers::Settings& settingsController; -- cgit v1.2.3-70-g09d2