diff options
author | Timothy Slater <tslater2006@gmail.com> | 2022-08-30 20:46:33 -0500 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2022-09-01 17:47:49 +0100 |
commit | e43e412fc86198a2dad600dc30a06618546f9d3a (patch) | |
tree | 92d30f0fe8b23c223141a6c15e81049fdc99f9f2 /Userland/Libraries/LibGUI/SpinBox.cpp | |
parent | 1183bc51846c3d4092a7e8d606f2e32a5037d2dd (diff) | |
download | serenity-e43e412fc86198a2dad600dc30a06618546f9d3a.zip |
LibGUI: Improve SpinBox usability
Previously the value of the SpinBox is re-evaluated after every change
to the TextBox control. This leads to very unintuitive behavior such as
the user deleting the contents of the box and it having no
visible effect. This happens because the TextBox no longer has a valid
number and so gets reset to the current m_value of the SpinBox.
By defering the update of to the SpinBox value until focus leaves the
control we provide a much more intuitive experience with the text box.
We do still validate when a user types something that it parses to an
int. If it does not we delete the most recent character. This in effect
prevents non-numeric numbers from being entered.
Upon losing focus the value will be checked. If empty we set the SpinBox
value to the minimum allowed value.
Diffstat (limited to 'Userland/Libraries/LibGUI/SpinBox.cpp')
-rw-r--r-- | Userland/Libraries/LibGUI/SpinBox.cpp | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/Userland/Libraries/LibGUI/SpinBox.cpp b/Userland/Libraries/LibGUI/SpinBox.cpp index 470a165f0e..95e379c7a0 100644 --- a/Userland/Libraries/LibGUI/SpinBox.cpp +++ b/Userland/Libraries/LibGUI/SpinBox.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -24,10 +25,15 @@ SpinBox::SpinBox() return; auto value = m_editor->text().to_uint(); + if (!value.has_value() && m_editor->text().length() > 0) + 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 - m_editor->set_text(String::number(m_value)); + set_value(min()); }; m_editor->on_up_pressed = [this] { set_value(m_value + 1); |