Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / tabs / tab_strip_controller.h
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
144 @property(nonatomic) CGFloat leftIndentForControls;
145 @property(nonatomic) CGFloat rightIndentForControls;
146
147 // Initialize the controller with a view and browser that contains
148 // everything else we'll need. |switchView| is the view whose contents get
149 // "switched" every time the user switches tabs. The children of this view
150 // will be released, so if you want them to stay around, make sure
151 // you have retained them.
152 // |delegate| is the one listening to filtered TabStripModelObserverBridge's
153 // events (see TabStripControllerDelegate for more details).
154 - (id)initWithView:(TabStripView*)view
155         switchView:(NSView*)switchView
156            browser:(Browser*)browser
157           delegate:(id<TabStripControllerDelegate>)delegate;
158
159 // Returns the model behind this controller.
160 - (TabStripModel*)tabStripModel;
161
162 // Returns all tab views.
163 - (NSArray*)tabViews;
164
165 // Return the view for the currently active tab.
166 - (NSView*)activeTabView;
167
168 // Find the model index based on the x coordinate of the placeholder. If there
169 // is no placeholder, this returns the end of the tab strip. Closing tabs are
170 // not considered in computing the index.
171 - (int)indexOfPlaceholder;
172
173 // Set the frame of |tabView|, also updates the internal frame dict.
174 - (void)setFrame:(NSRect)frame ofTabView:(NSView*)tabView;
175
176 // Move the given tab at index |from| in this window to the location of the
177 // current placeholder.
178 - (void)moveTabFromIndex:(NSInteger)from;
179
180 // Drop a given WebContents at |modelIndex|. Used when dragging from
181 // another window when we don't have access to the WebContents as part of our
182 // strip. |frame| is in the coordinate system of the tab strip view and
183 // represents where the user dropped the new tab so it can be animated into its
184 // correct location when the tab is added to the model. If the tab was pinned in
185 // its previous window, setting |pinned| to YES will propagate that state to the
186 // new window. Mini-tabs are either app or pinned tabs; the app state is stored
187 // by the |contents|, but the |pinned| state is the caller's responsibility.
188 // Setting |activate| to YES will make the new tab active.
189 - (void)dropWebContents:(content::WebContents*)contents
190                 atIndex:(int)modelIndex
191               withFrame:(NSRect)frame
192             asPinnedTab:(BOOL)pinned
193                activate:(BOOL)activate;
194
195 // Returns the index of the subview |view|. Returns -1 if not present. Takes
196 // closing tabs into account such that this index will correctly match the tab
197 // model. If |view| is in the process of closing, returns -1, as closing tabs
198 // are no longer in the model.
199 - (NSInteger)modelIndexForTabView:(NSView*)view;
200
201 // Returns all selected tab views.
202 - (NSArray*)selectedViews;
203
204 // Return the view at a given index.
205 - (NSView*)viewAtIndex:(NSUInteger)index;
206
207 // Return the number of tab views in the tab strip. It's same as number of tabs
208 // in the model, except when a tab is closing, which will be counted in views
209 // count, but no longer in the model.
210 - (NSUInteger)viewsCount;
211
212 // Set the placeholder for a dragged tab, allowing the |frame| to be specified.
213 // This causes this tab to be rendered in an arbitrary position.
214 - (void)insertPlaceholderForTab:(TabView*)tab frame:(NSRect)frame;
215
216 // Returns whether a tab is being dragged within the tab strip.
217 - (BOOL)isDragSessionActive;
218
219 // Returns whether or not |tab| can still be fully seen in the tab strip or if
220 // its current position would cause it be obscured by things such as the edge
221 // of the window or the window decorations. Returns YES only if the entire tab
222 // is visible.
223 - (BOOL)isTabFullyVisible:(TabView*)tab;
224
225 // Show or hide the new tab button. The button is hidden immediately, but
226 // waits until the next call to |-layoutTabs| to show it again.
227 - (void)showNewTabButton:(BOOL)show;
228
229 // Force the tabs to rearrange themselves to reflect the current model.
230 - (void)layoutTabs;
231 - (void)layoutTabsWithoutAnimation;
232
233 // Are we in rapid (tab) closure mode? I.e., is a full layout deferred (while
234 // the user closes tabs)? Needed to overcome missing clicks during rapid tab
235 // closure.
236 - (BOOL)inRapidClosureMode;
237
238 // Returns YES if the user is allowed to drag tabs on the strip at this moment.
239 // For example, this returns NO if there are any pending tab close animtations.
240 - (BOOL)tabDraggingAllowed;
241
242 // Default height for tabs.
243 + (CGFloat)defaultTabHeight;
244
245 // Default indentation for tabs (see |leftIndentForControls_|).
246 + (CGFloat)defaultLeftIndentForControls;
247
248 // Returns the currently active TabContentsController.
249 - (TabContentsController*)activeTabContentsController;
250
251 @end
252
253 @interface TabStripController(TestingAPI)
254 - (void)setTabTitle:(TabController*)tab
255        withContents:(content::WebContents*)contents;
256 @end
257
258 // Returns the parent view to use when showing a sheet for a given web contents.
259 NSView* GetSheetParentViewForWebContents(content::WebContents* web_contents);
260
261 // Returns the bounds to use when showing a sheet for a given parent view. This
262 // returns a rect in window coordinates.
263 NSRect GetSheetParentBoundsForParentView(NSView* view);
264
265 #endif  // CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_