/* * 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({ 40, 22 }); set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 }); 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_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 set_value(min()); }; 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] { if (on_return_pressed) on_return_pressed(); }; m_increment_button = add