2021-01-01 21:31:22 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2023-01-19 00:38:28 +00:00
|
|
|
use bevy_ecs::entity::Entity;
|
2021-01-25 04:06:06 +00:00
|
|
|
use bevy_math::{IVec2, Vec2};
|
2022-12-09 01:20:44 +00:00
|
|
|
use bevy_reflect::{FromReflect, Reflect};
|
|
|
|
|
|
|
|
#[cfg(feature = "serialize")]
|
|
|
|
use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
|
2020-04-19 19:13:04 +00:00
|
|
|
|
2022-06-16 13:20:37 +00:00
|
|
|
/// A window event that is sent whenever a window's logical size has changed.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-04-19 19:13:04 +00:00
|
|
|
pub struct WindowResized {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that has changed.
|
|
|
|
pub window: Entity,
|
2022-06-16 13:20:37 +00:00
|
|
|
/// The new logical width of the window.
|
2020-12-07 21:32:57 +00:00
|
|
|
pub width: f32,
|
2022-06-16 13:20:37 +00:00
|
|
|
/// The new logical height of the window.
|
2020-12-07 21:32:57 +00:00
|
|
|
pub height: f32,
|
2020-04-19 19:13:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 00:38:28 +00:00
|
|
|
// TODO: This would redraw all windows ? If yes, update docs to reflect this
|
Reduce power usage with configurable event loop (#3974)
# Objective
- Reduce power usage for games when not focused.
- Reduce power usage to ~0 when a desktop application is minimized (opt-in).
- Reduce power usage when focused, only updating on a `winit` event, or the user sends a redraw request. (opt-in)
https://user-images.githubusercontent.com/2632925/156904387-ec47d7de-7f06-4c6f-8aaf-1e952c1153a2.mp4
Note resource usage in the Task Manager in the above video.
## Solution
- Added a type `UpdateMode` that allows users to specify how the winit event loop is updated, without exposing winit types.
- Added two fields to `WinitConfig`, both with the `UpdateMode` type. One configures how the application updates when focused, and the other configures how the application behaves when it is not focused. Users can modify this resource manually to set the type of event loop control flow they want.
- For convenience, two functions were added to `WinitConfig`, that provide reasonable presets: `game()` (default) and `desktop_app()`.
- The `game()` preset, which is used by default, is unchanged from current behavior with one exception: when the app is out of focus the app updates at a minimum of 10fps, or every time a winit event is received. This has a huge positive impact on power use and responsiveness on my machine, which will otherwise continue running the app at many hundreds of fps when out of focus or minimized.
- The `desktop_app()` preset is fully reactive, only updating when user input (winit event) is supplied or a `RedrawRequest` event is sent. When the app is out of focus, it only updates on `Window` events - i.e. any winit event that directly interacts with the window. What this means in practice is that the app uses *zero* resources when minimized or not interacted with, but still updates fluidly when the app is out of focus and the user mouses over the application.
- Added a `RedrawRequest` event so users can force an update even if there are no events. This is useful in an application when you want to, say, run an animation even when the user isn't providing input.
- Added an example `low_power` to demonstrate these changes
## Usage
Configuring the event loop:
```rs
use bevy::winit::{WinitConfig};
// ...
.insert_resource(WinitConfig::desktop_app()) // preset
// or
.insert_resource(WinitConfig::game()) // preset
// or
.insert_resource(WinitConfig{ .. }) // manual
```
Requesting a redraw:
```rs
use bevy::window::RequestRedraw;
// ...
fn request_redraw(mut event: EventWriter<RequestRedraw>) {
event.send(RequestRedraw);
}
```
## Other details
- Because we have a single event loop for multiple windows, every time I've mentioned "focused" above, I more precisely mean, "if at least one bevy window is focused".
- Due to a platform bug in winit (https://github.com/rust-windowing/winit/issues/1619), we can't simply use `Window::request_redraw()`. As a workaround, this PR will temporarily set the window mode to `Poll` when a redraw is requested. This is then reset to the user's `WinitConfig` setting on the next frame.
2022-03-07 23:32:05 +00:00
|
|
|
/// An event that indicates the window should redraw, even if its control flow is set to `Wait` and
|
|
|
|
/// there have been no window events.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
Reduce power usage with configurable event loop (#3974)
# Objective
- Reduce power usage for games when not focused.
- Reduce power usage to ~0 when a desktop application is minimized (opt-in).
- Reduce power usage when focused, only updating on a `winit` event, or the user sends a redraw request. (opt-in)
https://user-images.githubusercontent.com/2632925/156904387-ec47d7de-7f06-4c6f-8aaf-1e952c1153a2.mp4
Note resource usage in the Task Manager in the above video.
## Solution
- Added a type `UpdateMode` that allows users to specify how the winit event loop is updated, without exposing winit types.
- Added two fields to `WinitConfig`, both with the `UpdateMode` type. One configures how the application updates when focused, and the other configures how the application behaves when it is not focused. Users can modify this resource manually to set the type of event loop control flow they want.
- For convenience, two functions were added to `WinitConfig`, that provide reasonable presets: `game()` (default) and `desktop_app()`.
- The `game()` preset, which is used by default, is unchanged from current behavior with one exception: when the app is out of focus the app updates at a minimum of 10fps, or every time a winit event is received. This has a huge positive impact on power use and responsiveness on my machine, which will otherwise continue running the app at many hundreds of fps when out of focus or minimized.
- The `desktop_app()` preset is fully reactive, only updating when user input (winit event) is supplied or a `RedrawRequest` event is sent. When the app is out of focus, it only updates on `Window` events - i.e. any winit event that directly interacts with the window. What this means in practice is that the app uses *zero* resources when minimized or not interacted with, but still updates fluidly when the app is out of focus and the user mouses over the application.
- Added a `RedrawRequest` event so users can force an update even if there are no events. This is useful in an application when you want to, say, run an animation even when the user isn't providing input.
- Added an example `low_power` to demonstrate these changes
## Usage
Configuring the event loop:
```rs
use bevy::winit::{WinitConfig};
// ...
.insert_resource(WinitConfig::desktop_app()) // preset
// or
.insert_resource(WinitConfig::game()) // preset
// or
.insert_resource(WinitConfig{ .. }) // manual
```
Requesting a redraw:
```rs
use bevy::window::RequestRedraw;
// ...
fn request_redraw(mut event: EventWriter<RequestRedraw>) {
event.send(RequestRedraw);
}
```
## Other details
- Because we have a single event loop for multiple windows, every time I've mentioned "focused" above, I more precisely mean, "if at least one bevy window is focused".
- Due to a platform bug in winit (https://github.com/rust-windowing/winit/issues/1619), we can't simply use `Window::request_redraw()`. As a workaround, this PR will temporarily set the window mode to `Poll` when a redraw is requested. This is then reset to the user's `WinitConfig` setting on the next frame.
2022-03-07 23:32:05 +00:00
|
|
|
pub struct RequestRedraw;
|
|
|
|
|
2022-05-05 13:35:43 +00:00
|
|
|
/// An event that is sent whenever a new window is created.
|
|
|
|
///
|
2023-01-19 00:38:28 +00:00
|
|
|
/// To create a new window, spawn an entity with a [`crate::Window`] on it.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2022-05-05 13:35:43 +00:00
|
|
|
pub struct WindowCreated {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that has been created.
|
|
|
|
pub window: Entity,
|
2020-04-19 19:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 13:35:43 +00:00
|
|
|
/// An event that is sent whenever the operating systems requests that a window
|
|
|
|
/// be closed. This will be sent when the close button of the window is pressed.
|
|
|
|
///
|
|
|
|
/// If the default [`WindowPlugin`] is used, these events are handled
|
2023-01-19 00:38:28 +00:00
|
|
|
/// by closing the corresponding [`Window`].
|
2022-05-05 13:35:43 +00:00
|
|
|
/// To disable this behaviour, set `close_when_requested` on the [`WindowPlugin`]
|
|
|
|
/// to `false`.
|
|
|
|
///
|
|
|
|
/// [`WindowPlugin`]: crate::WindowPlugin
|
|
|
|
/// [`Window`]: crate::Window
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2022-05-05 13:35:43 +00:00
|
|
|
pub struct WindowCloseRequested {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window to close.
|
|
|
|
pub window: Entity,
|
2020-04-19 19:13:04 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 00:38:28 +00:00
|
|
|
/// An event that is sent whenever a window is closed. This will be sent when
|
|
|
|
/// the window entity loses its `Window` component or is despawned.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2022-05-05 13:35:43 +00:00
|
|
|
pub struct WindowClosed {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that has been closed.
|
|
|
|
///
|
|
|
|
/// Note that this entity probably no longer exists
|
|
|
|
/// by the time this event is received.
|
|
|
|
pub window: Entity,
|
2020-04-25 01:55:15 +00:00
|
|
|
}
|
2023-01-19 00:38:28 +00:00
|
|
|
/// An event reporting that the mouse cursor has moved inside a window.
|
2022-06-26 13:40:43 +00:00
|
|
|
///
|
|
|
|
/// The event is sent only if the cursor is over one of the application's windows.
|
|
|
|
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate.
|
|
|
|
///
|
|
|
|
/// Not to be confused with the [`MouseMotion`] event from `bevy_input`.
|
|
|
|
///
|
|
|
|
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
|
|
|
|
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-06-04 06:22:32 +00:00
|
|
|
pub struct CursorMoved {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that the cursor moved inside.
|
|
|
|
pub window: Entity,
|
|
|
|
/// The cursor position in logical pixels.
|
2020-06-04 06:22:32 +00:00
|
|
|
pub position: Vec2,
|
2020-06-15 19:47:35 +00:00
|
|
|
}
|
2022-12-09 01:20:44 +00:00
|
|
|
|
2022-06-16 13:20:37 +00:00
|
|
|
/// An event that is sent whenever the user's cursor enters a window.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-12-03 19:30:27 +00:00
|
|
|
pub struct CursorEntered {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that the cursor entered.
|
|
|
|
pub window: Entity,
|
2020-12-03 19:30:27 +00:00
|
|
|
}
|
2022-12-09 01:20:44 +00:00
|
|
|
|
2022-06-16 13:20:37 +00:00
|
|
|
/// An event that is sent whenever the user's cursor leaves a window.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-12-03 19:30:27 +00:00
|
|
|
pub struct CursorLeft {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that the cursor left.
|
|
|
|
pub window: Entity,
|
2020-12-03 19:30:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:15:56 +00:00
|
|
|
/// An event that is sent whenever a window receives a character from the OS or underlying system.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-11-07 01:15:56 +00:00
|
|
|
pub struct ReceivedCharacter {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that received the character.
|
|
|
|
pub window: Entity,
|
|
|
|
/// Received character.
|
2020-11-07 01:15:56 +00:00
|
|
|
pub char: char,
|
|
|
|
}
|
2020-12-07 21:24:25 +00:00
|
|
|
|
2023-01-29 20:27:29 +00:00
|
|
|
/// A Input Method Editor event.
|
|
|
|
///
|
|
|
|
/// This event is the translated version of the `WindowEvent::Ime` from the `winit` crate.
|
|
|
|
///
|
|
|
|
/// It is only sent if IME was enabled on the window with [`Window::ime_enabled`](crate::window::Window::ime_enabled).
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
|
|
|
pub enum Ime {
|
|
|
|
/// Notifies when a new composing text should be set at the cursor position.
|
|
|
|
Preedit {
|
|
|
|
/// Window that received the event.
|
|
|
|
window: Entity,
|
|
|
|
/// Current value.
|
|
|
|
value: String,
|
|
|
|
/// Cursor begin and end position.
|
|
|
|
///
|
|
|
|
/// `None` indicated the cursor should be hidden
|
|
|
|
cursor: Option<(usize, usize)>,
|
|
|
|
},
|
|
|
|
/// Notifies when text should be inserted into the editor widget.
|
|
|
|
Commit {
|
|
|
|
/// Window that received the event.
|
|
|
|
window: Entity,
|
|
|
|
/// Input string
|
|
|
|
value: String,
|
|
|
|
},
|
|
|
|
/// Notifies when the IME was enabled.
|
|
|
|
///
|
|
|
|
/// After this event, you will receive events `Ime::Preedit` and `Ime::Commit`,
|
|
|
|
/// and stop receiving events [`ReceivedCharacter`].
|
|
|
|
Enabled {
|
|
|
|
/// Window that received the event.
|
|
|
|
window: Entity,
|
|
|
|
},
|
|
|
|
/// Notifies when the IME was disabled.
|
|
|
|
Disabled {
|
|
|
|
/// Window that received the event.
|
|
|
|
window: Entity,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-12-07 21:24:25 +00:00
|
|
|
/// An event that indicates a window has received or lost focus.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-12-07 21:24:25 +00:00
|
|
|
pub struct WindowFocused {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that changed focus.
|
|
|
|
pub window: Entity,
|
|
|
|
/// Whether it was focused (true) or lost focused (false).
|
2020-12-07 21:24:25 +00:00
|
|
|
pub focused: bool,
|
|
|
|
}
|
2020-12-28 20:26:50 +00:00
|
|
|
|
|
|
|
/// An event that indicates a window's scale factor has changed.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-12-28 20:26:50 +00:00
|
|
|
pub struct WindowScaleFactorChanged {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that had it's scale factor changed.
|
|
|
|
pub window: Entity,
|
|
|
|
/// The new scale factor.
|
2020-12-28 20:26:50 +00:00
|
|
|
pub scale_factor: f64,
|
|
|
|
}
|
2022-12-09 01:20:44 +00:00
|
|
|
|
2020-12-28 20:26:50 +00:00
|
|
|
/// An event that indicates a window's OS-reported scale factor has changed.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2020-12-28 20:26:50 +00:00
|
|
|
pub struct WindowBackendScaleFactorChanged {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that had it's scale factor changed by the backend.
|
|
|
|
pub window: Entity,
|
|
|
|
/// The new scale factor.
|
2020-12-28 20:26:50 +00:00
|
|
|
pub scale_factor: f64,
|
|
|
|
}
|
2021-01-01 21:31:22 +00:00
|
|
|
|
|
|
|
/// Events related to files being dragged and dropped on a window.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2021-01-01 21:31:22 +00:00
|
|
|
pub enum FileDragAndDrop {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// File is being dropped into a window.
|
|
|
|
DroppedFile {
|
|
|
|
/// Window the file was dropped into.
|
|
|
|
window: Entity,
|
|
|
|
/// Path to the file that was dropped in.
|
|
|
|
path_buf: PathBuf,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// File is currently being hovered over a window.
|
|
|
|
HoveredFile {
|
|
|
|
/// Window a file is possibly going to be dropped into.
|
|
|
|
window: Entity,
|
|
|
|
/// Path to the file that might be dropped in.
|
|
|
|
path_buf: PathBuf,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// File hovering was cancelled.
|
2023-04-05 21:25:53 +00:00
|
|
|
HoveredFileCanceled {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that had a cancelled file drop.
|
|
|
|
window: Entity,
|
|
|
|
},
|
2021-01-01 21:31:22 +00:00
|
|
|
}
|
2021-01-25 04:06:06 +00:00
|
|
|
|
|
|
|
/// An event that is sent when a window is repositioned in physical pixels.
|
2022-12-09 01:20:44 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
|
|
|
|
#[reflect(Debug, PartialEq)]
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serialize",
|
|
|
|
derive(serde::Serialize, serde::Deserialize),
|
|
|
|
reflect(Serialize, Deserialize)
|
|
|
|
)]
|
2021-01-25 04:06:06 +00:00
|
|
|
pub struct WindowMoved {
|
2023-01-19 00:38:28 +00:00
|
|
|
/// Window that moved.
|
|
|
|
pub entity: Entity,
|
|
|
|
/// Where the window moved to in physical pixels.
|
2021-01-25 04:06:06 +00:00
|
|
|
pub position: IVec2,
|
|
|
|
}
|