8 #include <AppKit/AppKit.h>
9 #include <AppKit/NSApplication.h>
10 #include <Foundation/Foundation.h>
11 #include <objc/message.h>
13 #include "flutter/fml/logging.h"
14 #include "flutter/fml/paths.h"
18 - (void)addObserverFor:(NSString*)name selector:(
SEL)selector {
19 [[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil];
22 - (instancetype)init {
23 if (
self = [super init]) {
26 #ifdef OBSERVE_NOTIFICATION
27 #error OBSERVE_NOTIFICATION ALREADY DEFINED!
29 #define OBSERVE_NOTIFICATION(SELECTOR) \
30 [self addObserverFor:NSApplication##SELECTOR##Notification selector:@selector(handle##SELECTOR:)]
47 #undef OBSERVE_NOTIFICATION
49 _delegates = [NSPointerArray weakObjectsPointerArray];
54 static BOOL IsPowerOfTwo(NSUInteger x) {
55 return x != 0 && (x & (x - 1)) == 0;
59 [_delegates addPointer:(__bridge void*)delegate];
60 if (IsPowerOfTwo([_delegates count])) {
66 NSUInteger index = [[_delegates allObjects] indexOfObject:delegate];
67 if (index != NSNotFound) {
68 [_delegates removePointerAtIndex:index];
75 #ifdef DISTRIBUTE_NOTIFICATION
76 #error DISTRIBUTE_NOTIFICATION ALREADY DEFINED!
78 #define DISTRIBUTE_NOTIFICATION(SELECTOR) \
79 -(void)handle##SELECTOR : (NSNotification*)notification { \
80 for (NSObject<FlutterAppLifecycleDelegate> * delegate in _delegates) { \
81 if ([delegate respondsToSelector:@selector(handle##SELECTOR:)]) { \
82 [delegate handle##SELECTOR:notification]; \
102 #undef DISTRIBUTE_NOTIFICATION