From 558fd5b1661070caa08e0f3f1f00f661e7226001 Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Thu, 6 Jan 2022 00:55:48 +0100 Subject: LibCore: Make `EventLoop::pump()` return event count Sometimes, pumping the event loop will cause new events to be generated. For example, an IPC message could be delivered which then dispatches a new event to be handled by the GUI. To the invoker of `EventLoop::pump()`, it is not obvious if any events were processed at all. Libraries like SDL2 might not have the opportunity to run the event loop often enough that events can be processed swiftly, since it might spend time doing other things. This can result in stuttering GUI interactions. This changes `EventLoop::pump()` to return the number of processed events. This functionality will be used by our SDL2 port in another PR. --- Userland/Libraries/LibCore/EventLoop.cpp | 9 ++++++--- Userland/Libraries/LibCore/EventLoop.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibCore/EventLoop.cpp b/Userland/Libraries/LibCore/EventLoop.cpp index 7859a9429d..40b5608747 100644 --- a/Userland/Libraries/LibCore/EventLoop.cpp +++ b/Userland/Libraries/LibCore/EventLoop.cpp @@ -379,7 +379,7 @@ void EventLoop::spin_until(Function goal_condition) pump(); } -void EventLoop::pump(WaitMode mode) +size_t EventLoop::pump(WaitMode mode) { wait_for_event(mode); @@ -389,6 +389,7 @@ void EventLoop::pump(WaitMode mode) events = move(m_queued_events); } + size_t processed_events = 0; for (size_t i = 0; i < events.size(); ++i) { auto& queued_event = events.at(i); auto receiver = queued_event.receiver.strong_ref(); @@ -400,7 +401,6 @@ void EventLoop::pump(WaitMode mode) switch (event.type()) { case Event::Quit: VERIFY_NOT_REACHED(); - return; default: dbgln_if(EVENTLOOP_DEBUG, "Event type {} with no receiver :(", event.type()); break; @@ -412,6 +412,7 @@ void EventLoop::pump(WaitMode mode) NonnullRefPtr protector(*receiver); receiver->dispatch_event(event); } + ++processed_events; if (m_exit_requested) { Threading::MutexLocker locker(m_private->lock); @@ -422,9 +423,11 @@ void EventLoop::pump(WaitMode mode) new_event_queue.unchecked_append(move(events[i])); new_event_queue.extend(move(m_queued_events)); m_queued_events = move(new_event_queue); - return; + break; } } + + return processed_events; } void EventLoop::post_event(Object& receiver, NonnullOwnPtr&& event) diff --git a/Userland/Libraries/LibCore/EventLoop.h b/Userland/Libraries/LibCore/EventLoop.h index 9b1a660e80..672b8b3f2d 100644 --- a/Userland/Libraries/LibCore/EventLoop.h +++ b/Userland/Libraries/LibCore/EventLoop.h @@ -42,7 +42,7 @@ public: // processe events, generally called by exec() in a loop. // this should really only be used for integrating with other event loops - void pump(WaitMode = WaitMode::WaitForEvents); + size_t pump(WaitMode = WaitMode::WaitForEvents); void spin_until(Function); -- cgit v1.2.3