mirror of
https://github.com/storytold/egui.git
synced 2026-11-28 09:19:12 +01:00
E1: eframe: ViewportHooks for per-viewport windows and raw input
NativeOptions::viewport_hooks reports each viewport's native window when eframe creates it (root, deferred and immediate; glow reports a window once even though its initialize_window runs every frame) and when the viewport is removed, and receives every viewport's raw input before egui processes it, including immediate viewports, which skip App::raw_input_hook. Needed by craft-tablet to route native Wayland pen events. Not proposed upstream yet.
This commit is contained in:
1 parent
49682f8baa
commit
e7e37ad580
7 files changed
+87
-5
No files matched your search
@@ -400,6 +400,10 @@ pub struct NativeOptions {
|
||||
/// [`with_android_app`]: winit::platform::android::EventLoopBuilderExtAndroid::with_android_app
|
||||
#[cfg(target_os = "android")]
|
||||
pub android_app: Option<winit::platform::android::activity::AndroidApp>,
|
||||
|
||||
/// Per-viewport window and raw-input callbacks, see [`crate::ViewportHooks`].
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
pub viewport_hooks: Option<std::sync::Arc<dyn crate::ViewportHooks>>,
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -425,6 +429,9 @@ impl Clone for NativeOptions {
|
||||
#[cfg(target_os = "android")]
|
||||
android_app: self.android_app.clone(),
|
||||
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
viewport_hooks: self.viewport_hooks.clone(),
|
||||
|
||||
..*self
|
||||
}
|
||||
}
|
||||
@@ -468,6 +475,9 @@ impl Default for NativeOptions {
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
android_app: None,
|
||||
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
viewport_hooks: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,6 +197,10 @@ pub use native::macos::WindowChromeMetrics;
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
pub use native::run::EframeWinitApplication;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
pub use native::viewport_hooks::ViewportHooks;
|
||||
|
||||
#[cfg(not(any(target_arch = "wasm32", target_os = "ios")))]
|
||||
#[cfg(any(feature = "glow", feature = "wgpu_no_default_features"))]
|
||||
pub use native::run::EframePumpStatus;
|
||||
@@ -310,6 +314,7 @@ pub fn run_native_ext(
|
||||
app_creator: AppCreator<'_>,
|
||||
) -> Result {
|
||||
let renderer = init_native(app_name, &mut native_options);
|
||||
native::viewport_hooks::install(native_options.viewport_hooks.clone());
|
||||
|
||||
match renderer {
|
||||
#[cfg(feature = "glow")]
|
||||
|
||||
@@ -359,6 +359,7 @@ impl EpiIntegration {
|
||||
|
||||
raw_input.time = Some(self.beginning.elapsed().as_secs_f64());
|
||||
|
||||
super::viewport_hooks::with(|hooks| hooks.raw_input(&self.egui_ctx, &mut raw_input));
|
||||
app.raw_input_hook(&self.egui_ctx, &mut raw_input);
|
||||
|
||||
raw_input
|
||||
|
||||
@@ -1173,6 +1173,7 @@ impl GlutinWindowContext {
|
||||
let mut viewport_info = ViewportInfo::default();
|
||||
if let Some(window) = &window {
|
||||
viewport_from_window.insert(window.id(), ViewportId::ROOT);
|
||||
super::viewport_hooks::with(|hooks| hooks.window_created(ViewportId::ROOT, window));
|
||||
window_from_viewport.insert(ViewportId::ROOT, window.id());
|
||||
egui_winit::update_viewport_info(&mut viewport_info, egui_ctx, window, true);
|
||||
|
||||
@@ -1374,7 +1375,10 @@ impl GlutinWindowContext {
|
||||
self.current_gl_context = Some(current_gl_context);
|
||||
}
|
||||
|
||||
self.viewport_from_window.insert(window.id(), viewport_id);
|
||||
// This runs on every frame; report a window only when it is new for this viewport.
|
||||
if self.viewport_from_window.insert(window.id(), viewport_id) != Some(viewport_id) {
|
||||
super::viewport_hooks::with(|hooks| hooks.window_created(viewport_id, &window));
|
||||
}
|
||||
self.window_from_viewport.insert(viewport_id, window.id());
|
||||
|
||||
Ok(())
|
||||
@@ -1442,8 +1446,13 @@ impl GlutinWindowContext {
|
||||
viewport_output: &OrderedViewportIdMap<ViewportOutput>,
|
||||
) {
|
||||
// GC old viewports
|
||||
self.viewports
|
||||
.retain(|id, _| viewport_output.contains_key(id));
|
||||
self.viewports.retain(|id, _| {
|
||||
let keep = viewport_output.contains_key(id);
|
||||
if !keep {
|
||||
super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id));
|
||||
}
|
||||
keep
|
||||
});
|
||||
self.viewport_from_window
|
||||
.retain(|_, id| viewport_output.contains_key(id));
|
||||
self.window_from_viewport
|
||||
@@ -1658,6 +1667,7 @@ fn render_immediate_viewport(
|
||||
.map(|(id, viewport)| (*id, viewport.info.clone()))
|
||||
.collect();
|
||||
raw_input.time = Some(beginning.elapsed().as_secs_f64());
|
||||
super::viewport_hooks::with(|hooks| hooks.raw_input(egui_ctx, &mut raw_input));
|
||||
raw_input
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ mod epi_integration;
|
||||
mod event_loop_context;
|
||||
pub mod run;
|
||||
|
||||
pub mod viewport_hooks;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub(crate) mod macos;
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
//! Per-viewport native window and raw-input hooks.
|
||||
//!
|
||||
//! eframe exposes the root window's handles through [`crate::CreationContext`] only, and runs
|
||||
//! [`crate::App::raw_input_hook`] for the root and deferred viewports but not for immediate ones.
|
||||
//! Integrations that need every viewport's native window (for example to match Wayland surfaces
|
||||
//! named by a protocol the windowing layer doesn't implement) or that must add input to every
|
||||
//! viewport before egui processes it can set [`crate::NativeOptions::viewport_hooks`].
|
||||
|
||||
use std::sync::{Arc, PoisonError, RwLock};
|
||||
|
||||
/// Callbacks eframe makes for every viewport. All methods have empty defaults.
|
||||
///
|
||||
/// They run on the event loop thread, inside eframe's frame handling: keep them short and don't
|
||||
/// call back into eframe or block.
|
||||
pub trait ViewportHooks: Send + Sync {
|
||||
/// eframe created the native window of `viewport` (root, deferred or immediate).
|
||||
fn window_created(&self, _viewport: egui::ViewportId, _window: &winit::window::Window) {}
|
||||
|
||||
/// eframe dropped `viewport`; its window is about to be (or has been) destroyed.
|
||||
fn window_destroyed(&self, _viewport: egui::ViewportId) {}
|
||||
|
||||
/// The raw input of one viewport (`raw_input.viewport_id`), before egui processes it. Runs for
|
||||
/// root, deferred and immediate viewports, before [`crate::App::raw_input_hook`].
|
||||
fn raw_input(&self, _ctx: &egui::Context, _raw_input: &mut egui::RawInput) {}
|
||||
}
|
||||
|
||||
static HOOKS: RwLock<Option<Arc<dyn ViewportHooks>>> = RwLock::new(None);
|
||||
|
||||
pub(crate) fn install(hooks: Option<Arc<dyn ViewportHooks>>) {
|
||||
*HOOKS.write().unwrap_or_else(PoisonError::into_inner) = hooks;
|
||||
}
|
||||
|
||||
pub(crate) fn with(f: impl FnOnce(&dyn ViewportHooks)) {
|
||||
let hooks = HOOKS.read().unwrap_or_else(PoisonError::into_inner).clone();
|
||||
if let Some(hooks) = hooks {
|
||||
f(&*hooks);
|
||||
}
|
||||
}
|
||||
@@ -335,6 +335,7 @@ impl<'app> WgpuWinitApp<'app> {
|
||||
|
||||
let mut viewport_from_window = HashMap::default();
|
||||
viewport_from_window.insert(window.id(), ViewportId::ROOT);
|
||||
super::viewport_hooks::with(|hooks| hooks.window_created(ViewportId::ROOT, &window));
|
||||
|
||||
let mut viewports = Viewports::default();
|
||||
viewports.insert(
|
||||
@@ -861,7 +862,13 @@ impl WgpuWinitRunning<'_> {
|
||||
);
|
||||
|
||||
// Prune dead viewports:
|
||||
viewports.retain(|id, _| active_viewports_ids.contains(id));
|
||||
viewports.retain(|id, _| {
|
||||
let keep = active_viewports_ids.contains(id);
|
||||
if !keep {
|
||||
super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id));
|
||||
}
|
||||
keep
|
||||
});
|
||||
viewport_from_window.retain(|_, id| active_viewports_ids.contains(id));
|
||||
painter.gc_viewports(&active_viewports_ids);
|
||||
|
||||
@@ -1067,6 +1074,7 @@ impl Viewport {
|
||||
match egui_winit::create_window(egui_ctx, event_loop, &self.builder) {
|
||||
Ok(window) => {
|
||||
windows_id.insert(window.id(), viewport_id);
|
||||
super::viewport_hooks::with(|hooks| hooks.window_created(viewport_id, &window));
|
||||
|
||||
let window = Arc::new(window);
|
||||
|
||||
@@ -1183,6 +1191,8 @@ fn render_immediate_viewport(
|
||||
.map(|(id, viewport)| (*id, viewport.info.clone()))
|
||||
.collect();
|
||||
input.time = Some(beginning.elapsed().as_secs_f64());
|
||||
let egui_ctx = egui_ctx.clone();
|
||||
super::viewport_hooks::with(|hooks| hooks.raw_input(&egui_ctx, &mut input));
|
||||
input
|
||||
};
|
||||
|
||||
@@ -1265,7 +1275,13 @@ pub(crate) fn remove_viewports_not_in(
|
||||
let active_viewports_ids: ViewportIdSet = viewport_output.keys().copied().collect();
|
||||
|
||||
// Prune dead viewports:
|
||||
viewports.retain(|id, _| active_viewports_ids.contains(id));
|
||||
viewports.retain(|id, _| {
|
||||
let keep = active_viewports_ids.contains(id);
|
||||
if !keep {
|
||||
super::viewport_hooks::with(|hooks| hooks.window_destroyed(*id));
|
||||
}
|
||||
keep
|
||||
});
|
||||
viewport_from_window.retain(|_, id| active_viewports_ids.contains(id));
|
||||
painter.gc_viewports(&active_viewports_ids);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user