Add PageIndicator widget to reduce code duplication (#1218)

* Move PageIndicator widget to its own files to reduce code duplication
* Use uint8_t in PageIndicator
This commit is contained in:
Riku Isokoski 2022-07-05 08:41:09 +03:00 committed by GitHub
parent c0770cde8a
commit ec8a845052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 70 additions and 90 deletions

View File

@ -405,6 +405,7 @@ list(APPEND SOURCE_FILES
displayapp/screens/Styles.cpp displayapp/screens/Styles.cpp
displayapp/Colors.cpp displayapp/Colors.cpp
displayapp/widgets/Counter.cpp displayapp/widgets/Counter.cpp
displayapp/widgets/PageIndicator.cpp
## Settings ## Settings
displayapp/screens/settings/QuickSettings.cpp displayapp/screens/settings/QuickSettings.cpp
@ -609,6 +610,7 @@ set(INCLUDE_FILES
displayapp/screens/Alarm.h displayapp/screens/Alarm.h
displayapp/Colors.h displayapp/Colors.h
displayapp/widgets/Counter.h displayapp/widgets/Counter.h
displayapp/widgets/PageIndicator.h
drivers/St7789.h drivers/St7789.h
drivers/SpiNorFlash.h drivers/SpiNorFlash.h
drivers/SpiMaster.h drivers/SpiMaster.h

View File

@ -3,34 +3,9 @@
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
Label::Label(uint8_t screenID, uint8_t numScreens, Pinetime::Applications::DisplayApp* app, lv_obj_t* labelText) 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) { 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(), 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);
}
} }
Label::~Label() { Label::~Label() {

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "displayapp/screens/Screen.h" #include "displayapp/screens/Screen.h"
#include "displayapp/widgets/PageIndicator.h"
#include <lvgl/lvgl.h> #include <lvgl/lvgl.h>
namespace Pinetime { namespace Pinetime {
@ -14,10 +15,7 @@ namespace Pinetime {
private: private:
lv_obj_t* labelText = nullptr; lv_obj_t* labelText = nullptr;
lv_point_t pageIndicatorBasePoints[2]; Widgets::PageIndicator pageIndicator;
lv_point_t pageIndicatorPoints[2];
lv_obj_t* pageIndicatorBase;
lv_obj_t* pageIndicator;
}; };
} }
} }

View File

@ -16,37 +16,14 @@ List::List(uint8_t screenID,
DisplayApp* app, DisplayApp* app,
Controllers::Settings& settingsController, Controllers::Settings& settingsController,
std::array<Applications, MAXLISTITEMS>& applications) std::array<Applications, MAXLISTITEMS>& applications)
: Screen(app), settingsController {settingsController} { : Screen(app), settingsController {settingsController}, pageIndicator(screenID, numScreens) {
// Set the background to Black // 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)); 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); settingsController.SetSettingsMenu(screenID);
if (numScreens > 1) { 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(), 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);
}
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr); lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);

View File

@ -4,6 +4,7 @@
#include <cstdint> #include <cstdint>
#include <array> #include <array>
#include "displayapp/screens/Screen.h" #include "displayapp/screens/Screen.h"
#include "displayapp/widgets/PageIndicator.h"
#include "displayapp/Apps.h" #include "displayapp/Apps.h"
#include "components/settings/Settings.h" #include "components/settings/Settings.h"
@ -35,10 +36,7 @@ namespace Pinetime {
lv_obj_t* itemApps[MAXLISTITEMS]; lv_obj_t* itemApps[MAXLISTITEMS];
lv_point_t pageIndicatorBasePoints[2]; Widgets::PageIndicator pageIndicator;
lv_point_t pageIndicatorPoints[2];
lv_obj_t* pageIndicatorBase;
lv_obj_t* pageIndicator;
}; };
} }
} }

View File

@ -29,7 +29,7 @@ Tile::Tile(uint8_t screenID,
Pinetime::Controllers::Battery& batteryController, Pinetime::Controllers::Battery& batteryController,
Controllers::DateTime& dateTimeController, Controllers::DateTime& dateTimeController,
std::array<Applications, 6>& applications) std::array<Applications, 6>& applications)
: Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController} { : Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController}, pageIndicator(screenID, numScreens) {
settingsController.SetAppMenu(screenID); settingsController.SetAppMenu(screenID);
@ -42,30 +42,7 @@ Tile::Tile(uint8_t screenID,
batteryIcon.Create(lv_scr_act()); batteryIcon.Create(lv_scr_act());
lv_obj_align(batteryIcon.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, -8, 0); lv_obj_align(batteryIcon.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, -8, 0);
if (numScreens > 1) { 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 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);
}
uint8_t btIndex = 0; uint8_t btIndex = 0;
for (uint8_t i = 0; i < 6; i++) { for (uint8_t i = 0; i < 6; i++) {

View File

@ -9,7 +9,8 @@
#include "components/settings/Settings.h" #include "components/settings/Settings.h"
#include "components/datetime/DateTimeController.h" #include "components/datetime/DateTimeController.h"
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
#include <displayapp/screens/BatteryIcon.h> #include "displayapp/screens/BatteryIcon.h"
#include "displayapp/widgets/PageIndicator.h"
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
@ -41,12 +42,10 @@ namespace Pinetime {
lv_task_t* taskUpdate; lv_task_t* taskUpdate;
lv_obj_t* label_time; 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; lv_obj_t* btnm1;
Widgets::PageIndicator pageIndicator;
BatteryIcon batteryIcon; BatteryIcon batteryIcon;
const char* btnmMap[8]; const char* btnmMap[8];

View File

@ -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);
}

View File

@ -0,0 +1,23 @@
#pragma once
#include <lvgl/lvgl.h>
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;
};
}
}
}