Tk Source Code

Artifact [c9d715d2]
Login

Artifact c9d715d2210ba88c99ae28ffce2c1ffa0c7b404830408b992e441d1cf042c151:

Attachment "ef5d3e29a4.diff" to ticket [ef5d3e29] added by chrstphrchvz 2023-06-11 11:10:12.
diff --git macosx/tkMacOSXWindowEvent.c macosx/tkMacOSXWindowEvent.c
index c0f8afc94..acc2b91c1 100644
--- macosx/tkMacOSXWindowEvent.c
+++ macosx/tkMacOSXWindowEvent.c
@@ -1237,29 +1237,8 @@ static const char *const accentNames[] = {
     } else if (effectiveAppearanceName == NSAppearanceNameDarkAqua) {
 	Tk_SendVirtualEvent(tkwin, "DarkAqua", NULL);
     }
-    if ([NSApp macOSVersion] < 101500) {
-
-	/*
-	 * Mojave cannot handle the KVO shenanigans that we need for the
-	 * highlight and accent color notifications.
-	 */
-
-	return;
-    }
     if (!defaultColor) {
 	defaultColor = [NSApp macOSVersion] < 110000 ? "Blue" : "Multicolor";
-	preferences = [[NSUserDefaults standardUserDefaults] retain];
-
-	/*
-	 * AppKit calls this method when the user changes the Accent Color
-	 * but not when the user changes the Highlight Color.  So we register
-	 * to receive KVO notifications for Highlight Color as well.
-	 */
-
-	[preferences addObserver:self
-		      forKeyPath:@"AppleHighlightColor"
-			 options:NSKeyValueObservingOptionNew
-			 context:NULL];
     }
     NSString *accent = [preferences stringForKey:@"AppleAccentColor"];
     NSArray *words = [[preferences stringForKey:@"AppleHighlightColor"]
diff --git macosx/tkMacOSXWm.c macosx/tkMacOSXWm.c
index 8b72faf16..ac0807ace 100644
--- macosx/tkMacOSXWm.c
+++ macosx/tkMacOSXWm.c
@@ -1289,6 +1289,15 @@ TkWmDeadWindow(
 	    [NSApp _setMainWindow:nil];
 	}
 	[deadNSWindow close];
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
+    if ([NSApp macOSVersion] < 101500) {
+	/* KVO notification was not registered on Mojave. */
+    } else {
+	NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
+	[preferences removeObserver:deadNSWindow.contentView
+		      forKeyPath:@"AppleHighlightColor"];
+    }
+#endif
 	[deadNSWindow release];
 
 #if DEBUG_ZOMBIES > 1
@@ -6763,6 +6772,30 @@ TkMacOSXMakeRealWindowExist(
     }
     TKContentView *contentView = [[TKContentView alloc]
 				     initWithFrame:NSZeroRect];
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
+    if ([NSApp macOSVersion] < 101500) {
+
+	/*
+	 * Mojave cannot handle the KVO shenanigans that we need for the
+	 * highlight and accent color notifications.
+	 */
+
+    } else {
+	NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
+
+	/*
+	 * AppKit calls the viewDidChangeEffectiveAppearance method when the
+	 * user changes the Accent Color but not when the user changes the
+	 * Highlight Color.  So we register to receive KVO notifications for
+	 * Highlight Color as well.
+	 */
+
+	[preferences addObserver:contentView
+		      forKeyPath:@"AppleHighlightColor"
+			 options:NSKeyValueObservingOptionNew
+			 context:NULL];
+    }
+#endif
     [window setContentView:contentView];
     [contentView release];
     [window setDelegate:NSApp];