summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLucas CHOLLET <lucas.chollet@free.fr>2023-01-17 17:31:45 -0500
committerAndreas Kling <kling@serenityos.org>2023-01-21 14:49:46 +0100
commitd4ef2e226cf0680d0921b9ec534ea830d1cd9aa7 (patch)
tree7f3221cb5de0a658a9610f5ba29f200055dea6f5
parent96b306312180855e8b234df8bffe985578932f38 (diff)
downloadserenity-d4ef2e226cf0680d0921b9ec534ea830d1cd9aa7.zip
LibGUI: Mimic a user click when calling `Button::click()`
The `mimic_pressed` function was primarily used in one place, the Calculator. This patch removes quite a lot of logic duplication there. It is also profitable to a lot of other places where `click()` was called without mimicking a click.
-rw-r--r--Userland/Applications/Calculator/CalculatorWidget.cpp78
-rw-r--r--Userland/Applications/Calculator/CalculatorWidget.h1
-rw-r--r--Userland/Libraries/LibGUI/AbstractButton.cpp5
-rw-r--r--Userland/Libraries/LibGUI/AbstractButton.h2
-rw-r--r--Userland/Libraries/LibGUI/Button.cpp5
5 files changed, 37 insertions, 54 deletions
diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp
index c15f6f9642..3b01de30c7 100644
--- a/Userland/Applications/Calculator/CalculatorWidget.cpp
+++ b/Userland/Applications/Calculator/CalculatorWidget.cpp
@@ -142,11 +142,6 @@ void CalculatorWidget::set_entry(Crypto::BigFraction value)
update_display();
}
-void CalculatorWidget::mimic_pressed_button(RefPtr<GUI::Button> button)
-{
- button->mimic_pressed();
-}
-
void CalculatorWidget::update_display()
{
m_entry->set_text(m_keypad.to_deprecated_string());
@@ -158,51 +153,34 @@ void CalculatorWidget::update_display()
void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
{
- if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Equal) {
- perform_operation(Calculator::Operation::Equals);
- mimic_pressed_button(m_equals_button);
- } else if (event.code_point() >= '0' && event.code_point() <= '9') {
- auto const 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 || event.key() == KeyCode::Key_Delete) {
- m_keypad.set_to_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 if (event.key() == KeyCode::Key_Backslash) {
- perform_operation(Calculator::Operation::ToggleSign);
- mimic_pressed_button(m_sign_button);
- } else if (event.key() == KeyCode::Key_S) {
- perform_operation(Calculator::Operation::Sqrt);
- mimic_pressed_button(m_sqrt_button);
- } else if (event.key() == KeyCode::Key_Percent) {
- perform_operation(Calculator::Operation::Percent);
- mimic_pressed_button(m_percent_button);
- } else if (event.key() == KeyCode::Key_I) {
- perform_operation(Calculator::Operation::Inverse);
- mimic_pressed_button(m_inverse_button);
- } else if (event.code_point() == '+') {
- perform_operation(Calculator::Operation::Add);
- mimic_pressed_button(m_add_button);
- } else if (event.code_point() == '-') {
- perform_operation(Calculator::Operation::Subtract);
- mimic_pressed_button(m_subtract_button);
- } else if (event.code_point() == '*') {
- perform_operation(Calculator::Operation::Multiply);
- mimic_pressed_button(m_multiply_button);
- } else if (event.code_point() == '/') {
- perform_operation(Calculator::Operation::Divide);
- mimic_pressed_button(m_divide_button);
- } else if (event.code_point() == '%') {
- perform_operation(Calculator::Operation::Percent);
- mimic_pressed_button(m_percent_button);
- }
+ if (event.key() == KeyCode::Key_Return || event.key() == KeyCode::Key_Equal)
+ m_equals_button->click();
+ else if (event.code_point() >= '0' && event.code_point() <= '9')
+ m_digit_button[event.code_point() - '0']->click();
+ else if (event.code_point() == '.')
+ m_decimal_point_button->click();
+ else if (event.key() == KeyCode::Key_Escape || event.key() == KeyCode::Key_Delete)
+ m_clear_button->click();
+ else if (event.key() == KeyCode::Key_Backspace)
+ m_backspace_button->click();
+ else if (event.key() == KeyCode::Key_Backslash)
+ m_sign_button->click();
+ else if (event.key() == KeyCode::Key_S)
+ m_sqrt_button->click();
+ else if (event.key() == KeyCode::Key_Percent)
+ m_percent_button->click();
+ else if (event.key() == KeyCode::Key_I)
+ m_inverse_button->click();
+ else if (event.code_point() == '+')
+ m_add_button->click();
+ else if (event.code_point() == '-')
+ m_subtract_button->click();
+ else if (event.code_point() == '*')
+ m_multiply_button->click();
+ else if (event.code_point() == '/')
+ m_divide_button->click();
+ else if (event.code_point() == '%')
+ m_percent_button->click();
update_display();
}
diff --git a/Userland/Applications/Calculator/CalculatorWidget.h b/Userland/Applications/Calculator/CalculatorWidget.h
index 6f91bf75db..3a6a009d2a 100644
--- a/Userland/Applications/Calculator/CalculatorWidget.h
+++ b/Userland/Applications/Calculator/CalculatorWidget.h
@@ -33,7 +33,6 @@ 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 perform_operation(Calculator::Operation operation);
void update_display();
diff --git a/Userland/Libraries/LibGUI/AbstractButton.cpp b/Userland/Libraries/LibGUI/AbstractButton.cpp
index 7e7dd535df..c66cd8ee01 100644
--- a/Userland/Libraries/LibGUI/AbstractButton.cpp
+++ b/Userland/Libraries/LibGUI/AbstractButton.cpp
@@ -131,12 +131,13 @@ void AbstractButton::mouseup_event(MouseEvent& event)
if (event.button() == m_pressed_mouse_button && m_being_pressed) {
bool was_auto_repeating = m_auto_repeat_timer->is_active();
m_auto_repeat_timer->stop();
- bool was_being_pressed = m_being_pressed;
+ m_was_being_pressed = m_being_pressed;
+ ScopeGuard update_was_being_pressed { [this] { m_was_being_pressed = m_being_pressed; } };
m_being_pressed = false;
m_pressed_mouse_button = MouseButton::None;
if (!is_checkable() || is_checked())
repaint();
- if (was_being_pressed && !was_auto_repeating) {
+ if (m_was_being_pressed && !was_auto_repeating) {
switch (event.button()) {
case MouseButton::Primary:
click(event.modifiers());
diff --git a/Userland/Libraries/LibGUI/AbstractButton.h b/Userland/Libraries/LibGUI/AbstractButton.h
index 716971794e..890d5f7c62 100644
--- a/Userland/Libraries/LibGUI/AbstractButton.h
+++ b/Userland/Libraries/LibGUI/AbstractButton.h
@@ -34,6 +34,7 @@ public:
bool is_hovered() const { return m_hovered; }
bool is_being_pressed() const { return m_being_pressed; }
+ bool was_being_pressed() const { return m_was_being_pressed; }
unsigned allowed_mouse_buttons_for_pressing() const { return m_allowed_mouse_buttons_for_pressing; }
void set_allowed_mouse_buttons_for_pressing(unsigned allowed_buttons) { m_allowed_mouse_buttons_for_pressing = allowed_buttons; }
@@ -66,6 +67,7 @@ private:
bool m_checkable { false };
bool m_hovered { false };
bool m_being_pressed { false };
+ bool m_was_being_pressed { false };
bool m_being_keyboard_pressed { false };
bool m_exclusive { false };
diff --git a/Userland/Libraries/LibGUI/Button.cpp b/Userland/Libraries/LibGUI/Button.cpp
index d3e4ef9113..be46f88ee1 100644
--- a/Userland/Libraries/LibGUI/Button.cpp
+++ b/Userland/Libraries/LibGUI/Button.cpp
@@ -136,6 +136,9 @@ void Button::click(unsigned modifiers)
return;
set_checked(!is_checked());
}
+
+ mimic_pressed();
+
if (on_click)
on_click(modifiers);
if (m_action)
@@ -248,7 +251,7 @@ void Button::set_default(bool default_button)
void Button::mimic_pressed()
{
- if (!is_being_pressed()) {
+ if (!is_being_pressed() && !was_being_pressed()) {
m_mimic_pressed = true;
stop_timer();