diff options
author | Ben Wiederhake <BenWiederhake.GitHub@gmx.de> | 2021-09-07 21:04:35 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-08 10:53:49 +0200 |
commit | 45126655cdda1c04857b238c65211b188a5decb1 (patch) | |
tree | a51c1756f872476fa57a0966d8b40e0f1f3f9ec7 /Userland/Libraries/LibGUI | |
parent | bde3c7301e89bade65231a2f3d2a96eaa1da1e91 (diff) | |
download | serenity-45126655cdda1c04857b238c65211b188a5decb1.zip |
LibGUI+WindowServer: Introduce new mouse tracking mechanism
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r-- | Userland/Libraries/LibGUI/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/MouseTracker.cpp | 36 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/MouseTracker.h | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/WindowServerConnection.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/WindowServerConnection.h | 1 |
5 files changed, 77 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/CMakeLists.txt b/Userland/Libraries/LibGUI/CMakeLists.txt index a3e3ec78e0..abee81eac0 100644 --- a/Userland/Libraries/LibGUI/CMakeLists.txt +++ b/Userland/Libraries/LibGUI/CMakeLists.txt @@ -66,6 +66,7 @@ set(SOURCES Model.cpp ModelIndex.cpp ModelSelection.cpp + MouseTracker.cpp MultiView.cpp Notification.cpp OpacitySlider.cpp diff --git a/Userland/Libraries/LibGUI/MouseTracker.cpp b/Userland/Libraries/LibGUI/MouseTracker.cpp new file mode 100644 index 0000000000..dd9926400a --- /dev/null +++ b/Userland/Libraries/LibGUI/MouseTracker.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibGUI/MouseTracker.h> +#include <LibGUI/WindowServerConnection.h> + +namespace GUI { + +MouseTracker::List MouseTracker::s_trackers; + +MouseTracker::MouseTracker() +{ + if (s_trackers.is_empty()) { + WindowServerConnection::the().async_set_global_mouse_tracking(true); + } + s_trackers.append(*this); +} +MouseTracker::~MouseTracker() +{ + m_list_node.remove(); + if (s_trackers.is_empty()) { + WindowServerConnection::the().async_set_global_mouse_tracking(false); + } +} + +void MouseTracker::track_mouse_move(Badge<WindowServerConnection>, Gfx::IntPoint const& point) +{ + for (auto& tracker : s_trackers) { + tracker.track_mouse_move(point); + } +} + +} diff --git a/Userland/Libraries/LibGUI/MouseTracker.h b/Userland/Libraries/LibGUI/MouseTracker.h new file mode 100644 index 0000000000..0897106248 --- /dev/null +++ b/Userland/Libraries/LibGUI/MouseTracker.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Badge.h> +#include <AK/Function.h> +#include <AK/IntrusiveList.h> +#include <LibGUI/Forward.h> +#include <LibGfx/Point.h> + +namespace GUI { + +class MouseTracker { +public: + MouseTracker(); + virtual ~MouseTracker(); + + static void track_mouse_move(Badge<WindowServerConnection>, Gfx::IntPoint const&); + +protected: + virtual void track_mouse_move(Gfx::IntPoint const&) = 0; + +private: + IntrusiveListNode<MouseTracker> m_list_node; + using List = IntrusiveList<MouseTracker, RawPtr<MouseTracker>, &MouseTracker::m_list_node>; + static List s_trackers; +}; + +} diff --git a/Userland/Libraries/LibGUI/WindowServerConnection.cpp b/Userland/Libraries/LibGUI/WindowServerConnection.cpp index 4ab7b0e710..fb3a21b1d9 100644 --- a/Userland/Libraries/LibGUI/WindowServerConnection.cpp +++ b/Userland/Libraries/LibGUI/WindowServerConnection.cpp @@ -16,6 +16,7 @@ #include <LibGUI/EmojiInputDialog.h> #include <LibGUI/Event.h> #include <LibGUI/Menu.h> +#include <LibGUI/MouseTracker.h> #include <LibGUI/Window.h> #include <LibGUI/WindowServerConnection.h> #include <LibGfx/Bitmap.h> @@ -370,6 +371,11 @@ void WindowServerConnection::display_link_notification() }); } +void WindowServerConnection::track_mouse_move(Gfx::IntPoint const& mouse_position) +{ + MouseTracker::track_mouse_move({}, mouse_position); +} + void WindowServerConnection::ping() { async_pong(); diff --git a/Userland/Libraries/LibGUI/WindowServerConnection.h b/Userland/Libraries/LibGUI/WindowServerConnection.h index f9d4b92751..68d768e915 100644 --- a/Userland/Libraries/LibGUI/WindowServerConnection.h +++ b/Userland/Libraries/LibGUI/WindowServerConnection.h @@ -55,6 +55,7 @@ private: virtual void update_system_fonts(String const&, String const&) override; virtual void window_state_changed(i32, bool, bool) override; virtual void display_link_notification() override; + virtual void track_mouse_move(Gfx::IntPoint const&) override; virtual void ping() override; bool m_display_link_notification_pending { false }; |