Add custom cursor images (OS-composited, no frame lag)

A cursor painted into the egui scene is composited with the frame, so it
always trails the pointer by a frame or more. This adds a path to hand a
cursor image to the windowing system instead, where the compositor draws
it with no lag.

- egui: PlatformOutput::cursor_image + Context::set_cursor_image().
  CursorImage carries an id alongside the pixels so backends can detect
  "same cursor as last frame" without hashing or comparing the buffer.
- egui-winit: handle_platform_output() stashes the image;
  State::apply_pending_cursor_image() builds the winit CustomCursor and
  applies it. The split is necessary because winit only creates custom
  cursors from an ActiveEventLoop, which handle_platform_output has no
  access to. Rebuilds only when the id changes, and suppresses
  set_cursor_icon while a custom cursor is active so the two don't fight.
- eframe (wgpu): run_ui_and_paint already had the ActiveEventLoop and
  simply wasn't passing it down; now it does, and applies the cursor.
- eframe (web): ignore the new field for now; the web backend could
  express it as a CSS `cursor: url(...)`.

Supported by winit on X11, Wayland, Windows, macOS and Web. The glow
backend compiles unchanged but does not apply cursor images yet.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Skyler Lehmkuhl
2026-07-14 13:02:51 -04:00
co-authored by Claude Opus 4.8
parent eb1756df3f
commit f5477e30e5
6 changed files with 136 additions and 5 deletions
+9 -2
View File
@@ -392,7 +392,7 @@ impl WinitApp for WgpuWinitApp<'_> {
self.initialized_all_windows(event_loop);
if let Some(running) = &mut self.running {
running.run_ui_and_paint(window_id)
running.run_ui_and_paint(event_loop, window_id)
} else {
Ok(EventResult::Wait)
}
@@ -540,7 +540,11 @@ impl WgpuWinitRunning<'_> {
}
/// This is called both for the root viewport, and all deferred viewports
fn run_ui_and_paint(&mut self, window_id: WindowId) -> Result<EventResult> {
fn run_ui_and_paint(
&mut self,
event_loop: &ActiveEventLoop,
window_id: WindowId,
) -> Result<EventResult> {
profiling::function_scope!();
let Some(viewport_id) = self
@@ -675,6 +679,9 @@ impl WgpuWinitRunning<'_> {
};
egui_winit.handle_platform_output(window, platform_output);
// Custom cursor images can only be created from an `ActiveEventLoop`, so they're applied
// here rather than inside `handle_platform_output`.
egui_winit.apply_pending_cursor_image(event_loop, window);
let clipped_primitives = egui_ctx.tessellate(shapes, pixels_per_point);
+1
View File
@@ -321,6 +321,7 @@ impl AppRunner {
let egui::PlatformOutput {
commands,
cursor_icon,
cursor_image: _, // TODO: web could express this as a CSS `cursor: url(...)`
events: _, // already handled
mutable_text_under_cursor: _, // TODO(#4569): https://github.com/emilk/egui/issues/4569
ime,