Skip to content

How do I create a window centered on the primary monitor? #3842

Answered by DogeDark
mamekoro asked this question in Q&A
Discussion options

You must be logged in to vote

I found that what works for me is a run-once effect that gets the monitor size since it doesn't seem easily accessible in main.

Something like:

const WINDOW_INNER_SIZE: PhysicalSize<u32> = PhysicalSize::new(800, 600);

#[component]
pub fn App() -> Element {
    use_effect(move || {
        let window = &window().window;

        // There should be a method on `window` to list the monitors instead of using the current one if desired.
        let monitor = window.current_monitor().unwrap();

        let size = monitor.size();
        let new_position = PhysicalPosition::new(
            (size.width - WINDOW_INNER_SIZE.width) / 2, 
            (size.height - WINDOW_INNER_SIZE.height) / 2,

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mamekoro
Comment options

Answer selected by mamekoro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants