Skip to content

Commit ffdb5d9

Browse files
BD103chompaa
authored andcommitted
Move close_on_esc to bevy_dev_tools (bevyengine#12855)
# Objective - As @james7132 said [on Discord](https://discord.com/channels/691052431525675048/692572690833473578/1224626740773523536), the `close_on_esc` system is forcing `bevy_window` to depend on `bevy_input`. - `close_on_esc` is not likely to be used in production, so it arguably does not have a place in `bevy_window`. ## Solution - As suggested by @afonsolage, move `close_on_esc` into `bevy_dev_tools`. - Add an example to the documentation too. - Remove `bevy_window`'s dependency on `bevy_input`. - Add `bevy_reflect`'s `smol_str` feature to `bevy_window` because it was implicitly depended upon with `bevy_input` before it was removed. - Remove any usage of `close_on_esc` from the examples. - `bevy_dev_tools` is not enabled by default. I personally find it frustrating to run examples with additional features, so I opted to remove it entirely. - This is up for discussion if you have an alternate solution. --- ## Changelog - Moved `bevy_window::close_on_esc` to `bevy_dev_tools::close_on_esc`. - Removed usage of `bevy_dev_tools::close_on_esc` from all examples. ## Migration Guide `bevy_window::close_on_esc` has been moved to `bevy_dev_tools::close_on_esc`. You will first need to enable `bevy_dev_tools` as a feature in your `Cargo.toml`: ```toml [dependencies] bevy = { version = "0.14", features = ["bevy_dev_tools"] } ``` Finally, modify any imports to use `bevy_dev_tools` instead: ```rust // Old: // use bevy::window::close_on_esc; // New: use bevy::dev_tools::close_on_esc; App::new() .add_systems(Update, close_on_esc) // ... .run(); ```
1 parent 4824a3f commit ffdb5d9

File tree

12 files changed

+45
-43
lines changed

12 files changed

+45
-43
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use bevy_ecs::prelude::*;
2+
use bevy_input::{keyboard::KeyCode, ButtonInput};
3+
use bevy_window::Window;
4+
5+
/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
6+
///
7+
/// This is useful for examples or prototyping.
8+
///
9+
/// # Example
10+
///
11+
/// ```no_run
12+
/// # use bevy_app::prelude::*;
13+
/// # use bevy_dev_tools::close_on_esc;
14+
/// #
15+
/// App::new()
16+
/// .add_systems(Update, close_on_esc);
17+
/// ```
18+
pub fn close_on_esc(
19+
mut commands: Commands,
20+
focused_windows: Query<(Entity, &Window)>,
21+
input: Res<ButtonInput<KeyCode>>,
22+
) {
23+
for (window, focus) in focused_windows.iter() {
24+
if !focus.focused {
25+
continue;
26+
}
27+
28+
if input.just_pressed(KeyCode::Escape) {
29+
commands.entity(window).despawn();
30+
}
31+
}
32+
}

crates/bevy_dev_tools/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ use bevy_app::prelude::*;
1212

1313
#[cfg(feature = "bevy_ci_testing")]
1414
pub mod ci_testing;
15+
1516
pub mod fps_overlay;
1617

1718
#[cfg(feature = "bevy_ui_debug")]
1819
pub mod ui_debug_overlay;
1920

21+
mod close_on_esc;
22+
23+
pub use crate::close_on_esc::close_on_esc;
24+
2025
/// Enables developer tools in an [`App`]. This plugin is added automatically with `bevy_dev_tools`
2126
/// feature.
2227
///

crates/bevy_window/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.14.0-dev" }
2020
bevy_math = { path = "../bevy_math", version = "0.14.0-dev" }
2121
bevy_reflect = { path = "../bevy_reflect", version = "0.14.0-dev", features = [
2222
"glam",
23+
"smol_str",
2324
] }
2425
bevy_utils = { path = "../bevy_utils", version = "0.14.0-dev" }
25-
# Used for close_on_esc
26-
bevy_input = { path = "../bevy_input", version = "0.14.0-dev" }
2726

2827
# other
2928
serde = { version = "1.0", features = ["derive"], optional = true }

crates/bevy_window/src/event.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,12 @@ pub struct WindowDestroyed {
117117
/// The event is sent only if the cursor is over one of the application's windows.
118118
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate with the addition of `delta`.
119119
///
120-
/// Not to be confused with the [`MouseMotion`] event from `bevy_input`.
120+
/// Not to be confused with the `MouseMotion` event from `bevy_input`.
121121
///
122122
/// Because the range of data is limited by the window area and it may have been transformed by the OS to implement certain effects like acceleration,
123-
/// you should not use it for non-cursor-like behaviour such as 3D camera control. Please see [`MouseMotion`] instead.
123+
/// you should not use it for non-cursor-like behaviour such as 3D camera control. Please see `MouseMotion` instead.
124124
///
125125
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
126-
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
127126
#[derive(Event, Debug, Clone, PartialEq, Reflect)]
128127
#[reflect(Debug, PartialEq)]
129128
#[cfg_attr(

crates/bevy_window/src/system.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{PrimaryWindow, Window, WindowCloseRequested};
22

33
use bevy_app::AppExit;
44
use bevy_ecs::prelude::*;
5-
use bevy_input::{keyboard::KeyCode, ButtonInput};
65

76
/// Exit the application when there are no open windows.
87
///
@@ -45,22 +44,3 @@ pub fn close_when_requested(mut commands: Commands, mut closed: EventReader<Wind
4544
commands.entity(event.window).despawn();
4645
}
4746
}
48-
49-
/// Close the focused window whenever the escape key (<kbd>Esc</kbd>) is pressed
50-
///
51-
/// This is useful for examples or prototyping.
52-
pub fn close_on_esc(
53-
mut commands: Commands,
54-
focused_windows: Query<(Entity, &Window)>,
55-
input: Res<ButtonInput<KeyCode>>,
56-
) {
57-
for (window, focus) in focused_windows.iter() {
58-
if !focus.focused {
59-
continue;
60-
}
61-
62-
if input.just_pressed(KeyCode::Escape) {
63-
commands.entity(window).despawn();
64-
}
65-
}
66-
}

crates/bevy_window/src/window.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub struct Window {
227227
///
228228
/// If enabled, the window will receive [`Ime`](crate::Ime) events instead of
229229
/// [`ReceivedCharacter`](crate::ReceivedCharacter) or
230-
/// [`KeyboardInput`](bevy_input::keyboard::KeyboardInput).
230+
/// `KeyboardInput` from `bevy_input`.
231231
///
232232
/// IME should be enabled during text input, but not when you expect to get the exact key pressed.
233233
///

examples/2d/rotation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn main() {
1717
rotate_to_player_system,
1818
),
1919
)
20-
.add_systems(Update, bevy::window::close_on_esc)
2120
.run();
2221
}
2322

