Struct bevy::window::Window

pub struct Window { /* private fields */ }
Expand description

An operating system window that can present content and receive user input.

To create a window, use a EventWriter<CreateWindow>.

Window Sizes

There are three sizes associated with a window. The physical size which is the height and width in physical pixels on the monitor. The logical size which is the physical size scaled by an operating system provided factor to account for monitors with differing pixel densities or user preference. And the requested size, measured in logical pixels, which is the value submitted to the API when creating the window, or requesting that it be resized.

The actual size, in logical pixels, of the window may not match the requested size due to operating system limits on the window size, or the quantization of the logical size when converting the physical size to the logical size through the scaling factor.

Accessing a Window from a system

To access a Window from a system, use [bevy_ecs::change_detection::ResMut]<crate::Windows>.

Example

fn access_window_system(mut windows: ResMut<Windows>){
    for mut window in windows.iter_mut() {
        window.set_title(String::from("Yay, I'm a window!"));
    }
}

To test code that uses Windows, one can test it with varying Window parameters by creating WindowResizeConstraints or WindowDescriptor structures. values by setting

let resize_constraints = WindowResizeConstraints {
                            min_width: 400.0,
                            min_height: 300.0,
                            max_width: 1280.0,
                            max_height: 1024.0,
};
let window_descriptor = WindowDescriptor {
    width: 800.0,
    height: 600.0,
    resizable: true,
    resize_constraints,
    ..default()
};
let mut window = Window::new(
   WindowId::new(),
   &window_descriptor,
   100, // physical_width
   100, // physical_height
   1.0, // scale_factor
   None, None);

let area = compute_window_area(&window);
assert_eq!(area, 100.0 * 100.0);

grow_window_to_text_size(&mut window, "very long text that does not wrap");
assert_eq!(window.physical_width(), window.requested_width() as u32);
grow_window_to_text_size(&mut window, "very long text that does wrap, creating a maximum width window");
assert_eq!(window.physical_width(), window.requested_width() as u32);

set_new_title(&mut window, "new title".to_string());
let mut found_command = false;
for command in window.drain_commands() {
    if command == (WindowCommand::SetTitle{ title: "new title".to_string() }) {
        found_command = true;
        break;
    }
}
assert_eq!(found_command, true);
}

Implementations§

Creates a new Window.

Get the window’s WindowId.

The current logical width of the window’s client area.

The current logical height of the window’s client area.

The requested window client area width in logical pixels from window creation or the last call to set_resolution.

This may differ from the actual width depending on OS size limits and the scaling factor for high DPI monitors.

The requested window client area height in logical pixels from window creation or the last call to set_resolution.

This may differ from the actual width depending on OS size limits and the scaling factor for high DPI monitors.

The window’s client area width in physical pixels.

The window’s client area height in physical pixels.

The window’s client resize constraint in logical pixels.

The window’s client position in physical pixels.

Set whether or not the window is maximized.

Sets the window to minimized or back.

Platform-specific
  • iOS / Android / Web: Unsupported.
  • Wayland: Un-minimize is unsupported.

Sets the position of the window on the selected monitor in physical pixels.

This automatically un-maximizes the window if it’s maximized.

Platform-specific
  • iOS: Can only be called on the main thread. Sets the top left coordinates of the window in the screen space coordinate system.
  • Web: Sets the top-left coordinates relative to the viewport.
  • Android / Wayland: Unsupported.

Modifies the position of the window to be in the center of the current monitor

Platform-specific
  • iOS: Can only be called on the main thread.
  • Web / Android / Wayland: Unsupported.

Modifies the minimum and maximum window bounds for resizing in logical pixels.

Request the OS to resize the window such the client area matches the specified width and height.

Override the os-reported scaling factor.

The ratio of physical pixels to logical pixels

physical_pixels = logical_pixels * scale_factor

The window scale factor as reported by the window backend.

This value is unaffected by scale_factor_override.

The scale factor set with set_scale_factor_override.

This value may be different from the scale factor reported by the window backend.

Get the window’s title.

Set the window’s title.

Get the window’s PresentMode.

Get the window’s CompositeAlphaMode.

Set the window’s PresentMode.

Get whether or not the window is resizable.

Set whether or not the window is resizable.

Get whether or not decorations are enabled.

(Decorations are the minimize, maximize, and close buttons on desktop apps)

Platform-specific

iOS, Android, and the Web do not have decorations.

Set whether or not decorations are enabled.

(Decorations are the minimize, maximize, and close buttons on desktop apps)

Platform-specific

iOS, Android, and the Web do not have decorations.

Get whether or how the cursor is grabbed.

Platform-specific

Since Windows and macOS have different CursorGrabMode support, it’s possible the value returned here is not the same as the one actually sent to winit.

Set whether and how the cursor is grabbed.

This doesn’t hide the cursor. For that, use set_cursor_visibility

Platform-specific

Since Windows and macOS have different CursorGrabMode support, we first try to set the grab mode that was asked for. If it doesn’t work then use the alternate grab mode.

Get whether or not the cursor is visible.

Platform-specific
  • Windows, X11, and Wayland: The cursor is hidden only when inside the window. To stop the cursor from leaving the window, use set_cursor_grab_mode.
  • macOS: The cursor is hidden only when the window is focused.
  • iOS and Android do not have cursors

Set whether or not the cursor is visible.

Platform-specific
  • Windows, X11, and Wayland: The cursor is hidden only when inside the window. To stop the cursor from leaving the window, use set_cursor_grab_mode.
  • macOS: The cursor is hidden only when the window is focused.
  • iOS and Android do not have cursors

Get the current CursorIcon

Set the CursorIcon

The current mouse position, in physical pixels.

The current mouse position, in logical pixels, taking into account the screen scale factor.

Set the cursor’s position

Get the window’s WindowMode

Set the window’s WindowMode

Close the operating system window corresponding to this Window.

This will also lead to this Window being removed from the Windows resource.

If the default WindowPlugin is used, when no windows are open, the app will exit. To disable this behaviour, set exit_on_all_closed on the WindowPlugin to false

Get whether or not the window has focus.

A window loses focus when the user switches to another window, and regains focus when the user uses the window again

Get the RawHandleWrapper corresponding to this window if set.

During normal use, this can be safely unwrapped; the value should only be None when synthetically constructed for tests.

The “html canvas” element selector.

If set, this selector will be used to find a matching html canvas element, rather than creating a new one. Uses the CSS selector format.

This value has no effect on non-web platforms.

Whether or not to fit the canvas element’s size to its parent element’s size.

Warning: this will not behave as expected for parents that set their size according to the size of their children. This creates a “feedback loop” that will result in the canvas growing on each resize. When using this feature, ensure the parent’s size is not affected by its children.

This value has no effect on non-web platforms.

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more
Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more
Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more
Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more
Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more