Skip to content

Commit

Permalink
Merge pull request #525 from bugsnag/Change-NSWorkspace-notifications…
Browse files Browse the repository at this point in the history
…-to-use-NSWorkspace-specific-notification-center

(fix)NSWorkspace-specific state change notifications now use the corr…
  • Loading branch information
robinmacharg committed Apr 3, 2020
2 parents 56e0e4e + 2baf0f2 commit 2984c63
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ Bugsnag Notifiers on other platforms.
* Increased the detail in handled event breadcrumbs
[#493](https://github.com/bugsnag/bugsnag-cocoa/pull/493)

* NSWorkspaceScreenSleep/Wake notifications now use the correct notification center.
(#525)[https://github.com/bugsnag/bugsnag-cocoa/pull/525]

## 5.23.0 (2019-12-10)

This release removes support for reporting 'partial' or 'minimal' crash reports
Expand Down
75 changes: 62 additions & 13 deletions Source/BugsnagClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,16 @@ - (void)updateAutomaticBreadcrumbDetectionSettings {
for (NSString *name in [self automaticBreadcrumbStateEvents]) {
[self startListeningForStateChangeNotification:name];
}


#if TARGET_OS_TV
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#elif TARGET_OS_MAC
// Workspace-specific events - MacOS only
for (NSString *name in [self workspaceBreadcrumbStateEvents]) {
[self startListeningForWorkspaceStateChangeNotifications:name];
}
#endif

// NSMenu events (Mac only)
for (NSString *name in [self automaticBreadcrumbMenuItemEvents]) {
[[NSNotificationCenter defaultCenter]
Expand Down Expand Up @@ -1109,6 +1118,23 @@ - (void)updateAutomaticBreadcrumbDetectionSettings {
}
}

/**
* NSWorkspace-specific automatic breadcrumb events
*/
- (NSArray<NSString *> *)workspaceBreadcrumbStateEvents {
#if TARGET_OS_TV
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#elif TARGET_OS_MAC
return @[
NSWorkspaceScreensDidSleepNotification,
NSWorkspaceScreensDidWakeNotification
];
#endif

// Fall-through
return nil;
}

- (NSArray<NSString *> *)automaticBreadcrumbStateEvents {
#if TARGET_OS_TV
return @[
Expand Down Expand Up @@ -1139,17 +1165,20 @@ - (void)updateAutomaticBreadcrumbDetectionSettings {
return @[
NSApplicationDidBecomeActiveNotification,
NSApplicationDidResignActiveNotification,
NSApplicationDidHideNotification, NSApplicationDidUnhideNotification,
NSApplicationDidHideNotification,
NSApplicationDidUnhideNotification,
NSApplicationWillTerminateNotification,
NSWorkspaceScreensDidSleepNotification,
NSWorkspaceScreensDidWakeNotification, NSWindowWillCloseNotification,
NSWindowDidBecomeKeyNotification, NSWindowWillMiniaturizeNotification,

NSWindowWillCloseNotification,
NSWindowDidBecomeKeyNotification,
NSWindowWillMiniaturizeNotification,
NSWindowDidEnterFullScreenNotification,
NSWindowDidExitFullScreenNotification
];
#else
return nil;
#endif

// Fall-through
return nil;
}

- (NSArray<NSString *> *)automaticBreadcrumbControlEvents {
Expand All @@ -1165,19 +1194,21 @@ - (void)updateAutomaticBreadcrumbDetectionSettings {
NSControlTextDidBeginEditingNotification,
NSControlTextDidEndEditingNotification
];
#else
return nil;
#endif

// Fall-through
return nil;
}

- (NSArray<NSString *> *)automaticBreadcrumbTableItemEvents {
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE || TARGET_OS_TV
return @[ UITableViewSelectionDidChangeNotification ];
#elif TARGET_OS_MAC
return @[ NSTableViewSelectionDidChangeNotification ];
#else
return nil;
#endif

// Fall-through
return nil;
}

- (NSArray<NSString *> *)automaticBreadcrumbMenuItemEvents {
Expand All @@ -1187,9 +1218,10 @@ - (void)updateAutomaticBreadcrumbDetectionSettings {
return nil;
#elif TARGET_OS_MAC
return @[ NSMenuWillSendActionNotification ];
#else
return nil;
#endif

// Fall-through
return nil;
}

/**
Expand All @@ -1205,6 +1237,23 @@ - (void)startListeningForStateChangeNotification:(NSString *)notificationName {
object:nil];
}

/**
* Configure an NSWorkspace-specific state change breadcrumb listener. MacOS only.
*
* @param notificationName The name of the notification.
*/
#if TARGET_OS_TV
#elif TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
#elif TARGET_OS_MAC
- (void)startListeningForWorkspaceStateChangeNotifications:(NSString *)notificationName {
[NSWorkspace.sharedWorkspace.notificationCenter
addObserver:self
selector:@selector(sendBreadcrumbForNotification:)
name:notificationName
object:nil];
}
#endif

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/BugsnagClientMirrorTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ - (void)setUp {
@"started c16@0:8",
@"setAppDidCrashLastLaunch: v20@0:8c16",
@"setMetadata: v24@0:8@16",
@"metadata @16@0:8"
@"metadata @16@0:8",
@"workspaceBreadcrumbStateEvents @16@0:8",
@"startListeningForWorkspaceStateChangeNotifications: v24@0:8@16"
]];

// the following methods are implemented on Bugsnag but do not need to
Expand Down

0 comments on commit 2984c63

Please sign in to comment.