@@ -44,28 +44,28 @@ @interface BFColorPickerPopover ()
44
44
45
45
46
46
@implementation BFColorPickerPopover {
47
- NSColor *_color;
47
+ NSColor *_color;
48
48
}
49
49
50
50
@synthesize observingColor = _observingColor;
51
51
52
52
- (void )setObservingColor : (BOOL )observingColor {
53
- if (_observingColor == observingColor) {
54
- return ;
55
- }
56
-
57
- if (!self.colorPanel ) {
58
- observingColor = NO ;
59
- }
60
-
61
- _observingColor = observingColor;
62
-
63
- void *context = (__bridge void *)self;
64
- if (_observingColor) {
65
- [self .colorPanel addObserver: self forKeyPath: @" color" options: NSKeyValueObservingOptionNew context: context];
66
- } else {
67
- [self .colorPanel removeObserver: self forKeyPath: @" color" context: context];
68
- }
53
+ if (_observingColor == observingColor) {
54
+ return ;
55
+ }
56
+
57
+ if (!self.colorPanel ) {
58
+ observingColor = NO ;
59
+ }
60
+
61
+ _observingColor = observingColor;
62
+
63
+ void *context = (__bridge void *)self;
64
+ if (_observingColor) {
65
+ [self .colorPanel addObserver: self forKeyPath: @" color" options: NSKeyValueObservingOptionNew context: context];
66
+ } else {
67
+ [self .colorPanel removeObserver: self forKeyPath: @" color" context: context];
68
+ }
69
69
}
70
70
71
71
#pragma mark -
@@ -77,6 +77,7 @@ + (BFColorPickerPopover *)sharedPopover
77
77
static dispatch_once_t onceToken;
78
78
dispatch_once (&onceToken, ^{
79
79
sharedPopover = [[BFColorPickerPopover alloc ] init ];
80
+ sharedPopover.delegate = sharedPopover;
80
81
});
81
82
return sharedPopover;
82
83
}
@@ -85,120 +86,126 @@ - (id)init
85
86
{
86
87
self = [super init ];
87
88
if (self) {
88
- self.behavior = NSPopoverBehaviorSemitransient;
89
- _color = [NSColor whiteColor ];
90
- }
89
+ self.behavior = NSPopoverBehaviorSemitransient;
90
+ _color = [NSColor whiteColor ];
91
+ }
91
92
return self;
92
93
}
93
94
94
95
#pragma mark -
95
96
#pragma mark Getters & Setters
96
97
97
98
- (NSColorPanel *)colorPanel {
98
- return ((BFColorPickerViewController *)self.contentViewController ).colorPanel ;
99
+ return ((BFColorPickerViewController *)self.contentViewController ).colorPanel ;
99
100
}
100
101
101
102
- (NSColor *)color {
102
- return self.colorPanel .color ;
103
+ return self.colorPanel .color ;
103
104
}
104
105
105
106
- (void )setColor : (NSColor *)color {
106
- _color = color;
107
- if (self.isShown ) {
108
- self.colorPanel .color = color;
109
- }
107
+ _color = color;
108
+ if (self.isShown ) {
109
+ self.colorPanel .color = color;
110
+ }
110
111
}
111
112
112
113
#pragma mark -
113
114
#pragma mark Popover Lifecycle
114
115
115
116
- (void )showRelativeToRect : (NSRect )positioningRect ofView : (NSView *)positioningView preferredEdge : (NSRectEdge )preferredEdge {
116
-
117
- // Close the popover without an animation if it's already on screen.
118
- if (self.isShown ) {
119
- id targetBackup = self.target ;
120
- SEL actionBackup = self.action ;
121
- BOOL animatesBackup = self.animates ;
122
- self.animates = NO ;
123
- [self close ];
124
- self.animates = animatesBackup;
125
- self.target = targetBackup;
126
- self.action = actionBackup;
127
- }
128
-
129
- self.contentViewController = [[BFColorPickerViewController alloc ] init ];
130
- [super showRelativeToRect: positioningRect ofView: positioningView preferredEdge: preferredEdge];
131
-
132
- self.colorPanel .color = _color;
133
- self.observingColor = YES ;
117
+
118
+ // Close the popover without an animation if it's already on screen.
119
+ if (self.isShown ) {
120
+ id targetBackup = self.target ;
121
+ SEL actionBackup = self.action ;
122
+ BOOL animatesBackup = self.animates ;
123
+ self.animates = NO ;
124
+ [self close ];
125
+ self.animates = animatesBackup;
126
+ self.target = targetBackup;
127
+ self.action = actionBackup;
128
+ }
129
+
130
+ self.contentViewController = [[BFColorPickerViewController alloc ] init ];
131
+ [super showRelativeToRect: positioningRect ofView: positioningView preferredEdge: preferredEdge];
132
+
133
+ self.colorPanel .color = _color;
134
+ self.observingColor = YES ;
134
135
}
135
136
136
137
// On pressing Esc, close the popover.
137
138
- (void )cancelOperation : (id )sender {
138
- [self close ];
139
+ [self close ];
139
140
}
140
141
141
142
- (void )removeTargetAndAction {
142
- self.target = nil ;
143
- self.action = nil ;
143
+ self.target = nil ;
144
+ self.action = nil ;
144
145
}
145
146
146
147
- (void )deactivateColorWell {
147
- [self .colorWell deactivate ];
148
- self.colorWell = nil ;
148
+ [self .colorWell deactivate ];
149
+ self.colorWell = nil ;
149
150
}
150
151
151
152
- (void )closeAndDeactivateColorWell : (BOOL )deactivate removeTarget : (BOOL )removeTarget removeObserver : (BOOL )removeObserver {
152
-
153
- if (removeTarget) {
154
- [ self removeTargetAndAction ];
155
- }
156
- if (removeObserver) {
157
- self. observingColor = NO ;
158
- }
159
-
160
- // For some strange reason I couldn't figure out, the panel changes it's color when closed.
161
- // To fix this, I reset the color after it's closed.
162
- NSColor *backupColor = self. colorPanel . color ;
163
- [ super close ];
164
- self. colorPanel . color = backupColor;
165
-
166
- if (deactivate) {
167
- [self deactivateColorWell ];
168
- }
153
+ if (removeObserver) {
154
+ self. observingColor = NO ;
155
+ }
156
+
157
+ // For some strange reason I couldn't figure out, the panel changes it's color when closed.
158
+ // To fix this, I reset the color after it's closed.
159
+ NSColor *backupColor = self. colorPanel . color ;
160
+ [ super close ];
161
+ self. colorPanel . color = backupColor;
162
+
163
+ if (removeTarget) {
164
+ [ self removeTargetAndAction ];
165
+ }
166
+
167
+ if (deactivate) {
168
+ [self deactivateColorWell ];
169
+ }
169
170
}
170
171
171
172
- (void )close {
172
- [self closeAndDeactivateColorWell: YES removeTarget: YES removeObserver: YES ];
173
+ [self closeAndDeactivateColorWell: YES removeTarget: YES removeObserver: YES ];
173
174
}
174
175
175
176
- (BOOL )_delegatePopoverShouldClose : (id )sender {
176
- if ([super _delegatePopoverShouldClose: sender]) {
177
- [self removeTargetAndAction ];
178
- self.observingColor = NO ;
179
- [self deactivateColorWell ];
180
- return YES ;
181
- }
182
- return NO ;
177
+ if ([super _delegatePopoverShouldClose: sender]) {
178
+ [self removeTargetAndAction ];
179
+ self.observingColor = NO ;
180
+ [self deactivateColorWell ];
181
+ return YES ;
182
+ }
183
+ return NO ;
184
+ }
185
+
186
+ -(void )popoverWillClose : (NSNotification *)notification {
187
+ if (_onCloseDelegate) {
188
+ [_onCloseDelegate performSelector: @selector (popoverClosed )];
189
+ }
183
190
}
184
191
185
192
#pragma mark -
186
193
#pragma mark Observation
187
194
188
195
// Notify the target when the color changes.
189
196
- (void )observeValueForKeyPath : (NSString *)keyPath ofObject : (id )object change : (NSDictionary *)change context : (void *)context {
190
- if (object == self.colorPanel && [keyPath isEqualToString: @" color" ] && context == (__bridge void *)self) {
191
- _color = self.colorPanel .color ;
192
- if (self.target && self.action && [self .target respondsToSelector: self .action]) {
193
-
197
+ if (object == self.colorPanel && [keyPath isEqualToString: @" color" ] && context == (__bridge void *)self) {
198
+ _color = self.colorPanel .color ;
199
+ if (self.target && self.action && [self .target respondsToSelector: self .action]) {
200
+
194
201
#pragma clang diagnostic push
195
202
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
196
-
197
- [self .target performSelector: self .action withObject: self ];
198
-
203
+
204
+ [self .target performSelector: self .action withObject: self ];
205
+
199
206
#pragma clang diagnostic pop
200
- }
201
- }
207
+ }
208
+ }
202
209
}
203
210
204
211
@end
0 commit comments