-
I'm trying to create a Linux desktop app and center its window on the primary monitor. (Other platforms need not be considered.) I know that Here's what my code looks like: use dioxus::{
desktop::{
wry::dpi::{PhysicalPosition, PhysicalSize},
Config, WindowBuilder,
},
prelude::*,
};
const WINDOW_INNER_SIZE: PhysicalSize<u32> = PhysicalSize::new(800, 600);
fn main() {
dioxus::LaunchBuilder::desktop()
.with_cfg(
Config::new().with_window(
WindowBuilder::new()
.with_position({
let primary_monitor_size: PhysicalSize<u32> = todo!();
PhysicalPosition::new(
(primary_monitor_size.width - WINDOW_INNER_SIZE.width) / 2,
(primary_monitor_size.height - WINDOW_INNER_SIZE.height) / 2,
)
})
.with_inner_size(WINDOW_INNER_SIZE),
),
)
.launch(App);
}
#[component]
fn App() -> Element {
rsx! { "hello" }
} |
Beta Was this translation helpful? Give feedback.
Answered by
DogeDark
Mar 7, 2025
Replies: 1 comment 1 reply
-
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 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,
);
window.set_outer_position(new_position);
});
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mamekoro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: