diff options
author | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-06 18:14:48 +0000 |
---|---|---|
committer | pinkerton@chromium.org <pinkerton@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-06 18:14:48 +0000 |
commit | cb17f7f681804aeac05df6c6e5e0c644df5694d9 (patch) | |
tree | b52138b0dd66ae48dc2bbb8b344342977d77f145 /chrome/browser/cocoa | |
parent | 517fd6dc47748030a321ceb54f0d7f8539c6d45e (diff) | |
download | chromium_src-cb17f7f681804aeac05df6c6e5e0c644df5694d9.zip chromium_src-cb17f7f681804aeac05df6c6e5e0c644df5694d9.tar.gz chromium_src-cb17f7f681804aeac05df6c6e5e0c644df5694d9.tar.bz2 |
Enable/disable buttons when the corresponding command updates in the model.
Review URL: http://codereview.chromium.org/21128
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9318 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r-- | chrome/browser/cocoa/tab_contents_controller.h | 21 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_contents_controller.mm | 98 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.h | 12 | ||||
-rw-r--r-- | chrome/browser/cocoa/tab_strip_controller.mm | 11 |
4 files changed, 133 insertions, 9 deletions
diff --git a/chrome/browser/cocoa/tab_contents_controller.h b/chrome/browser/cocoa/tab_contents_controller.h index 903b020..d2cee1f 100644 --- a/chrome/browser/cocoa/tab_contents_controller.h +++ b/chrome/browser/cocoa/tab_contents_controller.h @@ -7,6 +7,11 @@ #include <Cocoa/Cocoa.h> +class CommandUpdater; +class TabContents; +class TabContentsCommandObserver; +class TabStripModel; + // A class that controls the contents of a tab, including the toolbar and // web area. @@ -20,10 +25,24 @@ @interface TabContentsController : NSViewController { @private + CommandUpdater* commands_; // weak, may be nil + TabContentsCommandObserver* observer_; // nil if |commands_| is nil + IBOutlet NSButton* backButton_; + IBOutlet NSButton* forwardButton_; + IBOutlet NSButton* reloadStopButton_; + IBOutlet NSButton* starButton_; IBOutlet NSTextField* locationBar_; } -// take this view (toolbar and web contents) full screen +// Create the contents of a tab represented by |contents| and loaded from the +// nib given by |name|. |commands| allows tracking of what's enabled and +// disabled. It may be nil if no updating is desired. +- (id)initWithNibName:(NSString*)name + bundle:(NSBundle*)bundle + contents:(TabContents*)contents + commands:(CommandUpdater*)commands; + +// Take this view (toolbar and web contents) full screen - (IBAction)fullScreen:(id)sender; @end diff --git a/chrome/browser/cocoa/tab_contents_controller.mm b/chrome/browser/cocoa/tab_contents_controller.mm index 5ef5a4d..f31047a 100644 --- a/chrome/browser/cocoa/tab_contents_controller.mm +++ b/chrome/browser/cocoa/tab_contents_controller.mm @@ -4,11 +4,40 @@ #include "chrome/browser/cocoa/tab_contents_controller.h" +#import "chrome/app/chrome_dll_resource.h" +#import "chrome/browser/command_updater.h" + +@interface TabContentsController(CommandUpdates) +- (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled; +@end + +// A C++ bridge class that handles listening for updates to commands and +// passing them back to the controller. +class TabContentsCommandObserver : public CommandUpdater::CommandObserver { + public: + TabContentsCommandObserver(TabContentsController* controller, + CommandUpdater* commands); + ~TabContentsCommandObserver(); + + // Overridden from CommandUpdater::CommandObserver + void EnabledStateChangedForCommand(int command, bool enabled); + + private: + TabContentsController* controller_; // weak, owns us + CommandUpdater* commands_; // weak +}; + + @implementation TabContentsController -- (id)initWithNibName:(NSString*)name bundle:(NSBundle*)bundle { +- (id)initWithNibName:(NSString*)name + bundle:(NSBundle*)bundle + contents:(TabContents*)contents + commands:(CommandUpdater*)commands { if ((self = [super initWithNibName:name bundle:bundle])) { - // nothing to do + commands_ = commands; + if (commands_) + observer_ = new TabContentsCommandObserver(self, commands); } return self; } @@ -16,6 +45,7 @@ - (void)dealloc { // make sure our contents have been removed from the window [[self view] removeFromSuperview]; + delete observer_; [super dealloc]; } @@ -23,6 +53,41 @@ [locationBar_ setStringValue:@"http://dev.chromium.org"]; } +// Returns YES if the tab represented by this controller is the front-most. +- (BOOL)isCurrentTab { + // We're the current tab if we're in the view hierarchy, otherwise some other + // tab is. + return [[self view] superview] ? YES : NO; +} + +// Called when the state for a command changes to |enabled|. Update the +// corresponding UI element. +- (void)enabledStateChangedForCommand:(NSInteger)command enabled:(BOOL)enabled { + // We don't need to update anything if we're not the frontmost tab. + // TODO(pinkerton): i'm worried that observer ordering could cause the + // notification to be sent before we've been put into the view, but we + // appear to be called in the right order so far. + if (![self isCurrentTab]) + return; + + NSButton* button = nil; + switch (command) { + case IDC_BACK: + button = backButton_; + break; + case IDC_FORWARD: + button = forwardButton_; + break; + case IDC_HOME: + // TODO(pinkerton): add home button + break; + case IDC_STAR: + button = starButton_; + break; + } + [button setEnabled:enabled]; +} + - (IBAction)fullScreen:(id)sender { if ([[self view] isInFullScreenMode]) { [[self view] exitFullScreenModeWithOptions:nil]; @@ -32,3 +97,32 @@ } @end + +//-------------------------------------------------------------------------- + +TabContentsCommandObserver::TabContentsCommandObserver( + TabContentsController* controller, CommandUpdater* commands) + : controller_(controller), commands_(commands) { + DCHECK(controller_ && commands); + // Register for notifications about state changes for the toolbar buttons + commands_->AddCommandObserver(IDC_BACK, this); + commands_->AddCommandObserver(IDC_FORWARD, this); + commands_->AddCommandObserver(IDC_RELOAD, this); + commands_->AddCommandObserver(IDC_HOME, this); + commands_->AddCommandObserver(IDC_STAR, this); +} + +TabContentsCommandObserver::~TabContentsCommandObserver() { + // Unregister the notifications + commands_->RemoveCommandObserver(IDC_BACK, this); + commands_->RemoveCommandObserver(IDC_FORWARD, this); + commands_->RemoveCommandObserver(IDC_RELOAD, this); + commands_->RemoveCommandObserver(IDC_HOME, this); + commands_->RemoveCommandObserver(IDC_STAR, this); +} + +void TabContentsCommandObserver::EnabledStateChangedForCommand(int command, + bool enabled) { + [controller_ enabledStateChangedForCommand:command + enabled:enabled ? YES : NO]; +} diff --git a/chrome/browser/cocoa/tab_strip_controller.h b/chrome/browser/cocoa/tab_strip_controller.h index f1a4eb4..2330bd5 100644 --- a/chrome/browser/cocoa/tab_strip_controller.h +++ b/chrome/browser/cocoa/tab_strip_controller.h @@ -7,6 +7,7 @@ #import <Cocoa/Cocoa.h> +class CommandUpdater; @class TabStripView; class TabStripBridge; class TabStripModel; @@ -26,14 +27,19 @@ class TabStripModel; TabStripView* tabView_; // weak NSButton* newTabButton_; TabStripBridge* bridge_; - TabStripModel* model_; + TabStripModel* model_; // weak + CommandUpdater* commands_; // weak, may be nil // maps TabContents to a TabContentsController (which owns the parent view // for the toolbar and associated tab contents) NSMutableDictionary* tabContentsToController_; } -// Initialize the controller with a view and model. Both must be non-nil. -- (id)initWithView:(TabStripView*)view model:(TabStripModel*)model; +// Initialize the controller with a view, model, and command updater for +// tracking what's enabled and disabled. |commands| may be nil if no updating +// is desired. +- (id)initWithView:(TabStripView*)view + model:(TabStripModel*)model + commands:(CommandUpdater*)commands; @end diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm index d21a64c..2ba72e2 100644 --- a/chrome/browser/cocoa/tab_strip_controller.mm +++ b/chrome/browser/cocoa/tab_strip_controller.mm @@ -56,11 +56,14 @@ class TabStripBridge : public TabStripModelObserver { @implementation TabStripController -- (id)initWithView:(TabStripView*)view model:(TabStripModel*)model { +- (id)initWithView:(TabStripView*)view + model:(TabStripModel*)model + commands:(CommandUpdater*)commands { DCHECK(view && model); if ((self = [super init])) { tabView_ = view; model_ = model; + commands_ = commands; bridge_ = new TabStripBridge(model, self); tabContentsToController_ = [[NSMutableDictionary alloc] init]; @@ -193,8 +196,10 @@ class TabStripBridge : public TabStripModelObserver { // TODO(pinkerton): will eventually need to pass |contents| to the // controller to complete hooking things up. TabContentsController* contentsController = - [[[TabContentsController alloc] initWithNibName:@"TabContents" bundle:nil] - autorelease]; + [[[TabContentsController alloc] initWithNibName:@"TabContents" + bundle:nil + contents:contents + commands:commands_] autorelease]; NSValue* key = [NSValue valueWithPointer:contents]; [tabContentsToController_ setObject:contentsController forKey:key]; |