From c0b29fef24f85dbfdf28f56d0b42261d62e23b47 Mon Sep 17 00:00:00 2001 From: Jay Madden <25359755+Jay-Madden@users.noreply.github.com> Date: Sat, 8 Mar 2025 16:16:56 -0700 Subject: [PATCH] feat(macos): add support for titlebar background matching theme background on macos closes: #6558 --- Cargo.lock | 38 ++++++++++ Cargo.toml | 1 + wezterm-input-types/src/lib.rs | 6 ++ window/Cargo.toml | 1 + window/src/os/macos/window.rs | 125 +++++++++++++++++++++++++++++++-- 5 files changed, 167 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 282de9489..d20d2016d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3713,6 +3713,21 @@ dependencies = [ "objc2", ] +[[package]] +name = "objc2-core-graphics" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dca602628b65356b6513290a21a6405b4d4027b8b250f0b98dddbb28b7de02" +dependencies = [ + "bitflags 2.9.0", + "block2", + "libc", + "objc2", + "objc2-core-foundation", + "objc2-io-surface", + "objc2-metal", +] + [[package]] name = "objc2-core-location" version = "0.3.0" @@ -3742,6 +3757,28 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "objc2-io-surface" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "161a8b87e32610086e1a7a9e9ec39f84459db7b3a0881c1f16ca5a2605581c19" +dependencies = [ + "bitflags 2.9.0", + "objc2", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-metal" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01c41bc8b0e50ea7a5304a56f25e0066f526e99641b46fd7b9ad4421dd35bff6" +dependencies = [ + "bitflags 2.9.0", + "objc2", + "objc2-foundation", +] + [[package]] name = "objc2-user-notifications" version = "0.3.0" @@ -7050,6 +7087,7 @@ dependencies = [ "metrics", "mio 0.8.11", "objc", + "objc2-core-graphics", "plist", "promise", "raw-window-handle", diff --git a/Cargo.toml b/Cargo.toml index 230fdeb70..be8f73306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -149,6 +149,7 @@ num-derive = "0.4" num-traits = "0.2" objc = "0.2" objc2 = "0.6" +objc2-core-graphics = "0.3" objc2-foundation = "0.3" objc2-user-notifications = "0.3" openssl = "0.10.57" diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index 1726db846..9f93dc9ec 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -1977,6 +1977,7 @@ bitflags! { const MACOS_FORCE_ENABLE_SHADOW = 4|8; const INTEGRATED_BUTTONS = 16; const MACOS_FORCE_SQUARE_CORNERS = 32; + const MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR = 64; } } @@ -1992,6 +1993,9 @@ impl Into for &WindowDecorations { if self.contains(WindowDecorations::INTEGRATED_BUTTONS) { s.push("INTEGRATED_BUTTONS"); } + if self.contains(WindowDecorations::MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR) { + s.push("MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR") + } if self.contains(WindowDecorations::MACOS_FORCE_ENABLE_SHADOW) { s.push("MACOS_FORCE_ENABLE_SHADOW"); } else if self.contains(WindowDecorations::MACOS_FORCE_DISABLE_SHADOW) { @@ -2019,6 +2023,8 @@ impl TryFrom for WindowDecorations { flags = Self::NONE; } else if ele == "RESIZE" { flags |= Self::RESIZE; + } else if ele == "MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR" { + flags |= Self::MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR; } else if ele == "MACOS_FORCE_DISABLE_SHADOW" { flags |= Self::MACOS_FORCE_DISABLE_SHADOW; } else if ele == "MACOS_FORCE_ENABLE_SHADOW" { diff --git a/window/Cargo.toml b/window/Cargo.toml index 6872e78e6..b382ca4ad 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -93,5 +93,6 @@ cocoa.workspace = true core-foundation.workspace = true core-graphics.workspace = true objc.workspace = true +objc2-core-graphics.workspace = true plist.workspace = true shlex.workspace = true diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index ee01944c3..9b8f0b067 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -24,10 +24,10 @@ use cocoa::appkit::{ use cocoa::base::*; use cocoa::foundation::{ NSArray, NSAutoreleasePool, NSFastEnumeration, NSInteger, NSNotFound, NSPoint, NSRect, NSSize, - NSUInteger, + NSString, NSUInteger, }; use config::window::WindowLevel; -use config::ConfigHandle; +use config::{ConfigHandle, RgbaColor, SrgbaTuple}; use core_foundation::base::{CFTypeID, TCFType}; use core_foundation::bundle::{CFBundleGetBundleWithIdentifier, CFBundleGetFunctionPointerForName}; use core_foundation::data::{CFData, CFDataGetBytePtr, CFDataRef}; @@ -37,6 +37,7 @@ use objc::declare::ClassDecl; use objc::rc::{StrongPtr, WeakPtr}; use objc::runtime::{Class, Object, Protocol, Sel}; use objc::*; +use objc2_core_graphics::CGColorCreateSRGB; use promise::Future; use raw_window_handle::{ AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HandleError, HasDisplayHandle, @@ -44,7 +45,7 @@ use raw_window_handle::{ }; use std::any::Any; use std::cell::RefCell; -use std::ffi::c_void; +use std::ffi::{c_void, CStr}; use std::path::PathBuf; use std::ptr::NonNull; use std::rc::Rc; @@ -1111,6 +1112,46 @@ impl WindowInner { } } + fn update_titlebar_background(&self) { + if !self + .config + .window_decorations + .contains(WindowDecorations::MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR) + { + return; + } + + // Set the titlebar background to the theme color falling back to black if there is no + // specified color scheme + let color = self + .config + .resolved_palette + .background + .unwrap_or(RgbaColor::from(SrgbaTuple(0., 0., 0., 255.))); + + unsafe { + if let Some(titlebar_view_container) = get_titlebar_view_container(&self.window) { + let layer: id = msg_send![*titlebar_view_container.load(), layer]; + + if layer.is_null() { + return; + } + + // We need to make sure to convert the config color into an sRGB CGColor or the color will be slightly off + let srgb_cgcolor = CGColorCreateSRGB( + color.0.into(), + color.1.into(), + color.2.into(), + color.3.into(), + ); + + let _: () = msg_send![layer, setBackgroundColor: srgb_cgcolor]; + } else { + log::trace!("failed to get titlebar view container from window"); + } + } + } + fn update_window_background_blur(&mut self) { unsafe { CGSSetWindowBackgroundBlurRadius( @@ -1134,12 +1175,15 @@ impl WindowInner { // stuck with a scale factor of 2 despite us having configured 1. self.window .setStyleMask_(NSWindowStyleMask::NSBorderlessWindowMask); + apply_decorations_to_window( &self.window, self.config.window_decorations, self.config.integrated_title_button_style, ); + self.update_titlebar_background(); + self.window.makeKeyAndOrderFront_(nil) } } @@ -1308,6 +1352,7 @@ impl WindowInner { } self.update_window_shadow(); self.update_window_background_blur(); + self.update_titlebar_background(); self.apply_decorations(); } } @@ -1355,7 +1400,10 @@ fn apply_decorations_to_window( } else { appkit::NSWindowTitleVisibility::NSWindowTitleHidden }); - if decorations.contains(WindowDecorations::INTEGRATED_BUTTONS) { + + if decorations.contains(WindowDecorations::INTEGRATED_BUTTONS) + || decorations.contains(WindowDecorations::MACOS_USE_BACKGROUND_COLOR_AS_TITLEBAR_COLOR) + { window.setTitlebarAppearsTransparent_(YES); } else { window.setTitlebarAppearsTransparent_(hidden); @@ -1414,6 +1462,74 @@ fn decoration_to_mask( } } +unsafe fn get_view_class_name(id: id) -> Option { + if id.is_null() { + return None; + } + + let class_name: id = msg_send![id, className]; + + if class_name.is_null() { + return None; + } + + let cstr = CStr::from_ptr(class_name.UTF8String()).to_str(); + + match cstr { + Ok(s) => Some(s.to_string()), + Err(_) => None, + } +} + +fn get_titlebar_view_container(window: &StrongPtr) -> Option { + // The view container for the titlebar on macos is found next to the primary window view + // so we need to traverse up to the super view to find it + let super_view = get_view_superview(window)?; + + let sub_views = get_view_subviews(&super_view.load())?; + + let count = unsafe { sub_views.load().count() }; + + for i in 0..count { + let sub_view: id = unsafe { sub_views.load().objectAtIndex(i) }; + + if sub_view.is_null() { + continue; + } + + let class_name = unsafe { get_view_class_name(sub_view)? }; + + if class_name == TITLEBAR_VIEW_NAME { + let titlebar_view = unsafe { WeakPtr::new(sub_view) }; + return Some(titlebar_view); + } + } + + None +} + +fn get_view_superview(view: &StrongPtr) -> Option { + let super_view_id: id = unsafe { msg_send![view.contentView(), superview] }; + + if super_view_id.is_null() { + return None; + } + + let super_view = unsafe { WeakPtr::new(super_view_id) }; + + Some(super_view) +} + +fn get_view_subviews(view: &StrongPtr) -> Option { + let sub_views_id: id = unsafe { msg_send![**view, subviews] }; + if sub_views_id.is_null() { + return None; + } + + let sub_views = unsafe { WeakPtr::new(sub_views_id) }; + Some(sub_views) +} + #[derive(Debug)] struct DeadKeyState { /// The private dead key state preserved from UCKeyTranslate @@ -1707,6 +1823,7 @@ impl Inner { const VIEW_CLS_NAME: &str = "WezTermWindowView"; const WINDOW_CLS_NAME: &str = "WezTermWindow"; +const TITLEBAR_VIEW_NAME: &str = "NSTitlebarContainerView"; struct WindowView { inner: Rc>,