LibGUI: Allow specifying the mouse buttons able to press down a button

This patch adds the ability to modify the set of mouse buttons able to
press down a button
This commit is contained in:
networkException 2022-07-10 17:03:22 +02:00 committed by Andreas Kling
parent 37b4133c51
commit 18c84d2e63
2 changed files with 21 additions and 5 deletions

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
* Copyright (c) 2022, Jakob-Niklas See <git@nwex.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -93,7 +94,7 @@ void AbstractButton::mousemove_event(MouseEvent& event)
{
bool is_over = rect().contains(event.position());
m_hovered = is_over;
if (event.buttons() & MouseButton::Primary) {
if (event.buttons() & m_pressed_mouse_button) {
bool being_pressed = is_over;
if (being_pressed != m_being_pressed) {
m_being_pressed = being_pressed;
@ -111,8 +112,9 @@ void AbstractButton::mousemove_event(MouseEvent& event)
void AbstractButton::mousedown_event(MouseEvent& event)
{
if (event.button() == MouseButton::Primary) {
if (event.button() & m_allowed_mouse_buttons_for_pressing) {
m_being_pressed = true;
m_pressed_mouse_button = event.button();
repaint();
if (m_auto_repeat_interval) {
@ -126,14 +128,22 @@ void AbstractButton::mousedown_event(MouseEvent& event)
void AbstractButton::mouseup_event(MouseEvent& event)
{
if (event.button() == MouseButton::Primary && m_being_pressed) {
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_being_pressed = false;
m_pressed_mouse_button = MouseButton::None;
repaint();
if (was_being_pressed && !was_auto_repeating)
click(event.modifiers());
if (was_being_pressed && !was_auto_repeating) {
switch (event.button()) {
case MouseButton::Primary:
click(event.modifiers());
break;
default:
VERIFY_NOT_REACHED();
}
}
}
Widget::mouseup_event(event);
}

View file

@ -35,6 +35,9 @@ public:
bool is_hovered() const { return m_hovered; }
bool is_being_pressed() const { return m_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; }
virtual void click(unsigned modifiers = 0) = 0;
virtual bool is_uncheckable() const { return true; }
@ -65,6 +68,9 @@ private:
bool m_being_keyboard_pressed { false };
bool m_exclusive { false };
MouseButton m_pressed_mouse_button { MouseButton::None };
unsigned m_allowed_mouse_buttons_for_pressing { MouseButton::Primary };
int m_auto_repeat_interval { 0 };
RefPtr<Core::Timer> m_auto_repeat_timer;
};