1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

Calculator: Treat constants and pasted numbers as input

Constants and pasted numbers leave Keypad in the External
state which causes subsequent operations to be performed
without an argument.
This commit is contained in:
Samuel Eisenhandler 2023-02-02 23:10:13 -05:00 committed by Sam Atkins
parent f58668031d
commit c5360b1a5f
5 changed files with 26 additions and 7 deletions

View file

@ -142,6 +142,12 @@ void CalculatorWidget::set_entry(Crypto::BigFraction value)
update_display();
}
void CalculatorWidget::set_typed_entry(Crypto::BigFraction value)
{
m_keypad.set_typed_value(move(value));
update_display();
}
void CalculatorWidget::update_display()
{
m_entry->set_text(m_keypad.to_deprecated_string());

View file

@ -21,6 +21,7 @@ public:
virtual ~CalculatorWidget() override = default;
DeprecatedString get_entry();
void set_entry(Crypto::BigFraction);
void set_typed_entry(Crypto::BigFraction);
void shrink(unsigned);
unsigned rounding_length() const;

View file

@ -16,6 +16,7 @@ void Keypad::type_digit(int digit)
{
switch (m_state) {
case State::External:
case State::TypedExternal:
m_state = State::TypingInteger;
m_int_value = digit;
m_frac_value.set_to_0();
@ -40,6 +41,7 @@ void Keypad::type_decimal_point()
{
switch (m_state) {
case State::External:
case State::TypedExternal:
m_int_value.set_to_0();
m_frac_value.set_to_0();
m_frac_length.set_to_0();
@ -60,6 +62,7 @@ void Keypad::type_backspace()
{
switch (m_state) {
case State::External:
case State::TypedExternal:
m_int_value.set_to_0();
m_frac_value.set_to_0();
m_frac_length.set_to_0();
@ -83,7 +86,7 @@ void Keypad::type_backspace()
Crypto::BigFraction Keypad::value() const
{
if (m_state != State::External) {
if (m_state != State::External && m_state != State::TypedExternal) {
Crypto::SignedBigInteger sum { m_int_value.multiplied_by(Crypto::NumberTheory::Power("10"_bigint, m_frac_length)).plus(m_frac_value) };
Crypto::BigFraction res { move(sum), Crypto::NumberTheory::Power("10"_bigint, m_frac_length) };
@ -100,6 +103,13 @@ void Keypad::set_value(Crypto::BigFraction value)
m_internal_value = move(value);
}
void Keypad::set_typed_value(Crypto::BigFraction value)
{
m_state = State::TypedExternal;
m_internal_value = move(value);
}
void Keypad::set_to_0()
{
m_int_value.set_to_0();
@ -113,7 +123,7 @@ void Keypad::set_to_0()
DeprecatedString Keypad::to_deprecated_string() const
{
if (m_state == State::External)
if (m_state == State::External || m_state == State::TypedExternal)
return m_internal_value.to_deprecated_string(m_displayed_fraction_length);
StringBuilder builder;
@ -137,7 +147,7 @@ DeprecatedString Keypad::to_deprecated_string() const
bool Keypad::in_typing_state() const
{
return m_state == State::TypingDecimal || m_state == State::TypingInteger;
return m_state == State::TypedExternal || m_state == State::TypingDecimal || m_state == State::TypingInteger;
}
void Keypad::set_rounding_length(unsigned rounding_threshold)

View file

@ -27,6 +27,7 @@ public:
Crypto::BigFraction value() const;
void set_value(Crypto::BigFraction);
void set_typed_value(Crypto::BigFraction);
void set_to_0();
void shrink(unsigned);
@ -56,6 +57,7 @@ private:
enum class State {
External,
TypedExternal,
TypingInteger,
TypingDecimal
};

View file

@ -53,7 +53,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (clipboard.mime_type == "text/plain") {
if (!clipboard.data.is_empty()) {
auto const number = StringView(clipboard.data);
widget->set_entry(Crypto::BigFraction(number));
widget->set_typed_entry(Crypto::BigFraction(number));
}
}
}));
@ -62,13 +62,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto const power = Crypto::NumberTheory::Power("10"_bigint, "10"_bigint);
constants_menu.add_action(GUI::Action::create("&Pi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/pi.png"sv)), [&](auto&) {
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(31415926535), power });
widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(31415926535), power });
}));
constants_menu.add_action(GUI::Action::create("&Euler's Number", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/eulers_number.png"sv)), [&](auto&) {
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(27182818284), power });
widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(27182818284), power });
}));
constants_menu.add_action(GUI::Action::create("&Phi", TRY(Gfx::Bitmap::load_from_file("/res/icons/calculator/phi.png"sv)), [&](auto&) {
widget->set_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
widget->set_typed_entry(Crypto::BigFraction { Crypto::SignedBigInteger(16180339887), power });
}));
auto& round_menu = window->add_menu("&Round");