From e81fb6e7214094e0bd14bb61e87de8e9a7157e6a Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 6 May 2021 19:15:14 -0700 Subject: [PATCH] make alpha work on macos --- window/examples/wgpu.rs | 13 +++++++++++++ window/src/os/macos/window.rs | 24 +++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/window/examples/wgpu.rs b/window/examples/wgpu.rs index 0d9208728..04ecc1d40 100644 --- a/window/examples/wgpu.rs +++ b/window/examples/wgpu.rs @@ -1,6 +1,7 @@ use ::window::*; use anyhow::Context; use promise::spawn::spawn; +use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; pub struct GpuContext { pub swap_chain: wgpu::SwapChain, @@ -227,6 +228,18 @@ async fn spawn_window() -> anyhow::Result<()> { is_full_screen: _, } => { state.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]; + } + + state.paint()?; + } } WindowEvent::MouseEvent(event) => { state.cursor_pos = event.coords; diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 2b0a01f6d..a0b2229cb 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -17,8 +17,9 @@ use cocoa::appkit::{ NSWindowStyleMask, }; use cocoa::base::*; -use cocoa::foundation::NSAutoreleasePool; -use cocoa::foundation::{NSArray, NSNotFound, NSPoint, NSRect, NSSize, NSUInteger}; +use cocoa::foundation::{ + NSArray, NSAutoreleasePool, NSInteger, NSNotFound, NSPoint, NSRect, NSSize, NSUInteger, +}; use config::ConfigHandle; use core_foundation::base::{CFTypeID, TCFType}; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; @@ -39,6 +40,9 @@ use std::rc::Rc; use std::str::FromStr; use std::time::Instant; +#[allow(non_upper_case_globals)] +const NSViewLayerContentsPlacementTopLeft: NSInteger = 11; + fn round_away_from_zerof(value: f64) -> f64 { if value > 0. { value.max(1.).round() @@ -189,17 +193,6 @@ impl GlContextPair { } }; - #[allow(non_upper_case_globals)] - unsafe { - use cocoa::foundation::NSInteger; - const NSViewLayerContentsPlacementTopLeft: NSInteger = 11; - - let () = msg_send![ - view, - setLayerContentsPlacement: NSViewLayerContentsPlacementTopLeft - ]; - } - Ok(Self { context, backend }) } } @@ -455,6 +448,11 @@ impl Window { view.initWithFrame_(rect); view.setAutoresizingMask_(NSViewHeightSizable | NSViewWidthSizable); + let () = msg_send![ + *view, + setLayerContentsPlacement: NSViewLayerContentsPlacementTopLeft + ]; + window.setContentView_(*view); window.setDelegate_(*view);