browser_non_client_frame_view.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
1// Copyright (c) 2012 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#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
6
7#include "base/command_line.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/profiles/avatar_menu.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/profiles/profile_info_cache.h"
12#include "chrome/browser/profiles/profile_manager.h"
13#include "chrome/browser/profiles/profiles_state.h"
14#include "chrome/browser/ui/view_ids.h"
15#include "chrome/browser/ui/views/avatar_label.h"
16#include "chrome/browser/ui/views/avatar_menu_button.h"
17#include "chrome/browser/ui/views/frame/browser_view.h"
18#include "chrome/browser/ui/views/frame/taskbar_decorator.h"
19#include "chrome/browser/ui/views/new_avatar_button.h"
20#include "chrome/browser/ui/views/profile_chooser_view.h"
21#include "chrome/common/chrome_switches.h"
22#include "grit/generated_resources.h"
23#include "grit/theme_resources.h"
24#include "third_party/skia/include/core/SkColor.h"
25#include "ui/base/l10n/l10n_util.h"
26#include "ui/base/resource/resource_bundle.h"
27#include "ui/base/theme_provider.h"
28#include "ui/gfx/image/image.h"
29#include "ui/views/background.h"
30
31BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame,
32                                                     BrowserView* browser_view)
33    : frame_(frame),
34      browser_view_(browser_view),
35      avatar_button_(NULL),
36      avatar_label_(NULL),
37      new_avatar_button_(NULL) {
38}
39
40BrowserNonClientFrameView::~BrowserNonClientFrameView() {
41}
42
43void BrowserNonClientFrameView::VisibilityChanged(views::View* starting_from,
44                                                  bool is_visible) {
45  if (!is_visible)
46    return;
47  // The first time UpdateAvatarInfo() is called the window is not visible so
48  // DrawTaskBarDecoration() has no effect. Therefore we need to call it again
49  // once the window is visible.
50  if (!browser_view_->IsRegularOrGuestSession() ||
51      !profiles::IsNewProfileManagementEnabled())
52    UpdateAvatarInfo();
53}
54
55void BrowserNonClientFrameView::OnThemeChanged() {
56  if (avatar_label_)
57    avatar_label_->UpdateLabelStyle();
58}
59
60void BrowserNonClientFrameView::UpdateAvatarInfo() {
61  if (browser_view_->ShouldShowAvatar()) {
62    if (!avatar_button_) {
63      Profile* profile = browser_view_->browser()->profile();
64      if (profile->IsManaged() && !avatar_label_) {
65        avatar_label_ = new AvatarLabel(browser_view_);
66        avatar_label_->set_id(VIEW_ID_AVATAR_LABEL);
67        AddChildView(avatar_label_);
68      }
69      avatar_button_ = new AvatarMenuButton(
70          browser_view_->browser(), !browser_view_->IsRegularOrGuestSession());
71      avatar_button_->set_id(VIEW_ID_AVATAR_BUTTON);
72      AddChildView(avatar_button_);
73      frame_->GetRootView()->Layout();
74    }
75  } else if (avatar_button_) {
76    // The avatar label can just be there if there is also an avatar button.
77    if (avatar_label_) {
78      RemoveChildView(avatar_label_);
79      delete avatar_label_;
80      avatar_label_ = NULL;
81    }
82    RemoveChildView(avatar_button_);
83    delete avatar_button_;
84    avatar_button_ = NULL;
85    frame_->GetRootView()->Layout();
86  }
87
88  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
89  gfx::Image avatar;
90  string16 text;
91  bool is_rectangle = false;
92  if (browser_view_->IsGuestSession()) {
93    avatar = rb.GetImageNamed(browser_view_->GetGuestIconResourceID());
94  } else if (browser_view_->IsOffTheRecord()) {
95    avatar = rb.GetImageNamed(browser_view_->GetOTRIconResourceID());
96  } else if (avatar_button_ || AvatarMenu::ShouldShowAvatarMenu()) {
97    ProfileInfoCache& cache =
98        g_browser_process->profile_manager()->GetProfileInfoCache();
99    Profile* profile = browser_view_->browser()->profile();
100    size_t index = cache.GetIndexOfProfileWithPath(profile->GetPath());
101    if (index == std::string::npos)
102      return;
103    text = cache.GetNameOfProfileAtIndex(index);
104
105    AvatarMenu::GetImageForMenuButton(browser_view_->browser()->profile(),
106                                      &avatar,
107                                      &is_rectangle);
108    // Disable the menu when we should not show the menu.
109    if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
110      avatar_button_->SetEnabled(false);
111  }
112  if (avatar_button_) {
113    avatar_button_->SetAvatarIcon(avatar, is_rectangle);
114    if (!text.empty())
115      avatar_button_->SetText(text);
116  }
117
118  // For popups and panels which don't have the avatar button, we still
119  // need to draw the taskbar decoration. Draw the avatar for
120  // custom-user-data-dir windows which don't support having a shortcut icon.
121  // For non-custom-user-data-dir windows, the window's relaunch details are set
122  // so that the profile's shortcut icon is used, which includes this badge.
123  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir)) {
124    chrome::DrawTaskbarDecoration(
125        frame_->GetNativeWindow(),
126        AvatarMenu::ShouldShowAvatarMenu() ? &avatar : NULL);
127  }
128}
129
130void BrowserNonClientFrameView::UpdateNewStyleAvatarInfo(
131    views::ButtonListener* listener,
132    const NewAvatarButton::AvatarButtonStyle style) {
133  DCHECK(profiles::IsNewProfileManagementEnabled());
134  // This should never be called in incognito mode.
135  DCHECK(browser_view_->IsRegularOrGuestSession());
136
137  if (browser_view_->ShouldShowAvatar()) {
138    if (!new_avatar_button_) {
139      string16 profile_name =
140          profiles::GetActiveProfileDisplayName(browser_view_->browser());
141      new_avatar_button_ = new NewAvatarButton(
142          listener, profile_name, style, browser_view_->browser());
143      new_avatar_button_->set_id(VIEW_ID_NEW_AVATAR_BUTTON);
144      AddChildView(new_avatar_button_);
145      frame_->GetRootView()->Layout();
146    }
147  } else if (new_avatar_button_) {
148    delete new_avatar_button_;
149    new_avatar_button_ = NULL;
150    frame_->GetRootView()->Layout();
151  }
152}
153
154void BrowserNonClientFrameView::ShowProfileChooserViewBubble() {
155  gfx::Point origin;
156  views::View::ConvertPointToScreen(new_avatar_button(), &origin);
157  gfx::Rect bounds(origin, size());
158
159  ProfileChooserView::ShowBubble(
160      new_avatar_button(), views::BubbleBorder::TOP_RIGHT,
161      views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE, bounds,
162      browser_view_->browser());
163}
164