Skip to content

Commit

Permalink
Fix transparency on macOS (#260)
Browse files Browse the repository at this point in the history
* Fix transparency on macOS

Unlike other platforms, macOS add another opaque layer on CALayer.
This makes the window opaque even if winit window is set to transparent.

* Determine layer opaque based on NSWindow's opqaue
  • Loading branch information
wusyong authored Sep 29, 2023
1 parent be521a6 commit a90f783
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions surfman/src/platform/macos/cgl/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ impl Connection {
match raw_handle {
AppKit(handle) => Ok(NativeWidget {
view: NSView(unsafe { msg_send![handle.ns_view as id, retain] }),
opaque: unsafe { msg_send![handle.ns_window as id, isOpaque] },
}),
_ => Err(Error::IncompatibleNativeWidget),
}
Expand Down
4 changes: 4 additions & 0 deletions surfman/src/platform/macos/system/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,14 @@ impl Connection {
window: &Window,
) -> Result<NativeWidget, Error> {
let ns_view = window.ns_view() as id;
let ns_window = window.ns_window() as id;
if ns_view.is_null() {
return Err(Error::IncompatibleNativeWidget);
}
unsafe {
Ok(NativeWidget {
view: NSView(msg_send![ns_view, retain]),
opaque: msg_send![ns_window as id, isOpaque],
})
}
}
Expand All @@ -164,6 +166,7 @@ impl Connection {
) -> NativeWidget {
NativeWidget {
view: NSView(raw as id),
opaque: true,
}
}

Expand All @@ -179,6 +182,7 @@ impl Connection {
match raw_handle {
AppKit(handle) => Ok(NativeWidget {
view: NSView(unsafe { msg_send![handle.ns_view as id, retain] }),
opaque: unsafe { msg_send![handle.ns_window as id, isOpaque] },
}),
_ => Err(Error::IncompatibleNativeWidget),
}
Expand Down
13 changes: 9 additions & 4 deletions surfman/src/platform/macos/system/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub(crate) struct ViewInfo {
logical_size: NSSize,
display_link: DisplayLink,
next_vblank: Arc<VblankCond>,
opaque: bool,
}

struct VblankCond {
Expand All @@ -94,6 +95,8 @@ pub struct NSView(pub(crate) id);
pub struct NativeWidget {
/// The `NSView` object.
pub view: NSView,
/// A bool value that indicates whether widget's NSWindow is opaque.
pub opaque: bool,
}

/// Represents the CPU view of the pixel data of this surface.
Expand Down Expand Up @@ -204,12 +207,13 @@ impl Device {
}];
let logical_size = logical_rect.size;

let opaque = native_widget.opaque;
let layer = CALayer::new();
let layer_size = CGSize::new(logical_size.width as f64, logical_size.height as f64);
layer.set_frame(&CGRect::new(&CG_ZERO_POINT, &layer_size));
layer.set_contents(front_surface.obj as id);
layer.set_opaque(true);
layer.set_contents_opaque(true);
layer.set_opaque(opaque);
layer.set_contents_opaque(opaque);
superlayer.add_sublayer(&layer);

let view = native_widget.view.clone();
Expand All @@ -223,6 +227,7 @@ impl Device {
logical_size,
display_link,
next_vblank,
opaque,
}
}

Expand Down Expand Up @@ -284,8 +289,8 @@ impl Device {
view_info
.layer
.set_contents(view_info.front_surface.obj as id);
view_info.layer.set_opaque(true);
view_info.layer.set_contents_opaque(true);
view_info.layer.set_opaque(view_info.opaque);
view_info.layer.set_contents_opaque(view_info.opaque);
surface.io_surface = self.create_io_surface(&size, surface.access);
surface.size = size;
}
Expand Down

0 comments on commit a90f783

Please sign in to comment.