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

Expose winit's MonitorHandle #13669

Merged
merged 26 commits into from
Aug 6, 2024

Conversation

tychedelia
Copy link
Contributor

@tychedelia tychedelia commented Jun 4, 2024

Objective

Adds a new Monitor component representing a winit MonitorHandle that can be used to spawn new windows and check for system monitor information.

Closes #12955.

Solution

For every winit event, check available monitors and spawn them into the world as components.

Testing

TODO:

  • Test plugging in and unplugging monitor during app runtime
  • Test spawning a window on a second monitor by entity id
  • Since this touches winit, test all platforms

Changelog

  • Adds a new Monitor component that can be queried for information about available system monitors.

Migration Guide

  • WindowMode variants now take a MonitorSelection, which can be set to MonitorSelection::Primary to mirror the old behavior.

@tychedelia tychedelia force-pushed the 12955-winit-monitors branch from ffad6f7 to 29458b4 Compare June 4, 2024 06:54
Copy link
Contributor

@killercup killercup left a comment

Choose a reason for hiding this comment

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

Good work! Left some comments with suggestions, but no big things.

I haven't tested this, and I'm not sure how to easily do that. Would it make sense to add an example that prints metadata about connected monitors to show how to use this?

crates/bevy_winit/src/winit_monitors.rs Outdated Show resolved Hide resolved
crates/bevy_window/src/monitor.rs Outdated Show resolved Hide resolved
crates/bevy_window/src/monitor.rs Show resolved Hide resolved
crates/bevy_winit/src/system.rs Show resolved Hide resolved
crates/bevy_winit/src/system.rs Show resolved Hide resolved
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Windowing Platform-agnostic interface layer to run your app in M-Needs-Release-Note Work that should be called out in the blog due to impact X-Uncontroversial This work is generally agreed upon D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 4, 2024
@alice-i-cecile
Copy link
Member

I like this feature, and I'm on board with the general design.

@killercup
Copy link
Contributor

Made a small example, feel free to pull in this commit if you think it's useful: killercup@62baba9

@tychedelia
Copy link
Contributor Author

I haven't tested this, and I'm not sure how to easily do that.

Mostly we just need to check that things compile and run on most platforms, but I'm also going to do some experiments with un/replugging monitors just to make sure that we correctly spawn and despawn these as that happens.

Made a small example, feel free to pull in this commit if you think it's useful: killercup@62baba9

Thanks! Will absolutely take this.

@tychedelia tychedelia force-pushed the 12955-winit-monitors branch from 33759f8 to fa93d46 Compare June 4, 2024 18:58
@mockersf
Copy link
Member

mockersf commented Jun 4, 2024

would it be possible in the example to open a window in each monitor detected, displaying that monitor information?

@tychedelia
Copy link
Contributor Author

would it be possible in the example to open a window in each monitor detected, displaying that monitor information?

That's a great idea, I noticed we don't have any examples of that anywhere. Will add.

@tychedelia tychedelia force-pushed the 12955-winit-monitors branch from fa93d46 to cc65cb3 Compare June 5, 2024 22:30
@tychedelia
Copy link
Contributor Author

Okay, so in working through this new example, I realized that we really needed to also make the window's mode configurable by a monitor selection, as this is actually the correct way to open a window on a particular monitor in winit. Setting the window's position worked sometimes in my experiments, but depended a lot on whether the app was starting on the primary or secondary monitor.

I did confirm that plugging and unplugging monitors works.

@alice-i-cecile
Copy link
Member

For the record, I think we should document the limitations and merge this. It's immature, but fully opt-in, and virtually nothing more can be done on our end to improve this. These bugs need to be fixed in winit, and sending users / contributors their way is IMO the best way to actually improve things.

@tychedelia
Copy link
Contributor Author

Unfortuantely had to revert the changes to use BtreeMap/BtreeSet due to a new clippy lint that was complaining about the key potentially being unstable on some platforms. Auditing the winit code it seems as if this isn't an issue, but I wasn't sure that adding an ignore for the lint and making note to audit winit every time they release was worth the minor code quality improvement.

Btw minor suggestion for the example: This could use observers instead of Added/RemovedComponents.

Tried doing this, but the on_remove hook was actually more ugly due to having to query for MonitorRef on DeferredWorld.

@tychedelia tychedelia added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Blocked This cannot move forward until something else changes labels Jul 30, 2024
Copy link
Contributor

@killercup killercup left a comment

Choose a reason for hiding this comment

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

LGTM!

Let's merge this while noting the limitations and get fixes from winit over time as well as have people test this in main :)

@tychedelia tychedelia added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 4, 2024
Copy link
Contributor

@killercup killercup left a comment

Choose a reason for hiding this comment

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

Added a suggestion to add a note on the issues found here as docs to WinitMonitors. We might not want to to link to this PR but a tracking issue.

use bevy_ecs::system::Resource;

/// Stores [`winit`] monitors and their corresponding entities
#[derive(Resource, Debug, Default)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Any value in this being Reflect?

Copy link
Member

Choose a reason for hiding this comment

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

Resources being reflect is mildly useful, but IME not nearly as important as for things that want to be inspected or serialized.

crates/bevy_winit/src/winit_monitors.rs Show resolved Hide resolved
Co-authored-by: Pascal Hertleif <[email protected]>
@alice-i-cecile alice-i-cecile added X-Contentious There are nontrivial implications that should be thought through and removed X-Uncontroversial This work is generally agreed upon labels Aug 6, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 6, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 6, 2024
@alice-i-cecile
Copy link
Member

@tychedelia build failure on Android looks related; check the CI logs and ping me when it's fixed up?

@tychedelia
Copy link
Contributor Author

@alice-i-cecile should be fixed now?

@tychedelia tychedelia force-pushed the 12955-winit-monitors branch from b7da07c to 063be9a Compare August 6, 2024 02:03
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 6, 2024
Merged via the queue into bevyengine:main with commit 3360b45 Aug 6, 2024
30 checks passed
@alice-i-cecile
Copy link
Member

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to bevyengine/bevy-website#1668 if you'd like to help out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Windowing Platform-agnostic interface layer to run your app in C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose winit::monitor as queryable component
5 participants