fix(spinbox): fix range violation on zero range crossing

This commit is contained in:
Gabor Kiss-Vamosi
2022-08-23 14:11:36 +02:00
parent 327dbb6031
commit 7f0f826c5e

View File

@@ -272,22 +272,29 @@ void lv_spinbox_increment(lv_obj_t * obj)
LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_OBJ(obj, MY_CLASS);
lv_spinbox_t * spinbox = (lv_spinbox_t *)obj; lv_spinbox_t * spinbox = (lv_spinbox_t *)obj;
if(spinbox->value + spinbox->step <= spinbox->range_max) { int32_t v = spinbox->value;
/*Special mode when zero crossing*/ /* Special mode when zero crossing. E.g -3+10 should be 3, not 7.
if((spinbox->value + spinbox->step) > 0 && spinbox->value < 0) spinbox->value = -spinbox->value; * Pretend we are on -7 now.*/
spinbox->value += spinbox->step; if((spinbox->value < 0) && (spinbox->value + spinbox->step) > 0) {
v = -(spinbox->step + spinbox->value);
}
if(v + spinbox->step <= spinbox->range_max) {
v += spinbox->step;
} }
else { else {
// Rollover? /*Rollover?*/
if((spinbox->rollover) && (spinbox->value == spinbox->range_max)) if((spinbox->rollover) && (spinbox->value == spinbox->range_max))
spinbox->value = spinbox->range_min; v = spinbox->range_min;
else else
spinbox->value = spinbox->range_max; v = spinbox->range_max;
} }
if(v != spinbox->value) {
spinbox->value = v;
lv_spinbox_updatevalue(obj); lv_spinbox_updatevalue(obj);
} }
}
/** /**
* Decrement spinbox value by one step * Decrement spinbox value by one step
@@ -298,21 +305,29 @@ void lv_spinbox_decrement(lv_obj_t * obj)
LV_ASSERT_OBJ(obj, MY_CLASS); LV_ASSERT_OBJ(obj, MY_CLASS);
lv_spinbox_t * spinbox = (lv_spinbox_t *)obj; lv_spinbox_t * spinbox = (lv_spinbox_t *)obj;
if(spinbox->value - spinbox->step >= spinbox->range_min) { int32_t v = spinbox->value;
/*Special mode when zero crossing*/ /* Special mode when zero crossing. E.g 3-10 should be -3, not -7.
if((spinbox->value - spinbox->step) < 0 && spinbox->value > 0) spinbox->value = -spinbox->value; * Pretend we are on 7 now.*/
spinbox->value -= spinbox->step; if((spinbox->value > 0) && (spinbox->value - spinbox->step) < 0) {
v = spinbox->step - spinbox->value;
}
if(v - spinbox->step >= spinbox->range_min) {
v -= spinbox->step;
} }
else { else {
/*Rollover?*/ /*Rollover?*/
if((spinbox->rollover) && (spinbox->value == spinbox->range_min)) if((spinbox->rollover) && (spinbox->value == spinbox->range_min))
spinbox->value = spinbox->range_max; v = spinbox->range_max;
else else
spinbox->value = spinbox->range_min; v = spinbox->range_min;
} }
if(v != spinbox->value) {
spinbox->value = v;
lv_spinbox_updatevalue(obj); lv_spinbox_updatevalue(obj);
} }
}
/********************** /**********************
* STATIC FUNCTIONS * STATIC FUNCTIONS