forked from indragiek/INAppStoreWindow
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathINWindowButton.m
More file actions
292 lines (236 loc) · 9.2 KB
/
INWindowButton.m
File metadata and controls
292 lines (236 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// INWindowButton.m
//
// Copyright 2013 Vladislav Alexeev. All rights reserved.
//
// Licensed under the BSD License <http://www.opensource.org/licenses/bsd-license>
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "INWindowButton.h"
#pragma mark - Window Button Group
NSString *const INWindowButtonGroupDidUpdateRolloverStateNotification = @"INWindowButtonGroupDidUpdateRolloverStateNotification";
NSString *const kINWindowButtonGroupDefault = @"com.indragie.inappstorewindow.defaultWindowButtonGroup";
@interface INWindowButtonGroup : NSObject
+ (instancetype)groupWithIdentifier:(NSString *)identifier;
@property (nonatomic, copy, readonly) NSString *identifier;
- (void)didCaptureMousePointer;
- (void)didReleaseMousePointer;
- (BOOL)shouldDisplayRollOver;
- (void)resetMouseCaptures;
@end
@interface INWindowButtonGroup ()
@property (nonatomic, assign) NSInteger numberOfCaptures;
@end
@implementation INWindowButtonGroup
+ (instancetype)groupWithIdentifier:(NSString *)identifier {
static NSMutableDictionary *groups = nil;
if (groups == nil) {
groups = [[NSMutableDictionary alloc] init];
}
if (identifier == nil) {
identifier = kINWindowButtonGroupDefault;
}
INWindowButtonGroup *group = [groups objectForKey:identifier];
if (group == nil) {
group = [[[self class] alloc] initWithIdentifier:identifier];
[groups setObject:group forKey:identifier];
}
return group;
}
- (instancetype)initWithIdentifier:(NSString *)identifier {
self = [super init];
if (self) {
_identifier = [identifier copy];
}
return self;
}
#if !__has_feature(objc_arc)
- (void)dealloc
{
[_identifier release];
[super dealloc];
}
#endif
- (void)setNumberOfCaptures:(NSInteger)numberOfCaptures {
if (_numberOfCaptures != numberOfCaptures && numberOfCaptures >= 0) {
_numberOfCaptures = numberOfCaptures;
[[NSNotificationCenter defaultCenter] postNotificationName:INWindowButtonGroupDidUpdateRolloverStateNotification
object:self];
}
}
- (void)didCaptureMousePointer {
self.numberOfCaptures++;
}
- (void)didReleaseMousePointer {
self.numberOfCaptures--;
}
- (BOOL)shouldDisplayRollOver {
return (self.numberOfCaptures > 0);
}
- (void)resetMouseCaptures {
self.numberOfCaptures = 0;
}
@end
#pragma mark - Window Button
@interface INWindowButton ()
@property (nonatomic, copy) NSString *groupIdentifier;
@property (nonatomic, strong, readonly) INWindowButtonGroup *group;
@property (nonatomic, strong) NSTrackingArea *mouseTrackingArea;
@end
@implementation INWindowButton
+ (instancetype)windowButtonWithSize:(NSSize)size groupIdentifier:(NSString *)groupIdentifier {
INWindowButton *button = [[self alloc] initWithSize:size groupIdentifier:groupIdentifier];
return button;
}
#pragma mark - Init and Dealloc
- (instancetype)initWithSize:(NSSize)size groupIdentifier:(NSString *)groupIdentifier
{
self = [super initWithFrame:NSMakeRect(0, 0, size.width, size.height)];
if (self) {
_groupIdentifier = [groupIdentifier copy];
[self setButtonType:NSMomentaryChangeButton];
[self setBordered:NO];
[self setTitle:@""];
[self.cell setHighlightsBy:NSContentsCellMask];
[self.cell setImageDimsWhenDisabled:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowButtonGroupDidUpdateRolloverStateNotification:)
name:INWindowButtonGroupDidUpdateRolloverStateNotification
object:self.group];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
#if !__has_feature(objc_arc)
[_activeImage release];
[_inactiveImage release];
[_activeNotKeyWindowImage release];
[_rolloverImage release];
[_groupIdentifier release];
[super dealloc];
#endif
}
#pragma mark - Group
- (INWindowButtonGroup *)group {
return [INWindowButtonGroup groupWithIdentifier:self.groupIdentifier];
}
- (void)windowButtonGroupDidUpdateRolloverStateNotification:(NSNotification *)n {
[self updateRollOverImage];
}
#pragma mark - Tracking Area
- (void)updateTrackingAreas {
[super updateTrackingAreas];
if (self.mouseTrackingArea) {
[self removeTrackingArea:self.mouseTrackingArea];
}
self.mouseTrackingArea = [[NSTrackingArea alloc] initWithRect:NSInsetRect(self.bounds, -4, -4)
options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways
owner:self
userInfo:nil];
[self addTrackingArea:self.mouseTrackingArea];
}
#pragma mark - Window State Handling
- (void)viewDidMoveToWindow {
if (self.window) {
[self updateImage];
}
}
- (void)viewWillMoveToWindow:(NSWindow *)newWindow {
if (self.window) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillEnterFullScreenNotification object:self.window];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillExitFullScreenNotification object:self.window];
}
if (newWindow != nil) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidChangeFocus:)
name:NSWindowDidBecomeKeyNotification
object:newWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidChangeFocus:)
name:NSWindowDidResignKeyNotification
object:newWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillEnterFullScreen:)
name:NSWindowWillEnterFullScreenNotification
object:newWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillExitFullScreen:)
name:NSWindowWillExitFullScreenNotification
object:newWindow];
}
}
- (void)windowDidChangeFocus:(NSNotification *)n {
[self updateImage];
}
- (void)windowWillEnterFullScreen:(NSNotification *)n {
[self.group resetMouseCaptures];
[self setHidden:YES];
}
- (void)windowWillExitFullScreen:(NSNotification *)n {
[self.group resetMouseCaptures];
[self setHidden:NO];
}
#pragma mark - Event Handling
- (void)viewDidEndLiveResize {
[super viewDidEndLiveResize];
[self.group resetMouseCaptures];
}
- (void)mouseEntered:(NSEvent *)theEvent {
[super mouseEntered:theEvent];
[self.group didCaptureMousePointer];
[self updateRollOverImage];
}
- (void)mouseExited:(NSEvent *)theEvent {
[super mouseExited:theEvent];
[self.group didReleaseMousePointer];
[self updateRollOverImage];
}
#pragma mark - Button Appearance
- (void)setPressedImage:(NSImage *)pressedImage {
self.alternateImage = pressedImage;
}
- (NSImage *)pressedImage {
return self.alternateImage;
}
- (void)setEnabled:(BOOL)enabled {
[super setEnabled:enabled];
if (enabled) {
self.image = self.activeImage;
} else {
self.image = self.inactiveImage;
}
}
- (void)updateRollOverImage {
if ([self.group shouldDisplayRollOver] && [self isEnabled]) {
self.image = self.rolloverImage;
} else {
[self updateImage];
}
}
- (void)updateImage {
if ([self isEnabled]) {
[self updateActiveImage];
} else {
self.image = self.inactiveImage;
}
}
- (void)updateActiveImage {
if ([self.window isKeyWindow]) {
self.image = self.activeImage;
} else {
self.image = self.activeNotKeyWindowImage;
}
}
@end