1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:37:35 +00:00

Calculator: Use numeric InputBox for digit rounding and shrinking

Calc was using a bespoke SpinBox dialog which is no longer necessary.
This commit is contained in:
thankyouverycool 2023-04-16 16:07:11 -04:00 committed by Andreas Kling
parent a55d2be147
commit 8d86f2e69f
4 changed files with 9 additions and 109 deletions

View file

@ -9,7 +9,6 @@ set(SOURCES
main.cpp
Calculator.cpp
CalculatorWidget.cpp
RoundingDialog.cpp
Keypad.cpp
)

View file

@ -1,71 +0,0 @@
/*
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "RoundingDialog.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Label.h>
#include <LibGUI/SpinBox.h>
#include <LibGUI/TextEditor.h>
RoundingDialog::ExecResult RoundingDialog::show(GUI::Window* parent_window, StringView title, unsigned& rounding_value)
{
auto dialog = RoundingDialog::construct(parent_window, title);
if (parent_window) {
dialog->set_icon(parent_window->icon());
dialog->center_within(*parent_window);
}
dialog->m_rounding_spinbox->set_value(rounding_value);
auto const result = dialog->exec();
if (result != GUI::Dialog::ExecResult::OK)
return result;
rounding_value = dialog->m_rounding_spinbox->value();
return GUI::Dialog::ExecResult::OK;
}
RoundingDialog::RoundingDialog(GUI::Window* parent_window, StringView title)
: Dialog(parent_window)
{
resize(m_dialog_length, m_dialog_height);
set_resizable(false);
set_title(title);
auto main_widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
main_widget->set_fill_with_background_color(true);
main_widget->set_layout<GUI::VerticalBoxLayout>();
m_rounding_spinbox = GUI::SpinBox::construct();
m_buttons_container = GUI::Widget::construct();
m_ok_button = GUI::DialogButton::construct("OK"_short_string);
m_cancel_button = GUI::DialogButton::construct("Cancel"_short_string);
main_widget->add_child(*m_rounding_spinbox);
main_widget->add_child(*m_buttons_container);
m_buttons_container->set_layout<GUI::HorizontalBoxLayout>();
m_buttons_container->add_spacer().release_value_but_fixme_should_propagate_errors();
m_buttons_container->add_child(*m_ok_button);
m_buttons_container->add_child(*m_cancel_button);
m_rounding_spinbox->on_return_pressed = [this] {
m_ok_button->click();
};
m_ok_button->on_click = [this](auto) {
done(ExecResult::OK);
};
m_cancel_button->on_click = [this](auto) {
done(ExecResult::Cancel);
};
}

View file

@ -1,29 +0,0 @@
/*
* Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibGUI/Dialog.h>
class RoundingDialog : public GUI::Dialog {
C_OBJECT(RoundingDialog);
public:
static ExecResult show(GUI::Window* parent_window, StringView title, unsigned& rounding_value);
private:
RoundingDialog(GUI::Window* parent_window, StringView title);
virtual ~RoundingDialog() override = default;
RefPtr<GUI::SpinBox> m_rounding_spinbox;
RefPtr<GUI::Widget> m_buttons_container;
RefPtr<GUI::DialogButton> m_ok_button;
RefPtr<GUI::DialogButton> m_cancel_button;
static constexpr unsigned m_dialog_length = 200;
static constexpr unsigned m_dialog_height = 54;
};

View file

@ -5,7 +5,6 @@
*/
#include "CalculatorWidget.h"
#include "RoundingDialog.h"
#include <LibCore/System.h>
#include <LibCrypto/NumberTheory/ModularFunctions.h>
#include <LibGUI/Action.h>
@ -13,6 +12,7 @@
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/Icon.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Window.h>
@ -90,22 +90,23 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
constexpr auto format { "&Custom - {} ..."sv };
auto round_custom = GUI::Action::create_checkable(DeprecatedString::formatted(format, 0), [&](auto& action) {
unsigned custom_rounding_length = widget->rounding_length();
if (RoundingDialog::show(window, "Choose custom rounding"sv, custom_rounding_length) == GUI::Dialog::ExecResult::OK) {
int custom_rounding_length = widget->rounding_length();
auto result = GUI::InputBox::show_numeric(window, custom_rounding_length, 0, 100, "Round to"sv);
if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
action.set_text(DeprecatedString::formatted(format, custom_rounding_length));
widget->set_rounding_length(custom_rounding_length);
last_rounding_mode.clear();
} else if (last_rounding_mode.has_value())
round_menu.action_at(last_rounding_mode.value())->activate();
round_menu.action_at(last_rounding_mode.value())
->activate();
});
widget->set_rounding_custom(round_custom, format);
auto shrink_action = GUI::Action::create("&Shrink...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"sv)), [&](auto&) {
unsigned shrink_length = widget->rounding_length();
if (RoundingDialog::show(window, "Choose shrinking length"sv, shrink_length) == GUI::Dialog::ExecResult::OK) {
int shrink_length = widget->rounding_length();
auto result = GUI::InputBox::show_numeric(window, shrink_length, 0, 100, "Shrink to"sv);
if (!result.is_error() && result.value() == GUI::Dialog::ExecResult::OK) {
round_custom->set_checked(true);
round_custom->set_text(DeprecatedString::formatted(format, shrink_length));
widget->set_rounding_length(shrink_length);