/* * Copyright (c) 2018-2020, Andreas Kling * Copyright (c) 2022, the SerenityOS developers. * Copyright (c) 2022, Timothy Slater * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include REGISTER_WIDGET(GUI, SpinBox) namespace GUI { SpinBox::SpinBox() { set_min_size({ SpecialDimension::Shrink }); set_preferred_size({ SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink }); m_editor = add(); m_editor->set_text("0"sv); m_editor->on_change = [this, weak_this = make_weak_ptr()] { if (!weak_this) return; auto value = m_editor->text().to_number(); if (!value.has_value() && m_editor->text().length() > 0) m_editor->do_delete(); }; m_editor->on_focusout = [this] { set_value_from_current_text(); }; m_editor->on_up_pressed = [this] { set_value(m_value + 1); }; m_editor->on_down_pressed = [this] { set_value(m_value - 1); }; m_editor->on_return_pressed = [this] { set_value_from_current_text(); if (on_return_pressed) on_return_pressed(); }; m_increment_button = add