Skip to content

Commit ec8a845

Browse files
authored
Add PageIndicator widget to reduce code duplication (#1218)
* Move PageIndicator widget to its own files to reduce code duplication * Use uint8_t in PageIndicator
1 parent c0770cd commit ec8a845

File tree

9 files changed

+70
-90
lines changed

9 files changed

+70
-90
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ list(APPEND SOURCE_FILES
405405
displayapp/screens/Styles.cpp
406406
displayapp/Colors.cpp
407407
displayapp/widgets/Counter.cpp
408+
displayapp/widgets/PageIndicator.cpp
408409

409410
## Settings
410411
displayapp/screens/settings/QuickSettings.cpp
@@ -609,6 +610,7 @@ set(INCLUDE_FILES
609610
displayapp/screens/Alarm.h
610611
displayapp/Colors.h
611612
displayapp/widgets/Counter.h
613+
displayapp/widgets/PageIndicator.h
612614
drivers/St7789.h
613615
drivers/SpiNorFlash.h
614616
drivers/SpiMaster.h

src/displayapp/screens/Label.cpp

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,9 @@
33
using namespace Pinetime::Applications::Screens;
44

55
Label::Label(uint8_t screenID, uint8_t numScreens, Pinetime::Applications::DisplayApp* app, lv_obj_t* labelText)
6-
: Screen(app), labelText {labelText} {
6+
: Screen(app), labelText {labelText}, pageIndicator(screenID, numScreens) {
77

8-
if (numScreens > 1) {
9-
pageIndicatorBasePoints[0].x = LV_HOR_RES - 1;
10-
pageIndicatorBasePoints[0].y = 0;
11-
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
12-
pageIndicatorBasePoints[1].y = LV_VER_RES;
13-
14-
pageIndicatorBase = lv_line_create(lv_scr_act(), NULL);
15-
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
16-
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
17-
lv_obj_set_style_local_line_rounded(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, true);
18-
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2);
19-
20-
uint16_t indicatorSize = LV_VER_RES / numScreens;
21-
uint16_t indicatorPos = indicatorSize * screenID;
22-
23-
pageIndicatorPoints[0].x = LV_HOR_RES - 1;
24-
pageIndicatorPoints[0].y = indicatorPos;
25-
pageIndicatorPoints[1].x = LV_HOR_RES - 1;
26-
pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
27-
28-
pageIndicator = lv_line_create(lv_scr_act(), NULL);
29-
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
30-
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
31-
lv_obj_set_style_local_line_rounded(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, true);
32-
lv_line_set_points(pageIndicator, pageIndicatorPoints, 2);
33-
}
8+
pageIndicator.Create();
349
}
3510

3611
Label::~Label() {

src/displayapp/screens/Label.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "displayapp/screens/Screen.h"
4+
#include "displayapp/widgets/PageIndicator.h"
45
#include <lvgl/lvgl.h>
56

67
namespace Pinetime {
@@ -14,10 +15,7 @@ namespace Pinetime {
1415

1516
private:
1617
lv_obj_t* labelText = nullptr;
17-
lv_point_t pageIndicatorBasePoints[2];
18-
lv_point_t pageIndicatorPoints[2];
19-
lv_obj_t* pageIndicatorBase;
20-
lv_obj_t* pageIndicator;
18+
Widgets::PageIndicator pageIndicator;
2119
};
2220
}
2321
}

src/displayapp/screens/List.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,14 @@ List::List(uint8_t screenID,
1616
DisplayApp* app,
1717
Controllers::Settings& settingsController,
1818
std::array<Applications, MAXLISTITEMS>& applications)
19-
: Screen(app), settingsController {settingsController} {
19+
: Screen(app), settingsController {settingsController}, pageIndicator(screenID, numScreens) {
2020

2121
// Set the background to Black
2222
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, lv_color_make(0, 0, 0));
2323

2424
settingsController.SetSettingsMenu(screenID);
2525

26-
if (numScreens > 1) {
27-
pageIndicatorBasePoints[0].x = LV_HOR_RES - 1;
28-
pageIndicatorBasePoints[0].y = 0;
29-
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
30-
pageIndicatorBasePoints[1].y = LV_VER_RES;
31-
32-
pageIndicatorBase = lv_line_create(lv_scr_act(), NULL);
33-
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
34-
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
35-
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2);
36-
37-
const uint16_t indicatorSize = LV_VER_RES / numScreens;
38-
const uint16_t indicatorPos = indicatorSize * screenID;
39-
40-
pageIndicatorPoints[0].x = LV_HOR_RES - 1;
41-
pageIndicatorPoints[0].y = indicatorPos;
42-
pageIndicatorPoints[1].x = LV_HOR_RES - 1;
43-
pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
44-
45-
pageIndicator = lv_line_create(lv_scr_act(), NULL);
46-
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
47-
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
48-
lv_line_set_points(pageIndicator, pageIndicatorPoints, 2);
49-
}
26+
pageIndicator.Create();
5027

5128
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);
5229

src/displayapp/screens/List.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdint>
55
#include <array>
66
#include "displayapp/screens/Screen.h"
7+
#include "displayapp/widgets/PageIndicator.h"
78
#include "displayapp/Apps.h"
89
#include "components/settings/Settings.h"
910

@@ -35,10 +36,7 @@ namespace Pinetime {
3536

3637
lv_obj_t* itemApps[MAXLISTITEMS];
3738

38-
lv_point_t pageIndicatorBasePoints[2];
39-
lv_point_t pageIndicatorPoints[2];
40-
lv_obj_t* pageIndicatorBase;
41-
lv_obj_t* pageIndicator;
39+
Widgets::PageIndicator pageIndicator;
4240
};
4341
}
4442
}

