Skip to content

Commit

Permalink
Cleanup bevy_winit
Browse files Browse the repository at this point in the history
The `bevy_winit` crate has a very large `lib.rs`, the size is daunting
and discourages both contributors and reviewers. Beside, more code
implies more ways to accidentally introduce bugs.

This PR does a few things to reduce greatly the line count, without
really changing much logic:

- Move the winit event handler to a standalone function. This reduces
  the amount of indentation, and isolates relevant code.
- Replace the `WindowAndInputEventWriters` struct with direct calls
  to `World::send_event`. This is done through a new private extension
  trait called `AppSendEvent`.
  Now, instead of using
  `event_writers.specific_events.send(SpecificEvent { … })`, it directly
  does `app.send_event(SpecificEvent { … })`.
  Looking at the `send_event` implementation, I didn't see anything that
  would lead me to believe this is more costly or leads to different
  behaviors.
  With this change, we both reduce boilerplate on event sending and
  delete the `WindowAndInputEventWriters` struct
- Rename `window_entity` to `window`. This allows constructing most
  `bevy_window` events in a single line, rather than being split on
  several lines by `rustfmt`. This is also more consistent, since the
  `Entity` field of `bevy_window` events is called `window`, it makes
  sense to re-use the same name to designate the same thing.
  This removes a lot of boilerplate.
- Explicitly name the `CreateWindowParams` as a type alias instead of
  copy/pasting it about everywhere. In `create_windows`, instead of
  accepting each param field, accept the whole param. This removes a
  lot of boilerplate as well.

The combination of all those changes leads to a reduction of 200 lines.

**Notes to reviewers**

You should really use the "Hide whitespaces" diff display mode.

The trickiest bit is probably the scale factor handling. Because we now
directly access the world, I had to move event-sending code around, to
avoid breaking mutual exclusion rules. I'm fairly confident it's the
same behavior.

Another thing that deserves looking-at is non-linux plateform handling.
I've only compiled this for linux targets, hence it might fail to
compile on other plateforms.
  • Loading branch information
nicopap committed Jan 8, 2024
1 parent c1b785c commit 59b6f1e
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 563 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub enum FileDragAndDrop {
)]
pub struct WindowMoved {
/// Window that moved.
pub entity: Entity,
pub window: Entity,
/// Where the window moved to in physical pixels.
pub position: IVec2,
}
Expand Down
Loading

0 comments on commit 59b6f1e

Please sign in to comment.