From a144d93b43fe4cb2b563d3b3c5517297d28b9bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pa=CC=84vels=20Nadtoc=CC=8Cajevs?= <7645683+bruvzg@users.noreply.github.com> Date: Sat, 8 Mar 2025 16:28:25 +0200 Subject: [PATCH] [macOS] Enable transparency for windows with decorations. --- platform/macos/display_server_macos.mm | 30 ++++++++++++++++--------- platform/macos/godot_window_delegate.mm | 4 +++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index a563318d4c..e6fbb1d5c0 100644 --- a/platform/macos/display_server_macos.mm +++ b/platform/macos/display_server_macos.mm @@ -265,9 +265,15 @@ void DisplayServerMacOS::set_window_per_pixel_transparency_enabled(bool p_enable return; } if (p_enabled) { - [wd.window_object setBackgroundColor:[NSColor clearColor]]; [wd.window_object setOpaque:NO]; - [wd.window_object setHasShadow:NO]; + if (wd.borderless) { + [wd.window_object setBackgroundColor:[NSColor clearColor]]; + [wd.window_object setHasShadow:NO]; + } else { + // Note: transparent and not borderless - set alpha to non-zero value to ensure window shadow is rendered correctly. + [wd.window_object setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.004f]]; + [wd.window_object setHasShadow:YES]; + } CALayer *layer = [(NSView *)wd.window_view layer]; if (layer) { [layer setBackgroundColor:[NSColor clearColor].CGColor]; @@ -286,7 +292,9 @@ void DisplayServerMacOS::set_window_per_pixel_transparency_enabled(bool p_enable } [wd.window_object setBackgroundColor:bg_color]; [wd.window_object setOpaque:YES]; - [wd.window_object setHasShadow:YES]; + if (!wd.borderless) { + [wd.window_object setHasShadow:YES]; + } CALayer *layer = [(NSView *)wd.window_view layer]; if (layer) { [layer setBackgroundColor:bg_color.CGColor]; @@ -2622,12 +2630,17 @@ void DisplayServerMacOS::window_set_flag(WindowFlags p_flag, bool p_enabled, Win wd.borderless = p_enabled; if (p_enabled) { [wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; - } else { + [wd.window_object setHasShadow:NO]; if (wd.layered_window) { - wd.layered_window = false; - set_window_per_pixel_transparency_enabled(false, p_window); + [wd.window_object setBackgroundColor:[NSColor clearColor]]; } + } else { [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)]; + [wd.window_object setHasShadow:YES]; + if (wd.layered_window) { + // Note: transparent and not borderless - set alpha to non-zero value to ensure window shadow is rendered correctly. + [wd.window_object setBackgroundColor:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.004f]]; + } // Force update of the window styles. NSRect frameRect = [wd.window_object frame]; [wd.window_object setFrame:NSMakeRect(frameRect.origin.x, frameRect.origin.y, frameRect.size.width + 1, frameRect.size.height) display:NO]; @@ -2659,11 +2672,6 @@ void DisplayServerMacOS::window_set_flag(WindowFlags p_flag, bool p_enabled, Win if (wd.fullscreen) { return; } - if (p_enabled) { - [wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; // Force borderless. - } else if (!wd.borderless) { - [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)]; - } wd.layered_window = p_enabled; set_window_per_pixel_transparency_enabled(p_enabled, p_window); // Force update of the window styles. diff --git a/platform/macos/godot_window_delegate.mm b/platform/macos/godot_window_delegate.mm index 35c053dca5..7a8a085299 100644 --- a/platform/macos/godot_window_delegate.mm +++ b/platform/macos/godot_window_delegate.mm @@ -184,10 +184,12 @@ } // Restore borderless, transparent and resizability state. - if (wd.borderless || wd.layered_window) { + if (wd.borderless) { [wd.window_object setStyleMask:NSWindowStyleMaskBorderless]; + [wd.window_object setHasShadow:NO]; } else { [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)]; + [wd.window_object setHasShadow:YES]; } if (wd.layered_window) { ds->set_window_per_pixel_transparency_enabled(true, window_id);