diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-19 19:52:46 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-19 19:52:46 +0000 |
commit | 961a693913f9effd9b7eae0ab3d17dd899306188 (patch) | |
tree | 209b9783167d4f7d453a452b5c017d3d17eb4f3e /chrome/browser/ui | |
parent | 2e9c23f5c7049beead982ee0834b7e91395debee (diff) | |
download | chromium_src-961a693913f9effd9b7eae0ab3d17dd899306188.zip chromium_src-961a693913f9effd9b7eae0ab3d17dd899306188.tar.gz chromium_src-961a693913f9effd9b7eae0ab3d17dd899306188.tar.bz2 |
[Mac] Add the Profile menu to the main menubar.
This creates a ProfileMenuController that is owned by the AppController. It is
responsible for managing the menu and menu item title.
XIB changes:
* Add a new menu item with no submenu called Profile with tag
IDC_PROFILE_MAIN_MENU.
BUG=86179
TEST=With --multi-profiles, a Profile menu is displayed in the menubar.
Review URL: http://codereview.chromium.org/7331029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93080 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui')
-rw-r--r-- | chrome/browser/ui/cocoa/profile_menu_controller.h | 45 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/profile_menu_controller.mm | 79 |
2 files changed, 124 insertions, 0 deletions
diff --git a/chrome/browser/ui/cocoa/profile_menu_controller.h b/chrome/browser/ui/cocoa/profile_menu_controller.h new file mode 100644 index 0000000..68d32c2 --- /dev/null +++ b/chrome/browser/ui/cocoa/profile_menu_controller.h @@ -0,0 +1,45 @@ +// Copyright (c) 2011 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_UI_COCOA_PROFILE_MENU_CONTROLLER_H_ +#define CHROME_BROWSER_UI_COCOA_PROFILE_MENU_CONTROLLER_H_ +#pragma once + +#import <Cocoa/Cocoa.h> + +#include "base/memory/scoped_nsobject.h" +#include "base/memory/scoped_ptr.h" + +class Browser; +@class MenuController; +class ProfileMenuModel; + +namespace ProfileMenuControllerInternal { +class Observer; +} + +// This controller manages the title and submenu of the Profiles item in the +// system menu bar. It updates the contents of the menu and the menu's title +// whenever the active browser changes. +@interface ProfileMenuController : NSObject { + @private + // The model for the profile submenu. + scoped_ptr<ProfileMenuModel> submenuModel_; + + // The Cocoa controller that creates the NSMenu from the model. + scoped_nsobject<MenuController> submenuController_; + + // A BrowserList::Observer to be notified when the active browser changes. + scoped_ptr<ProfileMenuControllerInternal::Observer> observer_; + + // The main menu item to which the profile menu is attached. + __weak NSMenuItem* mainMenuItem_; +} + +// Designated initializer. +- (id)initWithMainMenuItem:(NSMenuItem*)item; + +@end + +#endif // CHROME_BROWSER_UI_COCOA_PROFILE_MENU_CONTROLLER_H_ diff --git a/chrome/browser/ui/cocoa/profile_menu_controller.mm b/chrome/browser/ui/cocoa/profile_menu_controller.mm new file mode 100644 index 0000000..f97a092 --- /dev/null +++ b/chrome/browser/ui/cocoa/profile_menu_controller.mm @@ -0,0 +1,79 @@ +// Copyright (c) 2011 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/ui/cocoa/profile_menu_controller.h" + +#include "base/sys_string_conversions.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_list.h" +#import "chrome/browser/ui/cocoa/menu_controller.h" +#include "chrome/browser/ui/profile_menu_model.h" +#include "grit/generated_resources.h" +#include "ui/base/l10n/l10n_util_mac.h" + +@interface ProfileMenuController (Private) +- (void)activeBrowserChangedTo:(Browser*)browser; +@end + +namespace ProfileMenuControllerInternal { + +class Observer : public BrowserList::Observer { + public: + Observer(ProfileMenuController* controller) : controller_(controller) { + BrowserList::AddObserver(this); + } + + ~Observer() { + BrowserList::RemoveObserver(this); + } + + // BrowserList::Observer: + virtual void OnBrowserAdded(const Browser* browser) {} + virtual void OnBrowserRemoved(const Browser* browser) {} + virtual void OnBrowserSetLastActive(const Browser* browser) { + [controller_ activeBrowserChangedTo:const_cast<Browser*>(browser)]; + } + + private: + ProfileMenuController* controller_; // Weak; owns this. +}; + +} // namespace ProfileMenuControllerInternal + +//////////////////////////////////////////////////////////////////////////////// + +@implementation ProfileMenuController + +- (id)initWithMainMenuItem:(NSMenuItem*)item { + if ((self = [super init])) { + mainMenuItem_ = item; + observer_.reset(new ProfileMenuControllerInternal::Observer(self)); + } + return self; +} + +// Private ///////////////////////////////////////////////////////////////////// + +// Notifies the controller that the active browser has changed and that the +// menu item and menu need to be updated to reflect that. +- (void)activeBrowserChangedTo:(Browser*)browser { + submenuModel_.reset(new ProfileMenuModel(browser)); + submenuController_.reset( + [[MenuController alloc] initWithModel:submenuModel_.get() + useWithPopUpButtonCell:NO]); + + [mainMenuItem_ setSubmenu:[submenuController_ menu]]; + + NSMenu* submenu = [mainMenuItem_ submenu]; + if (browser) { + [submenu setTitle: + base::SysUTF8ToNSString(browser->profile()->GetProfileName())]; + } + + if (![[submenu title] length]) + [submenu setTitle:l10n_util::GetNSString(IDS_PROFILES_OPTIONS_GROUP_NAME)]; +} + +@end |