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:
co-authored by
Claude Opus 4.8
parent
eb1756df3f
commit
f5477e30e5
@@ -85,6 +85,15 @@ pub struct State {
|
||||
any_pointer_button_down: bool,
|
||||
current_cursor_icon: Option<egui::CursorIcon>,
|
||||
|
||||
/// Custom cursor image requested by the app this frame, not yet applied.
|
||||
///
|
||||
/// Applying it needs an `ActiveEventLoop` (that's where winit creates custom cursors), which
|
||||
/// `handle_platform_output` doesn't have — so we stash it and let the integration call
|
||||
/// [`State::apply_pending_cursor_image`] from somewhere that does.
|
||||
pending_cursor_image: Option<Option<egui::CursorImage>>,
|
||||
/// `id` of the custom cursor currently set on the window, so we only rebuild it on change.
|
||||
current_cursor_image_id: Option<u64>,
|
||||
|
||||
clipboard: clipboard::Clipboard,
|
||||
|
||||
/// If `true`, mouse inputs will be treated as touches.
|
||||
@@ -141,6 +150,8 @@ impl State {
|
||||
pointer_pos_in_points: None,
|
||||
any_pointer_button_down: false,
|
||||
current_cursor_icon: None,
|
||||
pending_cursor_image: None,
|
||||
current_cursor_image_id: None,
|
||||
|
||||
clipboard: clipboard::Clipboard::new(
|
||||
display_target.display_handle().ok().map(|h| h.as_raw()),
|
||||
@@ -884,9 +895,11 @@ impl State {
|
||||
let contents = contents.replace("\r\n", "\n");
|
||||
if !contents.is_empty() {
|
||||
self.egui_input.events.push(egui::Event::Paste(contents));
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
// Clipboard is empty or unavailable — fall through to push a
|
||||
// Key event so the application can handle paste from its own cache.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -941,6 +954,7 @@ impl State {
|
||||
let egui::PlatformOutput {
|
||||
commands,
|
||||
cursor_icon,
|
||||
cursor_image,
|
||||
events: _, // handled elsewhere
|
||||
mutable_text_under_cursor: _, // only used in eframe web
|
||||
ime,
|
||||
@@ -964,7 +978,18 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
self.set_cursor_icon(window, cursor_icon);
|
||||
// A custom cursor image, when set, *is* the cursor — don't also apply the icon, or we'd
|
||||
// fight with it every frame. Stash it for `apply_pending_cursor_image`, which runs where
|
||||
// an `ActiveEventLoop` is in scope.
|
||||
if cursor_image.is_some() {
|
||||
self.pending_cursor_image = Some(cursor_image);
|
||||
} else {
|
||||
if self.current_cursor_image_id.is_some() {
|
||||
// Custom cursor was just cleared — go back to a normal icon.
|
||||
self.pending_cursor_image = Some(None);
|
||||
}
|
||||
self.set_cursor_icon(window, cursor_icon);
|
||||
}
|
||||
|
||||
// When IME is disabled as a workaround for buggy systems, don't enable IME at all.
|
||||
// This ensures the system doesn't try to intercept keyboard input.
|
||||
@@ -1013,6 +1038,56 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the custom cursor image requested by the last [`Self::handle_platform_output`].
|
||||
///
|
||||
/// Must be called from somewhere with an `ActiveEventLoop` — winit only creates custom cursors
|
||||
/// there. Cheap to call every frame: it's a no-op unless the image actually changed.
|
||||
pub fn apply_pending_cursor_image(
|
||||
&mut self,
|
||||
event_loop: &winit::event_loop::ActiveEventLoop,
|
||||
window: &Window,
|
||||
) {
|
||||
let Some(pending) = self.pending_cursor_image.take() else {
|
||||
return;
|
||||
};
|
||||
|
||||
match pending {
|
||||
Some(image) => {
|
||||
if self.current_cursor_image_id == Some(image.id) {
|
||||
return; // Already set — rebuilding it every frame would be wasteful.
|
||||
}
|
||||
|
||||
let source = winit::window::CustomCursor::from_rgba(
|
||||
image.rgba.as_ref().clone(),
|
||||
image.size.0,
|
||||
image.size.1,
|
||||
image.hotspot.0,
|
||||
image.hotspot.1,
|
||||
);
|
||||
|
||||
match source {
|
||||
Ok(source) => {
|
||||
let cursor = event_loop.create_custom_cursor(source);
|
||||
window.set_cursor_visible(true);
|
||||
window.set_cursor(winit::window::Cursor::Custom(cursor));
|
||||
self.current_cursor_image_id = Some(image.id);
|
||||
// Force the next icon change to be re-applied, since the window is no
|
||||
// longer showing whatever icon `current_cursor_icon` claims.
|
||||
self.current_cursor_icon = None;
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("Failed to build custom cursor: {err}");
|
||||
self.current_cursor_image_id = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
None => {
|
||||
self.current_cursor_image_id = None;
|
||||
self.current_cursor_icon = None; // Force the icon to be re-applied.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn set_cursor_icon(&mut self, window: &Window, cursor_icon: egui::CursorIcon) {
|
||||
if self.current_cursor_icon == Some(cursor_icon) {
|
||||
// Prevent flickering near frame boundary when Windows OS tries to control cursor icon for window resizing.
|
||||
|
||||
Reference in New Issue
Block a user