-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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] - Add option to center a window #4999
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer position
to become an enum, something like
enum WindowPosition {
Default, // equivalent to position = None
Centered, // equivalent to centered = true
At(Vec2), // equivalent to position = Some(vec2)
}
@mockersf I really like that idea. I also like the naming of |
@mockersf I considered something like this, but wasn't too convinced, because this would make it a breaking change. But it makes sense, so I'll update the PR. |
@CalinZBaenen That would be nice, but it would require deferring the centering to after the window is initialized and I'm not familiar enough with bevy+winit to do it (I don't know if we have any foundations for that or if that even is possible). You can use startup system as an bandaid (and this should include borders), but then it takes a frame for it to take effect: fn setup_window(mut windows: ResMut<Windows>) {
if let Some(window) = windows.get_primary_mut() {
window.center_window();
}
} If you do the |
d191ba2
to
490aa35
Compare
@LoipesMas I believe |
@CalinZBaenen sorry, I don't know what you're referring to. Could you give me a precise location? |
@LoipesMas It seems to be fixed now, but previously in self.command_queue.push(WindowCommand::center); Since then it has seemingly been given the correct casing, but I'd take this as an opportunity to double check the casing of anything. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to be rebased to get CI to pass, but I'm very happy with this API now. Good work to both @LoipesMas and @CalinZBaenan :D
crates/bevy_window/src/window.rs
Outdated
@@ -641,8 +667,8 @@ pub struct WindowDescriptor { | |||
/// May vary from the physical height due to different pixel density on different monitors. | |||
pub height: f32, | |||
/// The position on the screen that the window will be centered at. | |||
/// If set to `None`, some platform-specific position will be chosen. | |||
pub position: Option<Vec2>, | |||
/// See [`WindowPosition`] for available values. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// See [`WindowPosition`] for available values. |
this line is not very useful, cargo doc will already link to the enum on the field type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to explicitly point to WindowPosition
page, because just the name "WindowPosition" doesn't convey that it could be something else than just raw coordinates (at least in my opinion). But maybe you're right that this is unnecessary. Or maybe different wording/naming would fit this better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LoipesMas If you want a slightly better name: I recommend WindowLocatoon
if that's not taken.
It's a synonym, but it makes sense that Center
is a "Location
".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, maybe something like that would be clearer. But TBF I'm probably too involved(?) to make an objective judgement here, so I'll rely on other people's opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like WindowLocation
; I think that would help communicate that there's multiple subtle variants.
crates/bevy_window/src/window.rs
Outdated
/// Modifies the position of the window to be in the center of the current monitor | ||
/// | ||
/// # Platform-specific | ||
/// - iOS: Can only be called on the main thread. | ||
/// - Web / Android / Wayland: Unsupported. | ||
#[inline] | ||
pub fn center_window(&mut self) { | ||
self.command_queue.push(WindowCommand::Center); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this should be a new method, you could change the existing set_position
to take a WindowPosition
. It would support all possible extensions like if we add WindowPosition::BottomRight
... but then you could call set_position(WindowPosition::Automatic)
which doesn't make sense...
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your proposed design is better. I would just document that setting the window position to automatic is a no-op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mockersf Interesting idea suggesting other places to put the window except in the center.
[P.S. : I feel like this sounds unnecessarily passive-aggressive, so I wanted to clarify it's not meant to be. I just have no other comment to tack on.]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered the fact that we might want to add more positions, such as bottom-right etc., but I wasn't sure if there would be a use case for that and where do we draw the line of being a window manager.
But if you expect that there would be a use case for more values, then changing set_position
signature makes sense.
To avoid code duplication (between set_position
and create_window
) we would need to abstract out WindowPosition -> actual position
code, right? So perhaps a function that takes WindowPosition
, window size and screen size and returns screen space position?
Thank you. BTW, what is "CI"? |
CI == "Continuous Integration". It's the automated checks that you can see at the bottom of this thread that ensure that the code is working correctly :) |
@CalinZBaenen Not sure what could have happened there. But |
@LoipesMas can you rebase this? This is almost ready but the merge conflicts need to be resolved. Reviewers: should we block on the docs discussion? |
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
The multi-monitor user-story in Bevy atm is not very good. Most input and UI assumes only one monitor, as does a lot of positioning and events. I think we should sit down and flesh out what is the desired user-story here, but that is outside the scope of this PR hence I don't think it's worth blocking on. |
Rebased! |
I think that's reasonable. @Weibye are you comfortable making that issue? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took another look; there's a few easy wins here. We should make the failure behavior less panicky, and allow users to specify which window they want to center this on with a good default.
Not quite sure if this should be an issue or a discussion yet, but will definitely set this up once I have some time to map out the current capabilities (or lack of thereof) |
Since ðis is marked for |
@CalinZBaenen merging now! bors r+ |
# Objective - Fixes #4993 ## Solution - ~~Add `centered` property to `WindowDescriptor`~~ - Add `WindowPosition` enum - `WindowDescriptor.position` is now `WindowPosition` instead of `Option<Vec2>` - Add `center_window` function to `Window` ## Migration Guide - If using `WindowDescriptor`, replace `position: None` with `position: WindowPosition::Default` and `position: Some(vec2)` with `WindowPosition::At(vec2)`. I'm not sure if this is the best approach, so feel free to give any feedback. Also I'm not sure how `Option`s should be handled in `bevy_winit/src/lib.rs:161`. Also, on window creation we can't (or at least I couldn't) get `outer_size`, so this doesn't include decorations in calculations.
# Objective - Fixes bevyengine#4993 ## Solution - ~~Add `centered` property to `WindowDescriptor`~~ - Add `WindowPosition` enum - `WindowDescriptor.position` is now `WindowPosition` instead of `Option<Vec2>` - Add `center_window` function to `Window` ## Migration Guide - If using `WindowDescriptor`, replace `position: None` with `position: WindowPosition::Default` and `position: Some(vec2)` with `WindowPosition::At(vec2)`. I'm not sure if this is the best approach, so feel free to give any feedback. Also I'm not sure how `Option`s should be handled in `bevy_winit/src/lib.rs:161`. Also, on window creation we can't (or at least I couldn't) get `outer_size`, so this doesn't include decorations in calculations.
# Objective - Fixes bevyengine#4993 ## Solution - ~~Add `centered` property to `WindowDescriptor`~~ - Add `WindowPosition` enum - `WindowDescriptor.position` is now `WindowPosition` instead of `Option<Vec2>` - Add `center_window` function to `Window` ## Migration Guide - If using `WindowDescriptor`, replace `position: None` with `position: WindowPosition::Default` and `position: Some(vec2)` with `WindowPosition::At(vec2)`. I'm not sure if this is the best approach, so feel free to give any feedback. Also I'm not sure how `Option`s should be handled in `bevy_winit/src/lib.rs:161`. Also, on window creation we can't (or at least I couldn't) get `outer_size`, so this doesn't include decorations in calculations.
# Objective - Fixes bevyengine#4993 ## Solution - ~~Add `centered` property to `WindowDescriptor`~~ - Add `WindowPosition` enum - `WindowDescriptor.position` is now `WindowPosition` instead of `Option<Vec2>` - Add `center_window` function to `Window` ## Migration Guide - If using `WindowDescriptor`, replace `position: None` with `position: WindowPosition::Default` and `position: Some(vec2)` with `WindowPosition::At(vec2)`. I'm not sure if this is the best approach, so feel free to give any feedback. Also I'm not sure how `Option`s should be handled in `bevy_winit/src/lib.rs:161`. Also, on window creation we can't (or at least I couldn't) get `outer_size`, so this doesn't include decorations in calculations.
Yes please; this seems incorrect to me and a new issue is the correct way to track this. |
Objective
Solution
Addcentered
property toWindowDescriptor
WindowPosition
enumWindowDescriptor.position
is nowWindowPosition
instead ofOption<Vec2>
center_window
function toWindow
Migration Guide
WindowDescriptor
, replaceposition: None
withposition: WindowPosition::Default
andposition: Some(vec2)
withWindowPosition::At(vec2)
.I'm not sure if this is the best approach, so feel free to give any feedback.
Also I'm not sure how
Option
s should be handled inbevy_winit/src/lib.rs:161
.Also, on window creation we can't (or at least I couldn't) get
outer_size
, so this doesn't include decorations in calculations.