Skip to content

Commit ffa88be

Browse files
authored
Merge pull request #1 from Weibye/rename-fields
Rename fields to `window` & add some missing documentation
2 parents 79713e8 + 164c819 commit ffa88be

File tree

8 files changed

+179
-65
lines changed

8 files changed

+179
-65
lines changed

crates/bevy_render/src/camera/camera.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,21 @@ pub fn camera_system<T: CameraProjection + Component>(
382382
// handle resize events. latest events are handled first because we only want to resize each
383383
// window once
384384
for event in window_resized_events.iter().rev() {
385-
if changed_window_ids.contains(&event.entity) {
385+
if changed_window_ids.contains(&event.window) {
386386
continue;
387387
}
388388

389-
changed_window_ids.push(event.entity);
389+
changed_window_ids.push(event.window);
390390
}
391391

392392
// handle resize events. latest events are handled first because we only want to resize each
393393
// window once
394394
for event in window_created_events.iter().rev() {
395-
if changed_window_ids.contains(&event.entity) {
395+
if changed_window_ids.contains(&event.window) {
396396
continue;
397397
}
398398

399-
changed_window_ids.push(event.entity);
399+
changed_window_ids.push(event.window);
400400
}
401401

402402
let changed_image_handles: HashSet<&Handle<Image>> = image_asset_events

crates/bevy_window/src/event.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use bevy_math::{IVec2, Vec2};
66
/// A window event that is sent whenever a window's logical size has changed.
77
#[derive(Debug, Clone)]
88
pub struct WindowResized {
9-
pub entity: Entity,
10-
/// The new logical width of the window
9+
/// Window that has changed.
10+
pub window: Entity,
11+
/// The new logical width of the window.
1112
pub width: f64,
1213
/// The new logical height of the window.
1314
pub height: f64,
@@ -25,31 +26,35 @@ pub struct RequestRedraw;
2526
/// event will be sent in the handler for that event.
2627
#[derive(Debug, Clone)]
2728
pub struct WindowCreated {
28-
pub entity: Entity,
29+
/// Window that has been created.
30+
pub window: Entity,
2931
}
3032

3133
/// An event that is sent whenever the operating systems requests that a window
3234
/// be closed. This will be sent when the close button of the window is pressed.
3335
///
3436
/// If the default [`WindowPlugin`] is used, these events are handled
35-
/// by [closing] the corresponding [`Window`].
37+
/// by closing the corresponding [`Window`].
3638
/// To disable this behaviour, set `close_when_requested` on the [`WindowPlugin`]
3739
/// to `false`.
3840
///
3941
/// [`WindowPlugin`]: crate::WindowPlugin
4042
/// [`Window`]: crate::Window
4143
#[derive(Debug, Clone)]
4244
pub struct WindowCloseRequested {
43-
pub entity: Entity,
45+
/// Window to close.
46+
pub window: Entity,
4447
}
4548

4649
/// An event that is sent whenever a window is closed. This will be sent by the
4750
/// handler for [`WindowCloseRequested`] or similar.
4851
#[derive(Debug, Clone)]
4952
pub struct WindowClosed {
50-
pub entity: Entity,
53+
/// Window that has been closed.
54+
/// TODO: Does this entity actually exist anymore?
55+
pub window: Entity,
5156
}
52-
/// An event reporting that the mouse cursor has moved on a window.
57+
/// An event reporting that the mouse cursor has moved inside a window.
5358
///
5459
/// The event is sent only if the cursor is over one of the application's windows.
5560
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate.
@@ -61,7 +66,7 @@ pub struct WindowClosed {
6166
#[derive(Debug, Clone)]
6267
pub struct CursorMoved {
6368
/// Window that the cursor moved inside.
64-
pub entity: Entity,
69+
pub window: Entity,
6570
/// The cursor position in logical pixels.
6671
pub position: Vec2,
6772
}
@@ -70,29 +75,30 @@ pub struct CursorMoved {
7075
#[derive(Debug, Clone)]
7176
pub struct CursorEntered {
7277
/// Window that the cursor entered.
73-
pub entity: Entity,
78+
pub window: Entity,
7479
}
7580

7681
/// An event that is sent whenever the user's cursor leaves a window.
7782
#[derive(Debug, Clone)]
7883
pub struct CursorLeft {
7984
/// Window that the cursor left.
80-
pub entity: Entity,
85+
pub window: Entity,
8186
}
8287

8388
/// An event that is sent whenever a window receives a character from the OS or underlying system.
8489
#[derive(Debug, Clone)]
8590
pub struct ReceivedCharacter {
8691
/// Window that received the character.
87-
pub entity: Entity,
92+
pub window: Entity,
93+
/// Received character.
8894
pub char: char,
8995
}
9096

9197
/// An event that indicates a window has received or lost focus.
9298
#[derive(Debug, Clone)]
9399
pub struct WindowFocused {
94100
/// Window that changed focus.
95-
pub entity: Entity,
101+
pub window: Entity,
96102
/// Whether it was focused (true) or lost focused (false).
97103
pub focused: bool,
98104
}
@@ -101,38 +107,43 @@ pub struct WindowFocused {
101107
#[derive(Debug, Clone)]
102108
pub struct WindowScaleFactorChanged {
103109
/// Window that had it's scale factor changed.
104-
pub entity: Entity,
110+
pub window: Entity,
105111
/// The new scale factor.
106112
pub scale_factor: f64,
107113
}
108114

109115
/// An event that indicates a window's OS-reported scale factor has changed.
110116
#[derive(Debug, Clone)]
111117
pub struct WindowBackendScaleFactorChanged {
112-
pub entity: Entity,
118+
/// Window that had it's scale factor changed by the backend.
119+
pub window: Entity,
120+
/// The new scale factor.
113121
pub scale_factor: f64,
114122
}
115123

116124
/// Events related to files being dragged and dropped on a window.
117125
#[derive(Debug, Clone)]
118126
pub enum FileDragAndDrop {
127+
/// File is being dropped into a window.
119128
DroppedFile {
120129
/// Window the file was dropped into.
121-
entity: Entity,
130+
window: Entity,
122131
/// Path to the file that was dropped in.
123132
path_buf: PathBuf,
124133
},
125134

135+
/// File is currently being hovered over a window.
126136
HoveredFile {
127137
/// Window a file is possibly going to be dropped into.
128-
entity: Entity,
138+
window: Entity,
129139
/// Path to the file that might be dropped in.
130140
path_buf: PathBuf,
131141
},
132142

143+
/// File hovering was cancelled.
133144
HoveredFileCancelled {
134145
/// Window that had a cancelled file drop.
135-
entity: Entity,
146+
window: Entity,
136147
},
137148
}
138149

crates/bevy_window/src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ use bevy_ecs::{entity::Entity, schedule::SystemLabel, system::Resource};
2929
/// runs, to configure how it behaves.
3030
#[derive(Resource, Clone)]
3131
pub struct WindowSettings {
32+
3233
/// Whether to create a window when added.
3334
///
3435
/// Note that if there are no windows, by default the App will exit,
3536
/// due to [`exit_on_all_closed`].
3637
pub add_primary_window: bool,
38+
3739
/// Whether to exit the app when there are no open windows.
3840
///
3941
/// If disabling this, ensure that you send the [`bevy_app::AppExit`]
@@ -42,8 +44,8 @@ pub struct WindowSettings {
4244
/// surprise your users. It is recommended to leave this setting as `true`.
4345
///
4446
/// If true, this plugin will add [`exit_on_all_closed`] to [`CoreStage::Update`].
45-
// TODO: Update documentation here
4647
pub exit_condition: ExitCondition,
48+
4749
/// Whether to close windows when they are requested to be closed (i.e.
4850
/// when the close button is pressed).
4951
///
@@ -107,18 +109,9 @@ impl Plugin for WindowPlugin {
107109
.unwrap_or_default();
108110

109111
if settings.add_primary_window {
110-
// TODO: Creating primary window should ideally be done through commands instead of the old way
111-
// however, commands aren't executed until the end of the "build-stage"
112-
// which means the primary-window does not exist until just before startup-systems starts running (?)
113-
// which means bevy_render does not have a window to use as attach to during plugin build.
114-
115-
// Wishlist item; for this to work:
116-
// app.add_startup_system(create_primary_window);
117-
// or this:
118-
// app.add_build_system(create_primary_window)
119-
120-
// TODO: The unwrap_or_default is necessary for the user to setup ahead of time what the window should be
121-
// if not we'll regress on this
112+
// If the user has added a window-bundle resource, we should spawn that as as the
113+
// primary window. If not, we need to spawn a default WindowBundle,
114+
// hence the `unwrap_or_default()` here.
122115
let window_bundle = app
123116
.world
124117
.remove_resource::<WindowBundle>()
@@ -150,6 +143,7 @@ impl Plugin for WindowPlugin {
150143
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
151144
pub struct ModifiesWindows;
152145

146+
/// Defines the specific conditions the application should exit on
153147
#[derive(Clone)]
154148
pub enum ExitCondition {
155149
/// Close application when the primary window is closed
@@ -165,5 +159,6 @@ pub enum ExitCondition {
165159
/// This resource is allowed to not exist and should be handled gracefully if it doesn't.
166160
#[derive(Debug, Resource, Clone, Reflect)]
167161
pub struct PrimaryWindow {
162+
/// Window which is currently the primary window.
168163
pub window: Entity,
169164
}

crates/bevy_window/src/system.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pub fn exit_on_all_closed(mut app_exit_events: EventWriter<AppExit>, windows: Qu
2222
/// Exit the application when the primary window has been closed
2323
///
2424
/// This system is added by the [`WindowPlugin`]
25-
// TODO: More docs
25+
///
26+
/// [`WindowPlugin`]: crate::WindowPlugin
2627
pub fn exit_on_primary_closed(
2728
mut app_exit_events: EventWriter<AppExit>,
2829
primary_window: Option<Res<PrimaryWindow>>,
@@ -33,9 +34,9 @@ pub fn exit_on_primary_closed(
3334
for window in window_close.iter() {
3435
warn!(
3536
"primary_window: {:?}, closed: {:?}",
36-
primary_window.window, window.entity
37+
primary_window.window, window.window
3738
);
38-
if primary_window.window == window.entity {
39+
if primary_window.window == window.window {
3940
// Primary window has been closed
4041
app_exit_events.send(AppExit);
4142
}
@@ -60,7 +61,7 @@ pub fn close_when_requested(
6061
) {
6162
for event in closed.iter() {
6263
window_closed.send(WindowClosed {
63-
entity: event.entity,
64+
window: event.window,
6465
});
6566
}
6667
}
@@ -79,7 +80,7 @@ pub fn close_on_esc(
7980
}
8081

8182
if input.just_pressed(KeyCode::Escape) {
82-
window_closed.send(WindowClosed { entity });
83+
window_closed.send(WindowClosed { window: entity });
8384
}
8485
}
8586
}

0 commit comments

Comments
 (0)