Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Allow closing windows at runtime #3575

Closed
wants to merge 7 commits into from

Conversation

DJMcNab
Copy link
Member

@DJMcNab DJMcNab commented Jan 6, 2022

Objective

Fixes #3180, builds from #2898

Solution

Support requesting a window to be closed and closing a window in bevy_window, and handle this in bevy_winit.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

Changelog

Added

  • Window::close to allow closing windows.
  • WindowClosed to allow reacting to windows being closed.

Changed

Replaced bevy::system::exit_on_esc_system with bevy::window::close_on_esc.

Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows.

Migration Guide

bevy::input::system::exit_on_esc_system has been removed. Use bevy::window::close_on_esc instead.
CloseWindow has been removed. Use Window::close instead.
The Close variant has been added to WindowCommand. Handle this by closing the relevant window.

@DJMcNab DJMcNab changed the title Allow closing Allow closing windows at runtime Jan 6, 2022
@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Jan 6, 2022
@DJMcNab DJMcNab added A-Windowing Platform-agnostic interface layer to run your app in C-Feature A new feature, making something new possible and removed S-Needs-Triage This issue needs to be labelled labels Jan 6, 2022
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Well-engineered and useful. I carefully reviewed the whole thing, but all I could find were a couple nits.

@fowenix fowenix mentioned this pull request Apr 19, 2022
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Apr 25, 2022
@DJMcNab DJMcNab force-pushed the close-windows branch 2 times, most recently from 4f4d042 to e5d5be4 Compare April 25, 2022 15:21
@alice-i-cecile
Copy link
Member

@superdump, once you're happy with this, feel free to merge it in.

@DJMcNab
Copy link
Member Author

DJMcNab commented Apr 25, 2022

I still want to finish cleaning up the docs. E.g. there's a sentence fragment remaining.

Edit: I think it's now ready for final review/merge

@DJMcNab DJMcNab added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Apr 26, 2022
@DJMcNab DJMcNab requested a review from alice-i-cecile April 26, 2022 10:34
/// If true, this plugin will add [`close_when_requested`] to [`CoreStage::Update`].
/// If this system (or a replacement) is not running, the close button will have no effect.
/// This may surprise your users. It is recommended to leave this setting as `true`.
pub close_when_requested: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this field should exist. I agree with you that it's unintuitive behavior to set this to false; when would app developers want to do this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, if the user has unsaved changes, it's traditional to give an 'unsaved changes' warning window.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. In that case, this should be exposed as a resource: that behavior needs to be able to be changed dynamically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really? If a developer wants this functionality, they can easily enough add it themselves. This is merely making sure the default behaviour is correct, and we give an escape hatch for when people need more complicated logic.

That is, I don't really want to encourage disabling this, but I want to make the option available.

