mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 09:46:04 -05:00
Calculator: Utilize Button mimic_pressed
to show when keys are pressed
This commit is contained in:
parent
e8f6a650ad
commit
0d0ba375e2
Notes:
sideshowbarker
2024-07-17 17:59:00 +09:00
Author: https://github.com/ForLoveOfCats Commit: https://github.com/SerenityOS/serenity/commit/0d0ba375e2 Pull-request: https://github.com/SerenityOS/serenity/pull/12250 Reviewed-by: https://github.com/awesomekling
2 changed files with 37 additions and 1 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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 {};
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue