Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4f87e2c

Browse files
committedFeb 12, 2024·
weather: Pad forecast temperatures
This ensures temperatures are correctly aligned with one another
1 parent ec04d63 commit 4f87e2c

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
@@ -73,38 +73,32 @@ Weather::Weather(Controllers::Settings& settingsController, Controllers::SimpleW
7373
// LV_TABLE_PART_CELL1: Default table style
7474
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
7575
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, Colors::lightGray);
76-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL1, LV_STATE_DEFAULT, 6);
7776
// LV_TABLE_PART_CELL2: Condition icon
7877
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLACK);
7978
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
8079
lv_obj_set_style_local_text_font(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, &fontawesome_weathericons);
81-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, 6);
8280
// LV_TABLE_PART_CELL3: Freezing
8381
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_BLACK);
8482
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, Colors::blue);
85-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, 6);
8683
// LV_TABLE_PART_CELL4: Ice
8784
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_BLACK);
8885
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, LV_COLOR_CYAN);
89-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL4, LV_STATE_DEFAULT, 6);
9086
// LV_TABLE_PART_CELL5: Normal
9187
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, LV_COLOR_BLACK);
9288
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, Colors::orange);
93-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL5, LV_STATE_DEFAULT, 6);
9489
// LV_TABLE_PART_CELL6: Hot
9590
lv_obj_set_style_local_border_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, LV_COLOR_BLACK);
9691
lv_obj_set_style_local_text_color(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, Colors::deepOrange);
97-
lv_obj_set_style_local_pad_right(forecast, LV_TABLE_PART_CELL6, LV_STATE_DEFAULT, 6);
9892

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

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.