Skip to content

Commit 6e541c5

Browse files
committed
weather: Pad forecast temperatures
This ensures temperatures are correctly aligned with one another
1 parent 2d18237 commit 6e541c5

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/displayapp/screens/Weather.cpp

+25-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,27 @@ 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+
std::string maxPadding = "";
172+
std::string minPadding = "";
173+
int maxSize = snprintf(nullptr, 0, "%d", maxTemp);
174+
int minSize = snprintf(nullptr, 0, "%d", minTemp);
175+
switch (maxSize - minSize) {
176+
case -2:
177+
maxPadding = " ";
178+
break;
179+
case -1:
180+
maxPadding = " ";
181+
break;
182+
case 1:
183+
minPadding = " ";
184+
break;
185+
case 2:
186+
minPadding = " ";
187+
break;
188+
}
189+
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding.data(), maxTemp);
190+
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding.data(), minTemp);
178191
}
179192
} else {
180193
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {

0 commit comments

Comments
 (0)