1 // Copyright (c) 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 CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_ 7 8 #import <Cocoa/Cocoa.h> 9 10 #include "base/mac/scoped_nsobject.h" 11 #include "base/memory/scoped_ptr.h" 12 #import "chrome/browser/ui/cocoa/tabs/tab_controller_target.h" 13 #import "chrome/browser/ui/cocoa/url_drop_target.h" 14 #include "chrome/browser/ui/tabs/hover_tab_selector.h" 15 16 @class CrTrackingArea; 17 @class NewTabButton; 18 @class TabContentsController; 19 @class TabView; 20 @class TabStripDragController; 21 @class TabStripView; 22 23 class Browser; 24 class TabStripModelObserverBridge; 25 class TabStripModel; 26 27 namespace content { 28 class WebContents; 29 } 30 31 // The interface for the tab strip controller's delegate. 32 // Delegating TabStripModelObserverBridge's events (in lieu of directly 33 // subscribing to TabStripModelObserverBridge events, as TabStripController 34 // does) is necessary to guarantee a proper order of subviews layout updates, 35 // otherwise it might trigger unnesessary content relayout, UI flickering etc. 36 @protocol TabStripControllerDelegate 37 38 // Stripped down version of TabStripModelObserverBridge:selectTabWithContents. 39 - (void)onActivateTabWithContents:(content::WebContents*)contents; 40 41 // Stripped down version of TabStripModelObserverBridge:tabChangedWithContents. 42 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change 43 withContents:(content::WebContents*)contents; 44 45 // Stripped down version of TabStripModelObserverBridge:tabDetachedWithContents. 46 - (void)onTabDetachedWithContents:(content::WebContents*)contents; 47 48 @end 49 50 // A class that handles managing the tab strip in a browser window. It uses 51 // a supporting C++ bridge object to register for notifications from the 52 // TabStripModel. The Obj-C part of this class handles drag and drop and all 53 // the other Cocoa-y aspects. 54 // 55 // For a full description of the design, see 56 // http://www.chromium.org/developers/design-documents/tab-strip-mac 57 @interface TabStripController : 58 NSObject<TabControllerTarget, 59 URLDropTargetController> { 60 @private 61 base::scoped_nsobject<TabStripView> tabStripView_; 62 NSView* switchView_; // weak 63 base::scoped_nsobject<NSView> dragBlockingView_; // avoid bad window server 64 // drags 65 NewTabButton* newTabButton_; // weak, obtained from the nib. 66 67 // The controller that manages all the interactions of dragging tabs. 68 base::scoped_nsobject<TabStripDragController> dragController_; 69 70 // Tracks the newTabButton_ for rollovers. 71 base::scoped_nsobject<CrTrackingArea> newTabTrackingArea_; 72 scoped_ptr<TabStripModelObserverBridge> bridge_; 73 Browser* browser_; // weak 74 TabStripModel* tabStripModel_; // weak 75 // Delegate that is informed about tab state changes. 76 id<TabStripControllerDelegate> delegate_; // weak 77 78 // YES if the new tab button is currently displaying the hover image (if the 79 // mouse is currently over the button). 80 BOOL newTabButtonShowingHoverImage_; 81 82 // Access to the TabContentsControllers (which own the parent view 83 // for the toolbar and associated tab contents) given an index. Call 84 // |indexFromModelIndex:| to convert a |tabStripModel_| index to a 85 // |tabContentsArray_| index. Do NOT assume that the indices of 86 // |tabStripModel_| and this array are identical, this is e.g. not true while 87 // tabs are animating closed (closed tabs are removed from |tabStripModel_| 88 // immediately, but from |tabContentsArray_| only after their close animation 89 // has completed). 90 base::scoped_nsobject<NSMutableArray> tabContentsArray_; 91 // An array of TabControllers which manage the actual tab views. See note 92 // above |tabContentsArray_|. |tabContentsArray_| and |tabArray_| always 93 // contain objects belonging to the same tabs at the same indices. 94 base::scoped_nsobject<NSMutableArray> tabArray_; 95 96 // Set of TabControllers that are currently animating closed. 97 base::scoped_nsobject<NSMutableSet> closingControllers_; 98 99 // These values are only used during a drag, and override tab positioning. 100 TabView* placeholderTab_; // weak. Tab being dragged 101 NSRect placeholderFrame_; // Frame to use 102 NSRect droppedTabFrame_; // Initial frame of a dropped tab, for animation. 103 // Frame targets for all the current views. 104 // target frames are used because repeated requests to [NSView animator]. 105 // aren't coalesced, so we store frames to avoid redundant calls. 106 base::scoped_nsobject<NSMutableDictionary> targetFrames_; 107 NSRect newTabTargetFrame_; 108 // If YES, do not show the new tab button during layout. 109 BOOL forceNewTabButtonHidden_; 110 // YES if we've successfully completed the initial layout. When this is 111 // NO, we probably don't want to do any animation because we're just coming 112 // into being. 113 BOOL initialLayoutComplete_; 114 115 // Width available for resizing the tabs (doesn't include the new tab 116 // button). Used to restrict the available width when closing many tabs at 117 // once to prevent them from resizing to fit the full width. If the entire 118 // width should be used, this will have a value of |kUseFullAvailableWidth|. 119 float availableResizeWidth_; 120 // A tracking area that's the size of the tab strip used to be notified 121 // when the mouse moves in the tab strip 122 base::scoped_nsobject<CrTrackingArea> trackingArea_; 123 TabView* hoveredTab_; // weak. Tab that the mouse is hovering over 124 125 // Array of subviews which are permanent (and which should never be removed), 126 // such as the new-tab button, but *not* the tabs themselves. 127 base::scoped_nsobject<NSMutableArray> permanentSubviews_; 128 129 // The default favicon, so we can use one copy for all buttons. 130 base::scoped_nsobject<NSImage> defaultFavicon_; 131 132 // The amount by which to indent the tabs on the sides (to make room for the 133 // red/yellow/green and incognito/fullscreen buttons). 134 CGFloat leftIndentForControls_; 135 CGFloat rightIndentForControls_; 136 137 // Is the mouse currently inside the strip; 138 BOOL mouseInside_; 139 140 // Helper for performing tab selection as a result of dragging over a tab. 141 scoped_ptr<HoverTabSelector> hoverTabSelector_; 142 143 // A container view for the window controls, which must be manually added in 144 // fullscreen in 10.10+. 145 base::scoped_nsobject<NSView> fullscreenWindowControls_; 146 } 147 148 @property(nonatomic) CGFloat leftIndentForControls; 149 @property(nonatomic) CGFloat rightIndentForControls; 150 151 // Initialize the controller with a view and browser that contains 152 // everything else we'll need. |switchView| is the view whose contents get 153 // "switched" every time the user switches tabs. The children of this view 154 // will be released, so if you want them to stay around, make sure 155 // you have retained them. 156 // |delegate| is the one listening to filtered TabStripModelObserverBridge's 157 // events (see TabStripControllerDelegate for more details). 158 - (id)initWithView:(TabStripView*)view 159 switchView:(NSView*)switchView 160 browser:(Browser*)browser 161 delegate:(id<TabStripControllerDelegate>)delegate; 162 163 // Returns the model behind this controller. 164 - (TabStripModel*)tabStripModel; 165 166 // Returns all tab views. 167 - (NSArray*)tabViews; 168 169 // Return the view for the currently active tab. 170 - (NSView*)activeTabView; 171 172 // Find the model index based on the x coordinate of the placeholder. If there 173 // is no placeholder, this returns the end of the tab strip. Closing tabs are 174 // not considered in computing the index. 175 - (int)indexOfPlaceholder; 176 177 // Set the frame of |tabView|, also updates the internal frame dict. 178 - (void)setFrame:(NSRect)frame ofTabView:(NSView*)tabView; 179 180 // Move the given tab at index |from| in this window to the location of the 181 // current placeholder. 182 - (void)moveTabFromIndex:(NSInteger)from; 183 184 // Drop a given WebContents at |modelIndex|. Used when dragging from 185 // another window when we don't have access to the WebContents as part of our 186 // strip. |frame| is in the coordinate system of the tab strip view and 187 // represents where the user dropped the new tab so it can be animated into its 188 // correct location when the tab is added to the model. If the tab was pinned in 189 // its previous window, setting |pinned| to YES will propagate that state to the 190 // new window. Mini-tabs are either app or pinned tabs; the app state is stored 191 // by the |contents|, but the |pinned| state is the caller's responsibility. 192 // Setting |activate| to YES will make the new tab active. 193 - (void)dropWebContents:(content::WebContents*)contents 194 atIndex:(int)modelIndex 195 withFrame:(NSRect)frame 196 asPinnedTab:(BOOL)pinned 197 activate:(BOOL)activate; 198 199 // Returns the index of the subview |view|. Returns -1 if not present. Takes 200 // closing tabs into account such that this index will correctly match the tab 201 // model. If |view| is in the process of closing, returns -1, as closing tabs 202 // are no longer in the model. 203 - (NSInteger)modelIndexForTabView:(NSView*)view; 204 205 // Returns all selected tab views. 206 - (NSArray*)selectedViews; 207 208 // Return the view at a given index. 209 - (NSView*)viewAtIndex:(NSUInteger)index; 210 211 // Return the number of tab views in the tab strip. It's same as number of tabs 212 // in the model, except when a tab is closing, which will be counted in views 213 // count, but no longer in the model. 214 - (NSUInteger)viewsCount; 215 216 // Set the placeholder for a dragged tab, allowing the |frame| to be specified. 217 // This causes this tab to be rendered in an arbitrary position. 218 - (void)insertPlaceholderForTab:(TabView*)tab frame:(NSRect)frame; 219 220 // Returns whether a tab is being dragged within the tab strip. 221 - (BOOL)isDragSessionActive; 222 223 // Returns whether or not |tab| can still be fully seen in the tab strip or if 224 // its current position would cause it be obscured by things such as the edge 225 // of the window or the window decorations. Returns YES only if the entire tab 226 // is visible. 227 - (BOOL)isTabFullyVisible:(TabView*)tab; 228 229 // Show or hide the new tab button. The button is hidden immediately, but 230 // waits until the next call to |-layoutTabs| to show it again. 231 - (void)showNewTabButton:(BOOL)show; 232 233 // Force the tabs to rearrange themselves to reflect the current model. 234 - (void)layoutTabs; 235 - (void)layoutTabsWithoutAnimation; 236 237 // Are we in rapid (tab) closure mode? I.e., is a full layout deferred (while 238 // the user closes tabs)? Needed to overcome missing clicks during rapid tab 239 // closure. 240 - (BOOL)inRapidClosureMode; 241 242 // Returns YES if the user is allowed to drag tabs on the strip at this moment. 243 // For example, this returns NO if there are any pending tab close animtations. 244 - (BOOL)tabDraggingAllowed; 245 246 // Default height for tabs. 247 + (CGFloat)defaultTabHeight; 248 249 // Default indentation for tabs (see |leftIndentForControls_|). 250 + (CGFloat)defaultLeftIndentForControls; 251 252 // Returns the currently active TabContentsController. 253 - (TabContentsController*)activeTabContentsController; 254 255 // Adds traffic lights to the tab strip. Idempotent. 256 - (void)addWindowControls; 257 258 // Removes traffic lights from the tab strip. Idempotent. 259 - (void)removeWindowControls; 260 261 @end 262 263 @interface TabStripController(TestingAPI) 264 - (void)setTabTitle:(TabController*)tab 265 withContents:(content::WebContents*)contents; 266 @end 267 268 // Returns the parent view to use when showing a sheet for a given web contents. 269 NSView* GetSheetParentViewForWebContents(content::WebContents* web_contents); 270 271 // Returns the bounds to use when showing a sheet for a given parent view. This 272 // returns a rect in window coordinates. 273 NSRect GetSheetParentBoundsForParentView(NSView* view); 274 275 #endif // CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_ 276