-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathINPopoverWindow.m
211 lines (178 loc) · 6.4 KB
/
INPopoverWindow.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
//
// INPopoverWindow.m
// Copyright 2011-2014 Indragie Karunaratne. All rights reserved.
//
#import "INPopoverWindow.h"
#import "INPopoverControllerDefines.h"
#import "INPopoverWindowFrame.h"
#import "INPopoverController.h"
#import <QuartzCore/QuartzCore.h>
#define START_SIZE NSMakeSize(20, 20)
#define OVERSHOOT_FACTOR 1.2
// A lot of this code was adapted from the following article:
// <http://cocoawithlove.com/2008/12/drawing-custom-window-on-mac-os-x.html>
@implementation INPopoverWindow {
NSView *_popoverContentView;
NSWindow *_zoomWindow;
}
// Borderless, transparent window
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation
{
if ((self = [super initWithContentRect:contentRect styleMask:NSNonactivatingPanelMask backing:bufferingType defer:deferCreation])) {
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setHasShadow:YES];
}
return self;
}
// Leave some space around the content for drawing the arrow
- (NSRect)contentRectForFrameRect:(NSRect)windowFrame
{
windowFrame.origin = NSZeroPoint;
const CGFloat arrowHeight = self.frameView.arrowSize.height;
return NSInsetRect(windowFrame, arrowHeight, arrowHeight);
}
- (NSRect)frameRectForContentRect:(NSRect)contentRect
{
const CGFloat arrowHeight = self.frameView.arrowSize.height;
return NSInsetRect(contentRect, -arrowHeight, -arrowHeight);
}
// Allow the popover to become the key window
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (BOOL)canBecomeMainWindow
{
return NO;
}
- (BOOL)isVisible
{
return [super isVisible] || [_zoomWindow isVisible];
}
- (INPopoverWindowFrame *)frameView
{
return (INPopoverWindowFrame *) [self contentView];
}
- (void)setContentView:(NSView *)aView
{
[self setPopoverContentView:aView];
}
- (void)setPopoverContentView:(NSView *)aView
{
if ([_popoverContentView isEqualTo:aView]) {return;}
NSRect bounds = [self frame];
bounds.origin = NSZeroPoint;
INPopoverWindowFrame *frameView = [self frameView];
if (!frameView) {
frameView = [[INPopoverWindowFrame alloc] initWithFrame:bounds];
[super setContentView:frameView]; // Call on super or there will be infinite loop
}
if (_popoverContentView) {
[_popoverContentView removeFromSuperview];
}
_popoverContentView = aView;
[_popoverContentView setFrame:[self contentRectForFrameRect:bounds]];
[_popoverContentView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[frameView addSubview:_popoverContentView];
}
- (void)presentAnimated
{
if ([self isVisible])
return;
switch (self.popoverController.animationType) {
case INPopoverAnimationTypePop:
[self presentWithPopAnimation];
break;
case INPopoverAnimationTypeFadeIn:
case INPopoverAnimationTypeFadeInOut:
[self presentWithFadeAnimation];
break;
default:
break;
}
}
- (void)presentWithPopAnimation
{
NSRect endFrame = [self frame];
NSRect startFrame = [self.popoverController popoverFrameWithSize:START_SIZE andArrowDirection:self.frameView.arrowDirection];
NSRect overshootFrame = [self.popoverController popoverFrameWithSize:NSMakeSize(endFrame.size.width * OVERSHOOT_FACTOR, endFrame.size.height * OVERSHOOT_FACTOR) andArrowDirection:self.frameView.arrowDirection];
_zoomWindow = [self _zoomWindowWithRect:startFrame];
[_zoomWindow setAlphaValue:0.0];
[_zoomWindow orderFront:self];
// configure bounce-out animation
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
[anim setDelegate:self];
[anim setValues:[NSArray arrayWithObjects:[NSValue valueWithRect:startFrame], [NSValue valueWithRect:overshootFrame], [NSValue valueWithRect:endFrame], nil]];
[_zoomWindow setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:anim, @"frame", nil]];
[NSAnimationContext beginGrouping];
[[_zoomWindow animator] setAlphaValue:1.0];
[[_zoomWindow animator] setFrame:endFrame display:YES];
[NSAnimationContext endGrouping];
}
- (void)presentWithFadeAnimation
{
[self setAlphaValue:0.0];
[self makeKeyAndOrderFront:nil];
[[self animator] setAlphaValue:1.0];
}
- (void)dismissAnimated
{
[[_zoomWindow animator] setAlphaValue:0.0]; // in case zoom window is currently animating
[[self animator] setAlphaValue:0.0];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self setAlphaValue:1.0];
[self makeKeyAndOrderFront:self];
[_zoomWindow close];
_zoomWindow = nil;
// call the animation delegate of the "real" window
CAAnimation *windowAnimation = [self animationForKey:@"alphaValue"];
[[windowAnimation delegate] animationDidStop:anim finished:flag];
}
#pragma mark -
#pragma mark Private
// The following method is adapted from the following class:
// <https://github.com/MrNoodle/NoodleKit/blob/master/NSWindow-NoodleEffects.m>
// Copyright 2007-2009 Noodlesoft, LLC. All rights reserved.
- (NSWindow *)_zoomWindowWithRect:(NSRect)rect
{
BOOL isOneShot = [self isOneShot];
if (isOneShot)
[self setOneShot:NO];
if ([self windowNumber] <= 0) {
// force creation of window device by putting it on-screen. We make it transparent to minimize the chance of visible flicker
CGFloat alpha = [self alphaValue];
[self setAlphaValue:0.0];
[self orderBack:self];
[self orderOut:self];
[self setAlphaValue:alpha];
}
// get window content as image
NSRect frame = [self frame];
NSImage *image = [[NSImage alloc] initWithSize:frame.size];
[self displayIfNeeded]; // refresh view
NSView *view = self.contentView;
NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:view.bounds];
[view cacheDisplayInRect:view.bounds toBitmapImageRep:imageRep];
[image addRepresentation:imageRep];
// create zoom window
NSWindow *zoomWindow = [[NSWindow alloc] initWithContentRect:rect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[zoomWindow setBackgroundColor:[NSColor clearColor]];
[zoomWindow setHasShadow:[self hasShadow]];
[zoomWindow setLevel:[self level]];
[zoomWindow setOpaque:NO];
[zoomWindow setReleasedWhenClosed:NO];
[zoomWindow useOptimizedDrawing:YES];
NSImageView *imageView = [[NSImageView alloc] initWithFrame:[zoomWindow contentRectForFrameRect:frame]];
[imageView setImage:image];
[imageView setImageFrameStyle:NSImageFrameNone];
[imageView setImageScaling:NSScaleToFit];
[imageView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[zoomWindow setContentView:imageView];
// reset one shot flag
[self setOneShot:isOneShot];
return zoomWindow;
}
@end