-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathPHModalWindowController.m
362 lines (281 loc) · 12.3 KB
/
PHModalWindowController.m
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
* Phoenix is released under the MIT License. Refer to https://github.com/kasper/phoenix/blob/master/LICENSE.md
*/
#import "PHModalWindowController.h"
#import "PHContext.h"
@interface PHModalWindowController ()
@property NSColor *textColor;
#pragma mark - Views
@property(weak) IBOutlet NSView *containerView;
@property(weak) IBOutlet NSImageView *iconView;
@property(weak) IBOutlet NSTextField *textField;
#pragma mark - Constraints
@property(weak) IBOutlet NSLayoutConstraint *iconViewZeroWidthConstraint;
@property(weak) IBOutlet NSLayoutConstraint *separatorConstraint;
@property(weak) IBOutlet NSLayoutConstraint *textFieldTextWidthConstraint;
@property(weak) IBOutlet NSLayoutConstraint *textFieldInputWidthConstraint;
@end
@implementation PHModalWindowController
typedef NS_ENUM(NSInteger, PHModalWindowControllerAppearanceMaterial) {
PHModalWindowControllerAppearanceMaterialDark,
PHModalWindowControllerAppearanceMaterialLight,
PHModalWindowControllerAppearanceMaterialTransparent
};
static NSString *const PHModalWindowControllerAppearanceDark = @"dark";
static NSString *const PHModalWindowControllerIconKeyPath = @"icon";
static NSString *const PHModalWindowControllerMessageKeyPath = @"message";
static NSString *const PHModalWindowControllerOriginKeyPath = @"origin";
static NSString *const PHModalWindowControllerTextKeyPath = @"text";
#pragma mark - Initialising
- (instancetype)init {
if (self = [super init]) {
[self addObserverForKeyPaths:[PHModalWindowController keyPaths]];
self.animationDuration = 0.2;
self.weight = 24.0;
self.appearance = PHModalWindowControllerAppearanceDark;
self.hasShadow = YES;
self.text = @"";
self.textAlignment = @"left";
self.font = [NSFont systemFontOfSize:self.weight].fontName;
self.inputPlaceholder = @"";
// Observe context load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(close)
name:PHContextWillLoadNotification
object:nil];
}
return self;
}
#pragma mark - Deallocing
- (void)dealloc {
[super close];
[self removeObserverForKeyPaths:[PHModalWindowController keyPaths]];
[[NSNotificationCenter defaultCenter] removeObserver:self name:PHContextWillLoadNotification object:nil];
}
#pragma mark - Key Paths
+ (NSArray<NSString *> *)keyPaths {
static NSArray<NSString *> *keyPaths;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
keyPaths = @[
PHModalWindowControllerIconKeyPath,
PHModalWindowControllerMessageKeyPath,
PHModalWindowControllerOriginKeyPath,
PHModalWindowControllerTextKeyPath
];
});
return keyPaths;
}
#pragma mark - NSWindowController
- (NSString *)windowNibName {
return @"ModalWindow";
}
#pragma mark - NSWindowDelegate
- (void)windowDidResize:(NSNotification *)__unused notification {
JSValue *callback = self.didResize;
if (!callback.isUndefined) {
[callback callWithArguments:@[]];
}
}
#pragma mark - NSControlTextEditingDelegate
- (void)controlTextDidChange:(NSNotification *)__unused notification {
JSValue *callback = self.textDidChange;
NSString *value = self.text ? self.text : @"";
if (!callback.isUndefined) {
[callback callWithArguments:@[value]];
}
}
- (void)controlTextDidEndEditing:(NSNotification *)notification {
static NSDictionary<NSNumber *, NSString *> *actions;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
actions =
@{@(NSTextMovementReturn): @"return",
@(NSTextMovementTab): @"tab",
@(NSTextMovementBacktab): @"backtab"};
});
JSValue *callback = self.textDidCommit;
NSString *value = self.text ? self.text : @"";
NSNumber *textMovement = notification.userInfo[NSTextMovementUserInfoKey];
NSString *action = actions[textMovement];
if (!callback.isUndefined) {
NSArray *arguments = action ? @[value, action] : @[value];
[callback callWithArguments:arguments];
}
}
#pragma mark - Appearance
- (NSTextAlignment)alignment {
static NSDictionary<NSString *, NSNumber *> *alignments;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
alignments = @{@"left": @0, @"right": @2, @"center": @1, @"centre": @1};
});
NSNumber *value = alignments[self.textAlignment.lowercaseString];
if (!value) {
return NSTextAlignmentLeft;
}
return value.integerValue;
}
- (PHModalWindowControllerAppearanceMaterial)material {
static NSDictionary<NSString *, NSNumber *> *appearances;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
appearances = @{PHModalWindowControllerAppearanceDark: @0, @"light": @1, @"transparent": @2};
});
NSNumber *value = appearances[self.appearance.lowercaseString];
if (!value) {
return PHModalWindowControllerAppearanceMaterialDark;
}
return value.integerValue;
}
- (void)setupVibrantAppearance {
CGFloat cornerRadius = 10.0;
NSDictionary<NSString *, id> *views = @{@"container": self.containerView};
NSVisualEffectView *visualEffectView = [[NSVisualEffectView alloc] initWithFrame:self.window.contentView.frame];
visualEffectView.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
visualEffectView.state = NSVisualEffectStateActive;
// Use light appearance
if ([self material] == PHModalWindowControllerAppearanceMaterialLight) {
visualEffectView.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantLight];
self.textField.textColor = self.textColor ? self.textColor : [NSColor blackColor];
}
// Set mask image to rounded rectangle
CGFloat edgeSize = 1.0 + (2 * cornerRadius);
NSSize maskSize = NSMakeSize(edgeSize, edgeSize);
visualEffectView.maskImage = [NSImage imageWithSize:maskSize
flipped:NO
drawingHandler:^BOOL(NSRect destination) {
[[NSBezierPath bezierPathWithRoundedRect:destination
xRadius:cornerRadius
yRadius:cornerRadius] fill];
return YES;
}];
// Make edges smooth
visualEffectView.maskImage.capInsets = NSEdgeInsetsMake(cornerRadius, cornerRadius, cornerRadius, cornerRadius);
// Add container view as subview
[visualEffectView addSubview:self.containerView];
[visualEffectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[container]"
options:0
metrics:nil
views:views]];
[visualEffectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[container]-(0)-|"
options:0
metrics:nil
views:views]];
[visualEffectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[container]"
options:0
metrics:nil
views:views]];
[visualEffectView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[container]-(0)-|"
options:0
metrics:nil
views:views]];
// Set visual effect view as the content view
self.window.contentView = visualEffectView;
}
- (void)setTextColorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha {
self.textColor = [NSColor colorWithCalibratedRed:red / 255 green:green / 255 blue:blue / 255 alpha:alpha];
self.textField.textColor = self.textColor;
}
#pragma mark - Properties
- (BOOL)hasText {
return ![self.text isEqualToString:@""];
}
#pragma mark - KVO
- (void)addObserverForKeyPaths:(NSArray<NSString *> *)keyPaths {
for (NSString *keyPath in keyPaths) {
[self addObserver:self forKeyPath:keyPath options:0 context:NULL];
}
}
- (void)removeObserverForKeyPaths:(NSArray<NSString *> *)keyPaths {
for (NSString *keyPath in keyPaths) {
[self removeObserver:self forKeyPath:keyPath];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)__unused object
change:(NSDictionary<NSString *, id> *)__unused change
context:(void *)__unused context {
[self window];
// Update text-property
if ([keyPath isEqualToString:PHModalWindowControllerMessageKeyPath]) {
NSLog(@"Deprecated: Property “message” for modal is deprecated and will be removed in later versions, use "
@"“text” instead.");
self.text = self.message;
}
// Update window origin
if ([keyPath isEqualToString:PHModalWindowControllerOriginKeyPath]) {
if (!isnan(self.origin.x) && !isnan(self.origin.y)) {
[self.window setFrameOrigin:self.origin];
}
}
// Update layout
if ([keyPath isEqualToString:PHModalWindowControllerIconKeyPath] ||
[keyPath isEqualToString:PHModalWindowControllerTextKeyPath]) {
[self layout];
}
}
#pragma mark - Displaying
- (BOOL)isDisplayable {
return self.icon || self.isInput || [self hasText];
}
- (void)layout {
self.iconViewZeroWidthConstraint.priority = !self.icon ? 999 : NSLayoutPriorityDefaultLow;
self.separatorConstraint.constant = (!self.icon || (!self.isInput && ![self hasText])) ? 0.0 : 10.0;
self.textFieldTextWidthConstraint.priority =
self.isInput ? NSLayoutPriorityDefaultLow : NSLayoutPriorityDefaultHigh;
self.textFieldInputWidthConstraint.priority =
self.isInput ? NSLayoutPriorityDefaultHigh : NSLayoutPriorityDefaultLow;
}
- (NSRect)frame {
[self layout];
[self.window layoutIfNeeded];
return self.window.frame;
}
- (void)fadeWindowToAlpha:(CGFloat)alpha completionHandler:(void (^)(void))completionHandler {
[NSAnimationContext
runAnimationGroup:^(NSAnimationContext *context) {
context.duration = self.animationDuration;
[self.window animator].alphaValue = alpha;
}
completionHandler:completionHandler];
}
- (instancetype)show {
if (![self isDisplayable]) {
return self;
}
// Set vibrant appearance
if ([self material] != PHModalWindowControllerAppearanceMaterialTransparent) {
[self setupVibrantAppearance];
}
self.window.ignoresMouseEvents = !self.isInput;
self.window.hasShadow = self.hasShadow;
self.textField.alignment = [self alignment];
if (self.isInput) {
self.textField.placeholderString = self.inputPlaceholder;
// Required for text field to become key
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
[self layout];
[self showWindow:self];
[self fadeWindowToAlpha:1.0
completionHandler:^{
// Keep window open until closed
if (self.duration == 0) {
return;
}
[self performSelector:@selector(close) withObject:nil afterDelay:self.duration];
}];
return self;
}
- (void)focus {
[self.window makeKeyWindow];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
- (void)close {
[self fadeWindowToAlpha:0.0
completionHandler:^{
[super close];
}];
}
@end