examples/3d/parallax_mapping.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use std::fmt;
55

6-
use bevy::{prelude::*, render::render_resource::TextureFormat, window::close_on_esc};
6+
use bevy::{prelude::*, render::render_resource::TextureFormat};
77

88
fn main() {
99
App::new()
@@ -19,7 +19,6 @@ fn main() {
1919
update_parallax_depth_scale,
2020
update_parallax_layers,
2121
switch_method,
22-
close_on_esc,
2322
),
2423
)
2524
.run();

examples/games/alien_cake_addict.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ fn main() {
4242
.add_systems(OnEnter(GameState::GameOver), display_score)
4343
.add_systems(
4444
Update,
45-
(
46-
gameover_keyboard.run_if(in_state(GameState::GameOver)),
47-
bevy::window::close_on_esc,
48-
),
45+
gameover_keyboard.run_if(in_state(GameState::GameOver)),
4946
)
5047
.add_systems(OnExit(GameState::GameOver), teardown)
5148
.run();

examples/games/breakout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() {
7575
// `chain`ing systems together runs them in order
7676
.chain(),
7777
)
78-
.add_systems(Update, (update_scoreboard, bevy::window::close_on_esc))
78+
.add_systems(Update, update_scoreboard)
7979
.run();
8080
}
8181

0 commit comments

Comments
 (0)