From 5c4fb92b87ce02e3b2cfb5eb879f78b1f20ba363 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Jul 2021 12:46:23 -0700 Subject: [PATCH] fixup wgpu example --- window/examples/wgpu.rs | 31 ++++++++------ window/src/os/macos/window.rs | 77 ++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 50 deletions(-) diff --git a/window/examples/wgpu.rs b/window/examples/wgpu.rs index a7c1ff676..f152507f8 100644 --- a/window/examples/wgpu.rs +++ b/window/examples/wgpu.rs @@ -132,10 +132,12 @@ impl MyWindow { ); self.gpu.replace(gpu); + self.paint(win)?; Ok(()) } - fn paint(&mut self) -> anyhow::Result<()> { + fn paint(&mut self, win: &Window) -> anyhow::Result<()> { + self.fix_transparency(win); if let Some(gpu) = self.gpu.as_mut() { let frame = match gpu.swap_chain.get_current_frame() { Ok(frame) => frame, @@ -179,6 +181,19 @@ impl MyWindow { Ok(()) } + fn fix_transparency(&mut self, win: &Window) { + #[cfg(target_os = "macos")] + if let RawWindowHandle::MacOS(h) = win.raw_window_handle() { + use cocoa::base::{id, NO}; + use objc::*; + unsafe { + // Allow transparency, as the default for Metal is opaque + let layer: id = msg_send![h.ns_view as id, layer]; + let () = msg_send![layer, setOpaque: NO]; + } + } + } + fn resize(&mut self, dims: Dimensions) { if self.dims == dims { // May just be a move event @@ -211,17 +226,7 @@ impl MyWindow { } => { self.resize(dimensions); #[cfg(target_os = "macos")] - if let RawWindowHandle::MacOS(h) = win.raw_window_handle() { - use cocoa::base::{id, NO}; - use objc::*; - unsafe { - // Allow transparency, as the default for Metal is opaque - let layer: id = msg_send![h.ns_view as id, layer]; - let () = msg_send![layer, setOpaque: NO]; - } - - self.paint().unwrap(); - } + self.paint(win).unwrap(); } WindowEvent::MouseEvent(event) => { self.cursor_pos = event.coords; @@ -238,7 +243,7 @@ impl MyWindow { win.default_key_processing(key); } WindowEvent::NeedRepaint => { - self.paint().unwrap(); + self.paint(win).unwrap(); } WindowEvent::Notification(_) | WindowEvent::FocusChanged(_) => {} } diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index df41fc715..90c4531f6 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -358,7 +358,14 @@ fn function_key_to_keycode(function_key: char) -> KeyCode { } #[derive(Debug, Clone)] -pub struct Window(usize); +pub struct Window { + id: usize, + ns_window: *mut Object, + ns_view: *mut Object, +} + +unsafe impl Send for Window {} +unsafe impl Sync for Window {} impl Window { pub async fn new_window( @@ -388,9 +395,7 @@ impl Window { let conn = Connection::get().expect("Connection::init has not been called"); let window_id = conn.next_window_id(); - let window_handle = Window(window_id); - let mut events = WindowEventSender::new(event_handler); - events.assign_window(window_handle.clone()); + let events = WindowEventSender::new(event_handler); let inner = Rc::new(RefCell::new(Inner { events, @@ -470,6 +475,11 @@ impl Window { let height = backing_frame.size.height; let weak_window = window.weak(); + let window_handle = Window { + id: window_id, + ns_window: *window, + ns_view: *view, + }; let window_inner = Rc::new(RefCell::new(WindowInner { window, view, @@ -480,6 +490,11 @@ impl Window { .borrow_mut() .insert(window_id, Rc::clone(&window_inner)); + inner + .borrow_mut() + .events + .assign_window(window_handle.clone()); + window_handle.config_did_change(&config); // Synthesize a resize event immediately; this allows @@ -502,21 +517,18 @@ impl Window { unsafe impl HasRawWindowHandle for Window { fn raw_window_handle(&self) -> RawWindowHandle { - let conn = Connection::get().expect("raw_window_handle only callable on main thread"); - let handle = conn.window_by_id(self.0).expect("window handle invalid!?"); - - let inner = handle.borrow(); - let window_view = - WindowView::get_this(unsafe { &**inner.view }).expect("window view invalid!?"); - - window_view.inner.borrow().raw_window_handle() + RawWindowHandle::MacOS(MacOSHandle { + ns_window: self.ns_window as *mut _, + ns_view: self.ns_view as *mut _, + ..MacOSHandle::empty() + }) } } #[async_trait(?Send)] impl WindowOps for Window { async fn enable_opengl(&self) -> anyhow::Result> { - let window_id = self.0; + let window_id = self.id; promise::spawn::spawn(async move { if let Some(handle) = Connection::get().unwrap().window_by_id(window_id) { let mut inner = handle.borrow_mut(); @@ -532,7 +544,7 @@ impl WindowOps for Window { where Self: Sized, { - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { if let Some(window_view) = WindowView::get_this(unsafe { &**inner.view }) { window_view .inner @@ -545,35 +557,35 @@ impl WindowOps for Window { } fn close(&self) { - Connection::with_window_inner(self.0, |inner| { + Connection::with_window_inner(self.id, |inner| { inner.close(); Ok(()) }); } fn hide(&self) { - Connection::with_window_inner(self.0, |inner| { + Connection::with_window_inner(self.id, |inner| { inner.hide(); Ok(()) }); } fn show(&self) { - Connection::with_window_inner(self.0, |inner| { + Connection::with_window_inner(self.id, |inner| { inner.show(); Ok(()) }); } fn set_cursor(&self, cursor: Option) { - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { let _ = inner.set_cursor(cursor); Ok(()) }); } fn invalidate(&self) { - Connection::with_window_inner(self.0, |inner| { + Connection::with_window_inner(self.id, |inner| { inner.invalidate(); Ok(()) }); @@ -581,25 +593,28 @@ impl WindowOps for Window { fn set_title(&self, title: &str) { let title = title.to_owned(); - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { inner.set_title(&title); Ok(()) }); } fn set_inner_size(&self, width: usize, height: usize) { - Connection::with_window_inner(self.0, move |inner| Ok(inner.set_inner_size(width, height))); + Connection::with_window_inner( + self.id, + move |inner| Ok(inner.set_inner_size(width, height)), + ); } fn set_window_position(&self, coords: ScreenPoint) { - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { inner.set_window_position(coords); Ok(()) }); } fn set_text_cursor_position(&self, cursor: Rect) { - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { inner.set_text_cursor_position(cursor); Ok(()) }); @@ -622,7 +637,7 @@ impl WindowOps for Window { } fn toggle_fullscreen(&self) { - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { inner.toggle_fullscreen(); Ok(()) }); @@ -630,7 +645,7 @@ impl WindowOps for Window { fn config_did_change(&self, config: &ConfigHandle) { let config = config.clone(); - Connection::with_window_inner(self.0, move |inner| { + Connection::with_window_inner(self.id, move |inner| { inner.config_did_change(&config); Ok(()) }); @@ -987,18 +1002,6 @@ struct Inner { config: ConfigHandle, } -unsafe impl HasRawWindowHandle for Inner { - fn raw_window_handle(&self) -> RawWindowHandle { - let ns_window = self.window.as_ref().unwrap().load(); - let ns_view = self.view_id.as_ref().unwrap().load(); - RawWindowHandle::MacOS(MacOSHandle { - ns_window: *ns_window as *mut _, - ns_view: *ns_view as *mut _, - ..MacOSHandle::empty() - }) - } -} - #[repr(C)] pub struct __InputSource { _dummy: i32,