Center Screen #1801
Replies: 3 comments 6 replies
-
I find a way to make window show at center. #[derive(Deserialize, Serialize, Default)]
pub struct Weaver {}
impl App for Weaver {
fn update(&mut self, ctx: &Context, frame: &mut Frame) {}
fn persist_native_window(&self) -> bool {
false
}
}
fn main() {
let mut native_options = eframe::NativeOptions::default();
let mut monitors = winit::event_loop::EventLoop::new().available_monitors();
let first_monitor_size = monitors.next().unwrap().size();
let width = 300.0;
let height = 100.0;
native_options.initial_window_size = Some(egui::Vec2::new(width, height));
native_options.initial_window_pos = Some(egui::Pos2::new(
(first_monitor_size.width as f32 - width) / 2.0,
(first_monitor_size.height as f32 - height) / 2.0,
));
eframe::run_native(
"weaver2",
native_options,
Box::new(|creation_context| Box::new(Weaver::default())),
);
}
winit = "0.26.1" |
Beta Was this translation helpful? Give feedback.
-
btw with the latest winit you don't have the monitor information until you run the event loop. Would this mean that it is now impossible to center the window before the creation? Does it make sense to instantiate briefly the event loop just to retrieve the screen information needed? |
Beta Was this translation helpful? Give feedback.
-
I ended up doing this trick inside the update function. I don't really like it... if someone has better options please let me know: let mut desired_pos: Option<Vec2>= None;
ctx.input(|input| {
if let Some(size) = input.viewport().monitor_size {
let center = pos2(size.x/2.0, size.y /2.0) - pos2(310.0, 120.0) ;
desired_pos = input.viewport().outer_rect.and_then(|current_position| {
if current_position.min.x.round() == center.x && current_position.min.y.round() == center.y {
None
} else {
Some(center)
}
})
}
});
if let Some(pos) = desired_pos {
let move_command = ViewportCommand::OuterPosition(pos2(pos.x, pos.y));
ctx.send_viewport_cmd(move_command);
} |
Beta Was this translation helpful? Give feedback.
-
Hello, how can I make the window show at center screen when I launch the program instead of anywhere ?
Beta Was this translation helpful? Give feedback.
All reactions