OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/browser/avatar_base_controller.h" |
| 6 |
| 7 #include "chrome/app/chrome_command_ids.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile_info_cache_observer.h" |
| 10 #include "chrome/browser/profiles/profile_info_util.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" |
| 12 #include "chrome/browser/profiles/profile_metrics.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_commands.h" |
| 15 #include "chrome/browser/ui/browser_window.h" |
| 16 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" |
| 17 #import "chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.h" |
| 18 #import "chrome/browser/ui/cocoa/browser/profile_chooser_controller.h" |
| 19 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 20 #include "chrome/common/profile_management_switches.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 |
| 23 // Space between the avatar icon and the avatar menu bubble. |
| 24 const CGFloat kMenuYOffsetAdjust = 1.0; |
| 25 |
| 26 @interface AvatarBaseController (Private) |
| 27 // Shows the avatar bubble. |
| 28 - (IBAction)buttonClicked:(id)sender; |
| 29 - (void)bubbleWillClose:(NSNotification*)notif; |
| 30 // Updates the profile name displayed by the avatar button. If |layoutParent| is |
| 31 // yes, then the BrowserWindowController is notified to relayout the subviews, |
| 32 // as the button needs to be repositioned. |
| 33 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; |
| 34 @end |
| 35 |
| 36 class ProfileInfoUpdateObserver : public ProfileInfoCacheObserver { |
| 37 public: |
| 38 ProfileInfoUpdateObserver(AvatarBaseController* avatarButton) |
| 39 : avatarButton_(avatarButton) { |
| 40 g_browser_process->profile_manager()-> |
| 41 GetProfileInfoCache().AddObserver(this); |
| 42 } |
| 43 |
| 44 virtual ~ProfileInfoUpdateObserver() { |
| 45 g_browser_process->profile_manager()-> |
| 46 GetProfileInfoCache().RemoveObserver(this); |
| 47 } |
| 48 |
| 49 // ProfileInfoCacheObserver: |
| 50 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE { |
| 51 [avatarButton_ updateAvatarButtonAndLayoutParent:YES]; |
| 52 } |
| 53 |
| 54 virtual void OnProfileWasRemoved( |
| 55 const base::FilePath& profile_path, |
| 56 const base::string16& profile_name) OVERRIDE { |
| 57 [avatarButton_ updateAvatarButtonAndLayoutParent:YES]; |
| 58 } |
| 59 |
| 60 virtual void OnProfileNameChanged( |
| 61 const base::FilePath& profile_path, |
| 62 const base::string16& old_profile_name) OVERRIDE { |
| 63 [avatarButton_ updateAvatarButtonAndLayoutParent:YES]; |
| 64 } |
| 65 |
| 66 virtual void OnProfileAvatarChanged( |
| 67 const base::FilePath& profile_path) OVERRIDE { |
| 68 [avatarButton_ updateAvatarButtonAndLayoutParent:YES]; |
| 69 } |
| 70 |
| 71 private: |
| 72 AvatarBaseController* avatarButton_; // Weak; owns this. |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(ProfileInfoUpdateObserver); |
| 75 }; |
| 76 |
| 77 @implementation AvatarBaseController |
| 78 |
| 79 - (id)initWithBrowser:(Browser*)browser { |
| 80 if ((self = [super init])) { |
| 81 browser_ = browser; |
| 82 profileInfoObserver_.reset(new ProfileInfoUpdateObserver(self)); |
| 83 } |
| 84 return self; |
| 85 } |
| 86 |
| 87 - (void)dealloc { |
| 88 [[NSNotificationCenter defaultCenter] |
| 89 removeObserver:self |
| 90 name:NSWindowWillCloseNotification |
| 91 object:[menuController_ window]]; |
| 92 [super dealloc]; |
| 93 } |
| 94 |
| 95 - (NSButton*)buttonView { |
| 96 CHECK(button_.get()); // Subclasses must set this. |
| 97 return button_.get(); |
| 98 } |
| 99 |
| 100 - (void)showAvatarBubble:(NSView*)anchor { |
| 101 if (menuController_) |
| 102 return; |
| 103 |
| 104 DCHECK(chrome::IsCommandEnabled(browser_, IDC_SHOW_AVATAR_MENU)); |
| 105 |
| 106 NSWindowController* wc = |
| 107 [browser_->window()->GetNativeWindow() windowController]; |
| 108 if ([wc isKindOfClass:[BrowserWindowController class]]) { |
| 109 [static_cast<BrowserWindowController*>(wc) |
| 110 lockBarVisibilityForOwner:self withAnimation:NO delay:NO]; |
| 111 } |
| 112 |
| 113 NSPoint point = NSMakePoint(NSMidX([anchor bounds]), |
| 114 NSMaxY([anchor bounds]) - kMenuYOffsetAdjust); |
| 115 point = [anchor convertPoint:point toView:nil]; |
| 116 point = [[anchor window] convertBaseToScreen:point]; |
| 117 |
| 118 // |menuController_| will automatically release itself on close. |
| 119 if (switches::IsNewProfileManagement()) { |
| 120 menuController_ = |
| 121 [[ProfileChooserController alloc] initWithBrowser:browser_ |
| 122 anchoredAt:point]; |
| 123 } else { |
| 124 menuController_ = |
| 125 [[AvatarMenuBubbleController alloc] initWithBrowser:browser_ |
| 126 anchoredAt:point]; |
| 127 } |
| 128 |
| 129 [[NSNotificationCenter defaultCenter] |
| 130 addObserver:self |
| 131 selector:@selector(bubbleWillClose:) |
| 132 name:NSWindowWillCloseNotification |
| 133 object:[menuController_ window]]; |
| 134 [menuController_ showWindow:self]; |
| 135 |
| 136 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE); |
| 137 } |
| 138 |
| 139 - (IBAction)buttonClicked:(id)sender { |
| 140 DCHECK_EQ(sender, button_.get()); |
| 141 [self showAvatarBubble:button_]; |
| 142 } |
| 143 |
| 144 - (void)bubbleWillClose:(NSNotification*)notif { |
| 145 NSWindowController* wc = |
| 146 [browser_->window()->GetNativeWindow() windowController]; |
| 147 if ([wc isKindOfClass:[BrowserWindowController class]]) { |
| 148 [static_cast<BrowserWindowController*>(wc) |
| 149 releaseBarVisibilityForOwner:self withAnimation:YES delay:NO]; |
| 150 } |
| 151 menuController_ = nil; |
| 152 } |
| 153 |
| 154 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { |
| 155 NOTREACHED(); |
| 156 } |
| 157 |
| 158 - (BaseBubbleController*)menuController { |
| 159 return menuController_; |
| 160 } |
| 161 |
| 162 @end |
| 163 |
OLD | NEW |