Skip to content

Commit e3ff5d1

Browse files
committed
Make a translucent background color really see-through on macOS
SetBackgroundColor only set NSWindow.backgroundColor. A window that stays opaque composites that color over black, and a content view controller with a backing of its own - a FlutterViewController is opaque black - covers it anyway. A color with alpha now also clears NSWindow.opaque and is handed to a content view controller that has a backgroundColor. Found with the Flutter floating_toolbar_example: the frameless toolbar window showed a black rectangle around its rounded content.
1 parent bbf3549 commit e3ff5d1

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎src/platform/macos/window_macos.mm‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,17 @@ static void NativeApiUpdateWindowClass(NSWindow* window, bool non_activating) {
730730
blue:color.b / 255.0
731731
alpha:color.a / 255.0];
732732
[pimpl_->ns_window_ setBackgroundColor:nsColor];
733+
// A translucent background only shows through a window that says it is not
734+
// opaque; without this AppKit composites it over black.
735+
[pimpl_->ns_window_ setOpaque:color.a == 255];
736+
737+
// The content may paint a backing of its own over the window's background. A
738+
// view controller that has a background colour takes the same one: that is
739+
// what makes a FlutterViewController, opaque black by default, see-through.
740+
NSViewController* content = [pimpl_->ns_window_ contentViewController];
741+
if ([content respondsToSelector:@selector(setBackgroundColor:)]) {
742+
[content performSelector:@selector(setBackgroundColor:) withObject:nsColor];
743+
}
733744
}
734745

735746
Color Window::GetBackgroundColor() const {

‎src/window.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,10 @@ class Window : public NativeObjectProvider, public std::enable_shared_from_this<
847847
*
848848
* @note Platform behavior may vary:
849849
* - Windows: Sets the window background brush color
850-
* - macOS: Sets the window backgroundColor property
850+
* - macOS: Sets the window backgroundColor property. A color with alpha also
851+
* makes the window non-opaque, so that it really is see-through, and is
852+
* handed to a content view controller that paints a backing of its own
853+
* (a Flutter view is opaque black otherwise).
851854
* - Linux: Sets the window background color via GTK/X11
852855
*/
853856
void SetBackgroundColor(const Color& color);

0 commit comments

Comments
 (0)