bevy_window::WindowCommand::Close => {
let window = winit_windows.remove_window(id);
// Close the window
drop(window);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this explicit drop call? Shouldn't this just happen automatically at the end of the scope?

Copy link
Member Author

@DJMcNab DJMcNab Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I think I was trying to clarify that the drop is what actually closes the operating system window. Since Window owns that window, I thought it better to be explicit about the side effects, and we can be sure that this drops at the expected time.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I think I'd prefer to just call winit_windows.remove_window(id) and have a comment explaining this mechanism.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One blocking question about the necessity of the close_when_requested field.

I would also love to see tests for this, but I don't think that's feasible yet.

@alice-i-cecile alice-i-cecile added the A-Rendering Drawing game state to the screen label May 2, 2022
Copy link
Contributor

@superdump superdump left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I tested multiple_windows, hit esc twice, both windows closed and the app exited. Very nice!

@superdump
Copy link
Contributor

bors r+

bors bot pushed a commit that referenced this pull request May 5, 2022
# Objective

Fixes #3180, builds from #2898

## Solution

Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

## Changelog

### Added

- `Window::close` to allow closing windows.
- `WindowClosed` to allow reacting to windows being closed.

### Changed

Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`.

## Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. 

## Migration Guide

`bevy::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead.
`CloseWindow` has been removed. Use `Window::close` instead.
The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
@bors
Copy link
Contributor

bors bot commented May 5, 2022

Build failed:

DJMcNab added 6 commits May 5, 2022 08:40
Make the rest of bevy work with multiple windows

Remove exit_on_esc

Add suggested docs
Since their brokeness isn't my fault
@DJMcNab DJMcNab requested a review from superdump May 5, 2022 10:08
@superdump
Copy link
Contributor

bors r+

bors bot pushed a commit that referenced this pull request May 5, 2022
# Objective

Fixes #3180, builds from #2898

## Solution

Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

## Changelog

### Added

- `Window::close` to allow closing windows.
- `WindowClosed` to allow reacting to windows being closed.

### Changed

Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`.

## Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. 

## Migration Guide

`bevy::input::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead.
`CloseWindow` has been removed. Use `Window::close` instead.
The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
@bors bors bot changed the title Allow closing windows at runtime [Merged by Bors] - Allow closing windows at runtime May 5, 2022
@bors bors bot closed this May 5, 2022
@DJMcNab DJMcNab deleted the close-windows branch May 5, 2022 17:16
robtfm pushed a commit to robtfm/bevy that referenced this pull request May 10, 2022
# Objective

Fixes bevyengine#3180, builds from bevyengine#2898

## Solution

Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

## Changelog

### Added

- `Window::close` to allow closing windows.
- `WindowClosed` to allow reacting to windows being closed.

### Changed

Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`.

## Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. 

## Migration Guide

`bevy::input::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead.
`CloseWindow` has been removed. Use `Window::close` instead.
The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
exjam pushed a commit to exjam/bevy that referenced this pull request May 22, 2022
# Objective

Fixes bevyengine#3180, builds from bevyengine#2898

## Solution

Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

## Changelog

### Added

- `Window::close` to allow closing windows.
- `WindowClosed` to allow reacting to windows being closed.

### Changed

Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`.

## Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. 

## Migration Guide

`bevy::input::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead.
`CloseWindow` has been removed. Use `Window::close` instead.
The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

Fixes bevyengine#3180, builds from bevyengine#2898

## Solution

Support requesting a window to be closed and closing a window in `bevy_window`, and handle this in `bevy_winit`.

This is a stopgap until we move to windows as entites, which I'm sure I'll get around to eventually.

## Changelog

### Added

- `Window::close` to allow closing windows.
- `WindowClosed` to allow reacting to windows being closed.

### Changed

Replaced `bevy::system::exit_on_esc_system` with `bevy::window::close_on_esc`.

## Fixed

The app no longer exits when any window is closed. This difference is only observable when there are multiple windows. 

## Migration Guide

`bevy::input::system::exit_on_esc_system` has been removed. Use `bevy::window::close_on_esc` instead.
`CloseWindow` has been removed. Use `Window::close` instead.
The `Close` variant has been added to `WindowCommand`. Handle this by closing the relevant window.
github-merge-queue bot pushed a commit that referenced this pull request Mar 29, 2024
# Objective

- Avoid unbounded HashMap growth for opening/closing windows.

## Solution

- Remove map entry in `WinitWindows::remove_window`.

## Migration Guide

- `WinitWindows::get_window_entity` now returns `None` after a window is
closed, instead of a dead entity.

---

## Comments

The comment this PR replaces was added in
#3575. Since `get_window_entity`
now returns an `Entity` instead of a `WindowId`, this no longer seems
useful. Note that `get_window_entity` is only used
[here](https://github.com/bevyengine/bevy/blob/56bcbb097552b45e3ff48c48947ed8ee4e2c24b1/crates/bevy_winit/src/lib.rs#L436),
immediately followed by a warning if the entity returned doesn't exist.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen A-Windowing Platform-agnostic interface layer to run your app in C-Feature A new feature, making something new possible M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow users to close one window in a multi-window app
4 participants