Skip to content

Commit 26fc6bb

Browse files
committed
Fix crash on appearance change
The NSApplication (PBDarkMode) category overrode observeValueForKeyPath:ofObject:change:context:, shadowing AppKit's own implementation on NSApplication. On appearance change (dark/light or Increase Contrast), AppKit delivers its internal KVO notifications to NSApp using its own contexts; the override's else branch then forwarded to super, which reached NSObject's default implementation and raised the "message received but not handled" exception, aborting the app. Observe effectiveAppearance through a dedicated token object instead, so AppKit's own implementation is left untouched and our KVO change is delivered without shadowing anything. Fix gitx#578
1 parent 6d01d1c commit 26fc6bb

3 files changed

Lines changed: 52 additions & 37 deletions

File tree

‎Classes/GitX-Bridging-Header.h‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
#import <ObjectiveGit/ObjectiveGit.h>
1717

1818
// ── Converted files: headers kept so ObjC callers continue to compile ────────
19-
// NSAppearance+PBDarkMode.swift owns the implementations; .m only defines the constant.
19+
// Note: these Swift twins are NOT compiled (absent from the Xcode target);
20+
// the .m/.h implementations remain authoritative.
2021
#import "NSAppearance+PBDarkMode.h"
21-
// NSColor+RGB.swift owns the implementation.
2222
#import "NSColor+RGB.h"
23-
// NSSplitView+GitX.swift owns the implementation.
2423
#import "NSSplitView+GitX.h"
25-
// GitXRelativeDateFormatter.swift owns the implementation.
2624
#import "GitXRelativeDateFormatter.h"
2725

2826
// ── Add further headers below only when a Swift source file needs them ────────

‎Classes/NSAppearance+PBDarkMode.m‎

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,48 @@
22
// NSAppearance+PBDarkMode.m
33
// GitX
44
//
5-
// Only the global constant definition remains here — the category
6-
// implementations have been moved to NSAppearance+PBDarkMode.swift.
5+
// Objective-C helpers for detecting the current appearance (dark/light mode).
76
//
87

98
#import "NSAppearance+PBDarkMode.h"
109
#import <objc/runtime.h>
1110

12-
// This definition satisfies the `extern NSString *const` declaration in the
13-
// header for all Objective-C callers. The Swift side imports the same value
14-
// as a plain Swift String via the bridging header.
1511
NSString *const PBEffectiveAppearanceChanged = @"PBEffectiveAppearanceChanged";
1612

13+
// Shared between the KVO registration and the observing token, so the token can
14+
// tell its own changes apart from any it gets registered for elsewhere.
15+
static const void * const kAppearanceObservationContext = &kAppearanceObservationContext;
16+
17+
// AppKit routes appearance changes to NSApplication through its own
18+
// -observeValueForKeyPath:ofObject:change:context: implementation and private
19+
// contexts. Overriding that method on NSApplication in a category shadows
20+
// AppKit's implementation and crashes on appearance change, so observe
21+
// `effectiveAppearance` through this dedicated token object instead.
22+
@interface PBAppearanceObserverToken : NSObject
23+
@property (nonatomic, weak) id observer;
24+
@end
25+
26+
@implementation PBAppearanceObserverToken
27+
28+
- (void)observeValueForKeyPath:(__unused NSString *)keyPath
29+
ofObject:(__unused id)object
30+
change:(__unused NSDictionary<NSKeyValueChangeKey, id> *)change
31+
context:(void *)context
32+
{
33+
if (context != kAppearanceObservationContext) {
34+
// Not one of our observations; do not forward it up the responder
35+
// chain, NSObject's default implementation raises on unhandled KVO.
36+
return;
37+
}
38+
id observer = self.observer;
39+
if (observer) {
40+
[[NSNotificationCenter defaultCenter] postNotificationName:PBEffectiveAppearanceChanged
41+
object:observer];
42+
}
43+
}
44+
45+
@end
46+
1747
@implementation NSAppearance (PBDarkMode)
1848

1949
- (BOOL)isDarkMode
@@ -31,33 +61,22 @@ - (BOOL)isDarkMode
3161
return self.effectiveAppearance.isDarkMode;
3262
}
3363

34-
static char kAppearanceObservationKey;
35-
3664
- (void)registerObserverForAppearanceChanges:(id)observer
3765
{
38-
// Use traditional KVO addObserver method
39-
[self addObserver:self
40-
forKeyPath:@"effectiveAppearance"
41-
options:NSKeyValueObservingOptionNew
42-
context:&kAppearanceObservationKey];
66+
PBAppearanceObserverToken *previous = objc_getAssociatedObject(self, &kAppearanceObservationContext);
67+
if (previous) {
68+
[self removeObserver:previous forKeyPath:@"effectiveAppearance"];
69+
}
4370

44-
// Store the observer reference
45-
objc_setAssociatedObject(self, &kAppearanceObservationKey, observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
46-
}
71+
PBAppearanceObserverToken *token = [[PBAppearanceObserverToken alloc] init];
72+
token.observer = observer;
73+
[self addObserver:token
74+
forKeyPath:@"effectiveAppearance"
75+
options:0
76+
context:(void *)kAppearanceObservationContext];
4777

48-
- (void)observeValueForKeyPath:(NSString *)keyPath
49-
ofObject:(id)object
50-
change:(NSDictionary *)change
51-
context:(void *)context
52-
{
53-
if (context == &kAppearanceObservationKey) {
54-
id observer = objc_getAssociatedObject(self, &kAppearanceObservationKey);
55-
[[NSNotificationCenter defaultCenter] postNotificationName:PBEffectiveAppearanceChanged
56-
object:observer];
57-
} else {
58-
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
59-
}
78+
// Retain the token so KVO keeps forwarding appearance changes to it.
79+
objc_setAssociatedObject(self, &kAppearanceObservationContext, token, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
6080
}
6181

6282
@end
63-

‎Classes/NSAppearance+PBDarkMode.swift‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// NSAppearance+PBDarkMode.swift
33
// GitX
44
//
5-
// Converted from NSAppearance+PBDarkMode.m
6-
//
7-
// Note: NSAppearance+PBDarkMode.m is still compiled solely to provide the
8-
// `extern NSString *const PBEffectiveAppearanceChanged` definition for ObjC
9-
// callers. The category implementations live here.
5+
// Legacy Swift twin of NSAppearance+PBDarkMode.m. This file is NOT part of
6+
// the Xcode target and is never compiled; keep it in sync with the .m or
7+
// delete it.
108
//
119

1210
import AppKit

0 commit comments

Comments
 (0)