LibGUI+WindowServer: Introduce new mouse tracking mechanism
This commit is contained in:
parent
bde3c7301e
commit
45126655cd
Notes:
sideshowbarker
2024-07-18 04:27:55 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/45126655cdd Pull-request: https://github.com/SerenityOS/serenity/pull/9881
10 changed files with 94 additions and 3 deletions
|
@ -66,6 +66,7 @@ set(SOURCES
|
|||
Model.cpp
|
||||
ModelIndex.cpp
|
||||
ModelSelection.cpp
|
||||
MouseTracker.cpp
|
||||
MultiView.cpp
|
||||
Notification.cpp
|
||||
OpacitySlider.cpp
|
||||
|
|
36
Userland/Libraries/LibGUI/MouseTracker.cpp
Normal file
36
Userland/Libraries/LibGUI/MouseTracker.cpp
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
33
Userland/Libraries/LibGUI/MouseTracker.h
Normal file
33
Userland/Libraries/LibGUI/MouseTracker.h
Normal file
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
|
|
@ -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 };
|
||||
|
|
|
@ -663,6 +663,11 @@ void ClientConnection::set_global_cursor_tracking(i32 window_id, bool enabled)
|
|||
it->value->set_global_cursor_tracking_enabled(enabled);
|
||||
}
|
||||
|
||||
void ClientConnection::set_global_mouse_tracking(bool enabled)
|
||||
{
|
||||
m_does_global_mouse_tracking = enabled;
|
||||
}
|
||||
|
||||
void ClientConnection::set_window_cursor(i32 window_id, i32 cursor_type)
|
||||
{
|
||||
auto it = m_windows.find(window_id);
|
||||
|
|
|
@ -38,6 +38,7 @@ public:
|
|||
~ClientConnection() override;
|
||||
|
||||
bool is_unresponsive() const { return m_unresponsive; }
|
||||
bool does_global_mouse_tracking() const { return m_does_global_mouse_tracking; }
|
||||
|
||||
static ClientConnection* from_client_id(int client_id);
|
||||
static void for_each_client(Function<void(ClientConnection&)>);
|
||||
|
@ -113,6 +114,7 @@ private:
|
|||
virtual void invalidate_rect(i32, Vector<Gfx::IntRect> const&, bool) override;
|
||||
virtual void did_finish_painting(i32, Vector<Gfx::IntRect> const&) override;
|
||||
virtual void set_global_cursor_tracking(i32, bool) override;
|
||||
virtual void set_global_mouse_tracking(bool) override;
|
||||
virtual void set_window_opacity(i32, float) override;
|
||||
virtual void set_window_backing_store(i32, i32, i32, IPC::File const&, i32, bool, Gfx::IntSize const&, bool) override;
|
||||
virtual void set_window_has_alpha_channel(i32, bool) override;
|
||||
|
@ -179,6 +181,7 @@ private:
|
|||
bool m_has_display_link { false };
|
||||
bool m_show_screen_number { false };
|
||||
bool m_unresponsive { false };
|
||||
bool m_does_global_mouse_tracking { false };
|
||||
|
||||
// Need this to get private client connection stuff
|
||||
friend WMClientConnection;
|
||||
|
|
|
@ -44,5 +44,7 @@ endpoint WindowClient
|
|||
|
||||
display_link_notification() =|
|
||||
|
||||
track_mouse_move(Gfx::IntPoint mouse_position) =|
|
||||
|
||||
ping() =|
|
||||
}
|
||||
|
|
|
@ -1200,15 +1200,18 @@ void WindowManager::process_mouse_event(MouseEvent& event)
|
|||
if (process_ongoing_drag(event))
|
||||
return;
|
||||
|
||||
// 2. Send the mouse event to all windows with global cursor tracking enabled.
|
||||
// The active input tracking window is excluded here because we're sending the event to it
|
||||
// in the next step.
|
||||
// 2. Send the mouse event to all clients with global cursor tracking enabled.
|
||||
auto& window_stack = current_window_stack();
|
||||
for_each_visible_window_from_front_to_back([&](Window& window) {
|
||||
if (window.global_cursor_tracking() && &window != window_stack.active_input_tracking_window())
|
||||
deliver_mouse_event(window, event, false);
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
ClientConnection::for_each_client([&](ClientConnection& conn) {
|
||||
if (conn.does_global_mouse_tracking()) {
|
||||
conn.async_track_mouse_move(event.position());
|
||||
}
|
||||
});
|
||||
|
||||
// 3. If there's an active input tracking window, all mouse events go there.
|
||||
// Tracking ends after all mouse buttons have been released.
|
||||
|
|
|
@ -75,6 +75,7 @@ endpoint WindowServer
|
|||
did_finish_painting(i32 window_id, Vector<Gfx::IntRect> rects) =|
|
||||
|
||||
set_global_cursor_tracking(i32 window_id, bool enabled) =|
|
||||
set_global_mouse_tracking(bool enabled) =|
|
||||
set_window_opacity(i32 window_id, float opacity) =|
|
||||
|
||||
set_window_alpha_hit_threshold(i32 window_id, float threshold) =|
|
||||
|
|
Loading…
Add table
Reference in a new issue