From 0d0ba375e296f01b572d364d4db328c5932f880e Mon Sep 17 00:00:00 2001 From: ForLoveOfCats Date: Wed, 2 Mar 2022 12:54:03 -0500 Subject: [PATCH] Calculator: Utilize Button `mimic_pressed` to show when keys are pressed --- .../Calculator/CalculatorWidget.cpp | 34 ++++++++++++++++++- .../Calculator/CalculatorWidget.h | 4 +++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index 3a37fd4503e..b1bc8e3c51a 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -10,6 +10,7 @@ #include "CalculatorWidget.h" #include "KeypadValue.h" #include +#include #include #include #include @@ -134,6 +135,20 @@ void CalculatorWidget::set_entry(KeypadValue value) update_display(); } +void CalculatorWidget::mimic_pressed_button(RefPtr 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 84d2c087de2..62ad32413b8 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); 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 m_inverse_button; RefPtr m_percent_button; RefPtr m_equals_button; + + RefPtr m_mimic_pressed_button {}; };