Skip to content

Commit 9a0d392

Browse files
committed
weather: Pad forecast temperatures
This ensures temperatures are correctly aligned with one another
1 parent ad3b652 commit 9a0d392

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/displayapp/screens/Weather.cpp

+17-12
Original file line numberDiff line numberDiff line change
@@ -74,38 +74,32 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
7474
// LV_TABLE_PART_CELL1: Default table style
7575
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
7676
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, Colors::lightGray);
77-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 6);
7877
// LV_TABLE_PART_CELL2: Condition icon
7978
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
8079
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
8180
lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons);
82-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, 6);
8381
// LV_TABLE_PART_CELL3: Freezing
8482
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_BLACK);
8583
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, Colors::blue);
86-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, 6);
8784
// LV_TABLE_PART_CELL4: Ice
8885
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_BLACK);
8986
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_CYAN);
90-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, 6);
9187
// LV_TABLE_PART_CELL5: Normal
9288
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, LV_COLOR_BLACK);
9389
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, Colors::orange);
94-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, 6);
9590
// LV_TABLE_PART_CELL6: Hot
9691
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, LV_COLOR_BLACK);
9792
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, Colors::deepOrange);
98-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, 6);
9993

10094
lv_obj_align(forecast, nullptr, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
10195

10296
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {
10397
lv_table_set_col_width(forecast, i, 48);
10498
lv_table_set_cell_type(forecast, 1, i, LV_TABLE_PART_CELL2);
105-
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_RIGHT);
106-
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_RIGHT);
107-
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_RIGHT);
108-
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_RIGHT);
99+
lv_table_set_cell_align(forecast, 0, i, LV_LABEL_ALIGN_CENTER);
100+
lv_table_set_cell_align(forecast, 1, i, LV_LABEL_ALIGN_CENTER);
101+
lv_table_set_cell_align(forecast, 2, i, LV_LABEL_ALIGN_CENTER);
102+
lv_table_set_cell_align(forecast, 3, i, LV_LABEL_ALIGN_CENTER);
109103
}
110104

111105
taskRefresh = lv_task_create(RefreshTaskCallback, 1000, LV_TASK_PRIO_MID, this);
@@ -174,8 +168,19 @@ void Weather::Refresh() {
174168
minTemp = minTemp / 100 + (minTemp % 100 >= 50 ? 1 : 0);
175169
lv_table_set_cell_value(forecast, 0, i, Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday)));
176170
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
177-
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
178-
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
171+
// Pad cells based on the largest number of digits on each column
172+
char maxPadding[3] = " ";
173+
char minPadding[3] = " ";
174+
int diff = snprintf(nullptr, 0, "%d", maxTemp) - snprintf(nullptr, 0, "%d", minTemp);
175+
if (diff <= 0) {
176+
maxPadding[-diff] = '\0';
177+
minPadding[0] = '\0';
178+
} else {
179+
maxPadding[0] = '\0';
180+
minPadding[diff] = '\0';
181+
}
182+
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding, maxTemp);
183+
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding, minTemp);
179184
}
180185
} else {
181186
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {

0 commit comments

Comments
 (0)