1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

eframe: Add center to NativeOptions and monitor_size to WindowInfo (#2035)

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Adia Robbie
2022-09-17 16:20:40 +07:00
committed by GitHub
parent c4117066cf
commit bc6a823103
4 changed files with 96 additions and 5 deletions

View File

@@ -21,6 +21,14 @@ pub fn read_window_info(window: &winit::window::Window, pixels_per_point: f32) -
.map(|pos| pos.to_logical::<f32>(pixels_per_point.into()))
.map(|pos| egui::Pos2 { x: pos.x, y: pos.y });
let monitor = window.current_monitor().is_some();
let monitor_size = if monitor {
let size = window.current_monitor().unwrap().size();
Some(egui::vec2(size.width as _, size.height as _))
} else {
None
};
let size = window
.inner_size()
.to_logical::<f32>(pixels_per_point.into());
@@ -32,6 +40,7 @@ pub fn read_window_info(window: &winit::window::Window, pixels_per_point: f32) -
x: size.width,
y: size.height,
},
monitor_size,
}
}
@@ -138,6 +147,7 @@ pub fn handle_app_output(
drag_window,
window_pos,
visible,
always_on_top,
} = app_output;
if let Some(decorated) = decorated {
@@ -176,6 +186,10 @@ pub fn handle_app_output(
if let Some(visible) = visible {
window.set_visible(visible);
}
if let Some(always_on_top) = always_on_top {
window.set_always_on_top(always_on_top);
}
}
// ----------------------------------------------------------------------------

View File

@@ -223,6 +223,27 @@ fn run_and_exit(
})
}
fn centere_window_pos(
monitor: Option<winit::monitor::MonitorHandle>,
native_options: &mut epi::NativeOptions,
) {
// Get the current_monitor.
if let Some(monitor) = monitor {
let monitor_size = monitor.size();
let inner_size = native_options
.initial_window_size
.unwrap_or(egui::Vec2 { x: 800.0, y: 600.0 });
if monitor_size.width > 0 && monitor_size.height > 0 {
let x = (monitor_size.width - inner_size.x as u32) / 2;
let y = (monitor_size.height - inner_size.y as u32) / 2;
native_options.initial_window_pos = Some(egui::Pos2 {
x: x as _,
y: y as _,
});
}
}
}
// ----------------------------------------------------------------------------
/// Run an egui app
#[cfg(feature = "glow")]
@@ -588,13 +609,22 @@ mod glow_integration {
app_creator: epi::AppCreator,
) {
if native_options.run_and_return {
with_event_loop(native_options, |event_loop, native_options| {
with_event_loop(native_options, |event_loop, mut native_options| {
if native_options.centered {
centere_window_pos(event_loop.available_monitors().next(), &mut native_options);
}
let glow_eframe =
GlowWinitApp::new(event_loop, app_name, native_options, app_creator);
run_and_return(event_loop, glow_eframe);
});
} else {
let event_loop = create_event_loop_builder(&mut native_options).build();
if native_options.centered {
centere_window_pos(event_loop.available_monitors().next(), &mut native_options);
}
let glow_eframe = GlowWinitApp::new(&event_loop, app_name, native_options, app_creator);
run_and_exit(event_loop, glow_eframe);
}
@@ -959,13 +989,22 @@ mod wgpu_integration {
app_creator: epi::AppCreator,
) {
if native_options.run_and_return {
with_event_loop(native_options, |event_loop, native_options| {
with_event_loop(native_options, |event_loop, mut native_options| {
if native_options.centered {
centere_window_pos(event_loop.available_monitors().next(), &mut native_options);
}
let wgpu_eframe =
WgpuWinitApp::new(event_loop, app_name, native_options, app_creator);
run_and_return(event_loop, wgpu_eframe);
});
} else {
let event_loop = create_event_loop_builder(&mut native_options).build();
if native_options.centered {
centere_window_pos(event_loop.available_monitors().next(), &mut native_options);
}
let wgpu_eframe = WgpuWinitApp::new(&event_loop, app_name, native_options, app_creator);
run_and_exit(event_loop, wgpu_eframe);
}