Refactor window events - #47
Merged
Merged
Conversation
levovix0
reviewed
Aug 6, 2026
| hasFollowingEvent: bool, | ||
| ) = | ||
| var | ||
| ev = event |
Contributor
Author
There was a problem hiding this comment.
the caller doesn't have var ev type since it uses an expression if nextIndex >= 0: events[nextIndex] else: XEvent(). The ev var needs to be modified to loop later.
| <h2 align="center">manual main cycle</h2> | ||
| <h2 align="center">efficient application event loop</h2> | ||
|
|
||
| `runEventDriven` is the simplest efficiently blocking loop. It waits once for native input or an explicit wake and then services every window. The existing `run` remains available for compatibility and preserves its continuously delivered `onTick` events. |
Owner
There was a problem hiding this comment.
Readme is not a changelog. Describe how runEventDriven deals with onTick and how it is diffirent from the regular run
Contributor
Author
There was a problem hiding this comment.
Good point! Updated it.
levovix0
approved these changes
Aug 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds an application-global, efficiently blocking event loop for macOS, Windows, X11, and Wayland. It reduces CPU usage vs the previous event polling design.
Applications can now wait once for all native window events, wake the loop from worker threads or external queues, and service one or more windows without fixed-delay polling. This matches the underlying event design on MacOS and Windows as well.
I worked to make this API additive while keeping the previous
tickbehavior and semantics. The new APIs can be leveraged for applications to gain efficient blocking loop behavior. A future a breaking release could change the defaulttickbehavior.I've manually verified this works and reduces CPU usage to 0% on MacOS, X11, and Wayland. The event loops are also tested in CI as well.
Other Updates
New API
EventLoopWaker is a narrow, copyable capability suitable for worker
threads. Producers enqueue their work first and then call wake;
notifications may be coalesced, while the application queue remains the
source of truth.
Timed waits can also use the next animation deadline instead of polling
every 16 ms.
Platform implementations
macOS
Windows
X11
XInitThreads.
Wayland
Uses poll() on the Wayland display, libdecor, and a self-pipe.
Implements the race-safe prepare_read / flush / poll / read-or-cancel
sequence.
Incorporates keyboard-repeat deadlines into timed waits.
Loads the runtime libwayland-client.so.0 SONAME when the development
symlink is unavailable.
Compatibility
The existing run, runMultiple, and no-argument Window.step() APIs remain
available. step() is now a compatibility wrapper around the global wait
and nonblocking window service.
No wake callback was added to WindowEventsHandler, so its existing
layout remains unchanged.
Additive C API functions are also included:
Examples and documentation
Updates text_input_demo.nim to use deadline-driven waits for cursor
blinking and scroll decay.
Documents worker/queue wakeups and animation scheduling.
Adds a README example for the global event-loop API.
Testing
The cross-platform integration test covers:
Linux CI exercises both display backends:
CI passes on Ubuntu, macOS, and Windows.