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.
This commit is contained in:
Lucas CHOLLET 2023-01-17 17:31:45 -05:00 committed by Andreas Kling
parent 96b3063121
commit d4ef2e226c
Notes: sideshowbarker 2024-07-17 02:23:25 +09:00
5 changed files with 37 additions and 54 deletions

View file

@ -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();
}

View file

@ -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();

View file

@ -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());

View file

@ -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 };

View file

@ -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();