mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:47:45 +00:00
Calculator: Utilize Button mimic_pressed
to show when keys are pressed
This commit is contained in:
parent
e8f6a650ad
commit
0d0ba375e2
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue