From 07873332e79d257a3ee6a26a3139b3420c8f34d2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 12 Jan 2019 06:57:17 +0100 Subject: [PATCH] Coalesce mouse events to make the GUI go fast. A simple but effective optimization that avoids tons of redraw. :^) --- Widgets/EventLoop.cpp | 17 ++++++++++++++++- Widgets/Rect.cpp | 2 -- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Widgets/EventLoop.cpp b/Widgets/EventLoop.cpp index f470201f58..d4c026cebc 100644 --- a/Widgets/EventLoop.cpp +++ b/Widgets/EventLoop.cpp @@ -66,11 +66,26 @@ void EventLoop::postEvent(Object* receiver, OwnPtr&& event) void EventLoop::waitForEvent() { auto& mouse = PS2MouseDevice::the(); + auto& screen = AbstractScreen::the(); + bool prev_left_button = screen.left_mouse_button_pressed(); + bool prev_right_button = screen.right_mouse_button_pressed(); + int dx = 0; + int dy = 0; while (mouse.has_data_available_for_reading()) { signed_byte data[3]; ssize_t nread = mouse.read((byte*)data, 3); ASSERT(nread == 3); - AbstractScreen::the().on_receive_mouse_data(data[1], -data[2], data[0] & 1, data[0] & 2); + bool left_button = data[0] & 1; + bool right_button = data[0] & 2; + dx += data[1]; + dy += -data[2]; + if (left_button != prev_left_button || right_button != prev_right_button || !mouse.has_data_available_for_reading()) { + prev_left_button = left_button; + prev_right_button = right_button; + screen.on_receive_mouse_data(dx, dy, left_button, right_button); + dx = 0; + dy = 0; + } } } #endif diff --git a/Widgets/Rect.cpp b/Widgets/Rect.cpp index fa4aa7aef9..4656a82f0f 100644 --- a/Widgets/Rect.cpp +++ b/Widgets/Rect.cpp @@ -19,6 +19,4 @@ void Rect::intersect(const Rect& other) m_location.setY(t); m_size.setWidth((r - l) + 1); m_size.setHeight((b - t) + 1); - - dbgprintf("intersection result: %d,%d %dx%d\n", x(), y(), width(), height()); }