forked from BigZaphod/Chameleon
/
UIPopoverController.m
381 lines (313 loc) · 16.6 KB
/
UIPopoverController.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* Copyright (c) 2011, The Iconfactory. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of The Iconfactory nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* 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 ICONFACTORY 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 "UIPopoverController+UIPrivate.h"
#import "UIViewController.h"
#import "UIWindow.h"
#import "UIScreen+UIPrivate.h"
#import "UIScreenAppKitIntegration.h"
#import "UIKitView.h"
#import "UITouch.h"
#import "UIApplication+UIPrivate.h"
#import "UIPopoverView.h"
#import "UIPopoverNSWindow.h"
#import "UIPopoverOverlayNSView.h"
#import "UIImage+UIPrivate.h"
static BOOL SizeIsLessThanOrEqualSize(NSSize size1, NSSize size2)
{
return (size1.width <= size2.width) && (size1.height <= size2.height);
}
static NSPoint PopoverWindowOrigin(NSWindow *inWindow, NSRect fromRect, NSSize popoverSize, UIPopoverArrowDirection arrowDirections, NSPoint *pointTo, UIPopoverArrowDirection *arrowDirection)
{
// 1) define a set of possible quads around fromRect that the popover could appear in
// 2) eliminate quads based on arrow direction restrictions and sizes
// 3) the first quad that is large enough "wins"
NSRect screenRect = [[inWindow screen] visibleFrame];
NSRect bottomQuad = NSMakeRect(screenRect.origin.x, screenRect.origin.y, screenRect.size.width, fromRect.origin.y-screenRect.origin.y);
NSRect topQuad = NSMakeRect(screenRect.origin.x, fromRect.origin.y+fromRect.size.height, screenRect.size.width, screenRect.size.height-fromRect.origin.y-fromRect.size.height-screenRect.origin.y);
NSRect leftQuad = NSMakeRect(screenRect.origin.x, screenRect.origin.y, fromRect.origin.x-screenRect.origin.x, screenRect.size.height-screenRect.origin.y);
NSRect rightQuad = NSMakeRect(fromRect.origin.x+fromRect.size.width, screenRect.origin.y, screenRect.size.width-fromRect.origin.x-fromRect.size.width-screenRect.origin.x, screenRect.size.height-screenRect.origin.y);
pointTo->x = fromRect.origin.x+(fromRect.size.width/2.f);
pointTo->y = fromRect.origin.y+(fromRect.size.height/2.f);
NSPoint origin;
origin.x = fromRect.origin.x + (fromRect.size.width/2.f) - (popoverSize.width/2.f);
origin.y = fromRect.origin.y + (fromRect.size.height/2.f) - (popoverSize.height/2.f);
const CGFloat minimumPadding = 40;
const BOOL allowTopOrBottom = (pointTo->x >= NSMinX(screenRect)+minimumPadding && pointTo->x <= NSMaxX(screenRect)-minimumPadding);
const BOOL allowLeftOrRight = (pointTo->y >= NSMinY(screenRect)+minimumPadding && pointTo->y <= NSMaxY(screenRect)-minimumPadding);
const BOOL allowTopQuad = ((arrowDirections & UIPopoverArrowDirectionDown) != 0) && topQuad.size.width > 0 && topQuad.size.height > 0 && allowTopOrBottom;
const BOOL allowBottomQuad = ((arrowDirections & UIPopoverArrowDirectionUp) != 0) && bottomQuad.size.width > 0 && bottomQuad.size.height > 0 && allowTopOrBottom;
const BOOL allowLeftQuad = ((arrowDirections & UIPopoverArrowDirectionRight) != 0) && leftQuad.size.width > 0 && leftQuad.size.height > 0 && allowLeftOrRight;
const BOOL allowRightQuad = ((arrowDirections & UIPopoverArrowDirectionLeft) != 0) && rightQuad.size.width > 0 && rightQuad.size.height > 0 && allowLeftOrRight;
const CGFloat arrowPadding = 8; // the arrow images are slightly larger to account for shadows, but the arrow point needs to be up against the rect exactly so this helps with that
if (allowBottomQuad && SizeIsLessThanOrEqualSize(popoverSize,bottomQuad.size)) {
pointTo->y = fromRect.origin.y;
origin.y = fromRect.origin.y - popoverSize.height + arrowPadding;
*arrowDirection = UIPopoverArrowDirectionUp;
} else if (allowRightQuad && SizeIsLessThanOrEqualSize(popoverSize,rightQuad.size)) {
pointTo->x = fromRect.origin.x + fromRect.size.width;
origin.x = pointTo->x - arrowPadding;
*arrowDirection = UIPopoverArrowDirectionLeft;
} else if (allowLeftQuad && SizeIsLessThanOrEqualSize(popoverSize,leftQuad.size)) {
pointTo->x = fromRect.origin.x;
origin.x = fromRect.origin.x - popoverSize.width + arrowPadding;
*arrowDirection = UIPopoverArrowDirectionRight;
} else if (allowTopQuad && SizeIsLessThanOrEqualSize(popoverSize,topQuad.size)) {
pointTo->y = fromRect.origin.y + fromRect.size.height;
origin.y = pointTo->y - arrowPadding;
*arrowDirection = UIPopoverArrowDirectionDown;
} else {
*arrowDirection = UIPopoverArrowDirectionUnknown;
}
NSRect windowRect;
windowRect.origin = origin;
windowRect.size = popoverSize;
if (NSMaxX(windowRect) > NSMaxX(screenRect)) {
windowRect.origin.x = NSMaxX(screenRect) - popoverSize.width;
}
if (NSMinX(windowRect) < NSMinX(screenRect)) {
windowRect.origin.x = NSMinX(screenRect);
}
if (NSMaxY(windowRect) > NSMaxY(screenRect)) {
windowRect.origin.y = NSMaxY(screenRect) - popoverSize.height;
}
if (NSMinY(windowRect) < NSMinY(screenRect)) {
windowRect.origin.y = NSMinY(screenRect);
}
windowRect.origin.x = roundf(windowRect.origin.x);
windowRect.origin.y = roundf(windowRect.origin.y);
return windowRect.origin;
}
@implementation UIPopoverController
@synthesize delegate=_delegate, contentViewController=_contentViewController, passthroughViews=_passthroughViews;
@synthesize popoverArrowDirection=_popoverArrowDirection;
- (id)init
{
if ((self=[super init])) {
_popoverArrowDirection = UIPopoverArrowDirectionUnknown;
}
return self;
}
- (id)initWithContentViewController:(UIViewController *)viewController
{
if ((self=[self init])) {
self.contentViewController = viewController;
}
return self;
}
- (void)dealloc
{
[self dismissPopoverAnimated:NO];
[_passthroughViews release];
[_contentViewController release];
[super dealloc];
}
- (void)setDelegate:(id<UIPopoverControllerDelegate>)newDelegate
{
_delegate = newDelegate;
_delegateHas.popoverControllerDidDismissPopover = [_delegate respondsToSelector:@selector(popoverControllerDidDismissPopover:)];
_delegateHas.popoverControllerShouldDismissPopover = [_delegate respondsToSelector:@selector(popoverControllerShouldDismissPopover:)];
}
- (void)setContentViewController:(UIViewController *)controller animated:(BOOL)animated
{
if (controller != _contentViewController) {
if ([self isPopoverVisible]) {
[_popoverView setContentView:controller.view animated:animated];
}
[_contentViewController release];
_contentViewController = [controller retain];
}
}
- (void)setContentViewController:(UIViewController *)viewController
{
[self setContentViewController:viewController animated:NO];
}
- (void)presentPopoverFromRect:(CGRect)rect inView:(UIView *)view permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
{
assert(view != nil);
assert(arrowDirections != UIPopoverArrowDirectionUnknown);
assert(!CGRectIsNull(rect));
assert(!CGRectEqualToRect(rect,CGRectZero));
NSWindow *viewNSWindow = [[view.window.screen UIKitView] window];
// only create new stuff if the popover isn't already visible
if (![self isPopoverVisible]) {
// build an overlay window which will capture any clicks on the main window the popover is being presented from and then dismiss it.
// this overlay can also be used to implement the pass-through views of the popover, but I'm not going to do that right now since
// we don't need it. attach the overlay window to the "main" window.
NSRect windowFrame = [viewNSWindow frame];
NSRect overlayContentRect = NSMakeRect(0,0,windowFrame.size.width,windowFrame.size.height);
_overlayWindow = [[NSWindow alloc] initWithContentRect:overlayContentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[_overlayWindow setIgnoresMouseEvents:NO];
[_overlayWindow setOpaque:NO];
[(NSWindow *)_overlayWindow setBackgroundColor:[NSColor clearColor]];
[_overlayWindow setFrameOrigin:windowFrame.origin];
[_overlayWindow setContentView:[[(UIPopoverOverlayNSView *)[UIPopoverOverlayNSView alloc] initWithFrame:overlayContentRect popoverController:self] autorelease]];
[viewNSWindow addChildWindow:_overlayWindow ordered:NSWindowAbove];
[_contentViewController viewWillAppear:animated];
// now build the actual popover view which represents the popover's chrome, and since it's a UIView, we need to build a UIKitView
// as well to put it in our NSWindow...
_popoverView = [[UIPopoverView alloc] initWithContentView:_contentViewController.view size:_contentViewController.contentSizeForViewInPopover popoverController:self];
UIKitView *hostingView = [(UIKitView *)[UIKitView alloc] initWithFrame:NSRectFromCGRect([_popoverView bounds])];
[[hostingView UIScreen] _setPopoverController:self];
[[hostingView UIWindow] addSubview:_popoverView];
// this prevents a visible flash from sometimes occuring due to the fact that the window is created and added as a child before it has the
// proper origin set. this means it it ends up flashing at the bottom left corner of the screen sometimes before it
// gets down farther in this method where the actual origin is calculated and set. since the window is transparent, simply setting the UIView
// hidden gets around the problem since you then can't see any of the actual content that's in the window :)
_popoverView.hidden = YES;
// now finally make the actual popover window itself and attach it to the overlay window
_popoverWindow = [[UIPopoverNSWindow alloc] initWithContentRect:[hostingView bounds] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[_popoverWindow setPopoverController:self];
[_popoverWindow setOpaque:NO];
[(NSWindow *)_popoverWindow setBackgroundColor:[NSColor clearColor]];
[_popoverWindow setContentView:hostingView];
[_overlayWindow addChildWindow:_popoverWindow ordered:NSWindowAbove];
[_popoverWindow makeFirstResponder:hostingView];
[hostingView release];
}
// cancel current touches (if any) to prevent the main window from losing track of events (such as if the user was holding down the mouse
// button and a timer triggered the appearance of this popover. the window would possibly then not receive the mouseUp depending on how
// all this works out... I first ran into this problem with NSMenus. A NSWindow is a bit different, but I think this makes sense here
// too so premptively doing it to avoid potential problems.)
[[UIApplication sharedApplication] _cancelTouchesInView:nil];
// now position the popover window according to the passed in parameters.
CGRect windowRect = [view convertRect:rect toView:nil];
CGRect screenRect = [view.window convertRect:windowRect toWindow:nil];
CGRect desktopScreenRect = [view.window.screen convertRect:screenRect toScreen:nil];
NSPoint pointTo = NSMakePoint(0,0);
// finally, let's show it!
[_popoverWindow setFrameOrigin:PopoverWindowOrigin(_overlayWindow, NSRectFromCGRect(desktopScreenRect), NSSizeFromCGSize(_popoverView.frame.size), arrowDirections, &pointTo, &_popoverArrowDirection)];
_popoverView.hidden = NO;
[_popoverWindow makeKeyWindow];
// the window has to be visible before these coordinate conversions will work correctly (otherwise the UIScreen isn't attached to anything
// and blah blah blah...)
// finally, set the arrow position so it points to the right place and looks all purty.
if (_popoverArrowDirection != UIPopoverArrowDirectionUnknown) {
CGPoint screenPointTo = [view.window.screen convertPoint:NSPointToCGPoint(pointTo) fromScreen:nil];
CGPoint windowPointTo = [view.window convertPoint:screenPointTo fromWindow:nil];
CGPoint viewPointTo = [view convertPoint:windowPointTo fromView:nil];
[_popoverView pointTo:viewPointTo inView:view];
}
if (animated) {
_popoverView.transform = CGAffineTransformMakeScale(0.98f,0.98f);
_popoverView.alpha = 0.4f;
[UIView beginAnimations:@"Ploop" context:NULL];
[UIView setAnimationDuration:0.08];
_popoverView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
[UIView beginAnimations:@"Fade" context:NULL];
[UIView setAnimationDuration:0.1];
_popoverView.alpha = 1.f;
[UIView commitAnimations];
}
[_contentViewController viewDidAppear:animated];
}
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections animated:(BOOL)animated
{
}
- (BOOL)isPopoverVisible
{
return (_popoverView || _popoverWindow || _overlayWindow);
}
- (void)_destroyPopover
{
[_overlayWindow orderOut:nil];
[_popoverWindow orderOut:nil];
NSWindow *parentWindow = [_overlayWindow parentWindow];
[_overlayWindow removeChildWindow:_popoverWindow];
[parentWindow removeChildWindow:_overlayWindow];
[parentWindow makeKeyAndOrderFront:self];
[_popoverView release];
[_popoverWindow release];
[_overlayWindow release];
_popoverView = nil;
_popoverWindow = nil;
_overlayWindow = nil;
_popoverArrowDirection = UIPopoverArrowDirectionUnknown;
}
- (void)dismissPopoverAnimated:(BOOL)animated
{
if ([self isPopoverVisible]) {
if (animated) {
[UIView beginAnimations:@"dismissPopoverAnimated" context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(_destroyPopover)];
_popoverView.alpha = 0;
[UIView commitAnimations];
} else {
[self _destroyPopover];
}
}
}
- (void)_closePopoverWindowIfPossible
{
const BOOL shouldDismiss = _delegateHas.popoverControllerShouldDismissPopover? [_delegate popoverControllerShouldDismissPopover:self] : YES;
if (shouldDismiss) {
[self dismissPopoverAnimated:YES];
if (_delegateHas.popoverControllerDidDismissPopover) {
[_delegate popoverControllerDidDismissPopover:self];
}
}
}
+ (UIEdgeInsets)insetForArrows
{
return UIEdgeInsetsMake(17,12,8,12);
}
+ (CGRect)backgroundRectForBounds:(CGRect)bounds
{
return UIEdgeInsetsInsetRect(bounds, [self insetForArrows]);
}
+ (CGRect)contentRectForBounds:(CGRect)bounds withNavigationBar:(BOOL)hasNavBar
{
const CGFloat navBarOffset = hasNavBar? 32 : 0;
return UIEdgeInsetsInsetRect(CGRectMake(14,9+navBarOffset,bounds.size.width-28,bounds.size.height-28-navBarOffset), [self insetForArrows]);
}
+ (CGSize)frameSizeForContentSize:(CGSize)contentSize withNavigationBar:(BOOL)hasNavBar
{
UIEdgeInsets insets = [self insetForArrows];
CGSize frameSize;
frameSize.width = contentSize.width + 28 + insets.left + insets.right;
frameSize.height = contentSize.height + 28 + (hasNavBar? 32 : 0) + insets.top + insets.bottom;
return frameSize;
}
+ (UIImage *)backgroundImage {
return [UIImage _popoverBackgroundImage];
}
+ (UIImage *)leftArrowImage {
return [UIImage _leftPopoverArrowImage];
}
+ (UIImage *)rightArrowImage {
return [UIImage _rightPopoverArrowImage];
}
+ (UIImage *)topArrowImage {
return [UIImage _topPopoverArrowImage];
}
+ (UIImage *)bottomArrowImage {
return [UIImage _bottomPopoverArrowImage];
}
@end