From e43e412fc86198a2dad600dc30a06618546f9d3a Mon Sep 17 00:00:00 2001 From: Timothy Slater Date: Tue, 30 Aug 2022 20:46:33 -0500 Subject: [PATCH] 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. --- Userland/Libraries/LibGUI/SpinBox.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Timothy Slater * * 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);