diff options
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/cocoa/tab_controller.h | 44 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_controller.mm | 53 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.h | 14 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.mm | 79 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_view.h | 23 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_view.mm | 50 |
6 files changed, 221 insertions, 42 deletions
diff --git a/chrome/browser/cocoa/tab_controller.h b/chrome/browser/cocoa/tab_controller.h new file mode 100644 index 0000000..0760ccd --- /dev/null +++ b/chrome/browser/cocoa/tab_controller.h @@ -0,0 +1,44 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_COCOA_TAB_CONTROLLER_H_ +#define CHROME_BROWSER_COCOA_TAB_CONTROLLER_H_ + +#import <Cocoa/Cocoa.h> + +@class TabView; + +// A class that manages a single tab in the tab strip. Set its target/action +// to be sent a message when the tab is selected by the user clicking. Setting +// the |loading| property to YES visually indicates that this tab is currently +// loading content via a spinner. + +@interface TabController : NSViewController { + @private + IBOutlet NSButton *backgroundButton_; + IBOutlet NSButton *closeButton_; + IBOutlet NSProgressIndicator *progressIndicator_; + BOOL selected_; + BOOL loading_; + NSImage *image_; + id target_; // weak, where actions are sent, eg selectTab: + SEL action_; // selector sent when tab is seleted by clicking +} + +@property(retain, nonatomic) NSImage *image; +@property(assign, nonatomic) BOOL selected; +@property(assign, nonatomic) BOOL loading; +@property(assign, nonatomic) id target; +@property(assign, nonatomic) SEL action; + +// Minimum and maximum allowable tab width. ++ (float)minTabWidth; ++ (float)maxTabWidth; + +// The view associated with this controller, pre-casted as a TabView +- (TabView *)tabView; + +@end + +#endif // CHROME_BROWSER_COCOA_TAB_CONTROLLER_H_ diff --git a/chrome/browser/cocoa/tab_controller.mm b/chrome/browser/cocoa/tab_controller.mm new file mode 100644 index 0000000..1c10172 --- /dev/null +++ b/chrome/browser/cocoa/tab_controller.mm @@ -0,0 +1,53 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "chrome/browser/cocoa/tab_controller.h" + +@implementation TabController + +@synthesize image = image_; +@synthesize loading = loading_; +@synthesize target = target_; +@synthesize action = action_; + ++ (float)minTabWidth { return 64.0; } ++ (float)maxTabWidth { return 160.0; } + +- (TabView*)tabView { + return static_cast<TabView*>([self view]); +} + +- (id)init { + self = [super initWithNibName:@"TabView" bundle:nil]; + if (self != nil) { + [self setImage:[NSImage imageNamed:@"nav"]]; + } + return self; +} + +// Called when the tab's nib is done loading and all outlets are hooked up. +- (void)awakeFromNib { + [[self view] addSubview:backgroundButton_ + positioned:NSWindowBelow + relativeTo:nil]; + // TODO(alcor): figure out what to do with the close button v. cell. Note + // there is no close button in the nib at the moment. + [closeButton_ setWantsLayer:YES]; + [closeButton_ setAlphaValue:0.2]; + [self setSelected:NO]; +} + +- (void)setSelected:(BOOL)selected { + if (selected_ != selected) { + selected_ = selected; + [backgroundButton_ setState:selected]; + [[self view] setNeedsDisplay:YES]; + } +} + +- (BOOL)selected { + return selected_; +} + +@end diff --git a/chrome/browser/cocoa/tab_strip_controller.h b/chrome/browser/cocoa/tab_strip_controller.h index bc1f8c0..1e76e40 100644 --- a/chrome/browser/cocoa/tab_strip_controller.h +++ b/chrome/browser/cocoa/tab_strip_controller.h @@ -20,10 +20,11 @@ class ToolbarModel; // TabStripModel. The Obj-C part of this class handles drag and drop and all // the other Cocoa-y aspects. // -// When a new tab is created, it loads the contents, including -// toolbar, from a separate nib file and replaces the contentView of the -// window. As tabs are switched, the single child of the contentView is -// swapped around to hold the contents (toolbar and all) representing that tab. +// When a new tab is created, we create a TabController which manages loading +// the contents, including toolbar, from a separate nib file. This controller +// then handles replacing the contentView of the window. As tabs are switched, +// the single child of the contentView is swapped around to hold the contents +// (toolbar and all) representing that tab. @interface TabStripController : NSObject { @private @@ -38,7 +39,10 @@ class ToolbarModel; // for the toolbar and associated tab contents) given an index. This needs // to be kept in the same order as the tab strip's model as we will be // using its index from the TabStripModelObserver calls. - NSMutableArray* tabControllerArray_; + NSMutableArray* tabContentsArray_; + // an array of TabControllers which manage the actual tab views. As above, + // this is kept in the same order as the tab strip model. + NSMutableArray* tabArray_; } // Initialize the controller with a view, model, and command updater for diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm index 2e23032..49cbb56 100644 --- a/chrome/browser/cocoa/tab_strip_controller.mm +++ b/chrome/browser/cocoa/tab_strip_controller.mm @@ -9,6 +9,8 @@ #import "chrome/browser/cocoa/tab_strip_view.h" #import "chrome/browser/cocoa/tab_cell.h" #import "chrome/browser/cocoa/tab_contents_controller.h" +#import "chrome/browser/cocoa/tab_controller.h" +#import "chrome/browser/cocoa/tab_view.h" #import "chrome/browser/tab_contents/tab_contents.h" #import "chrome/browser/tabs/tab_strip_model.h" @@ -71,7 +73,8 @@ class TabStripBridge : public TabStripModelObserver { toolbarModel_ = toolbarModel; commands_ = commands; bridge_ = new TabStripBridge(tabModel, self); - tabControllerArray_ = [[NSMutableArray alloc] init]; + tabContentsArray_ = [[NSMutableArray alloc] init]; + tabArray_ = [[NSMutableArray alloc] init]; // Create the new tab button separate from the nib so we can make sure // it's always at the end of the subview list. @@ -91,7 +94,8 @@ class TabStripBridge : public TabStripModelObserver { - (void)dealloc { delete bridge_; - [tabControllerArray_ release]; + [tabContentsArray_ release]; + [tabArray_ release]; [newTabButton_ release]; [super dealloc]; } @@ -99,7 +103,7 @@ class TabStripBridge : public TabStripModelObserver { // Finds the associated TabContentsController at the given |index| and swaps // out the sole child of the contentArea to display its contents. - (void)swapInTabAtIndex:(NSInteger)index { - TabContentsController* controller = [tabControllerArray_ objectAtIndex:index]; + TabContentsController* controller = [tabContentsArray_ objectAtIndex:index]; // Resize the new view to fit the window NSView* contentView = [[tabView_ window] contentView]; @@ -123,24 +127,21 @@ class TabStripBridge : public TabStripModelObserver { // Create a new tab view and set its cell correctly so it draws the way we // want it to. -- (NSButton*)newTabWithFrame:(NSRect)frame { - NSButton* button = [[[NSButton alloc] initWithFrame:frame] autorelease]; - TabCell* cell = [[[TabCell alloc] init] autorelease]; - [button setCell:cell]; - [button setButtonType:NSMomentaryPushInButton]; - [button setTitle:@"New Tab"]; - [button setBezelStyle:NSRegularSquareBezelStyle]; - [button setTarget:self]; - [button setAction:@selector(selectTab:)]; - - return button; +- (TabController*)newTabWithFrame:(NSRect)frame { + TabController* controller = [[[TabController alloc] init] autorelease]; + [controller setTarget:self]; + [controller setAction:@selector(selectTab:)]; + TabView* view = [controller tabView]; + [view setFrame:frame]; + + return controller; } -// Returns the number of tab buttons in the tab strip by counting the children. -// Recall the last view is the "new tab" button, so the number of tabs is one -// less than the count. +// Returns the number of tabs in the tab strip. This is just the number +// of TabControllers we know about as there's a 1-to-1 mapping from these +// controllers to a tab. - (NSInteger)numberOfTabViews { - return [[tabView_ subviews] count] - 1; + return [tabArray_ count]; } // Returns the index of the subview |view|. Returns -1 if not present. @@ -167,7 +168,7 @@ class TabStripBridge : public TabStripModelObserver { // as to not cover the window controls. - (NSRect)frameForNewTabAtIndex:(NSInteger)index { const short kIndentLeavingSpaceForControls = 66; - const short kNewTabWidth = 160; + const short kNewTabWidth = [TabController maxTabWidth]; short xOffset = kIndentLeavingSpaceForControls; if (index > 0) { @@ -180,7 +181,7 @@ class TabStripBridge : public TabStripModelObserver { // Handles setting the title of the tab based on the given |contents|. Uses // a canned string if |contents| is NULL. -- (void)setTabTitle:(NSButton*)tab withContents:(TabContents*)contents { +- (void)setTabTitle:(NSViewController*)tab withContents:(TabContents*)contents { NSString* titleString = nil; if (contents) titleString = base::SysUTF16ToNSString(contents->GetTitle()); @@ -208,20 +209,22 @@ class TabStripBridge : public TabStripModelObserver { commands:commands_ toolbarModel:toolbarModel_] autorelease]; - [tabControllerArray_ insertObject:contentsController atIndex:index]; + [tabContentsArray_ insertObject:contentsController atIndex:index]; // Remove the new tab button so the only views present are the tabs, // we'll add it back when we're done [newTabButton_ removeFromSuperview]; - // Make a new tab view and add it to the strip. + // Make a new tab and add it to the strip. Keep track of its controller. // TODO(pinkerton): move everyone else over and animate. Also will need to // move the "add tab" button over. NSRect newTabFrame = [self frameForNewTabAtIndex:index]; - NSButton* newView = [self newTabWithFrame:newTabFrame]; + TabController* newController = [self newTabWithFrame:newTabFrame]; + [tabArray_ insertObject:newController atIndex:index]; + NSView* newView = [newController view]; [tabView_ addSubview:newView]; - [self setTabTitle:newView withContents:contents]; + [self setTabTitle:newController withContents:contents]; // Add the new tab button back in to the right of the last tab. const NSInteger kNewTabXOffset = 10; @@ -247,14 +250,14 @@ class TabStripBridge : public TabStripModelObserver { // De-select all other tabs and select the new tab. const int numSubviews = [self numberOfTabViews]; for (int i = 0; i < numSubviews; i++) { - NSButton* current = [[tabView_ subviews] objectAtIndex:i]; - [current setState:(i == index) ? NSOnState : NSOffState]; + TabController* current = [tabArray_ objectAtIndex:i]; + [current setSelected:(i == index) ? YES : NO]; } // Tell the new tab contents it is about to become the selected tab. Here it // can do things like make sure the toolbar is up to date. TabContentsController* newController = - [tabControllerArray_ objectAtIndex:index]; + [tabContentsArray_ objectAtIndex:index]; [newController willBecomeSelectedTab]; // Swap in the contents for the new tab @@ -270,7 +273,7 @@ class TabStripBridge : public TabStripModelObserver { // will remove all the tab content Cocoa views from the hierarchy. A // subsequent "select tab" notification will follow from the model. To // tell us what to swap in in its absence. - [tabControllerArray_ removeObjectAtIndex:index]; + [tabContentsArray_ removeObjectAtIndex:index]; // Remove the |index|th view from the tab strip NSView* tab = [[tabView_ subviews] objectAtIndex:index]; @@ -286,23 +289,25 @@ class TabStripBridge : public TabStripModelObserver { newFrame.origin.x -= tabWidth - kTabOverlap; [curr setFrame:newFrame]; } + + // Once we're totally done with the tab, delete its controller + [tabArray_ removeObjectAtIndex:index]; } // Called when a notification is received from the model that the given tab // has been updated. - (void)tabChangedWithContents:(TabContents*)contents atIndex:(NSInteger)index { - NSButton* tab = [[tabView_ subviews] objectAtIndex:index]; - [self setTabTitle:tab withContents:contents]; + [self setTabTitle:[tabArray_ objectAtIndex:index] withContents:contents]; TabContentsController* updatedController = - [tabControllerArray_ objectAtIndex:index]; + [tabContentsArray_ objectAtIndex:index]; [updatedController tabDidChange:contents]; } - (LocationBar*)locationBar { TabContentsController* selectedController = - [tabControllerArray_ objectAtIndex:tabModel_->selected_index()]; + [tabContentsArray_ objectAtIndex:tabModel_->selected_index()]; return [selectedController locationBar]; } @@ -316,13 +321,13 @@ class TabStripBridge : public TabStripModelObserver { // YES means we're going back to this tab and should put back any state // associated with it. TabContentsController* controller = - [tabControllerArray_ objectAtIndex:tabModel_->GetIndexOfTabContents(tab)]; + [tabContentsArray_ objectAtIndex:tabModel_->GetIndexOfTabContents(tab)]; [controller updateToolbarWithContents:shouldRestore ? tab : nil]; } - (void)setStarredState:(BOOL)isStarred { TabContentsController* selectedController = - [tabControllerArray_ objectAtIndex:tabModel_->selected_index()]; + [tabContentsArray_ objectAtIndex:tabModel_->selected_index()]; [selectedController setStarredState:isStarred]; } @@ -337,7 +342,7 @@ class TabStripBridge : public TabStripModelObserver { selectedIndex = 0; } TabContentsController* selectedController = - [tabControllerArray_ objectAtIndex:selectedIndex]; + [tabContentsArray_ objectAtIndex:selectedIndex]; if (!selectedController) return NSZeroRect; return [selectedController growBoxRect]; @@ -349,14 +354,14 @@ class TabStripBridge : public TabStripModelObserver { // a spinner? TabContentsController* selectedController = - [tabControllerArray_ objectAtIndex:tabModel_->selected_index()]; + [tabContentsArray_ objectAtIndex:tabModel_->selected_index()]; [selectedController setIsLoading:isLoading]; } // Make the location bar the first responder, if possible. - (void)focusLocationBar { TabContentsController* selectedController = - [tabControllerArray_ objectAtIndex:tabModel_->selected_index()]; + [tabContentsArray_ objectAtIndex:tabModel_->selected_index()]; [selectedController focusLocationBar]; } diff --git a/chrome/browser/cocoa/tab_view.h b/chrome/browser/cocoa/tab_view.h new file mode 100644 index 0000000..a1ddee7 --- /dev/null +++ b/chrome/browser/cocoa/tab_view.h @@ -0,0 +1,23 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_COCOA_TAB_VIEW_H_ +#define CHROME_BROWSER_COCOA_TAB_VIEW_H_ + +#import <Cocoa/Cocoa.h> + +@class TabController; + +// A view that handles the event tracking (clicking and dragging) for a tab +// on the tab strip. Relies on an associated TabController to provide a +// target/action for selecting the tab. + +@interface TabView : NSView { + @private + IBOutlet TabController* controller_; +} + +@end + +#endif // CHROME_BROWSER_COCOA_TAB_VIEW_H_ diff --git a/chrome/browser/cocoa/tab_view.mm b/chrome/browser/cocoa/tab_view.mm new file mode 100644 index 0000000..ffca1de --- /dev/null +++ b/chrome/browser/cocoa/tab_view.mm @@ -0,0 +1,50 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/cocoa/tab_view.h" + +@implementation TabView + +- (id)initWithFrame:(NSRect)frame { + self = [super initWithFrame:frame]; + if (self) { + // TODO(alcor): register for theming, either here or the cell + // [self gtm_registerForThemeNotifications]; + } + return self; +} + +- (void)dealloc { + // [self gtm_unregisterForThemeNotifications]; + [super dealloc]; +} + +// Overridden so that mouse clicks come to this view (the parent of the +// hierarchy) first. We want to handle clicks and drags in this class and +// leave the background button for display purposes only. +- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent { + return YES; +} + +// Determines which view a click in our frame actually hit. It's always this +// view, never a child. +// TODO(alcor): Figure out what to do with the close button. Are we using a +// NSButton for it, or drawing it ourselves with a cell? +- (NSView *)hitTest:(NSPoint)aPoint { + if (NSPointInRect(aPoint, [self frame])) return self; + return nil; +} + +// Handle clicks and drags in this button. We get here because we have +// overridden acceptsFirstMouse: and the click is within our bounds. +- (void)mouseDown:(NSEvent *)theEvent { + // fire the action to select the tab + if ([[controller_ target] respondsToSelector:[controller_ action]]) + [[controller_ target] performSelector:[controller_ action] + withObject:self]; + + // TODO(alcor): handle dragging... +} + +@end |