mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -04:00
eframe: Improve window centering (#2651)
* Create the winit::Window in one unified place * Fix wrong unit of initial_window_pos * Improve centering
This commit is contained in:
@@ -290,27 +290,6 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp +
|
||||
})
|
||||
}
|
||||
|
||||
fn center_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")]
|
||||
@@ -516,8 +495,7 @@ mod glow_integration {
|
||||
let window_settings = epi_integration::load_window_settings(storage);
|
||||
|
||||
let winit_window =
|
||||
epi_integration::window_builder(event_loop, title, native_options, window_settings)
|
||||
.build(event_loop)?;
|
||||
epi_integration::build_window(event_loop, title, native_options, window_settings)?;
|
||||
// a lot of the code below has been lifted from glutin example in their repo.
|
||||
let glutin_window_context =
|
||||
unsafe { GlutinWindowContext::new(winit_window, native_options)? };
|
||||
@@ -863,22 +841,13 @@ mod glow_integration {
|
||||
app_creator: epi::AppCreator,
|
||||
) -> Result<()> {
|
||||
if native_options.run_and_return {
|
||||
with_event_loop(native_options, |event_loop, mut native_options| {
|
||||
if native_options.centered {
|
||||
center_window_pos(event_loop.available_monitors().next(), &mut native_options);
|
||||
}
|
||||
|
||||
with_event_loop(native_options, |event_loop, 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 {
|
||||
center_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);
|
||||
}
|
||||
@@ -946,12 +915,9 @@ mod wgpu_integration {
|
||||
storage: Option<&dyn epi::Storage>,
|
||||
title: &str,
|
||||
native_options: &NativeOptions,
|
||||
) -> Result<winit::window::Window> {
|
||||
) -> std::result::Result<winit::window::Window, winit::error::OsError> {
|
||||
let window_settings = epi_integration::load_window_settings(storage);
|
||||
Ok(
|
||||
epi_integration::window_builder(event_loop, title, native_options, window_settings)
|
||||
.build(event_loop)?,
|
||||
)
|
||||
epi_integration::build_window(event_loop, title, native_options, window_settings)
|
||||
}
|
||||
|
||||
#[allow(unsafe_code)]
|
||||
@@ -1286,22 +1252,13 @@ mod wgpu_integration {
|
||||
app_creator: epi::AppCreator,
|
||||
) -> Result<()> {
|
||||
if native_options.run_and_return {
|
||||
with_event_loop(native_options, |event_loop, mut native_options| {
|
||||
if native_options.centered {
|
||||
center_window_pos(event_loop.available_monitors().next(), &mut native_options);
|
||||
}
|
||||
|
||||
with_event_loop(native_options, |event_loop, 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 {
|
||||
center_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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user