From 3d73af4401c4098cba931ad8117329ed08e01895 Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Thu, 19 Oct 2023 17:47:22 -0700 Subject: [PATCH] Fix macOS 14 Sonoma non-native full screen background color Non-native full screen's configured background color (default to black) stopped working when building using macOS 14 SDK, because the main CoreText view was now drawing over it in drawRect, due to the new clipToBounds property defaulting to false in macOS 14 SDK. We could just fix this issue by setting clipToBounds to true on the text view, but we would lose the benefits of the new behavior which allows us to show tall texts (e.g. Tibetan texts or other characters with composing chars) on the first line and not have it be clipped at the top. Currently with the unclipped behavior, the character can be drawn and poke up into the window frame or the notch area. To properly fix this, just clip the background color fill in drawRect, and allow texts etc to still draw outside the rect. --- src/MacVim/MMCoreTextView.m | 18 ++++++++++++++++++ src/MacVim/MacVim.h | 3 +++ 2 files changed, 21 insertions(+) diff --git a/src/MacVim/MMCoreTextView.m b/src/MacVim/MMCoreTextView.m index 0aa27eb866..83c37dd95c 100644 --- a/src/MacVim/MMCoreTextView.m +++ b/src/MacVim/MMCoreTextView.m @@ -760,6 +760,17 @@ - (void)drawRect:(NSRect)rect NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; const BOOL clipTextToRow = [ud boolForKey:MMRendererClipToRowKey]; // Specify whether to clip tall characters by the row boundary. + +#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_14_0 + // On macOS 14+ by default views don't clip their content, which is good as it allows tall texts + // on first line to be drawn fully without getting clipped. However, in this case we should make + // sure the background color fill is clipped properly, as otherwise it will interfere with + // non-native fullscreen's background color setting. + const BOOL clipBackground = !self.clipsToBounds; +#else + const BOOL clipBackground = NO; +#endif + NSGraphicsContext *context = [NSGraphicsContext currentContext]; #if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_10 CGContextRef ctx = context.CGContext; @@ -791,7 +802,14 @@ - (void)drawRect:(NSRect)rect const unsigned defaultBg = defaultBackgroundColor.argbInt; CGContextSetFillColor(ctx, COMPONENTS(defaultBg)); + if (clipBackground) { + CGContextSaveGState(ctx); + CGContextClipToRect(ctx, self.bounds); + } CGContextFillRect(ctx, rect); + if (clipBackground) { + CGContextRestoreGState(ctx); + } // Function to draw all rows void (^drawAllRows)(void (^)(CGContextRef,CGRect,int)) = ^(void (^drawFunc)(CGContextRef,CGRect,int)){ diff --git a/src/MacVim/MacVim.h b/src/MacVim/MacVim.h index 6da8a3b757..946e729c45 100644 --- a/src/MacVim/MacVim.h +++ b/src/MacVim/MacVim.h @@ -52,6 +52,9 @@ #ifndef MAC_OS_VERSION_13_0 # define MAC_OS_VERSION_13_0 130000 #endif +#ifndef MAC_OS_VERSION_14_0 +# define MAC_OS_VERSION_14_0 140000 +#endif #ifndef NSAppKitVersionNumber10_10 # define NSAppKitVersionNumber10_10 1343