Home | History | Annotate | Download | only in tabs
      1 // Copyright (c) 2011 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 #pragma once
      8 
      9 #import <Cocoa/Cocoa.h>
     10 
     11 #include "base/memory/scoped_nsobject.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #import "chrome/browser/ui/cocoa/tab_contents/tab_contents_controller.h"
     14 #import "chrome/browser/ui/cocoa/tabs/tab_controller_target.h"
     15 #import "chrome/browser/ui/cocoa/url_drop_target.h"
     16 #import "third_party/GTM/AppKit/GTMWindowSheetController.h"
     17 
     18 @class CrTrackingArea;
     19 @class NewTabButton;
     20 @class ProfileMenuButton;
     21 @class TabContentsController;
     22 @class TabView;
     23 @class TabStripView;
     24 
     25 class Browser;
     26 class ConstrainedWindowMac;
     27 class TabStripModelObserverBridge;
     28 class TabStripModel;
     29 class TabContents;
     30 class ToolbarModel;
     31 
     32 namespace TabStripControllerInternal {
     33 class NotificationBridge;
     34 } // namespace TabStripControllerInternal
     35 
     36 // The interface for the tab strip controller's delegate.
     37 // Delegating TabStripModelObserverBridge's events (in lieu of directly
     38 // subscribing to TabStripModelObserverBridge events, as TabStripController
     39 // does) is necessary to guarantee a proper order of subviews layout updates,
     40 // otherwise it might trigger unnesessary content relayout, UI flickering etc.
     41 @protocol TabStripControllerDelegate
     42 
     43 // Stripped down version of TabStripModelObserverBridge:selectTabWithContents.
     44 - (void)onSelectTabWithContents:(TabContents*)contents;
     45 
     46 // Stripped down version of TabStripModelObserverBridge:tabReplacedWithContents.
     47 - (void)onReplaceTabWithContents:(TabContents*)contents;
     48 
     49 // Stripped down version of TabStripModelObserverBridge:tabChangedWithContents.
     50 - (void)onSelectedTabChange:(TabStripModelObserver::TabChangeType)change;
     51 
     52 // Stripped down version of TabStripModelObserverBridge:tabDetachedWithContents.
     53 - (void)onTabDetachedWithContents:(TabContents*)contents;
     54 
     55 @end
     56 
     57 // A class that handles managing the tab strip in a browser window. It uses
     58 // a supporting C++ bridge object to register for notifications from the
     59 // TabStripModel. The Obj-C part of this class handles drag and drop and all
     60 // the other Cocoa-y aspects.
     61 //
     62 // For a full description of the design, see
     63 // http://www.chromium.org/developers/design-documents/tab-strip-mac
     64 @interface TabStripController :
     65   NSObject<TabControllerTarget,
     66            URLDropTargetController,
     67            GTMWindowSheetControllerDelegate,
     68            TabContentsControllerDelegate> {
     69  @protected
     70   // YES if tabs are to be laid out vertically instead of horizontally.
     71   BOOL verticalLayout_;
     72 
     73  @private
     74   scoped_nsobject<TabStripView> tabStripView_;
     75   NSView* switchView_;  // weak
     76   scoped_nsobject<NSView> dragBlockingView_;  // avoid bad window server drags
     77   NewTabButton* newTabButton_;  // weak, obtained from the nib.
     78   ProfileMenuButton* profileMenuButton_;  // weak, obtained from the nib.
     79   BOOL hasUpdatedProfileMenuButtonXOffset_;
     80 
     81   // Tracks the newTabButton_ for rollovers.
     82   scoped_nsobject<CrTrackingArea> newTabTrackingArea_;
     83   scoped_ptr<TabStripModelObserverBridge> bridge_;
     84   Browser* browser_;  // weak
     85   TabStripModel* tabStripModel_;  // weak
     86   // Delegate that is informed about tab state changes.
     87   id<TabStripControllerDelegate> delegate_;  // weak
     88 
     89   // YES if the new tab button is currently displaying the hover image (if the
     90   // mouse is currently over the button).
     91   BOOL newTabButtonShowingHoverImage_;
     92 
     93   // Access to the TabContentsControllers (which own the parent view
     94   // for the toolbar and associated tab contents) given an index. Call
     95   // |indexFromModelIndex:| to convert a |tabStripModel_| index to a
     96   // |tabContentsArray_| index. Do NOT assume that the indices of
     97   // |tabStripModel_| and this array are identical, this is e.g. not true while
     98   // tabs are animating closed (closed tabs are removed from |tabStripModel_|
     99   // immediately, but from |tabContentsArray_| only after their close animation
    100   // has completed).
    101   scoped_nsobject<NSMutableArray> tabContentsArray_;
    102   // An array of TabControllers which manage the actual tab views. See note
    103   // above |tabContentsArray_|. |tabContentsArray_| and |tabArray_| always
    104   // contain objects belonging to the same tabs at the same indices.
    105   scoped_nsobject<NSMutableArray> tabArray_;
    106 
    107   // Set of TabControllers that are currently animating closed.
    108   scoped_nsobject<NSMutableSet> closingControllers_;
    109 
    110   // These values are only used during a drag, and override tab positioning.
    111   TabView* placeholderTab_;  // weak. Tab being dragged
    112   NSRect placeholderFrame_;  // Frame to use
    113   CGFloat placeholderStretchiness_; // Vertical force shown by streching tab.
    114   NSRect droppedTabFrame_;  // Initial frame of a dropped tab, for animation.
    115   // Frame targets for all the current views.
    116   // target frames are used because repeated requests to [NSView animator].
    117   // aren't coalesced, so we store frames to avoid redundant calls.
    118   scoped_nsobject<NSMutableDictionary> targetFrames_;
    119   NSRect newTabTargetFrame_;
    120   // If YES, do not show the new tab button during layout.
    121   BOOL forceNewTabButtonHidden_;
    122   // YES if we've successfully completed the initial layout. When this is
    123   // NO, we probably don't want to do any animation because we're just coming
    124   // into being.
    125   BOOL initialLayoutComplete_;
    126 
    127   // Width available for resizing the tabs (doesn't include the new tab
    128   // button). Used to restrict the available width when closing many tabs at
    129   // once to prevent them from resizing to fit the full width. If the entire
    130   // width should be used, this will have a value of |kUseFullAvailableWidth|.
    131   float availableResizeWidth_;
    132   // A tracking area that's the size of the tab strip used to be notified
    133   // when the mouse moves in the tab strip
    134   scoped_nsobject<CrTrackingArea> trackingArea_;
    135   TabView* hoveredTab_;  // weak. Tab that the mouse is hovering over
    136 
    137   // Array of subviews which are permanent (and which should never be removed),
    138   // such as the new-tab button, but *not* the tabs themselves.
    139   scoped_nsobject<NSMutableArray> permanentSubviews_;
    140 
    141   // The default favicon, so we can use one copy for all buttons.
    142   scoped_nsobject<NSImage> defaultFavicon_;
    143 
    144   // The amount by which to indent the tabs on the left (to make room for the
    145   // red/yellow/green buttons).
    146   CGFloat indentForControls_;
    147 
    148   // Manages per-tab sheets.
    149   scoped_nsobject<GTMWindowSheetController> sheetController_;
    150 
    151   // Is the mouse currently inside the strip;
    152   BOOL mouseInside_;
    153 
    154   // Used for monitoring the profile name pref.
    155   scoped_ptr<TabStripControllerInternal::NotificationBridge>
    156       notificationBridge_;
    157 }
    158 
    159 @property(nonatomic) CGFloat indentForControls;
    160 
    161 // Initialize the controller with a view and browser that contains
    162 // everything else we'll need. |switchView| is the view whose contents get
    163 // "switched" every time the user switches tabs. The children of this view
    164 // will be released, so if you want them to stay around, make sure
    165 // you have retained them.
    166 // |delegate| is the one listening to filtered TabStripModelObserverBridge's
    167 // events (see TabStripControllerDelegate for more details).
    168 - (id)initWithView:(TabStripView*)view
    169         switchView:(NSView*)switchView
    170            browser:(Browser*)browser
    171           delegate:(id<TabStripControllerDelegate>)delegate;
    172 
    173 // Return the view for the currently selected tab.
    174 - (NSView*)selectedTabView;
    175 
    176 // Set the frame of the selected tab, also updates the internal frame dict.
    177 - (void)setFrameOfSelectedTab:(NSRect)frame;
    178 
    179 // Move the given tab at index |from| in this window to the location of the
    180 // current placeholder.
    181 - (void)moveTabFromIndex:(NSInteger)from;
    182 
    183 // Drop a given TabContents at the location of the current placeholder. If there
    184 // is no placeholder, it will go at the end. Used when dragging from another
    185 // window when we don't have access to the TabContents as part of our strip.
    186 // |frame| is in the coordinate system of the tab strip view and represents
    187 // where the user dropped the new tab so it can be animated into its correct
    188 // location when the tab is added to the model. If the tab was pinned in its
    189 // 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 - (void)dropTabContents:(TabContentsWrapper*)contents
    193               withFrame:(NSRect)frame
    194             asPinnedTab:(BOOL)pinned;
    195 
    196 // Returns the index of the subview |view|. Returns -1 if not present. Takes
    197 // closing tabs into account such that this index will correctly match the tab
    198 // model. If |view| is in the process of closing, returns -1, as closing tabs
    199 // are no longer in the model.
    200 - (NSInteger)modelIndexForTabView:(NSView*)view;
    201 
    202 // Return the view at a given index.
    203 - (NSView*)viewAtIndex:(NSUInteger)index;
    204 
    205 // Return the number of tab views in the tab strip. It's same as number of tabs
    206 // in the model, except when a tab is closing, which will be counted in views
    207 // count, but no longer in the model.
    208 - (NSUInteger)viewsCount;
    209 
    210 // Set the placeholder for a dragged tab, allowing the |frame| and |strechiness|
    211 // to be specified. This causes this tab to be rendered in an arbitrary position
    212 - (void)insertPlaceholderForTab:(TabView*)tab
    213                           frame:(NSRect)frame
    214                   yStretchiness:(CGFloat)yStretchiness;
    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 
    232 // Are we in rapid (tab) closure mode? I.e., is a full layout deferred (while
    233 // the user closes tabs)? Needed to overcome missing clicks during rapid tab
    234 // closure.
    235 - (BOOL)inRapidClosureMode;
    236 
    237 // Returns YES if the user is allowed to drag tabs on the strip at this moment.
    238 // For example, this returns NO if there are any pending tab close animtations.
    239 - (BOOL)tabDraggingAllowed;
    240 
    241 // Default height for tabs.
    242 + (CGFloat)defaultTabHeight;
    243 
    244 // Default indentation for tabs (see |indentForControls_|).
    245 + (CGFloat)defaultIndentForControls;
    246 
    247 // Returns the (lazily created) window sheet controller of this window. Used
    248 // for the per-tab sheets.
    249 - (GTMWindowSheetController*)sheetController;
    250 
    251 // Destroys the window sheet controller of this window, if it exists.  The sheet
    252 // controller can be recreated by a subsequent call to |-sheetController|.  Must
    253 // not be called if any sheets are currently open.
    254 // TODO(viettrungluu): This is temporary code needed to allow sheets to work
    255 // (read: not crash) in fullscreen mode.  Once GTMWindowSheetController is
    256 // modified to support moving sheets between windows, this code can go away.
    257 // http://crbug.com/19093.
    258 - (void)destroySheetController;
    259 
    260 // Returns the currently active TabContentsController.
    261 - (TabContentsController*)activeTabContentsController;
    262 
    263   // See comments in browser_window_controller.h for documentation about these
    264   // functions.
    265 - (void)attachConstrainedWindow:(ConstrainedWindowMac*)window;
    266 - (void)removeConstrainedWindow:(ConstrainedWindowMac*)window;
    267 
    268 @end
    269 
    270 // Notification sent when the number of tabs changes. The object will be this
    271 // controller.
    272 extern NSString* const kTabStripNumberOfTabsChanged;
    273 
    274 #endif  // CHROME_BROWSER_UI_COCOA_TABS_TAB_STRIP_CONTROLLER_H_
    275