mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Calculator: Added keyboard input
This commit is contained in:
parent
3e8b60c618
commit
488c510e02
Notes:
sideshowbarker
2024-07-19 09:59:41 +09:00
Author: https://github.com/RyanGrieb Commit: https://github.com/SerenityOS/serenity/commit/488c510e02a Pull-request: https://github.com/SerenityOS/serenity/pull/1088 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/bugaevc
2 changed files with 43 additions and 1 deletions
|
@ -197,3 +197,44 @@ void CalculatorWidget::update_display()
|
|||
else
|
||||
m_label->set_text("");
|
||||
}
|
||||
|
||||
void CalculatorWidget::keydown_event(GKeyEvent& event)
|
||||
{
|
||||
//Clear button selection when we are typing
|
||||
m_equals_button->set_focus(true);
|
||||
m_equals_button->set_focus(false);
|
||||
|
||||
if (event.key() == KeyCode::Key_Return) {
|
||||
m_keypad.set_value(m_calculator.finish_operation(m_keypad.value()));
|
||||
|
||||
} else if (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) {
|
||||
m_keypad.type_digit(atoi(event.text().characters()));
|
||||
|
||||
} else {
|
||||
Calculator::Operation operation;
|
||||
|
||||
switch (event.key()) {
|
||||
case KeyCode::Key_Plus:
|
||||
operation = Calculator::Operation::Add;
|
||||
break;
|
||||
case KeyCode::Key_Minus:
|
||||
operation = Calculator::Operation::Subtract;
|
||||
break;
|
||||
case KeyCode::Key_Asterisk:
|
||||
operation = Calculator::Operation::Multiply;
|
||||
break;
|
||||
case KeyCode::Key_Slash:
|
||||
operation = Calculator::Operation::Divide;
|
||||
break;
|
||||
case KeyCode::Key_Percent:
|
||||
operation = Calculator::Operation::Percent;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
m_keypad.set_value(m_calculator.begin_operation(operation, m_keypad.value()));
|
||||
}
|
||||
|
||||
update_display();
|
||||
}
|
|
@ -22,6 +22,8 @@ private:
|
|||
|
||||
void update_display();
|
||||
|
||||
virtual void keydown_event(GKeyEvent&) override;
|
||||
|
||||
Calculator m_calculator;
|
||||
Keypad m_keypad;
|
||||
|
||||
|
@ -46,5 +48,4 @@ private:
|
|||
RefPtr<GButton> m_inverse_button;
|
||||
RefPtr<GButton> m_percent_button;
|
||||
RefPtr<GButton> m_equals_button;
|
||||
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue