1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:38:10 +00:00

LibGUI: Allow SpinBox to set its value with return key

Previously SpinBox did not update on return or changes to the editor.
The widget had to lose focus or be manually incremented. This lets
the editor update on return and now always displays the most recent
clamped value. set_value_from_current_text() will also be useful to
programmatically set SpinBox within layouts whose default buttons
consume return key presses.
This commit is contained in:
thankyouverycool 2023-04-16 16:03:11 -04:00 committed by Andreas Kling
parent 7c314f3855
commit b66a76f73b
2 changed files with 13 additions and 6 deletions

View file

@ -29,11 +29,7 @@ SpinBox::SpinBox()
m_editor->do_delete();
};
m_editor->on_focusout = [this] {
auto value = m_editor->text().to_int();
if (value.has_value())
set_value(value.value());
else
set_value(min());
set_value_from_current_text();
};
m_editor->on_up_pressed = [this] {
set_value(m_value + 1);
@ -42,6 +38,7 @@ SpinBox::SpinBox()
set_value(m_value - 1);
};
m_editor->on_return_pressed = [this] {
set_value_from_current_text();
if (on_return_pressed)
on_return_pressed();
};
@ -66,6 +63,7 @@ SpinBox::SpinBox()
void SpinBox::set_value(int value, AllowCallback allow_callback)
{
value = clamp(value, m_min, m_max);
m_editor->set_text(DeprecatedString::number(value));
if (m_value == value)
return;
m_value = value;
@ -73,12 +71,20 @@ void SpinBox::set_value(int value, AllowCallback allow_callback)
m_increment_button->set_enabled(m_value < m_max);
m_decrement_button->set_enabled(m_value > m_min);
m_editor->set_text(DeprecatedString::number(value));
update();
if (on_change && allow_callback == AllowCallback::Yes)
on_change(value);
}
void SpinBox::set_value_from_current_text()
{
auto value = m_editor->text().to_int();
if (value.has_value())
set_value(value.value());
else
set_value(min());
}
void SpinBox::set_range(int min, int max, AllowCallback allow_callback)
{
VERIFY(min <= max);

View file

@ -18,6 +18,7 @@ public:
int value() const { return m_value; }
void set_value(int, AllowCallback = AllowCallback::Yes);
void set_value_from_current_text();
int min() const { return m_min; }
int max() const { return m_max; }