Skip to content

Commit 6a014c9

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

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);
@@ -175,8 +169,19 @@ void Weather::Refresh() {
175169
const char* dayOfWeek = Controllers::DateTime::DayOfWeekShortToStringLow(static_cast<Controllers::DateTime::Days>(wday));
176170
lv_table_set_cell_value(forecast, 0, i, dayOfWeek);
177171
lv_table_set_cell_value(forecast, 1, i, Symbols::GetSymbol(optCurrentForecast->days[i].iconId));
178-
lv_table_set_cell_value_fmt(forecast, 2, i, "%d", maxTemp);
179-
lv_table_set_cell_value_fmt(forecast, 3, i, "%d", minTemp);
172+
// Pad cells based on the largest number of digits on each column
173+
char maxPadding[3] = " ";
174+
char minPadding[3] = " ";
175+
int diff = snprintf(nullptr, 0, "%d", maxTemp) - snprintf(nullptr, 0, "%d", minTemp);
176+
if (diff <= 0) {
177+
maxPadding[-diff] = '\0';
178+
minPadding[0] = '\0';
179+
} else {
180+
maxPadding[0] = '\0';
181+
minPadding[diff] = '\0';
182+
}
183+
lv_table_set_cell_value_fmt(forecast, 2, i, "%s%d", maxPadding, maxTemp);
184+
lv_table_set_cell_value_fmt(forecast, 3, i, "%s%d", minPadding, minTemp);
180185
}
181186
} else {
182187
for (int i = 0; i < Controllers::SimpleWeatherService::MaxNbForecastDays; i++) {

0 commit comments

Comments
 (0)