[NumberEdit] add an option to avoid instant change on modification (change is done later when edit mode is disabled)
This commit is contained in:
+32
-4
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#include "form.h"
|
#include "form.h"
|
||||||
|
|
||||||
class BaseNumberEdit : public FormField
|
class BaseNumberEdit: public FormField
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
BaseNumberEdit(Window * parent, const rect_t &rect, int vmin, int vmax,
|
BaseNumberEdit(Window * parent, const rect_t &rect, int vmin, int vmax,
|
||||||
@@ -76,10 +76,23 @@ class BaseNumberEdit : public FormField
|
|||||||
|
|
||||||
void setValue(int value)
|
void setValue(int value)
|
||||||
{
|
{
|
||||||
_setValue(limit(vmin, value, vmax));
|
currentValue = limit(vmin, value, vmax);
|
||||||
|
if (instantChange) {
|
||||||
|
_setValue(currentValue);
|
||||||
|
}
|
||||||
invalidate();
|
invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void enableInstantChange(bool value)
|
||||||
|
{
|
||||||
|
instantChange = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void disableInstantChange()
|
||||||
|
{
|
||||||
|
enableInstantChange(false);
|
||||||
|
}
|
||||||
|
|
||||||
void setSetValueHandler(std::function<void(int)> handler)
|
void setSetValueHandler(std::function<void(int)> handler)
|
||||||
{
|
{
|
||||||
_setValue = std::move(handler);
|
_setValue = std::move(handler);
|
||||||
@@ -87,7 +100,21 @@ class BaseNumberEdit : public FormField
|
|||||||
|
|
||||||
int32_t getValue() const
|
int32_t getValue() const
|
||||||
{
|
{
|
||||||
return _getValue();
|
return (editMode && !instantChange) ? currentValue : _getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
void setEditMode(bool newEditMode) override
|
||||||
|
{
|
||||||
|
auto previousEditMode = editMode;
|
||||||
|
FormField::setEditMode(newEditMode);
|
||||||
|
if (!instantChange) {
|
||||||
|
if (!previousEditMode && newEditMode) {
|
||||||
|
currentValue = _getValue();
|
||||||
|
}
|
||||||
|
else if (previousEditMode && !newEditMode) {
|
||||||
|
_setValue(currentValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -95,7 +122,8 @@ class BaseNumberEdit : public FormField
|
|||||||
int vmin;
|
int vmin;
|
||||||
int vmax;
|
int vmax;
|
||||||
int step = 1;
|
int step = 1;
|
||||||
|
int currentValue;
|
||||||
|
bool instantChange = true;
|
||||||
std::function<int()> _getValue;
|
std::function<int()> _getValue;
|
||||||
std::function<void(int)> _setValue;
|
std::function<void(int)> _setValue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user