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 #ifndef IOS_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONTROLLER_H_ |
| 6 #define IOS_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONTROLLER_H_ |
| 7 |
| 8 #import <UIKit/UIKit.h> |
| 9 |
| 10 #include <stdint.h> |
| 11 |
| 12 #include <map> |
| 13 |
| 14 #include "base/ios/weak_nsobject.h" |
| 15 #import "base/mac/scoped_nsobject.h" |
| 16 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 17 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" |
| 18 #include "ios/chrome/browser/ui/ui_util.h" |
| 19 #import "ios/chrome/browser/ui/util/relaxed_bounds_constraints_hittest.h" |
| 20 |
| 21 class ReadingListModel; |
| 22 @class ToolsMenuContext; |
| 23 |
| 24 // The time delay before non-initial button images are loaded. |
| 25 extern const int64_t kNonInitialImageAdditionDelayNanosec; |
| 26 // Notification when the tools menu is opened. |
| 27 extern NSString* const kMenuWillShowNotification; |
| 28 // Notification when the tools menu is closed. |
| 29 extern NSString* const kMenuWillHideNotification; |
| 30 // Accessibility identifier of the toolbar view (for use by integration tests). |
| 31 extern NSString* const kToolbarIdentifier; |
| 32 // Accessibility identifier of the incognito toolbar view (for use by |
| 33 // integration tests). |
| 34 extern NSString* const kIncognitoToolbarIdentifier; |
| 35 // Accessibility identifier of the tools menu button (for use by integration |
| 36 // tests). |
| 37 extern NSString* const kToolbarToolsMenuButtonIdentifier; |
| 38 // Accessibility identifier of the stack button (for use by integration |
| 39 // tests). |
| 40 extern NSString* const kToolbarStackButtonIdentifier; |
| 41 // The maximum number to display in the tab switcher button. |
| 42 extern NSInteger const kStackButtonMaxTabCount; |
| 43 |
| 44 // Toolbar style. Determines which button images are used. |
| 45 enum ToolbarControllerStyle { |
| 46 ToolbarControllerStyleLightMode = 0, |
| 47 ToolbarControllerStyleDarkMode, |
| 48 ToolbarControllerStyleIncognitoMode, |
| 49 ToolbarControllerStyleMaxStyles |
| 50 }; |
| 51 |
| 52 enum ToolbarButtonMode { |
| 53 ToolbarButtonModeNormal, |
| 54 ToolbarButtonModeReversed, |
| 55 }; |
| 56 |
| 57 enum ToolbarButtonUIState { |
| 58 ToolbarButtonUIStateNormal = 0, |
| 59 ToolbarButtonUIStatePressed, |
| 60 ToolbarButtonUIStateDisabled, |
| 61 NumberOfToolbarButtonUIStates, |
| 62 }; |
| 63 |
| 64 // This enumerates the different buttons used by the toolbar and is used to map |
| 65 // the resource IDs for the button's icons. Subclasses with additional buttons |
| 66 // should extend these values. The first new enum should be set to |
| 67 // |NumberOfToolbarButtonNames|. Note that functions that use these values use |
| 68 // an int rather than the |ToolbarButtonName| to accommodate additional values. |
| 69 // Also, if this enum is extended by a subclass, the subclass must also override |
| 70 // -imageIdForImageEnum:style:forState: to provide mapping from enum to resource |
| 71 // ID for the various states. |
| 72 enum ToolbarButtonName { |
| 73 ToolbarButtonNameStack = 0, |
| 74 ToolbarButtonNameShare, |
| 75 NumberOfToolbarButtonNames, |
| 76 }; |
| 77 |
| 78 // Style used to specify the direction of the toolbar transition animations. |
| 79 typedef enum { |
| 80 TOOLBAR_TRANSITION_STYLE_TO_STACK_VIEW, |
| 81 TOOLBAR_TRANSITION_STYLE_TO_BVC |
| 82 } ToolbarTransitionStyle; |
| 83 |
| 84 // Toolbar frames shared with subclasses. |
| 85 extern const CGRect kToolbarFrame[INTERFACE_IDIOM_COUNT]; |
| 86 |
| 87 // Create a thin wrapper around UIImageView to catch frame change and window |
| 88 // events. |
| 89 @protocol ToolbarFrameDelegate |
| 90 - (void)frameDidChangeFrame:(CGRect)newFrame fromFrame:(CGRect)oldFrame; |
| 91 - (void)windowDidChange; |
| 92 @end |
| 93 |
| 94 @interface ToolbarView : UIImageView<RelaxedBoundsConstraintsHitTestSupport> { |
| 95 base::WeakNSProtocol<id<ToolbarFrameDelegate>> delegate_; |
| 96 } |
| 97 - (void)setDelegate:(id<ToolbarFrameDelegate>)delegate; |
| 98 // Records whether or not the toolbar is currently involved in a transition |
| 99 // animation. |
| 100 @property(nonatomic, assign, getter=isAnimatingTransition) |
| 101 BOOL animatingTransition; |
| 102 @end |
| 103 |
| 104 // Base class for a toolbar, containing the standard button set that is common |
| 105 // across different types of toolbars and action handlers for those buttons |
| 106 // (forwarding to the delegate). |
| 107 // This is not intended to be used on its own, but to be subclassed by more |
| 108 // specific toolbars that provide more buttons in the empty space. |
| 109 @interface ToolbarController : NSObject<PopupMenuDelegate> |
| 110 |
| 111 // The top-level toolbar view. It is a |UIImageView| even though it does not |
| 112 // hold any image for testability: unlike |UIView|, a |UIImageView| that is |
| 113 // visible in the UI automation view hierarchy does not prevent its subviews |
| 114 // from also being visible. |
| 115 // TODO(blundell): Figure out how to fix this and have the top-level view be a |
| 116 // UIView. b/6167700 |
| 117 @property(nonatomic, readonly, retain) ToolbarView* view; |
| 118 // The view for the toolbar background image. This is a subview of |view| to |
| 119 // allow clients to alter the transparency of the background image without |
| 120 // affecting the other components of the toolbar. |
| 121 @property(nonatomic, readonly, retain) UIImageView* backgroundView; |
| 122 // The view for the toolbar shadow image. This is a subview of |view| to allow |
| 123 // clients to alter the visibility of the shadow without affecting other |
| 124 // components of the toolbar. |
| 125 @property(nonatomic, readonly, retain) UIImageView* shadowView; |
| 126 |
| 127 // The tools popup controller. Nil if the tools popup menu is not visible. |
| 128 @property(nonatomic, readonly, retain) |
| 129 ToolsPopupController* toolsPopupController; |
| 130 |
| 131 // Style of this toolbar. |
| 132 @property(nonatomic, readonly, assign) ToolbarControllerStyle style; |
| 133 |
| 134 // The reading list model reflected by the toolbar. |
| 135 @property(nonatomic, readwrite, assign) ReadingListModel* readingListModel; |
| 136 |
| 137 // Designated initializer. |style| determines how the toolbar draws itself. |
| 138 - (instancetype)initWithStyle:(ToolbarControllerStyle)style |
| 139 NS_DESIGNATED_INITIALIZER; |
| 140 |
| 141 - (instancetype)init NS_UNAVAILABLE; |
| 142 |
| 143 // Called when the application has entered the background. |
| 144 - (void)applicationDidEnterBackground:(NSNotification*)notify; |
| 145 |
| 146 // Sets whether the share button is enabled or not. |
| 147 - (void)setShareButtonEnabled:(BOOL)enabled; |
| 148 |
| 149 // Sets up |button| with images named by the given |imageEnum| and the current |
| 150 // toolbar style. Sets images synchronously for |initialState|, and |
| 151 // asynchronously for the other states. Optionally sets the image for the |
| 152 // disabled state as well. Meant to be called during initialization. |
| 153 // Note: |withImageEnum| should be one of the ToolbarButtonName values, or an |
| 154 // extended value provided by a subclass. It is an int to support "subclassing" |
| 155 // of the enum and overriding helper functions. |
| 156 - (void)setUpButton:(UIButton*)button |
| 157 withImageEnum:(int)imageEnum |
| 158 forInitialState:(UIControlState)initialState |
| 159 hasDisabledImage:(BOOL)hasDisabledImage |
| 160 synchronously:(BOOL)synchronously; |
| 161 |
| 162 // TRUE if |imageEnum| should be flipped when in RTL layout. |
| 163 // Currently none of this class' images have this property, but subclasses |
| 164 // can override this method if they need to flip some of their images. |
| 165 - (BOOL)imageShouldFlipForRightToLeftLayoutDirection:(int)imageEnum; |
| 166 |
| 167 // Shows the tools popup menu. |
| 168 - (void)showToolsMenuPopupWithContext:(ToolsMenuContext*)context; |
| 169 |
| 170 // If |toolsPopupController_| is non-nil, dismisses the tools popup menu with |
| 171 // animation. |
| 172 - (void)dismissToolsMenuPopup; |
| 173 |
| 174 // Returns the bound of the share button. Used to position the share menu. |
| 175 - (CGRect)shareButtonAnchorRect; |
| 176 |
| 177 // Returns the share button's view. Used to position the share menu. |
| 178 - (UIView*)shareButtonView; |
| 179 |
| 180 // Sets the background to a particular alpha value. Intended for use by |
| 181 // subcleasses that need to set the opacity of the entire toolbar. |
| 182 - (void)setBackgroundAlpha:(CGFloat)alpha; |
| 183 |
| 184 // Called whenever one of the standard controls is triggered. Does nothing, but |
| 185 // can be overridden by subclasses to clear any state (e.g., close menus). |
| 186 - (void)standardButtonPressed:(UIButton*)sender; |
| 187 |
| 188 // Updates the tab stack button (if there is one) based on the given tab count. |
| 189 // If |tabCount| > |kStackButtonMaxTabCount|, an easter egg is shown instead of |
| 190 // the actual number of tabs. |
| 191 - (void)setTabCount:(NSInteger)tabCount; |
| 192 |
| 193 // Called when buttons are pressed. Records action metrics. |
| 194 // Subclasses must call |super| if they override this method. |
| 195 - (IBAction)recordUserMetrics:(id)sender; |
| 196 |
| 197 // Called when a touch down is registered on the stack view button. |
| 198 - (IBAction)stackButtonTouchDown:(id)sender; |
| 199 |
| 200 // Height of the toolbar's drop shadow. This drop shadow is drawn by the |
| 201 // toolbar and included in the toolbar's height, so it must be subtracted away |
| 202 // when positioning the web content area. |
| 203 + (CGFloat)toolbarDropShadowHeight; |
| 204 |
| 205 // Height and Y offset to account for the status bar. Overridden by subclasses |
| 206 // if the toolbar shouldn't extend through the status bar. |
| 207 - (CGFloat)statusBarOffset; |
| 208 |
| 209 - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection; |
| 210 |
| 211 @end |
| 212 |
| 213 @interface ToolbarController (ProtectedMethods) |
| 214 |
| 215 // Returns the area that subclasses should use for adding their own controls. |
| 216 // Nothing should be added outside this frame without first calling |
| 217 // |setStandardControlsVisible:NO|. |
| 218 - (CGRect)specificControlsArea; |
| 219 |
| 220 // Sets the standard control set to be visible or invisible. Uses alpha, so it |
| 221 // can be animated if called from an animation context. Intended for use by |
| 222 // subclasses that need to temporarily take over the entire toolbar. |
| 223 - (void)setStandardControlsVisible:(BOOL)visible; |
| 224 |
| 225 // Sets the standard control to a particular alpha value. Intended for use by |
| 226 // subclasses that need to temporarily take over the entire toolbar. |
| 227 - (void)setStandardControlsAlpha:(CGFloat)alpha; |
| 228 |
| 229 // Sets the transform that is applied to the standard controls. Can be |
| 230 // animated if called from an animation context. Intended for use by subclasses |
| 231 // that need to apply a transform to all toolbar buttons. |
| 232 - (void)setStandardControlsTransform:(CGAffineTransform)transform; |
| 233 |
| 234 // Returns the UIImage from the resources bundle for the |imageEnum| and |
| 235 // |state|. Uses the toolbar's current style. |
| 236 - (UIImage*)imageForImageEnum:(int)imageEnum |
| 237 forState:(ToolbarButtonUIState)state; |
| 238 |
| 239 // Returns the image enum for a given button object. If the user taps a button |
| 240 // before its secondary images have been loaded, the image(s) will be loaded |
| 241 // then, synchronously. Called by -standardButtonPressed. |
| 242 - (int)imageEnumForButton:(UIButton*)button; |
| 243 |
| 244 // Returns the resource ID for the image with enum |index|, corresponding to |
| 245 // |style| and |state|. Returns 0 if there is not a corresponding ID. |
| 246 // If a subclass extends the enum ToolbarButtonName, it must also override this |
| 247 // method to provide the correct mapping from enum to resource ID. |
| 248 - (int)imageIdForImageEnum:(int)index |
| 249 style:(ToolbarControllerStyle)style |
| 250 forState:(ToolbarButtonUIState)state; |
| 251 |
| 252 // Creates a hash of the UI state of the toolbar. |
| 253 - (uint32_t)snapshotHash; |
| 254 |
| 255 // Animates the tools menu button and stack button for full bleed omnibox |
| 256 // animation used for Material. |
| 257 - (void)animateStandardControlsForOmniboxExpansion:(BOOL)growOmnibox; |
| 258 |
| 259 // Add position and opacity animations to |view|'s layer. The opacity animation |
| 260 // goes from 0 to 1. The position animation goes from [view.layer.position |
| 261 // offset in the leading direction by |leadingOffset|) to view.layer.position. |
| 262 // Both animations occur after |delay| seconds. |
| 263 - (void)fadeInView:(UIView*)view |
| 264 fromLeadingOffset:(LayoutOffset)leadingOffset |
| 265 withDuration:(NSTimeInterval)duration |
| 266 afterDelay:(NSTimeInterval)delay; |
| 267 |
| 268 // Performs the transition animation specified by |style|, animating the toolbar |
| 269 // view from |beginFrame| to |endFrame|. |
| 270 // Animations are added to subview depending on |style|: |
| 271 // - ToolbarTransitionStyleToStackView: faded out immediately |
| 272 // - ToolbarTransitionStyleToBVC: fade in from a vertical offset after a delay |
| 273 - (void)animateTransitionWithBeginFrame:(CGRect)beginFrame |
| 274 endFrame:(CGRect)endFrame |
| 275 transitionStyle:(ToolbarTransitionStyle)style; |
| 276 |
| 277 // Reverses transition animations that are cancelled before they can finish. |
| 278 - (void)reverseTransitionAnimations; |
| 279 |
| 280 // Called when transition animations can be removed. |
| 281 - (void)cleanUpTransitionAnimations; |
| 282 |
| 283 // Shows/hides iPhone toolbar views for when the new tab page is displayed. |
| 284 - (void)hideViewsForNewTabPage:(BOOL)hide; |
| 285 |
| 286 @end |
| 287 |
| 288 #endif // IOS_CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_CONTROLLER_H_ |
OLD | NEW |