src/displayapp/screens/Tile.cpp

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Tile::Tile(uint8_t screenID,
2929
Pinetime::Controllers::Battery& batteryController,
3030
Controllers::DateTime& dateTimeController,
3131
std::array<Applications, 6>& applications)
32-
: Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController} {
32+
: Screen(app), batteryController {batteryController}, dateTimeController {dateTimeController}, pageIndicator(screenID, numScreens) {
3333

3434
settingsController.SetAppMenu(screenID);
3535

@@ -42,30 +42,7 @@ Tile::Tile(uint8_t screenID,
4242
batteryIcon.Create(lv_scr_act());
4343
lv_obj_align(batteryIcon.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, -8, 0);
4444

45-
if (numScreens > 1) {
46-
pageIndicatorBasePoints[0].x = LV_HOR_RES - 1;
47-
pageIndicatorBasePoints[0].y = 0;
48-
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
49-
pageIndicatorBasePoints[1].y = LV_VER_RES;
50-
51-
pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr);
52-
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
53-
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
54-
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2);
55-
56-
const uint16_t indicatorSize = LV_VER_RES / numScreens;
57-
const uint16_t indicatorPos = indicatorSize * screenID;
58-
59-
pageIndicatorPoints[0].x = LV_HOR_RES - 1;
60-
pageIndicatorPoints[0].y = indicatorPos;
61-
pageIndicatorPoints[1].x = LV_HOR_RES - 1;
62-
pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
63-
64-
pageIndicator = lv_line_create(lv_scr_act(), nullptr);
65-
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
66-
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
67-
lv_line_set_points(pageIndicator, pageIndicatorPoints, 2);
68-
}
45+
pageIndicator.Create();
6946

7047
uint8_t btIndex = 0;
7148
for (uint8_t i = 0; i < 6; i++) {

src/displayapp/screens/Tile.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include "components/settings/Settings.h"
1010
#include "components/datetime/DateTimeController.h"
1111
#include "components/battery/BatteryController.h"
12-
#include <displayapp/screens/BatteryIcon.h>
12+
#include "displayapp/screens/BatteryIcon.h"
13+
#include "displayapp/widgets/PageIndicator.h"
1314

1415
namespace Pinetime {
1516
namespace Applications {
@@ -41,12 +42,10 @@ namespace Pinetime {
4142
lv_task_t* taskUpdate;
4243

4344
lv_obj_t* label_time;
44-
lv_point_t pageIndicatorBasePoints[2];
45-
lv_point_t pageIndicatorPoints[2];
46-
lv_obj_t* pageIndicatorBase;
47-
lv_obj_t* pageIndicator;
4845
lv_obj_t* btnm1;
4946

47+
Widgets::PageIndicator pageIndicator;
48+
5049
BatteryIcon batteryIcon;
5150

5251
const char* btnmMap[8];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "displayapp/widgets/PageIndicator.h"
2+
3+
using namespace Pinetime::Applications::Widgets;
4+
5+
PageIndicator::PageIndicator(uint8_t nCurrentScreen, uint8_t nScreens) : nCurrentScreen {nCurrentScreen}, nScreens {nScreens} {
6+
}
7+
8+
void PageIndicator::Create() {
9+
pageIndicatorBasePoints[0].x = LV_HOR_RES - 1;
10+
pageIndicatorBasePoints[0].y = 0;
11+
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
12+
pageIndicatorBasePoints[1].y = LV_VER_RES;
13+
14+
pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr);
15+
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
16+
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
17+
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints, 2);
18+
19+
const int16_t indicatorSize = LV_VER_RES / nScreens;
20+
const int16_t indicatorPos = indicatorSize * nCurrentScreen;
21+
22+
pageIndicatorPoints[0].x = LV_HOR_RES - 1;
23+
pageIndicatorPoints[0].y = indicatorPos;
24+
pageIndicatorPoints[1].x = LV_HOR_RES - 1;
25+
pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
26+
27+
pageIndicator = lv_line_create(lv_scr_act(), nullptr);
28+
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
29+
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
30+
lv_line_set_points(pageIndicator, pageIndicatorPoints, 2);
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
#include <lvgl/lvgl.h>
3+
4+
namespace Pinetime {
5+
namespace Applications {
6+
namespace Widgets {
7+
class PageIndicator {
8+
public:
9+
PageIndicator(uint8_t nCurrentScreen, uint8_t nScreens);
10+
void Create();
11+
12+
private:
13+
uint8_t nCurrentScreen;
14+
uint8_t nScreens;
15+
16+
lv_point_t pageIndicatorBasePoints[2];
17+
lv_point_t pageIndicatorPoints[2];
18+
lv_obj_t* pageIndicatorBase;
19+
lv_obj_t* pageIndicator;
20+
};
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)