Add `OutputCommand::SetPointerPosition` to set mouse position (#5776)

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to test and add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

This is not supported on the web and not yet on Wayland.

~~I also had to update `ring` and add an exception for `paste` being
unmaintained.~~ Has since been updated on master.

* [x] I have followed the instructions in the PR template
This commit is contained in:
Sven Niederberger 2025-03-20 10:51:02 +01:00 committed by GitHub
parent 267485976b
commit b0bbca4e69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 22 additions and 0 deletions

View File

@ -336,6 +336,9 @@ impl AppRunner {
egui::OutputCommand::OpenUrl(open_url) => {
super::open_url(&open_url.url, open_url.new_tab);
}
egui::OutputCommand::SetPointerPosition(_) => {
// Not supported on the web.
}
}
}

View File

@ -850,6 +850,9 @@ impl State {
egui::OutputCommand::OpenUrl(open_url) => {
open_url_in_browser(&open_url.url);
}
egui::OutputCommand::SetPointerPosition(egui::Pos2 { x, y }) => {
let _ = window.set_cursor_position(winit::dpi::LogicalPosition { x, y });
}
}
}

View File

@ -1482,6 +1482,11 @@ impl Context {
self.send_cmd(crate::OutputCommand::CopyImage(image));
}
/// Set the mouse cursor position (if the platform supports it).
pub fn set_pointer_position(&self, position: Pos2) {
self.send_cmd(crate::OutputCommand::SetPointerPosition(position));
}
/// Format the given shortcut in a human-readable way (e.g. `Ctrl+Shift+X`).
///
/// Can be used to get the text for [`crate::Button::shortcut_text`].

View File

@ -95,6 +95,9 @@ pub enum OutputCommand {
/// Open this url in a browser.
OpenUrl(OpenUrl),
/// Set the mouse cursor position (if the platform supports it).
SetPointerPosition(emath::Pos2),
}
/// The non-rendering part of what egui emits each frame.

View File

@ -16,6 +16,14 @@ impl crate::Demo for CursorTest {
impl crate::View for CursorTest {
fn ui(&mut self, ui: &mut egui::Ui) {
if ui
.button("Center pointer in window")
.on_hover_text("The platform may not support this.")
.clicked()
{
let position = ui.ctx().available_rect().center();
ui.ctx().set_pointer_position(position);
}
ui.vertical_centered_justified(|ui| {
ui.heading("Hover to switch cursor icon:");
for &cursor_icon in &egui::CursorIcon::ALL {