diff options
author | ForLoveOfCats <floc@unpromptedtirade.com> | 2022-03-02 12:54:03 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-03-03 22:06:14 +0100 |
commit | 0d0ba375e296f01b572d364d4db328c5932f880e (patch) | |
tree | a2e6aab3594b3a8f0c72bb3720f9dbf1a3ad273f /Userland | |
parent | e8f6a650ad77fdbdbcebfd6e6d8935ba49eb7b09 (diff) | |
download | serenity-0d0ba375e296f01b572d364d4db328c5932f880e.zip |
Calculator: Utilize Button `mimic_pressed` to show when keys are pressed
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.cpp | 34 | ||||
-rw-r--r-- | Userland/Applications/Calculator/CalculatorWidget.h | 4 |
2 files changed, 37 insertions, 1 deletions
diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index 3a37fd4503..b1bc8e3c51 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -10,6 +10,7 @@ #include "CalculatorWidget.h" #include "KeypadValue.h" #include <Applications/Calculator/CalculatorGML.h> +#include <LibCore/Event.h> #include <LibGUI/Button.h> #include <LibGUI/Label.h> #include <LibGUI/TextBox.h> @@ -134,6 +135,20 @@ void CalculatorWidget::set_entry(KeypadValue value) update_display(); } +void CalculatorWidget::mimic_pressed_button(RefPtr<GUI::Button> button) +{ + constexpr int TIMER_MS = 80; + + if (!m_mimic_pressed_button.is_null()) + m_mimic_pressed_button->set_mimic_pressed(false); + + button->set_mimic_pressed(true); + m_mimic_pressed_button = button; + + stop_timer(); + start_timer(TIMER_MS, Core::TimerShouldFireWhenNotVisible::Yes); +} + void CalculatorWidget::update_display() { m_entry->set_text(m_keypad.to_string()); @@ -151,33 +166,44 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event) if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Equal) { m_keypad.set_value(m_calculator.finish_operation(m_keypad.value())); + mimic_pressed_button(m_equals_button); } else if (event.code_point() >= '0' && event.code_point() <= '9') { - m_keypad.type_digit(event.code_point() - '0'); + u32 digit = event.code_point() - '0'; + m_keypad.type_digit(digit); + mimic_pressed_button(m_digit_button[digit]); } else if (event.code_point() == '.') { m_keypad.type_decimal_point(); + mimic_pressed_button(m_decimal_point_button); } else if (event.key() == KeyCode::Key_Escape) { m_keypad.set_value(0.0); m_calculator.clear_operation(); + mimic_pressed_button(m_clear_button); } else if (event.key() == KeyCode::Key_Backspace) { m_keypad.type_backspace(); + mimic_pressed_button(m_backspace_button); } else { Calculator::Operation operation; switch (event.code_point()) { case '+': operation = Calculator::Operation::Add; + mimic_pressed_button(m_add_button); break; case '-': operation = Calculator::Operation::Subtract; + mimic_pressed_button(m_subtract_button); break; case '*': operation = Calculator::Operation::Multiply; + mimic_pressed_button(m_multiply_button); break; case '/': operation = Calculator::Operation::Divide; + mimic_pressed_button(m_divide_button); break; case '%': operation = Calculator::Operation::Percent; + mimic_pressed_button(m_percent_button); break; default: return; @@ -188,3 +214,9 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event) update_display(); } + +void CalculatorWidget::timer_event(Core::TimerEvent&) +{ + if (!m_mimic_pressed_button.is_null()) + m_mimic_pressed_button->set_mimic_pressed(false); +} diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h index 84d2c087de..62ad32413b 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.h +++ b/Userland/Applications/Calculator/CalculatorWidget.h @@ -26,9 +26,11 @@ private: void add_operation_button(GUI::Button&, Calculator::Operation); void add_digit_button(GUI::Button&, int digit); + void mimic_pressed_button(RefPtr<GUI::Button>); void update_display(); virtual void keydown_event(GUI::KeyEvent&) override; + virtual void timer_event(Core::TimerEvent&) override; Calculator m_calculator; Keypad m_keypad; @@ -54,4 +56,6 @@ private: RefPtr<GUI::Button> m_inverse_button; RefPtr<GUI::Button> m_percent_button; RefPtr<GUI::Button> m_equals_button; + + RefPtr<GUI::Button> m_mimic_pressed_button {}; }; |