Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NativeOptions::window_builder for more customization #3390

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion crates/eframe/src/epi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use static_assertions::assert_not_impl_any;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub use winit::event_loop::EventLoopBuilder;
pub use winit::{event_loop::EventLoopBuilder, window::WindowBuilder};

/// Hook into the building of an event loop before it is run
///
Expand All @@ -38,6 +38,14 @@ pub use winit::event_loop::EventLoopBuilder;
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub type EventLoopBuilderHook = Box<dyn FnOnce(&mut EventLoopBuilder<UserEvent>)>;

/// Hook into the building of a the native window.
///
/// You can configure any platform specific details required on top of the default configuration
/// done by `eframe`.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub type WindowBuilderHook = Box<dyn FnOnce(WindowBuilder) -> WindowBuilder>;

/// This is how your app is created.
///
/// You can use the [`CreationContext`] to setup egui, restore state, setup OpenGL things, etc.
Expand Down Expand Up @@ -397,6 +405,15 @@ pub struct NativeOptions {
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub event_loop_builder: Option<EventLoopBuilderHook>,

/// Hook into the building of a window.
///
/// Specify a callback here in case you need to make platform specific changes to the
/// window appearance.
///
/// Note: A [`NativeOptions`] clone will not include any `window_builder` hook.
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub window_builder: Option<WindowBuilderHook>,

#[cfg(feature = "glow")]
/// Needed for cross compiling for VirtualBox VMSVGA driver with OpenGL ES 2.0 and OpenGL 2.1 which doesn't support SRGB texture.
/// See <https://github.com/emilk/egui/pull/1993>.
Expand Down Expand Up @@ -466,6 +483,9 @@ impl Clone for NativeOptions {
#[cfg(any(feature = "glow", feature = "wgpu"))]
event_loop_builder: None, // Skip any builder callbacks if cloning

#[cfg(any(feature = "glow", feature = "wgpu"))]
window_builder: None, // Skip any builder callbacks if cloning

#[cfg(feature = "wgpu")]
wgpu_options: self.wgpu_options.clone(),

Expand Down Expand Up @@ -520,6 +540,9 @@ impl Default for NativeOptions {
#[cfg(any(feature = "glow", feature = "wgpu"))]
event_loop_builder: None,

#[cfg(any(feature = "glow", feature = "wgpu"))]
window_builder: None,

#[cfg(feature = "glow")]
shader_version: None,

Expand Down
7 changes: 5 additions & 2 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn read_window_info(
pub fn window_builder<E>(
event_loop: &EventLoopWindowTarget<E>,
title: &str,
native_options: &epi::NativeOptions,
native_options: &mut epi::NativeOptions,
Copy link
Owner

@emilk emilk Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding mut everywhere is quite ugly.

This can be fixed by using Rc<dyn Fn(…)> as the callback type, instead of just Box. That will also allow cloning.

…but we won't get to use FnOnce, which is a shame

window_settings: Option<WindowSettings>,
) -> winit::window::WindowBuilder {
let epi::NativeOptions {
Expand Down Expand Up @@ -179,7 +179,10 @@ pub fn window_builder<E>(
}
}

window_builder
match std::mem::take(&mut native_options.window_builder) {
Some(hook) => hook(window_builder),
None => window_builder,
}
}

pub fn apply_native_options_to_window(
Expand Down
10 changes: 5 additions & 5 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ mod glow_integration {
event_loop: &EventLoopWindowTarget<UserEvent>,
storage: Option<&dyn epi::Storage>,
title: &str,
native_options: &NativeOptions,
native_options: &mut NativeOptions,
) -> Result<(GlutinWindowContext, glow::Context)> {
crate::profile_function!();

Expand Down Expand Up @@ -749,7 +749,7 @@ mod glow_integration {
event_loop,
storage.as_deref(),
&self.app_name,
&self.native_options,
&mut self.native_options,
)?;
let gl = Arc::new(gl);

Expand Down Expand Up @@ -1184,7 +1184,7 @@ mod wgpu_integration {
event_loop: &EventLoopWindowTarget<UserEvent>,
storage: Option<&dyn epi::Storage>,
title: &str,
native_options: &NativeOptions,
native_options: &mut NativeOptions,
) -> std::result::Result<winit::window::Window, winit::error::OsError> {
crate::profile_function!();

Expand Down Expand Up @@ -1439,7 +1439,7 @@ mod wgpu_integration {
event_loop,
running.integration.frame.storage(),
&self.app_name,
&self.native_options,
&mut self.native_options,
)?;
self.set_window(window)?;
}
Expand All @@ -1454,7 +1454,7 @@ mod wgpu_integration {
event_loop,
storage.as_deref(),
&self.app_name,
&self.native_options,
&mut self.native_options,
)?;
self.init_run_state(event_loop, storage, window)?;
}
Expand Down
Loading