-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYHintArrow.m
350 lines (278 loc) · 10.4 KB
/
SSYHintArrow.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
#import "SSYHintArrow.h"
#import <QuartzCore/CoreAnimation.h>
#include <tgmath.h>
NSString* const SSYHintArrowDidCloseNotification = @"SSYHintArrowDidCloseNotification" ;
NSString* const SSYHintArrowEventKey = @"SSYHintArrowEventKey" ;
static SSYHintArrow* static_helpArrow = nil ;
@implementation SSYHintArrow
# pragma mark * Heavy Lifters
- (CGFloat)scaleFactor {
CGFloat factor = 1.0 ;
return factor ;
}
- (void)updateGeometry {
[m_view setFrame:NSMakeRect(0.0, 0.0, m_size.width, m_size.height)] ;
NSRect contentRect = NSZeroRect ;
contentRect.size = [m_view frame].size ;
// Account for viewMargin.
m_viewFrame = NSMakeRect(m_viewMargin * [self scaleFactor],
m_viewMargin * [self scaleFactor],
[m_view frame].size.width, [m_view frame].size.height) ;
contentRect = NSInsetRect(contentRect,
-m_viewMargin * [self scaleFactor],
-m_viewMargin * [self scaleFactor]) ;
CGFloat scaledArrowHeight = m_arrowHeight * [self scaleFactor] ;
m_viewFrame.origin.x += scaledArrowHeight ;
contentRect.size.width += scaledArrowHeight ;
// Position frame origin appropriately, accounting for arrow-inset.
if (m_window) {
NSRect screenRect = [m_window convertRectToScreen:NSMakeRect(m_point.x, m_point.y, 0, 0)] ;
contentRect.origin = screenRect.origin ;
}
else {
contentRect.origin = m_point ;
}
CGFloat halfHeight = contentRect.size.height / 2.0 ;
contentRect.origin.y -= halfHeight ;
// Account for distance in new window frame.
contentRect.origin.x += m_distance ;
// Reconfigure window and view frames appropriately.
[self setFrame:contentRect display:NO] ;
[m_view setFrame:m_viewFrame] ;
}
- (NSBezierPath *)backgroundPath {
CGFloat scaleFactor = [self scaleFactor] ;
CGFloat scaledRadius = m_cornerRadius * scaleFactor ;
NSRect contentArea = NSInsetRect(m_viewFrame,
-m_viewMargin * scaleFactor,
-m_viewMargin * scaleFactor) ;
CGFloat minX = ceilf(NSMinX(contentArea) * scaleFactor + 0.5f) ;
CGFloat maxX = floorf(NSMaxX(contentArea) * scaleFactor - 0.5f) ;
CGFloat minY = ceilf(NSMinY(contentArea) * scaleFactor + 0.5f) ;
CGFloat midY = NSMidY(contentArea) * scaleFactor ;
CGFloat maxY = floor(NSMaxY(contentArea) * scaleFactor - 0.5f) ;
NSBezierPath* path = [NSBezierPath bezierPath] ;
[path setLineJoinStyle:NSRoundLineJoinStyle] ;
// Begin at top-left.
NSPoint currPt = NSMakePoint(minX, maxY) ;
[path moveToPoint:currPt] ;
// Line to rounded corner at top right.
NSPoint endOfLine = NSMakePoint(maxX - scaledRadius, maxY) ;
[path lineToPoint:endOfLine] ;
// Rounded corner on top-right.
[path appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY)
toPoint:NSMakePoint(maxX, maxY - scaledRadius)
radius:scaledRadius] ;
// Right side, beginning at top.
endOfLine = NSMakePoint(maxX, minY + scaledRadius) ;
[path lineToPoint:endOfLine] ;
[path appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
toPoint:NSMakePoint(maxX - scaledRadius, minY)
radius:scaledRadius] ;
// Bottom side, beginning at right.
endOfLine = NSMakePoint(minX, minY) ;
[path lineToPoint:endOfLine] ;
// Draw to arrow point, beginning at the bottom-left.
endOfLine = NSMakePoint(minX - (maxY - midY), midY) ;
[path lineToPoint:endOfLine] ;
// Return to top left
[path closePath] ;
return path ;
}
- (NSColor *)backgroundColorPatternImage {
NSImage* bg = [NSImage imageWithSize:[self frame].size
flipped:NO
drawingHandler:^(NSRect dstRect) {
NSBezierPath* bgPath = [self backgroundPath] ;
[NSGraphicsContext saveGraphicsState] ;
[bgPath addClip] ;
// Draw background gradient.
[m_innerColor set];
[bgPath fill];
// Draw border if appropriate.
if (m_borderWidth > 0) {
// Double the borderWidth since we're drawing inside the path.
[bgPath setLineWidth:(m_borderWidth * 2.0) * [self scaleFactor]] ;
[m_borderColor set] ;
[bgPath stroke] ;
}
[NSGraphicsContext restoreGraphicsState] ;
return YES ;
}] ;
return [NSColor colorWithPatternImage:bg] ;
}
- (void)updateBackground {
// Call NSWindow's implementation of -setBackgroundColor: because we override
// it in this class to let us set the entire background image of the window
// as an NSColor patternImage.
[super setBackgroundColor:[self backgroundColorPatternImage]] ;
if ([self isVisible]) {
[self display] ;
[self invalidateShadow] ;
}
}
- (void)redisplay {
if (m_isResizingLockout) {
return ;
}
m_isResizingLockout = YES ;
[self updateGeometry] ;
[self updateBackground] ;
m_isResizingLockout = NO ;
}
# pragma mark * Basic Infrastructure
- (SSYHintArrow *)initAttachedToPoint:(NSPoint)point
inWindow:(NSWindow *)window
atDistance:(CGFloat)distance {
// Create dummy initial contentRect for window.
NSRect contentRect = NSMakeRect(0.0, 0.0, m_size.width, m_size.height) ;
contentRect.size = m_size ;
if ((self = [super initWithContentRect:contentRect
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO])) {
// Parameters for displaying the fat blue arrow.
NSColor* color;
if (@available(macOS 10.14, *)) {
color = [NSColor controlAccentColor];
} else {
color = [NSColor colorWithCalibratedRed:(54.0/256.0) green:(90.0/256.0) blue:(245.0/256.0) alpha:0.0];
}
m_innerColor = [[color colorWithAlphaComponent:0.5] retain];
m_size = NSMakeSize(74.0, 28.0) ;
m_borderColor = [[NSColor whiteColor] retain];
m_borderWidth = 3.0 ;
m_viewMargin = 3.0 ;
m_cornerRadius = 10.0 ;
m_arrowHeight = m_size.height/2 ;
m_isResizingLockout = NO ;
m_window = window ;
m_point = point ;
m_distance = distance ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(endWithNote:)
name:NSWindowWillCloseNotification
object:window] ;
// Configure window
[super setBackgroundColor:[NSColor clearColor]] ;
[self setMovableByWindowBackground:NO] ;
[self setExcludedFromWindowsMenu:YES] ;
[self setAlphaValue:1.0] ;
[self setOpaque:NO] ;
[self setHasShadow:YES] ;
// Make the view
m_view = [[NSView alloc] initWithFrame:contentRect] ;
// Add view as subview of our contentView.
[[self contentView] addSubview:m_view] ;
[m_view release] ;
// Configure our initial geometry.
[self updateGeometry] ;
// Update the background.
[self updateBackground] ;
}
return self ;
}
- (void)dealloc {
[m_borderColor release] ;
[m_innerColor release] ;
[super dealloc] ;
}
- (NSWindow*)window {
return m_window ;
}
# pragma mark * Other Methods
- (void)endWithNote:(NSNotification*)notUsed {
[[NSNotificationCenter defaultCenter] removeObserver:self] ;
[[self window] removeChildWindow:static_helpArrow] ;
[self orderOut:self] ;
[self autorelease] ;
// I tried a -release instead of the above, but it
// would usually crash, often reporting that
// _runningDocModal was sent to the deallocated self
static_helpArrow = nil ;
self = nil ;
[[NSNotificationCenter defaultCenter] postNotificationName:SSYHintArrowDidCloseNotification
object:self
userInfo:nil] ;
}
- (void)sendEvent:(NSEvent *)event {
[SSYHintArrow removeIfEvent:event] ;
[super sendEvent:event] ;
}
# pragma mark * Overrides
- (BOOL)canBecomeMainWindow {
return NO ;
}
- (BOOL)canBecomeKeyWindow {
return YES ;
}
- (BOOL)isExcludedFromWindowsMenu {
return YES ;
}
- (BOOL)validateMenuItem:(NSMenuItem *)item {
if (m_window) {
return [m_window validateMenuItem:item] ;
}
return [super validateMenuItem:item] ;
}
# pragma mark * Class Methods
+ (CAKeyframeAnimation *)shakeAnimation:(NSRect)frame {
CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animation] ;
CGMutablePathRef shakePath = CGPathCreateMutable() ;
CGPathMoveToPoint(shakePath, NULL, NSMinX(frame), NSMinY(frame)) ;
NSInteger i ;
for (i = 0; i < 3; ++i) {
CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame), NSMinY(frame)) ;
CGPathAddLineToPoint(shakePath, NULL, NSMinX(frame) + 25.0, NSMinY(frame)) ;
}
CGPathCloseSubpath(shakePath) ;
shakeAnimation.path = shakePath ;
shakeAnimation.duration = 3 ;
CFRelease(shakePath) ; // I hope that CAKeyframeAnimation retains its -path.
return shakeAnimation ;
}
+ (void)showHelpArrowAtPoint:(NSPoint)point
inWindow:(NSWindow*)window {
if (static_helpArrow) {
// Only one SSYHintArrow at a time! Destroy the prior one.
[static_helpArrow endWithNote:nil] ;
}
static_helpArrow = [[SSYHintArrow alloc] initAttachedToPoint:point
inWindow:window
atDistance:0.0] ;
[window addChildWindow:static_helpArrow
ordered:NSWindowAbove] ;
NSDictionary* animations = [NSDictionary dictionaryWithObject:[self shakeAnimation:[static_helpArrow frame]]
forKey:@"frameOrigin"] ;
[static_helpArrow setAnimations:animations];
[[static_helpArrow animator] setFrameOrigin:[static_helpArrow frame].origin] ;
// static_helpArrow will be released in its -endWithNote: message
}
+ (void)showHelpArrowRightOfView:(NSView*)view {
NSView* superview = [view superview] ;
NSRect targetRect = [view frame] ;
NSWindow* window = [view window] ;
NSPoint point = NSMakePoint(NSMaxX(targetRect), NSMidY(targetRect)) ;
point = [superview convertPoint:point
toView:[window contentView]] ;
[SSYHintArrow showHelpArrowAtPoint:point
inWindow:window] ;
}
+ (void)remove {
[static_helpArrow endWithNote:nil] ;
}
+ (void)removeIfEvent:(NSEvent*)event {
NSEventType eventType = [event type] ;
if (
(eventType == NSEventTypeLeftMouseDown)
||
(eventType == NSEventTypeRightMouseDown)
||
(eventType == NSEventTypeOtherMouseDown)
||
(eventType == NSEventTypeKeyDown)
) {
[static_helpArrow endWithNote:nil] ;
}
}
@end