Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MacOS handles window opacity better #7928

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion alacritty/src/display/window.rs
Expand Up @@ -23,6 +23,7 @@ use {
cocoa::appkit::NSColorSpace,
cocoa::base::{id, nil, NO, YES},
objc::{msg_send, sel, sel_impl},
objc::runtime::{Class, Object},
winit::platform::macos::{OptionAsAlt, WindowBuilderExtMacOS, WindowExtMacOS},
};

Expand Down Expand Up @@ -167,7 +168,7 @@ impl Window {
.with_title(&identity.title)
.with_theme(config.window.theme())
.with_visible(false)
.with_transparent(true)
.with_transparent(config.window_opacity() < 1.)
.with_blur(config.window.blur)
.with_maximized(config.window.maximized())
.with_fullscreen(config.window.fullscreen())
Expand Down Expand Up @@ -343,6 +344,9 @@ impl Window {

pub fn set_transparent(&self, transparent: bool) {
self.window.set_transparent(transparent);

#[cfg(target_os = "macos")]
set_transparent_macos(&self.window, transparent);
}

pub fn set_blur(&self, blur: bool) {
Expand Down Expand Up @@ -494,3 +498,25 @@ fn use_srgb_color_space(window: &WinitWindow) {
let _: () = msg_send![raw_window, setColorSpace: NSColorSpace::sRGBColorSpace(nil)];
}
}

#[cfg(target_os = "macos")]
fn set_transparent_macos(window: &WinitWindow, transparent: bool) {
let raw_window = match window.raw_window_handle() {
RawWindowHandle::AppKit(handle) => handle.ns_window as id,
_ => return,
};

// If transparent, fill background with NSColor.clearColor,
// otherwise fill with NSColor.windowBackgroundColor so that MacOS
// properly draws the window's border and opaque title bar.
unsafe {
let color_class = Class::get("NSColor").unwrap();
let bg_color: *const Object = if transparent {
msg_send![color_class, clearColor]
} else {
msg_send![color_class, windowBackgroundColor]
};
let _: () = msg_send![raw_window, setBackgroundColor: bg_color];
}
}