OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/no_tabs/no_tabs_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #import "ios/chrome/browser/ui/background_generator.h" |
| 11 #import "ios/chrome/browser/ui/no_tabs/no_tabs_controller_testing.h" |
| 12 #import "ios/chrome/browser/ui/no_tabs/no_tabs_toolbar_controller.h" |
| 13 #import "ios/chrome/browser/ui/toolbar/toolbar_controller.h" |
| 14 #import "ios/chrome/browser/ui/tools_menu/tools_menu_context.h" |
| 15 #include "ios/chrome/browser/ui/ui_util.h" |
| 16 |
| 17 @interface NoTabsController (PrivateMethods) |
| 18 // Creates and installs the toolbar for the No-Tabs UI. |
| 19 - (void)installNoTabsToolbarInView:(UIView*)view; |
| 20 // Creates and installs the background view for the No-Tabs UI. |
| 21 - (void)installNoTabsBackgroundInView:(UIView*)view; |
| 22 // Returns the frame of a button's image view. |
| 23 - (CGRect)imageFrameForButton:(UIButton*)button inView:(UIView*)view; |
| 24 // Creates and installs the UIImageView that is used to animate the mode toggle |
| 25 // button. |
| 26 - (void)installNoTabsAnimationToggleImageForButton:(UIButton*)button |
| 27 inView:(UIView*)view; |
| 28 @end |
| 29 |
| 30 @implementation NoTabsController { |
| 31 // The No-Tabs toolbar. |
| 32 base::scoped_nsobject<NoTabsToolbarController> toolbarController_; |
| 33 |
| 34 // The parent view of the No-Tabs UI. Must outlive this object. |
| 35 UIView* parentView_; // weak |
| 36 |
| 37 // The background view behind the No-Tabs UI. |
| 38 base::scoped_nsobject<UIView> backgroundView_; |
| 39 |
| 40 // The image view used to animate the mode toggle button when entering or |
| 41 // leaving the No-Tabs UI. |
| 42 base::scoped_nsobject<UIImageView> modeToggleImageView_; |
| 43 |
| 44 // The ending frame for the mode toggle button animation. Equal to CGRectZero |
| 45 // if the mode toggle button is not being animated. |
| 46 CGRect toggleImageEndFrame_; |
| 47 |
| 48 base::mac::ObjCPropertyReleaser propertyReleaser_NoTabsController_; |
| 49 } |
| 50 |
| 51 // Designated initializer. |
| 52 - (id)initWithView:(UIView*)view { |
| 53 self = [super init]; |
| 54 if (self) { |
| 55 propertyReleaser_NoTabsController_.Init(self, [NoTabsController class]); |
| 56 |
| 57 parentView_ = view; |
| 58 [self installNoTabsToolbarInView:view]; |
| 59 [self installNoTabsBackgroundInView:view]; |
| 60 } |
| 61 return self; |
| 62 } |
| 63 |
| 64 // Passes the call through to the toolbar controller. |
| 65 - (void)setHasModeToggleSwitch:(BOOL)hasModeToggle { |
| 66 [toolbarController_ setHasModeToggleSwitch:hasModeToggle]; |
| 67 } |
| 68 |
| 69 // Prepares the given button for animation. |
| 70 - (void)installAnimationImageForButton:(UIButton*)button |
| 71 inView:(UIView*)view |
| 72 show:(BOOL)show { |
| 73 UIButton* fromButton = nil; |
| 74 UIButton* toButton = nil; |
| 75 |
| 76 if (show) { |
| 77 fromButton = button; |
| 78 toButton = [toolbarController_ modeToggleButton]; |
| 79 } else { |
| 80 fromButton = [toolbarController_ modeToggleButton]; |
| 81 toButton = button; |
| 82 } |
| 83 |
| 84 [self installNoTabsAnimationToggleImageForButton:fromButton inView:view]; |
| 85 toggleImageEndFrame_ = [self imageFrameForButton:toButton inView:view]; |
| 86 } |
| 87 |
| 88 - (void)showToolsMenuPopup { |
| 89 base::scoped_nsobject<ToolsMenuContext> context( |
| 90 [[ToolsMenuContext alloc] initWithDisplayView:parentView_]); |
| 91 [context setNoOpenedTabs:YES]; |
| 92 [toolbarController_ showToolsMenuPopupWithContext:context]; |
| 93 } |
| 94 |
| 95 - (void)dismissToolsMenuPopup { |
| 96 [toolbarController_ dismissToolsMenuPopup]; |
| 97 } |
| 98 |
| 99 - (void)prepareForShowAnimation { |
| 100 CGFloat toolbarHeight = CGRectGetHeight([[toolbarController_ view] frame]); |
| 101 |
| 102 // The toolbar icons start offscreen and animate in. |
| 103 [toolbarController_ |
| 104 setControlsTransform:CGAffineTransformMakeTranslation(0, -toolbarHeight)]; |
| 105 } |
| 106 |
| 107 - (void)showNoTabsUI { |
| 108 [toolbarController_ setControlsTransform:CGAffineTransformIdentity]; |
| 109 |
| 110 if (modeToggleImageView_.get()) { |
| 111 DCHECK(!CGRectEqualToRect(toggleImageEndFrame_, CGRectZero)); |
| 112 [modeToggleImageView_ setFrame:toggleImageEndFrame_]; |
| 113 toggleImageEndFrame_ = CGRectZero; |
| 114 } |
| 115 } |
| 116 |
| 117 - (void)showAnimationDidFinish { |
| 118 // When the animation is finished, remove the temporary toggle switch image. |
| 119 [modeToggleImageView_ removeFromSuperview]; |
| 120 modeToggleImageView_.reset(); |
| 121 } |
| 122 |
| 123 - (void)prepareForDismissAnimation { |
| 124 // Hide the toolbar mode toggle button so that it does not visibly animate |
| 125 // out. |
| 126 [[toolbarController_ modeToggleButton] setHidden:YES]; |
| 127 } |
| 128 |
| 129 - (void)dismissNoTabsUI { |
| 130 CGFloat toolbarHeight = [[toolbarController_ view] frame].size.height; |
| 131 [toolbarController_ |
| 132 setControlsTransform:CGAffineTransformMakeTranslation(0, -toolbarHeight)]; |
| 133 |
| 134 if (modeToggleImageView_.get()) { |
| 135 DCHECK(!CGRectEqualToRect(toggleImageEndFrame_, CGRectZero)); |
| 136 [modeToggleImageView_ setFrame:toggleImageEndFrame_]; |
| 137 toggleImageEndFrame_ = CGRectZero; |
| 138 } |
| 139 } |
| 140 |
| 141 - (void)dismissAnimationDidFinish { |
| 142 [modeToggleImageView_ removeFromSuperview]; |
| 143 modeToggleImageView_.reset(); |
| 144 [backgroundView_ removeFromSuperview]; |
| 145 backgroundView_.reset(); |
| 146 [[toolbarController_ view] removeFromSuperview]; |
| 147 toolbarController_.reset(); |
| 148 } |
| 149 |
| 150 // Creates and installs the toolbar for the No-Tabs UI. |
| 151 - (void)installNoTabsToolbarInView:(UIView*)view { |
| 152 toolbarController_.reset([[NoTabsToolbarController alloc] initWithNoTabs]); |
| 153 UIView* toolbarView = [toolbarController_ view]; |
| 154 CGFloat toolbarHeight = CGRectGetHeight(toolbarView.frame); |
| 155 |
| 156 CGRect toolbarFrame = [view bounds]; |
| 157 toolbarFrame.origin.y = StatusBarHeight(); |
| 158 toolbarFrame.size.height = toolbarHeight; |
| 159 toolbarView.frame = toolbarFrame; |
| 160 [view addSubview:toolbarView]; |
| 161 } |
| 162 |
| 163 // Creates and installs the background view for the No-Tabs UI. |
| 164 - (void)installNoTabsBackgroundInView:(UIView*)view { |
| 165 DCHECK(toolbarController_); |
| 166 UIView* toolbarView = [toolbarController_ view]; |
| 167 |
| 168 backgroundView_.reset([[UIView alloc] initWithFrame:view.bounds]); |
| 169 [backgroundView_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
| 170 UIViewAutoresizingFlexibleHeight)]; |
| 171 InstallBackgroundInView(backgroundView_); |
| 172 [view insertSubview:backgroundView_ belowSubview:toolbarView]; |
| 173 } |
| 174 |
| 175 // Returns the frame of a button's image view. |
| 176 - (CGRect)imageFrameForButton:(UIButton*)button inView:(UIView*)view { |
| 177 // Convert the existing button's bounds to the coordinate system of |view| and |
| 178 // trim away any padding present in the button. |
| 179 CGRect rect = UIEdgeInsetsInsetRect(button.bounds, button.imageEdgeInsets); |
| 180 return [view convertRect:rect fromView:button]; |
| 181 } |
| 182 |
| 183 // Installs the UIImageView that is used to animate the mode toggle switch while |
| 184 // the No-Tabs UI is shown or hidden. This image view is initially set up to |
| 185 // exactly cover the image of an existing button. |
| 186 - (void)installNoTabsAnimationToggleImageForButton:(UIButton*)button |
| 187 inView:(UIView*)view { |
| 188 UIImage* image = [button imageForState:UIControlStateNormal]; |
| 189 modeToggleImageView_.reset([[UIImageView alloc] initWithImage:image]); |
| 190 [modeToggleImageView_ |
| 191 setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | |
| 192 UIViewAutoresizingFlexibleTopMargin)]; |
| 193 [modeToggleImageView_ setFrame:[self imageFrameForButton:button inView:view]]; |
| 194 [view addSubview:modeToggleImageView_]; |
| 195 } |
| 196 |
| 197 @end |
| 198 |
| 199 #pragma mark - TestingAdditions |
| 200 |
| 201 @implementation NoTabsController (JustForTesting) |
| 202 - (UIButton*)modeToggleButton { |
| 203 return [toolbarController_ modeToggleButton]; |
| 204 } |
| 205 |
| 206 - (UIView*)toolbarView { |
| 207 return [toolbarController_ view]; |
| 208 } |
| 209 |
| 210 - (UIView*)backgroundView { |
| 211 return backgroundView_.get(); |
| 212 } |
| 213 @end |
OLD | NEW |