Skip to content

Commit

Permalink
Add hook for raw winit WindowEvents
Browse files Browse the repository at this point in the history
Based on [@twvd's commit](twvd@856d675)
to work around [egui#3653](emilk#3653)
  • Loading branch information
HactarCE committed Feb 4, 2025
1 parent 83d81fa commit 7aeeab8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,9 @@ dependencies = [
"arboard",
"document-features",
"egui",
"lazy_static",
"log",
"parking_lot",
"profiling",
"raw-window-handle 0.6.2",
"serde",
Expand Down
4 changes: 4 additions & 0 deletions crates/egui-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ document-features = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
webbrowser = { version = "1.0.0", optional = true }

# hack for raw keyboard input
parking_lot = { workspace = true }
lazy_static = "1.5.0"

[target.'cfg(any(target_os="linux", target_os="dragonfly", target_os="freebsd", target_os="netbsd", target_os="openbsd"))'.dependencies]
smithay-clipboard = { version = "0.7.2", optional = true }

Expand Down
15 changes: 15 additions & 0 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ use winit::{
window::{CursorGrabMode, Window, WindowButtons, WindowLevel},
};

lazy_static::lazy_static! {
static ref WINDOWEVENT_HOOK:
parking_lot::RwLock<Option<std::sync::mpsc::Sender<winit::event::WindowEvent>>,
> = parking_lot::RwLock::new(None);
}
pub fn install_windowevent_hook(sender: std::sync::mpsc::Sender<winit::event::WindowEvent>) {
let mut hook = WINDOWEVENT_HOOK.write();
assert!(hook.is_none(), "duplicate windowevent hook");
*hook = Some(sender);
}

pub fn screen_size_in_pixels(window: &Window) -> egui::Vec2 {
let size = if cfg!(target_os = "ios") {
// `outer_size` Includes the area behind the "dynamic island".
Expand Down Expand Up @@ -272,6 +283,10 @@ impl State {
accesskit.process_event(window, event);
}

if let Some(sender) = &*WINDOWEVENT_HOOK.read() {
sender.send(event.clone()).unwrap();
}

use winit::event::WindowEvent;
match event {
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
Expand Down

0 comments on commit 7aeeab8

Please sign in to comment.