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

Expose Raw Window and Display Handles #3073

Merged
merged 3 commits into from
Jun 11, 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C


## Unreleased

* Expose raw window and display handles in `CreationContext` and `Frame`

## 0.22.0 - 2023-05-23
* Fix: `request_repaint_after` works even when called from background thread [#2939](https://github.com/emilk/egui/pull/2939)
Expand Down
1 change: 1 addition & 0 deletions crates/eframe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ egui_glow = { version = "0.22.0", path = "../egui_glow", optional = true, defaul
glow = { version = "0.12", optional = true }
ron = { version = "0.8", optional = true, features = ["integer128"] }
serde = { version = "1", optional = true, features = ["derive"] }
static_assertions = "1.1.0"

# -------------------------------------------
# native:
Expand Down
63 changes: 63 additions & 0 deletions crates/eframe/src/epi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ use std::any::Any;
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub use crate::native::run::UserEvent;

#[cfg(not(target_arch = "wasm32"))]
use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
#[cfg(not(target_arch = "wasm32"))]
use static_assertions::assert_not_impl_any;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(any(feature = "glow", feature = "wgpu"))]
pub use winit::event_loop::EventLoopBuilder;
Expand Down Expand Up @@ -64,6 +71,34 @@ pub struct CreationContext<'s> {
/// Can be used to manage GPU resources for custom rendering with WGPU using [`egui::PaintCallback`]s.
#[cfg(feature = "wgpu")]
pub wgpu_render_state: Option<egui_wgpu::RenderState>,

/// Raw platform window handle
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_window_handle: RawWindowHandle,

/// Raw platform display handle for window
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_display_handle: RawDisplayHandle,
}

// Implementing `Clone` would violate the guarantees of `HasRawWindowHandle` and `HasRawDisplayHandle`.
#[cfg(not(target_arch = "wasm32"))]
assert_not_impl_any!(CreationContext<'_>: Clone);

#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawWindowHandle for CreationContext<'_> {
fn raw_window_handle(&self) -> RawWindowHandle {
self.raw_window_handle
}
}

#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawDisplayHandle for CreationContext<'_> {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.raw_display_handle
}
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -695,6 +730,34 @@ pub struct Frame {
/// such that it can be retrieved during [`App::post_rendering`] with [`Frame::screenshot`]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) screenshot: std::cell::Cell<Option<egui::ColorImage>>,

/// Raw platform window handle
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_window_handle: RawWindowHandle,

/// Raw platform display handle for window
#[cfg(not(target_arch = "wasm32"))]
pub(crate) raw_display_handle: RawDisplayHandle,
}

// Implementing `Clone` would violate the guarantees of `HasRawWindowHandle` and `HasRawDisplayHandle`.
#[cfg(not(target_arch = "wasm32"))]
assert_not_impl_any!(Frame: Clone);

#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawWindowHandle for Frame {
fn raw_window_handle(&self) -> RawWindowHandle {
self.raw_window_handle
}
}

#[allow(unsafe_code)]
#[cfg(not(target_arch = "wasm32"))]
unsafe impl HasRawDisplayHandle for Frame {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.raw_display_handle
}
}

impl Frame {
Expand Down
4 changes: 4 additions & 0 deletions crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use winit::event_loop::EventLoopWindowTarget;
#[cfg(target_os = "macos")]
use winit::platform::macos::WindowBuilderExtMacOS as _;

use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};

#[cfg(feature = "accesskit")]
use egui::accesskit;
use egui::NumExt as _;
Expand Down Expand Up @@ -380,6 +382,8 @@ impl EpiIntegration {
#[cfg(feature = "wgpu")]
wgpu_render_state,
screenshot: std::cell::Cell::new(None),
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
};

let mut egui_winit = egui_winit::State::new(event_loop);
Expand Down
6 changes: 5 additions & 1 deletion crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::time::Instant;

use raw_window_handle::{HasRawDisplayHandle as _, HasRawWindowHandle as _};
use winit::event_loop::{
ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget,
};
Expand Down Expand Up @@ -345,7 +346,6 @@ mod glow_integration {
prelude::{GlDisplay, NotCurrentGlContextSurfaceAccessor, PossiblyCurrentGlContext},
surface::GlSurface,
};
use raw_window_handle::HasRawWindowHandle;

use super::*;

Expand Down Expand Up @@ -756,6 +756,8 @@ mod glow_integration {
gl: Some(gl.clone()),
#[cfg(feature = "wgpu")]
wgpu_render_state: None,
raw_display_handle: gl_window.window().raw_display_handle(),
raw_window_handle: gl_window.window().raw_window_handle(),
});

if app.warm_up_enabled() {
Expand Down Expand Up @@ -1215,6 +1217,8 @@ mod wgpu_integration {
#[cfg(feature = "glow")]
gl: None,
wgpu_render_state,
raw_display_handle: window.raw_display_handle(),
raw_window_handle: window.raw_window_handle(),
});

if app.warm_up_enabled() {
Expand Down