OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/clean/chrome/browser/ui/presenters/menu_presentation_controller.h" | 5 #import "ios/clean/chrome/browser/ui/presenters/menu_presentation_controller.h" |
6 | 6 |
7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
8 | 8 |
9 #include "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h" | 9 #include "ios/clean/chrome/browser/ui/commands/tools_menu_commands.h" |
10 #include "ios/clean/chrome/browser/ui/presenters/menu_presentation_delegate.h" | 10 #include "ios/clean/chrome/browser/ui/presenters/menu_presentation_delegate.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 @synthesize presentationFrame = _presentationFrame; | 24 @synthesize presentationFrame = _presentationFrame; |
25 @synthesize dispatcher = _dispatcher; | 25 @synthesize dispatcher = _dispatcher; |
26 @synthesize dismissRecognizer = _dismissRecognizer; | 26 @synthesize dismissRecognizer = _dismissRecognizer; |
27 | 27 |
28 #pragma mark - UIPresentationDelegate | 28 #pragma mark - UIPresentationDelegate |
29 | 29 |
30 - (CGRect)frameOfPresentedViewInContainerView { | 30 - (CGRect)frameOfPresentedViewInContainerView { |
31 if (CGRectIsEmpty(self.presentationFrame)) { | 31 if (CGRectIsEmpty(self.presentationFrame)) { |
32 [self updatePresentationDelegate]; | 32 [self updatePresentationDelegate]; |
33 if (self.presentationDelegate) { | 33 if (self.presentationDelegate) { |
34 self.presentationFrame = | 34 self.presentationFrame = [self |
35 [self.presentationDelegate frameForMenuPresentation:self]; | 35 frameForPresentationWithSize:self.presentedView.frame.size |
| 36 origin:[self.presentationDelegate |
| 37 originForMenuPresentation] |
| 38 bounds:[self.presentationDelegate |
| 39 boundsForMenuPresentation]]; |
36 } else { | 40 } else { |
37 // Placeholder default frame: centered in the presenting view. | 41 // Placeholder default frame: centered in the presenting view. |
38 CGSize menuSize = self.presentedView.frame.size; | 42 CGSize menuSize = self.presentedView.frame.size; |
39 self.presentationFrame.size = menuSize; | 43 self.presentationFrame.size = menuSize; |
40 self.presentationFrame.origin = CGPointMake( | 44 self.presentationFrame.origin = CGPointMake( |
41 (self.containerView.bounds.size.width - menuSize.width) / 2.0, | 45 (self.containerView.bounds.size.width - menuSize.width) / 2.0, |
42 (self.containerView.bounds.size.height - menuSize.height) / 2.0); | 46 (self.containerView.bounds.size.height - menuSize.height) / 2.0); |
43 } | 47 } |
44 } | 48 } |
45 return self.presentationFrame; | 49 return self.presentationFrame; |
(...skipping 22 matching lines...) Expand all Loading... |
68 // presentation delegate. This can't be done at init time, becuase the | 72 // presentation delegate. This can't be done at init time, becuase the |
69 // presenting view controller may not have been determined by UIKit yet. | 73 // presenting view controller may not have been determined by UIKit yet. |
70 - (void)updatePresentationDelegate { | 74 - (void)updatePresentationDelegate { |
71 if ([self.presentingViewController | 75 if ([self.presentingViewController |
72 conformsToProtocol:@protocol(MenuPresentationDelegate)]) { | 76 conformsToProtocol:@protocol(MenuPresentationDelegate)]) { |
73 self.presentationDelegate = static_cast<id<MenuPresentationDelegate>>( | 77 self.presentationDelegate = static_cast<id<MenuPresentationDelegate>>( |
74 self.presentingViewController); | 78 self.presentingViewController); |
75 } | 79 } |
76 } | 80 } |
77 | 81 |
| 82 - (CGRect)frameForPresentationWithSize:(CGSize)menuSize |
| 83 origin:(CGRect)menuOriginRect |
| 84 bounds:(CGRect)presentationBounds { |
| 85 CGRect menuRect; |
| 86 menuRect.size = menuSize; |
| 87 |
| 88 if (CGRectIsNull(menuOriginRect)) { |
| 89 menuRect.origin = CGPointMake(50, 50); |
| 90 return menuRect; |
| 91 } |
| 92 // Calculate which corner of the menu the origin rect is in. This is |
| 93 // determined by comparing frames, and thus is RTL-independent. |
| 94 if (CGRectGetMinX(menuOriginRect) - CGRectGetMinX(presentationBounds) < |
| 95 CGRectGetMaxX(presentationBounds) - CGRectGetMaxX(menuOriginRect)) { |
| 96 // Origin rect is closer to the left edge of |self.view| than to the right. |
| 97 menuRect.origin.x = CGRectGetMinX(menuOriginRect); |
| 98 } else { |
| 99 // Origin rect is closer to the right edge of |self.view| than to the left. |
| 100 menuRect.origin.x = CGRectGetMaxX(menuOriginRect) - menuSize.width; |
| 101 } |
| 102 |
| 103 if (CGRectGetMinY(menuOriginRect) - CGRectGetMinY(presentationBounds) < |
| 104 CGRectGetMaxY(presentationBounds) - CGRectGetMaxY(menuOriginRect)) { |
| 105 // Origin rect is closer to the top edge of |self.view| than to the bottom. |
| 106 menuRect.origin.y = CGRectGetMinY(menuOriginRect); |
| 107 } else { |
| 108 // Origin rect is closer to the bottom edge of |self.view| than to the top. |
| 109 menuRect.origin.y = CGRectGetMaxY(menuOriginRect) - menuSize.height; |
| 110 } |
| 111 |
| 112 return menuRect; |
| 113 } |
| 114 |
78 @end | 115 @end |
OLD | NEW |