diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index 3b01de30c7..971457bc11 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -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()); diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h index 3a6a009d2a..1333a8581d 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.h +++ b/Userland/Applications/Calculator/CalculatorWidget.h @@ -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; diff --git a/Userland/Applications/Calculator/Keypad.cpp b/Userland/Applications/Calculator/Keypad.cpp index ad8399c5cd..e8e2c04ad2 100644 --- a/Userland/Applications/Calculator/Keypad.cpp +++ b/Userland/Applications/Calculator/Keypad.cpp @@ -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) diff --git a/Userland/Applications/Calculator/Keypad.h b/Userland/Applications/Calculator/Keypad.h index af32de260f..3ef3be3a9d 100644 --- a/Userland/Applications/Calculator/Keypad.h +++ b/Userland/Applications/Calculator/Keypad.h @@ -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 }; diff --git a/Userland/Applications/Calculator/main.cpp b/Userland/Applications/Calculator/main.cpp index fc82d6e23a..88dcd2b22b 100644 --- a/Userland/Applications/Calculator/main.cpp +++ b/Userland/Applications/Calculator/main.cpp @@ -53,7 +53,7 @@ ErrorOr 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 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");