mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 06:28:13 +00:00
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.
This commit is contained in:
parent
fa902cd5d5
commit
558fd5b166
2 changed files with 7 additions and 4 deletions
|
@ -379,7 +379,7 @@ void EventLoop::spin_until(Function<bool()> 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<Object> 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>&& event)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue