use objc2_app_kit::NSView;
use objc2_app_kit::NSWindow;
use objc2_foundation::NSOperatingSystemVersion;
use objc2_foundation::NSProcessInfo;
use objc2_quartz_core::CALayer;
use objc2_quartz_core::CATransaction;
use super::layout::class_name;
use super::layout::find_descendant;
use super::layout::titlebar_view;
const TRANSLUCENT_ALPHA: f64 = 0.5;
pub fn update(window: &NSWindow, translucent: bool) {
if !NSProcessInfo::processInfo().isOperatingSystemAtLeastVersion(NSOperatingSystemVersion {
majorVersion: 27,
minorVersion: 0,
patchVersion: 0,
}) {
return;
}
let Some(titlebar) = titlebar_view(window) else { return };
update_backgrounds(&titlebar, translucent);
if let Some(tab_bar) = find_descendant(&titlebar, "NSTabBar")
&& let Some(layer) = tab_bar.layer()
{
CATransaction::begin();
CATransaction::setDisableActions(true);
update_track_material(&layer, translucent);
CATransaction::commit();
}
}
fn update_backgrounds(view: &NSView, translucent: bool) {
let subviews = view.subviews();
let name = class_name(view.as_ref());
let alpha = if translucent { TRANSLUCENT_ALPHA } else { 1.0 };
if (name == "NSGlassContainerView" || name.ends_with("NSSubduedGlassEffectView"))
&& let Some(background) = subviews.iter().next()
&& class_name(background.as_ref()) == "NSView"
&& background.subviews().is_empty()
{
if background.alphaValue() != alpha {
background.setAlphaValue(alpha);
}
}
for child in subviews.iter() {
update_backgrounds(&child, translucent);
}
}
fn update_track_material(layer: &CALayer, translucent: bool) {
if layer.name().is_some_and(|name| name.to_string() == "NSTabBarTrackFilterHost") {
layer.setHidden(translucent);
layer.setOpacity(if translucent { 0.0 } else { 1.0 });
}
if let Some(children) = unsafe { layer.sublayers() } {
for child in children.iter() {
update_track_material(&child, translucent);
}
}
}