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