From ec8a84505277f2dbaaa957947d432ea03758f9bf Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Tue, 5 Jul 2022 08:41:09 +0300 Subject: [PATCH] Add PageIndicator widget to reduce code duplication (#1218) * Move PageIndicator widget to its own files to reduce code duplication * Use uint8_t in PageIndicator --- src/CMakeLists.txt | 2 ++ src/displayapp/screens/Label.cpp | 29 ++-------------------- src/displayapp/screens/Label.h | 6 ++--- src/displayapp/screens/List.cpp | 27 ++------------------- src/displayapp/screens/List.h | 6 ++--- src/displayapp/screens/Tile.cpp | 27 ++------------------- src/displayapp/screens/Tile.h | 9 +++---- src/displayapp/widgets/PageIndicator.cpp | 31 ++++++++++++++++++++++++ src/displayapp/widgets/PageIndicator.h | 23 ++++++++++++++++++ 9 files changed, 70 insertions(+), 90 deletions(-) create mode 100644 src/displayapp/widgets/PageIndicator.cpp create mode 100644 src/displayapp/widgets/PageIndicator.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b7503fd..9de1ca11 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -405,6 +405,7 @@ list(APPEND SOURCE_FILES displayapp/screens/Styles.cpp displayapp/Colors.cpp displayapp/widgets/Counter.cpp + displayapp/widgets/PageIndicator.cpp ## Settings displayapp/screens/settings/QuickSettings.cpp @@ -609,6 +610,7 @@ set(INCLUDE_FILES displayapp/screens/Alarm.h displayapp/Colors.h displayapp/widgets/Counter.h + displayapp/widgets/PageIndicator.h drivers/St7789.h drivers/SpiNorFlash.h drivers/SpiMaster.h diff --git a/src/displayapp/screens/Label.cpp b/src/displayapp/screens/Label.cpp index 4486d6fd..d5a09be9 100644 --- a/src/displayapp/screens/Label.cpp +++ b/src/displayapp/screens/Label.cpp @@ -3,34 +3,9 @@ using namespace Pinetime::Applications::Screens; Label::Label(uint8_t screenID, uint8_t numScreens, Pinetime::Applications::DisplayApp* app, lv_obj_t* labelText) - : Screen(app), labelText {labelText} { + : Screen(app), labelText {labelText}, pageIndicator(screenID, numScreens) { - if (numScreens > 1) { - pageIndicatorBasePoints[0].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[0].y = 0; - pageIndicatorBasePoints[1].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[1].y = LV_VER_RES; - - pageIndicatorBase = lv_line_create(lv_scr_act(), NULL); - lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111)); - lv_obj_set_style_local_line_rounded(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, true); - lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2); - - uint16_t indicatorSize = LV_VER_RES / numScreens; - uint16_t indicatorPos = indicatorSize * screenID; - - pageIndicatorPoints[0].x = LV_HOR_RES - 1; - pageIndicatorPoints[0].y = indicatorPos; - pageIndicatorPoints[1].x = LV_HOR_RES - 1; - pageIndicatorPoints[1].y = indicatorPos + indicatorSize; - - pageIndicator = lv_line_create(lv_scr_act(), NULL); - lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); - lv_obj_set_style_local_line_rounded(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, true); - lv_line_set_points(pageIndicator, pageIndicatorPoints, 2); - } + pageIndicator.Create(); } Label::~Label() { diff --git a/src/displayapp/screens/Label.h b/src/displayapp/screens/Label.h index 3fe5111f..acd8b377 100644 --- a/src/displayapp/screens/Label.h +++ b/src/displayapp/screens/Label.h @@ -1,6 +1,7 @@ #pragma once #include "displayapp/screens/Screen.h" +#include "displayapp/widgets/PageIndicator.h" #include namespace Pinetime { @@ -14,10 +15,7 @@ namespace Pinetime { private: lv_obj_t* labelText = nullptr; - lv_point_t pageIndicatorBasePoints[2]; - lv_point_t pageIndicatorPoints[2]; - lv_obj_t* pageIndicatorBase; - lv_obj_t* pageIndicator; + Widgets::PageIndicator pageIndicator; }; } } diff --git a/src/displayapp/screens/List.cpp b/src/displayapp/screens/List.cpp index 0bc7da80..f44825c7 100644 --- a/src/displayapp/screens/List.cpp +++ b/src/displayapp/screens/List.cpp @@ -16,37 +16,14 @@ List::List(uint8_t screenID, DisplayApp* app, Controllers::Settings& settingsController, std::array& applications) - : Screen(app), settingsController {settingsController} { + : Screen(app), settingsController {settingsController}, pageIndicator(screenID, numScreens) { // Set the background to Black lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(0, 0, 0)); settingsController.SetSettingsMenu(screenID); - if (numScreens > 1) { - pageIndicatorBasePoints[0].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[0].y = 0; - pageIndicatorBasePoints[1].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[1].y = LV_VER_RES; - - pageIndicatorBase = lv_line_create(lv_scr_act(), NULL); - lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111)); - lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2); - - const uint16_t indicatorSize = LV_VER_RES / numScreens; - const uint16_t indicatorPos = indicatorSize * screenID; - - pageIndicatorPoints[0].x = LV_HOR_RES - 1; - pageIndicatorPoints[0].y = indicatorPos; - pageIndicatorPoints[1].x = LV_HOR_RES - 1; - pageIndicatorPoints[1].y = indicatorPos + indicatorSize; - - pageIndicator = lv_line_create(lv_scr_act(), NULL); - lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); - lv_line_set_points(pageIndicator, pageIndicatorPoints, 2); - } + pageIndicator.Create(); lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr); diff --git a/src/displayapp/screens/List.h b/src/displayapp/screens/List.h index 88ef28ea..b5bc032c 100644 --- a/src/displayapp/screens/List.h +++ b/src/displayapp/screens/List.h @@ -4,6 +4,7 @@ #include #include #include "displayapp/screens/Screen.h" +#include "displayapp/widgets/PageIndicator.h" #include "displayapp/Apps.h" #include "components/settings/Settings.h" @@ -35,10 +36,7 @@ namespace Pinetime { lv_obj_t* itemApps[MAXLISTITEMS]; - lv_point_t pageIndicatorBasePoints[2]; - lv_point_t pageIndicatorPoints[2]; - lv_obj_t* pageIndicatorBase; - lv_obj_t* pageIndicator; + Widgets::PageIndicator pageIndicator; }; } } diff --git a/src/displayapp/screens/Tile.cpp b/src/displayapp/screens/Tile.cpp index cce1d2ed..c633e17b 100644 --- a/src/displayapp/screens/Tile.cpp +++ b/src/displayapp/screens/Tile.cpp @@ -29,7 +29,7 @@ Tile::Tile(uint8_t screenID, Pinetime::Controllers::Battery& batteryController, Controllers::DateTime& dateTimeController, std::array& applications) - : Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController} { + : Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController}, pageIndicator(screenID, numScreens) { settingsController.SetAppMenu(screenID); @@ -42,30 +42,7 @@ Tile::Tile(uint8_t screenID, batteryIcon.Create(lv_scr_act()); lv_obj_align(batteryIcon.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, -8, 0); - if (numScreens > 1) { - pageIndicatorBasePoints[0].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[0].y = 0; - pageIndicatorBasePoints[1].x = LV_HOR_RES - 1; - pageIndicatorBasePoints[1].y = LV_VER_RES; - - pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr); - lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111)); - lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2); - - const uint16_t indicatorSize = LV_VER_RES / numScreens; - const uint16_t indicatorPos = indicatorSize * screenID; - - pageIndicatorPoints[0].x = LV_HOR_RES - 1; - pageIndicatorPoints[0].y = indicatorPos; - pageIndicatorPoints[1].x = LV_HOR_RES - 1; - pageIndicatorPoints[1].y = indicatorPos + indicatorSize; - - pageIndicator = lv_line_create(lv_scr_act(), nullptr); - lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); - lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); - lv_line_set_points(pageIndicator, pageIndicatorPoints, 2); - } + pageIndicator.Create(); uint8_t btIndex = 0; for (uint8_t i = 0; i < 6; i++) { diff --git a/src/displayapp/screens/Tile.h b/src/displayapp/screens/Tile.h index 48747144..ff121376 100644 --- a/src/displayapp/screens/Tile.h +++ b/src/displayapp/screens/Tile.h @@ -9,7 +9,8 @@ #include "components/settings/Settings.h" #include "components/datetime/DateTimeController.h" #include "components/battery/BatteryController.h" -#include +#include "displayapp/screens/BatteryIcon.h" +#include "displayapp/widgets/PageIndicator.h" namespace Pinetime { namespace Applications { @@ -41,12 +42,10 @@ namespace Pinetime { lv_task_t* taskUpdate; lv_obj_t* label_time; - lv_point_t pageIndicatorBasePoints[2]; - lv_point_t pageIndicatorPoints[2]; - lv_obj_t* pageIndicatorBase; - lv_obj_t* pageIndicator; lv_obj_t* btnm1; + Widgets::PageIndicator pageIndicator; + BatteryIcon batteryIcon; const char* btnmMap[8]; diff --git a/src/displayapp/widgets/PageIndicator.cpp b/src/displayapp/widgets/PageIndicator.cpp new file mode 100644 index 00000000..06058beb --- /dev/null +++ b/src/displayapp/widgets/PageIndicator.cpp @@ -0,0 +1,31 @@ +#include "displayapp/widgets/PageIndicator.h" + +using namespace Pinetime::Applications::Widgets; + +PageIndicator::PageIndicator(uint8_t nCurrentScreen, uint8_t nScreens) : nCurrentScreen {nCurrentScreen}, nScreens {nScreens} { +} + +void PageIndicator::Create() { + pageIndicatorBasePoints[0].x = LV_HOR_RES - 1; + pageIndicatorBasePoints[0].y = 0; + pageIndicatorBasePoints[1].x = LV_HOR_RES - 1; + pageIndicatorBasePoints[1].y = LV_VER_RES; + + pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); + lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111)); + lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2); + + const int16_t indicatorSize = LV_VER_RES / nScreens; + const int16_t indicatorPos = indicatorSize * nCurrentScreen; + + pageIndicatorPoints[0].x = LV_HOR_RES - 1; + pageIndicatorPoints[0].y = indicatorPos; + pageIndicatorPoints[1].x = LV_HOR_RES - 1; + pageIndicatorPoints[1].y = indicatorPos + indicatorSize; + + pageIndicator = lv_line_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); + lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); + lv_line_set_points(pageIndicator, pageIndicatorPoints, 2); +} diff --git a/src/displayapp/widgets/PageIndicator.h b/src/displayapp/widgets/PageIndicator.h new file mode 100644 index 00000000..8484735e --- /dev/null +++ b/src/displayapp/widgets/PageIndicator.h @@ -0,0 +1,23 @@ +#pragma once +#include + +namespace Pinetime { + namespace Applications { + namespace Widgets { + class PageIndicator { + public: + PageIndicator(uint8_t nCurrentScreen, uint8_t nScreens); + void Create(); + + private: + uint8_t nCurrentScreen; + uint8_t nScreens; + + lv_point_t pageIndicatorBasePoints[2]; + lv_point_t pageIndicatorPoints[2]; + lv_obj_t* pageIndicatorBase; + lv_obj_t* pageIndicator; + }; + } + } +}