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/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"
16 @
class CrTrackingArea
;
18 @
class TabContentsController
;
20 @
class TabStripDragController
;
24 class TabStripModelObserverBridge
;
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
38 // Stripped down version of TabStripModelObserverBridge:selectTabWithContents.
39 - (void)onActivateTabWithContents
:(content::WebContents
*)contents
;
41 // Stripped down version of TabStripModelObserverBridge:tabChangedWithContents.
42 - (void)onTabChanged
:(TabStripModelObserver::TabChangeType
)change
43 withContents
:(content::WebContents
*)contents
;
45 // Stripped down version of TabStripModelObserverBridge:tabDetachedWithContents.
46 - (void)onTabDetachedWithContents
:(content::WebContents
*)contents
;
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.
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
> {
61 base::scoped_nsobject
<TabStripView
> tabStripView_
;
62 NSView
* switchView_
; // weak
63 base::scoped_nsobject
<NSView
> dragBlockingView_
; // avoid bad window server
65 NewTabButton
* newTabButton_
; // weak, obtained from the nib.
67 // The controller that manages all the interactions of dragging tabs.
68 base::scoped_nsobject
<TabStripDragController
> dragController_
;
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
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_
;
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
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_
;
96 // Set of TabControllers that are currently animating closed.
97 base::scoped_nsobject
<NSMutableSet
> closingControllers_
;
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
113 BOOL initialLayoutComplete_
;
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
125 // A transparent subview of |tabStripView_| used to show the hovered tab's
127 base::scoped_nsobject
<NSView
> toolTipView_
;
129 // Array of subviews which are permanent (and which should never be removed),
130 // such as the new-tab button, but *not* the tabs themselves.
131 base::scoped_nsobject
<NSMutableArray
> permanentSubviews_
;
133 // The default favicon, so we can use one copy for all buttons.
134 base::scoped_nsobject
<NSImage
> defaultFavicon_
;
136 // The amount by which to indent the tabs on the sides (to make room for the
137 // red/yellow/green and incognito/fullscreen buttons).
138 CGFloat leftIndentForControls_
;
139 CGFloat rightIndentForControls_
;
141 // Is the mouse currently inside the strip;
144 // Helper for performing tab selection as a result of dragging over a tab.
145 scoped_ptr
<HoverTabSelector
> hoverTabSelector_
;
147 // A container view for custom traffic light buttons, which must be manually
148 // added in fullscreen in 10.10+.
149 base::scoped_nsobject
<NSView
> customWindowControls_
;
152 @
property(nonatomic
) CGFloat leftIndentForControls
;
153 @
property(nonatomic
) CGFloat rightIndentForControls
;
155 @
property(assign
, nonatomic
) TabView
* hoveredTab
;
157 // Initialize the controller with a view and browser that contains
158 // everything else we'll need. |switchView| is the view whose contents get
159 // "switched" every time the user switches tabs. The children of this view
160 // will be released, so if you want them to stay around, make sure
161 // you have retained them.
162 // |delegate| is the one listening to filtered TabStripModelObserverBridge's
163 // events (see TabStripControllerDelegate for more details).
164 - (id
)initWithView
:(TabStripView
*)view
165 switchView
:(NSView
*)switchView
166 browser
:(Browser
*)browser
167 delegate
:(id
<TabStripControllerDelegate
>)delegate
;
169 // Returns the model behind this controller.
170 - (TabStripModel
*)tabStripModel
;
172 // Returns all tab views.
173 - (NSArray
*)tabViews
;
175 // Return the view for the currently active tab.
176 - (NSView
*)activeTabView
;
178 // Find the model index based on the x coordinate of the placeholder. If there
179 // is no placeholder, this returns the end of the tab strip. Closing tabs are
180 // not considered in computing the index.
181 - (int)indexOfPlaceholder
;
183 // Set the frame of |tabView|, also updates the internal frame dict.
184 - (void)setFrame
:(NSRect
)frame ofTabView
:(NSView
*)tabView
;
186 // Move the given tab at index |from| in this window to the location of the
187 // current placeholder.
188 - (void)moveTabFromIndex
:(NSInteger
)from
;
190 // Drop a given WebContents at |modelIndex|. Used when dragging from
191 // another window when we don't have access to the WebContents as part of our
192 // strip. |frame| is in the coordinate system of the tab strip view and
193 // represents where the user dropped the new tab so it can be animated into its
194 // correct location when the tab is added to the model. If the tab was pinned in
195 // its previous window, setting |pinned| to YES will propagate that state to the
196 // new window. Mini-tabs are either app or pinned tabs; the app state is stored
197 // by the |contents|, but the |pinned| state is the caller's responsibility.
198 // Setting |activate| to YES will make the new tab active.
199 - (void)dropWebContents
:(content::WebContents
*)contents
200 atIndex
:(int)modelIndex
201 withFrame
:(NSRect
)frame
202 asPinnedTab
:(BOOL
)pinned
203 activate
:(BOOL
)activate
;
205 // Returns the index of the subview |view|. Returns -1 if not present. Takes
206 // closing tabs into account such that this index will correctly match the tab
207 // model. If |view| is in the process of closing, returns -1, as closing tabs
208 // are no longer in the model.
209 - (NSInteger
)modelIndexForTabView
:(NSView
*)view
;
211 // Returns all selected tab views.
212 - (NSArray
*)selectedViews
;
214 // Return the view at a given index.
215 - (NSView
*)viewAtIndex
:(NSUInteger
)index
;
217 // Return the number of tab views in the tab strip. It's same as number of tabs
218 // in the model, except when a tab is closing, which will be counted in views
219 // count, but no longer in the model.
220 - (NSUInteger
)viewsCount
;
222 // Set the placeholder for a dragged tab, allowing the |frame| to be specified.
223 // This causes this tab to be rendered in an arbitrary position.
224 - (void)insertPlaceholderForTab
:(TabView
*)tab frame
:(NSRect
)frame
;
226 // Returns whether a tab is being dragged within the tab strip.
227 - (BOOL
)isDragSessionActive
;
229 // Returns whether or not |tab| can still be fully seen in the tab strip or if
230 // its current position would cause it be obscured by things such as the edge
231 // of the window or the window decorations. Returns YES only if the entire tab
233 - (BOOL
)isTabFullyVisible
:(TabView
*)tab
;
235 // Show or hide the new tab button. The button is hidden immediately, but
236 // waits until the next call to |-layoutTabs| to show it again.
237 - (void)showNewTabButton
:(BOOL
)show
;
239 // Force the tabs to rearrange themselves to reflect the current model.
241 - (void)layoutTabsWithoutAnimation
;
243 // Are we in rapid (tab) closure mode? I.e., is a full layout deferred (while
244 // the user closes tabs)? Needed to overcome missing clicks during rapid tab
246 - (BOOL
)inRapidClosureMode
;
248 // Returns YES if the user is allowed to drag tabs on the strip at this moment.
249 // For example, this returns NO if there are any pending tab close animtations.
250 - (BOOL
)tabDraggingAllowed
;
252 // Default height for tabs.
253 + (CGFloat
)defaultTabHeight
;
255 // Default indentation for tabs (see |leftIndentForControls_|).
256 + (CGFloat
)defaultLeftIndentForControls
;
258 // Returns the currently active TabContentsController.
259 - (TabContentsController
*)activeTabContentsController
;
261 // Adds custom traffic light buttons to the tab strip. Idempotent.
262 - (void)addCustomWindowControls
;
264 // Removes custom traffic light buttons from the tab strip. Idempotent.
265 - (void)removeCustomWindowControls
;
269 @interface
TabStripController(TestingAPI
)
270 - (void)setTabTitle
:(TabController
*)tab
271 withContents
:(content::WebContents
*)contents
;
274 #endif // CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_