mirror of
https://github.com/InfiniTimeOrg/InfiniTime.git
synced 2024-12-25 02:32:41 +03:00
Determine the number of digits from the max value. (#1271)
This commit is contained in:
parent
c2b6a8de3e
commit
62c4ff9c2d
@ -18,9 +18,17 @@ namespace {
|
||||
widget->DownBtnPressed();
|
||||
}
|
||||
}
|
||||
constexpr int digitCount(int number) {
|
||||
int digitCount = 0;
|
||||
while (number > 0) {
|
||||
digitCount++;
|
||||
number /= 10;
|
||||
}
|
||||
return digitCount;
|
||||
}
|
||||
}
|
||||
|
||||
Counter::Counter(int min, int max, lv_font_t& font) : min {min}, max {max}, value {min}, font {font} {
|
||||
Counter::Counter(int min, int max, lv_font_t& font) : min {min}, max {max}, value {min}, font {font}, leadingZeroCount {digitCount(max)} {
|
||||
}
|
||||
|
||||
void Counter::UpBtnPressed() {
|
||||
@ -72,14 +80,14 @@ void Counter::UpdateLabel() {
|
||||
if (value == 0) {
|
||||
lv_label_set_text_static(number, "12");
|
||||
} else if (value <= 12) {
|
||||
lv_label_set_text_fmt(number, "%.2i", value);
|
||||
lv_label_set_text_fmt(number, "%.*i", leadingZeroCount, value);
|
||||
} else {
|
||||
lv_label_set_text_fmt(number, "%.2i", value - 12);
|
||||
lv_label_set_text_fmt(number, "%.*i", leadingZeroCount, value - 12);
|
||||
}
|
||||
} else if (monthMode) {
|
||||
lv_label_set_text(number, Controllers::DateTime::MonthShortToStringLow(static_cast<Controllers::DateTime::Months>(value)));
|
||||
} else {
|
||||
lv_label_set_text_fmt(number, "%.2i", value);
|
||||
lv_label_set_text_fmt(number, "%.*i", leadingZeroCount, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,6 +103,8 @@ void Counter::EnableMonthMode() {
|
||||
monthMode = true;
|
||||
}
|
||||
|
||||
// Counter cannot be resized after creation,
|
||||
// so the newMax value must have the same number of digits as the old one
|
||||
void Counter::SetMax(int newMax) {
|
||||
max = newMax;
|
||||
if (value > max) {
|
||||
|
@ -41,6 +41,7 @@ namespace Pinetime {
|
||||
int min;
|
||||
int max;
|
||||
int value;
|
||||
const int leadingZeroCount;
|
||||
bool twelveHourMode = false;
|
||||
bool monthMode = false;
|
||||
lv_font_t& font;
|
||||
|
Loading…
Reference in New Issue
Block a user