Skip to content

Split CursorOptions off of Window #19668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl Default for WindowPlugin {
fn default() -> Self {
WindowPlugin {
primary_window: Some(Window::default()),
primary_cursor_options: Some(CursorOptions::default()),
exit_condition: ExitCondition::OnAllClosed,
close_when_requested: true,
}
Expand All @@ -76,6 +77,13 @@ pub struct WindowPlugin {
/// [`exit_on_all_closed`].
pub primary_window: Option<Window>,

/// Settings for the cursor on the primary window.
///
/// Defaults to `Some(CursorOptions::default())`.
///
/// Has no effect if [`WindowPlugin::primary_window`] is `None`.
pub primary_cursor_options: Option<CursorOptions>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this in for parity with the existing API, but since it's a required component, we could also omit it and let the user override it in an observer for OnAdd, PrimaryWindow

Copy link
Contributor

@ickshonpe ickshonpe Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit tangential but I don't really like how in the window module we use "cursor" to refer to the mouse pointer and in the UI and picking modules we use "pointer". I'd prefer using "pointer" consistently.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point. I'll open an issue for unifying terminology later :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and already resolved: #19688 (comment)


/// Whether to exit the app when there are no open windows.
///
/// If disabling this, ensure that you send the [`bevy_app::AppExit`]
Expand Down Expand Up @@ -122,10 +130,14 @@ impl Plugin for WindowPlugin {
.add_event::<AppLifecycle>();

if let Some(primary_window) = &self.primary_window {
app.world_mut().spawn(primary_window.clone()).insert((
let mut entity_commands = app.world_mut().spawn(primary_window.clone());
entity_commands.insert((
PrimaryWindow,
RawHandleWrapperHolder(Arc::new(Mutex::new(None))),
));
if let Some(primary_cursor_options) = &self.primary_cursor_options {
entity_commands.insert(primary_cursor_options.clone());
}
}

match self.exit_condition {
Expand Down Expand Up @@ -168,7 +180,8 @@ impl Plugin for WindowPlugin {
// Register window descriptor and related types
#[cfg(feature = "bevy_reflect")]
app.register_type::<Window>()
.register_type::<PrimaryWindow>();
.register_type::<PrimaryWindow>()
.register_type::<CursorOptions>();
}
}

Expand Down
9 changes: 3 additions & 6 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,8 @@ impl ContainsEntity for NormalizedWindowRef {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[require(CursorOptions)]
pub struct Window {
/// The cursor options of this window. Cursor icons are set with the `Cursor` component on the
/// window entity.
pub cursor_options: CursorOptions,
/// What presentation mode to give the window.
pub present_mode: PresentMode,
/// Which fullscreen or windowing mode should be used.
Expand Down Expand Up @@ -470,7 +468,6 @@ impl Default for Window {
Self {
title: DEFAULT_WINDOW_TITLE.to_owned(),
name: None,
cursor_options: Default::default(),
present_mode: Default::default(),
mode: Default::default(),
position: Default::default(),
Expand Down Expand Up @@ -728,11 +725,11 @@ impl WindowResizeConstraints {
}

/// Cursor data for a [`Window`].
#[derive(Debug, Clone)]
#[derive(Component, Debug, Clone)]
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Debug, Default, Clone)
reflect(Component, Debug, Default, Clone)
)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use winit::{event_loop::EventLoop, window::WindowId};
use bevy_a11y::AccessibilityRequested;
use bevy_app::{App, Last, Plugin};
use bevy_ecs::prelude::*;
use bevy_window::{exit_on_all_closed, Window, WindowCreated};
use system::{changed_windows, check_keyboard_focus_lost, despawn_windows};
use bevy_window::{exit_on_all_closed, CursorOptions, Window, WindowCreated};
use system::{changed_cursor_options, changed_windows, check_keyboard_focus_lost, despawn_windows};
pub use system::{create_monitors, create_windows};
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
pub use winit::platform::web::CustomCursorExtWebSys;
Expand Down Expand Up @@ -142,6 +142,7 @@ impl<T: BufferedEvent> Plugin for WinitPlugin<T> {
// `exit_on_all_closed` only checks if windows exist but doesn't access data,
// so we don't need to care about its ordering relative to `changed_windows`
changed_windows.ambiguous_with(exit_on_all_closed),
changed_cursor_options,
despawn_windows,
check_keyboard_focus_lost,
)
Expand Down Expand Up @@ -211,6 +212,7 @@ pub type CreateWindowParams<'w, 's, F = ()> = (
(
Entity,
&'static mut Window,
&'static CursorOptions,
Option<&'static RawHandleWrapperHolder>,
),
F,
Expand Down
11 changes: 7 additions & 4 deletions crates/bevy_winit/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use bevy_window::{
WindowScaleFactorChanged, WindowThemeChanged,
};
#[cfg(target_os = "android")]
use bevy_window::{PrimaryWindow, RawHandleWrapper};
use bevy_window::{CursorOptions, PrimaryWindow, RawHandleWrapper};

use crate::{
accessibility::ACCESS_KIT_ADAPTERS,
Expand Down Expand Up @@ -474,7 +474,7 @@ impl<T: BufferedEvent> ApplicationHandler<T> for WinitAppRunnerState<T> {
if let Ok((window_component, mut cache)) = windows.get_mut(self.world_mut(), window)
{
if window_component.is_changed() {
cache.window = window_component.clone();
**cache = window_component.clone();
}
}
});
Expand Down Expand Up @@ -605,10 +605,12 @@ impl<T: BufferedEvent> WinitAppRunnerState<T> {
{
// Get windows that are cached but without raw handles. Those window were already created, but got their
// handle wrapper removed when the app was suspended.

let mut query = self.world_mut()
.query_filtered::<(Entity, &Window), (With<CachedWindow>, Without<RawHandleWrapper>)>();
if let Ok((entity, window)) = query.single(&self.world()) {
.query_filtered::<(Entity, &Window, &CursorOptions), (With<CachedWindow>, Without<RawHandleWrapper>)>();
if let Ok((entity, window, cursor_options)) = query.single(&self.world()) {
let window = window.clone();
let cursor_options = cursor_options.clone();

WINIT_WINDOWS.with_borrow_mut(|winit_windows| {
ACCESS_KIT_ADAPTERS.with_borrow_mut(|adapters| {
Expand All @@ -622,6 +624,7 @@ impl<T: BufferedEvent> WinitAppRunnerState<T> {
event_loop,
entity,
&window,
&cursor_options,
adapters,
&mut handlers,
&accessibility_requested,
Expand Down
Loading