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