diff options
author | networkException <git@nwex.de> | 2022-07-10 17:03:22 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-07-14 13:17:33 +0200 |
commit | 18c84d2e63f4af6342639820882c7c081516cedf (patch) | |
tree | af3d1490954d690c8e0582c63f4ff90473bc0fa0 | |
parent | 37b4133c5154157664100dbfb268aa9008a2b832 (diff) | |
download | serenity-18c84d2e63f4af6342639820882c7c081516cedf.zip |
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
-rw-r--r-- | Userland/Libraries/LibGUI/AbstractButton.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/AbstractButton.h | 6 |
2 files changed, 21 insertions, 5 deletions
diff --git a/Userland/Libraries/LibGUI/AbstractButton.cpp b/Userland/Libraries/LibGUI/AbstractButton.cpp index be77913634..12d9043e9a 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.cpp +++ b/Userland/Libraries/LibGUI/AbstractButton.cpp @@ -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); } diff --git a/Userland/Libraries/LibGUI/AbstractButton.h b/Userland/Libraries/LibGUI/AbstractButton.h index 0a29c8b9fc..3d14e5c0c4 100644 --- a/Userland/Libraries/LibGUI/AbstractButton.h +++ b/Userland/Libraries/LibGUI/AbstractButton.h @@ -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; }; |