summaryrefslogtreecommitdiff
path: root/Servers/WindowServer
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-05-15 22:17:09 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-15 22:45:28 +0200
commita4b0dfff431579b45fc4d4c6439e74d79fc0e859 (patch)
tree9f524d58f26dd75a950be8a66f6e668eca366bad /Servers/WindowServer
parent3cba2a8a7872794516488ac5dfe240e946b1288d (diff)
downloadserenity-a4b0dfff431579b45fc4d4c6439e74d79fc0e859.zip
Move double click events from LibGUI to the window server
Diffstat (limited to 'Servers/WindowServer')
-rw-r--r--Servers/WindowServer/WSAPITypes.h1
-rw-r--r--Servers/WindowServer/WSEvent.h3
-rw-r--r--Servers/WindowServer/WSWindow.cpp1
-rw-r--r--Servers/WindowServer/WSWindowManager.cpp62
-rw-r--r--Servers/WindowServer/WSWindowManager.h20
5 files changed, 80 insertions, 7 deletions
diff --git a/Servers/WindowServer/WSAPITypes.h b/Servers/WindowServer/WSAPITypes.h
index aa7f22f5bd..7ba9095516 100644
--- a/Servers/WindowServer/WSAPITypes.h
+++ b/Servers/WindowServer/WSAPITypes.h
@@ -73,6 +73,7 @@ struct WSAPI_ServerMessage {
Paint,
MouseMove,
MouseDown,
+ MouseDoubleClick,
MouseUp,
MouseWheel,
WindowEntered,
diff --git a/Servers/WindowServer/WSEvent.h b/Servers/WindowServer/WSEvent.h
index 8dc1f67fea..d894044e21 100644
--- a/Servers/WindowServer/WSEvent.h
+++ b/Servers/WindowServer/WSEvent.h
@@ -17,6 +17,7 @@ public:
WM_ClientDisconnected,
MouseMove,
MouseDown,
+ MouseDoubleClick,
MouseUp,
MouseWheel,
WindowEntered,
@@ -75,7 +76,7 @@ public:
virtual ~WSEvent() { }
bool is_client_request() const { return type() > __Begin_API_Client_Requests && type() < __End_API_Client_Requests; }
- bool is_mouse_event() const { return type() == MouseMove || type() == MouseDown || type() == MouseUp || type() == MouseWheel; }
+ bool is_mouse_event() const { return type() == MouseMove || type() == MouseDown || type() == MouseDoubleClick || type() == MouseUp || type() == MouseWheel; }
bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
};
diff --git a/Servers/WindowServer/WSWindow.cpp b/Servers/WindowServer/WSWindow.cpp
index 0295b1fd81..88561122c8 100644
--- a/Servers/WindowServer/WSWindow.cpp
+++ b/Servers/WindowServer/WSWindow.cpp
@@ -94,6 +94,7 @@ void WSWindow::handle_mouse_event(const WSMouseEvent& event)
switch (event.type()) {
case WSEvent::MouseMove: server_message.type = WSAPI_ServerMessage::Type::MouseMove; break;
case WSEvent::MouseDown: server_message.type = WSAPI_ServerMessage::Type::MouseDown; break;
+ case WSEvent::MouseDoubleClick: server_message.type = WSAPI_ServerMessage::Type::MouseDoubleClick; break;
case WSEvent::MouseUp: server_message.type = WSAPI_ServerMessage::Type::MouseUp; break;
case WSEvent::MouseWheel: server_message.type = WSAPI_ServerMessage::Type::MouseWheel; break;
default: ASSERT_NOT_REACHED();
diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp
index 0a7116c755..5605f30fdd 100644
--- a/Servers/WindowServer/WSWindowManager.cpp
+++ b/Servers/WindowServer/WSWindowManager.cpp
@@ -658,7 +658,58 @@ void WSWindowManager::set_cursor_tracking_button(WSButton* button)
m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
}
-void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*& hovered_window)
+CElapsedTimer& WSWindowManager::DoubleClickInfo::click_clock(MouseButton button)
+{
+ switch (button) {
+ case MouseButton::Left: return m_left_click_clock;
+ case MouseButton::Right: return m_right_click_clock;
+ case MouseButton::Middle: return m_middle_click_clock;
+ default:
+ ASSERT_NOT_REACHED();
+ }
+}
+
+// #define DOUBLECLICK_DEBUG
+
+void WSWindowManager::deliver_mouse_event(WSWindow& window, WSMouseEvent& event)
+{
+ if (event.type() == WSEvent::MouseUp) {
+ if (&window != m_double_click_info.m_clicked_window) {
+ // we either haven't clicked anywhere, or we haven't clicked on this
+ // window. set the current click window, and reset the timers.
+#if defined(DOUBLECLICK_DEBUG)
+ dbgprintf("Initial mouseup on window %p (previous was %p)\n", &window, m_double_click_info.m_clicked_window);
+#endif
+ m_double_click_info.m_clicked_window = window.make_weak_ptr();
+ m_double_click_info.reset();
+ }
+
+ auto& clock = m_double_click_info.click_clock(event.button());
+
+ // if the clock is invalid, we haven't clicked with this button on this
+ // window yet, so there's nothing to do.
+ if (clock.is_valid()) {
+ int elapsed_since_last_click = clock.elapsed();
+ clock.start();
+
+ // FIXME: It might be a sensible idea to also add a distance travel check.
+ // If the pointer moves too far, it's not a double click.
+ if (elapsed_since_last_click < 250) {
+#if defined(DOUBLECLICK_DEBUG)
+ dbgprintf("Transforming MouseUp to MouseDoubleClick!\n");
+#endif
+ event = WSMouseEvent(WSEvent::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
+ }
+ }
+
+ // start (or re-start, if it was invalid) the double click timer again.
+ clock.start();
+ }
+
+ window.event(event);
+}
+
+void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& hovered_window)
{
hovered_window = nullptr;
@@ -684,7 +735,7 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
ASSERT(!window->is_minimized()); // Maybe this should also be supported? Idk.
windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
auto translated_event = event.translated(-window->position());
- window->event(translated_event);
+ deliver_mouse_event(*window, translated_event);
}
if (menubar_rect().contains(event.position())) {
@@ -703,7 +754,7 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
} else {
hovered_window = &window;
auto translated_event = event.translated(-window.position());
- window.event(translated_event);
+ deliver_mouse_event(window, translated_event);
}
return;
}
@@ -736,10 +787,11 @@ void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*&
if (window.rect().contains(event.position())) {
if (window.type() == WSWindowType::Normal && event.type() == WSEvent::MouseDown)
move_to_front_and_make_active(window);
+
hovered_window = &window;
if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window)) {
auto translated_event = event.translated(-window.position());
- window.event(translated_event);
+ deliver_mouse_event(window, translated_event);
}
return IterationDecision::Abort;
}
@@ -1004,7 +1056,7 @@ void WSWindowManager::event(CEvent& event)
{
if (static_cast<WSEvent&>(event).is_mouse_event()) {
WSWindow* hovered_window = nullptr;
- process_mouse_event(static_cast<const WSMouseEvent&>(event), hovered_window);
+ process_mouse_event(static_cast<WSMouseEvent&>(event), hovered_window);
set_hovered_window(hovered_window);
return;
}
diff --git a/Servers/WindowServer/WSWindowManager.h b/Servers/WindowServer/WSWindowManager.h
index c3a4cd1671..4c11c5e95f 100644
--- a/Servers/WindowServer/WSWindowManager.h
+++ b/Servers/WindowServer/WSWindowManager.h
@@ -15,6 +15,7 @@
#include <WindowServer/WSCursor.h>
#include <WindowServer/WSEvent.h>
#include <WindowServer/WSCPUMonitor.h>
+#include <LibCore/CElapsedTimer.h>
class WSAPIClientRequest;
class WSScreen;
@@ -122,7 +123,8 @@ public:
void start_window_resize(WSWindow&, const WSMouseEvent&);
private:
- void process_mouse_event(const WSMouseEvent&, WSWindow*& hovered_window);
+ void process_mouse_event(WSMouseEvent&, WSWindow*& hovered_window);
+ void deliver_mouse_event(WSWindow& window, WSMouseEvent& event);
bool process_ongoing_window_resize(const WSMouseEvent&, WSWindow*& hovered_window);
bool process_ongoing_window_drag(const WSMouseEvent&, WSWindow*& hovered_window);
void handle_menu_mouse_event(WSMenu&, const WSMouseEvent&);
@@ -171,6 +173,22 @@ private:
HashTable<WSWindow*> m_windows;
InlineLinkedList<WSWindow> m_windows_in_order;
+ struct DoubleClickInfo
+ {
+ CElapsedTimer& click_clock(MouseButton);
+ void reset() {
+ m_left_click_clock = CElapsedTimer();
+ m_right_click_clock = CElapsedTimer();
+ m_middle_click_clock = CElapsedTimer();
+ }
+
+ WeakPtr<WSWindow> m_clicked_window;
+ CElapsedTimer m_left_click_clock;
+ CElapsedTimer m_right_click_clock;
+ CElapsedTimer m_middle_click_clock;
+ };
+ DoubleClickInfo m_double_click_info;
+
WeakPtr<WSWindow> m_active_window;
WeakPtr<WSWindow> m_hovered_window;
WeakPtr<WSWindow> m_highlight_window;