Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: chrome/browser/ui/views/frame/browser_non_client_frame_view.cc

Issue 1972033002: Simplify some old avatar menu button code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reinstate ash browser test fix Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h" 5 #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 #include "chrome/browser/browser_process.h" 8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/avatar_menu.h" 9 #include "chrome/browser/profiles/avatar_menu.h"
10 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_avatar_icon_util.h" 11 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
12 #include "chrome/browser/profiles/profile_manager.h" 12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/profiles/profiles_state.h" 13 #include "chrome/browser/profiles/profiles_state.h"
14 #include "chrome/browser/themes/theme_properties.h" 14 #include "chrome/browser/themes/theme_properties.h"
15 #include "chrome/browser/ui/view_ids.h" 15 #include "chrome/browser/ui/view_ids.h"
16 #include "chrome/browser/ui/views/frame/browser_view.h" 16 #include "chrome/browser/ui/views/frame/browser_view.h"
17 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
18 #include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
19 #include "chrome/browser/ui/views/tabs/tab_strip.h" 17 #include "chrome/browser/ui/views/tabs/tab_strip.h"
20 #include "components/signin/core/common/profile_management_switches.h" 18 #include "components/signin/core/common/profile_management_switches.h"
21 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
22 #include "third_party/skia/include/core/SkColor.h" 20 #include "third_party/skia/include/core/SkColor.h"
23 #include "ui/base/material_design/material_design_controller.h" 21 #include "ui/base/material_design/material_design_controller.h"
24 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/theme_provider.h" 23 #include "ui/base/theme_provider.h"
26 #include "ui/gfx/color_palette.h" 24 #include "ui/gfx/color_palette.h"
27 #include "ui/gfx/image/image.h" 25 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/paint_vector_icon.h" 26 #include "ui/gfx/paint_vector_icon.h"
29 #include "ui/gfx/vector_icons_public.h" 27 #include "ui/gfx/vector_icons_public.h"
30 #include "ui/resources/grit/ui_resources.h" 28 #include "ui/resources/grit/ui_resources.h"
31 #include "ui/views/background.h" 29 #include "ui/views/background.h"
32 #include "ui/views/resources/grit/views_resources.h" 30 #include "ui/views/resources/grit/views_resources.h"
33 31
32 #if defined(OS_WIN)
33 #include "chrome/browser/ui/views/frame/taskbar_decorator_win.h"
34 #endif
35
34 BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame, 36 BrowserNonClientFrameView::BrowserNonClientFrameView(BrowserFrame* frame,
35 BrowserView* browser_view) 37 BrowserView* browser_view)
36 : frame_(frame), 38 : frame_(frame),
37 browser_view_(browser_view) { 39 browser_view_(browser_view),
40 profile_indicator_icon_(nullptr) {
38 // The profile manager may by null in tests. 41 // The profile manager may by null in tests.
39 if (g_browser_process->profile_manager()) { 42 if (g_browser_process->profile_manager()) {
40 g_browser_process->profile_manager()-> 43 g_browser_process->profile_manager()->
41 GetProfileAttributesStorage().AddObserver(this); 44 GetProfileAttributesStorage().AddObserver(this);
42 } 45 }
43 } 46 }
44 47
45 BrowserNonClientFrameView::~BrowserNonClientFrameView() { 48 BrowserNonClientFrameView::~BrowserNonClientFrameView() {
46 // The profile manager may by null in tests. 49 // The profile manager may by null in tests.
47 if (g_browser_process->profile_manager()) { 50 if (g_browser_process->profile_manager()) {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 150 }
148 151
149 gfx::ImageSkia BrowserNonClientFrameView::GetFrameImage() const { 152 gfx::ImageSkia BrowserNonClientFrameView::GetFrameImage() const {
150 return GetFrameImage(ShouldPaintAsActive()); 153 return GetFrameImage(ShouldPaintAsActive());
151 } 154 }
152 155
153 gfx::ImageSkia BrowserNonClientFrameView::GetFrameOverlayImage() const { 156 gfx::ImageSkia BrowserNonClientFrameView::GetFrameOverlayImage() const {
154 return GetFrameOverlayImage(ShouldPaintAsActive()); 157 return GetFrameOverlayImage(ShouldPaintAsActive());
155 } 158 }
156 159
157 void BrowserNonClientFrameView::UpdateOldAvatarButton() { 160 void BrowserNonClientFrameView::UpdateProfileIndicatorIcon() {
158 if (browser_view_->ShouldShowAvatar()) { 161 if (!profile_indicator_icon_) {
159 if (!avatar_button_) { 162 profile_indicator_icon_ = new ProfileIndicatorIcon();
160 avatar_button_ = new AvatarMenuButton(browser_view_); 163 profile_indicator_icon_->set_id(VIEW_ID_PROFILE_INDICATOR_ICON);
161 avatar_button_->set_id(VIEW_ID_AVATAR_BUTTON); 164 AddChildView(profile_indicator_icon_);
162 AddChildView(avatar_button_); 165 // Invalidate here because adding a child does not invalidate the layout.
163 // Invalidate here because adding a child does not invalidate the layout. 166 InvalidateLayout();
164 InvalidateLayout();
165 frame_->GetRootView()->Layout();
166 }
167 } else if (avatar_button_) {
168 RemoveChildView(avatar_button_);
169 delete avatar_button_;
170 avatar_button_ = nullptr;
171 frame_->GetRootView()->Layout(); 167 frame_->GetRootView()->Layout();
172 } 168 }
173 169
174 gfx::Image avatar; 170 gfx::Image icon;
175 gfx::Image taskbar_badge_avatar; 171 const Profile* profile = browser_view()->browser()->profile();
176 bool is_rectangle = false; 172 if (profile->GetProfileType() == Profile::INCOGNITO_PROFILE) {
173 icon = gfx::Image(GetOTRAvatarIcon());
174 if (!ui::MaterialDesignController::IsModeMaterial())
175 profile_indicator_icon_->EnableCanvasFlippingForRTLUI(true);
176 } else {
177 #if defined(OS_CHROMEOS)
178 AvatarMenu::GetImageForMenuButton(profile->GetPath(), &icon);
179 #else
180 NOTREACHED();
181 #endif
182 }
177 183
178 // Update the avatar button in the window frame and the taskbar overlay. 184 profile_indicator_icon_->SetIcon(icon);
179 bool should_show_avatar_menu =
180 avatar_button_ || AvatarMenu::ShouldShowAvatarMenu();
181
182 if (!AvatarMenuButton::GetAvatarImages(this, should_show_avatar_menu, &avatar,
183 &taskbar_badge_avatar, &is_rectangle))
184 return;
185
186 // Disable the menu when we should not show the menu.
187 if (avatar_button_ && !AvatarMenu::ShouldShowAvatarMenu())
188 avatar_button_->SetEnabled(false);
189 if (avatar_button_)
190 avatar_button_->SetAvatarIcon(avatar, is_rectangle);
191 } 185 }
192 186
193 void BrowserNonClientFrameView::ViewHierarchyChanged( 187 void BrowserNonClientFrameView::ViewHierarchyChanged(
194 const ViewHierarchyChangedDetails& details) { 188 const ViewHierarchyChangedDetails& details) {
195 if (details.is_add && details.child == this) 189 if (details.is_add && details.child == this)
196 UpdateAvatar(); 190 UpdateProfileIcons();
197 } 191 }
198 192
199 void BrowserNonClientFrameView::ActivationChanged(bool active) { 193 void BrowserNonClientFrameView::ActivationChanged(bool active) {
200 if (ui::MaterialDesignController::IsModeMaterial()) { 194 if (ui::MaterialDesignController::IsModeMaterial()) {
201 // On Windows, while deactivating the widget, this is called before the 195 // On Windows, while deactivating the widget, this is called before the
202 // active HWND has actually been changed. Since we want the avatar state to 196 // active HWND has actually been changed. Since we want the avatar state to
203 // reflect that the window is inactive, we force NonClientFrameView to see 197 // reflect that the window is inactive, we force NonClientFrameView to see
204 // the "correct" state as an override. 198 // the "correct" state as an override.
205 set_active_state_override(&active); 199 set_active_state_override(&active);
206 UpdateAvatar(); 200 UpdateProfileIcons();
207 set_active_state_override(nullptr); 201 set_active_state_override(nullptr);
208 202
209 // Changing the activation state may change the toolbar top separator color 203 // Changing the activation state may change the toolbar top separator color
210 // that's used as the stroke around tabs/the new tab button. 204 // that's used as the stroke around tabs/the new tab button.
211 browser_view_->tabstrip()->SchedulePaint(); 205 browser_view_->tabstrip()->SchedulePaint();
212 } 206 }
213 207
214 // Changing the activation state may change the visible frame color. 208 // Changing the activation state may change the visible frame color.
215 SchedulePaint(); 209 SchedulePaint();
216 } 210 }
217 211
218 void BrowserNonClientFrameView::OnProfileAdded( 212 void BrowserNonClientFrameView::OnProfileAdded(
219 const base::FilePath& profile_path) { 213 const base::FilePath& profile_path) {
220 OnProfileAvatarChanged(profile_path); 214 OnProfileAvatarChanged(profile_path);
221 } 215 }
222 216
223 void BrowserNonClientFrameView::OnProfileWasRemoved( 217 void BrowserNonClientFrameView::OnProfileWasRemoved(
224 const base::FilePath& profile_path, 218 const base::FilePath& profile_path,
225 const base::string16& profile_name) { 219 const base::string16& profile_name) {
226 OnProfileAvatarChanged(profile_path); 220 OnProfileAvatarChanged(profile_path);
227 } 221 }
228 222
229 void BrowserNonClientFrameView::OnProfileAvatarChanged( 223 void BrowserNonClientFrameView::OnProfileAvatarChanged(
230 const base::FilePath& profile_path) { 224 const base::FilePath& profile_path) {
231 UpdateTaskbarDecoration(); 225 UpdateTaskbarDecoration();
232 UpdateAvatar(); 226 UpdateProfileIcons();
233 } 227 }
234 228
235 const ui::ThemeProvider* 229 const ui::ThemeProvider*
236 BrowserNonClientFrameView::GetThemeProviderForProfile() const { 230 BrowserNonClientFrameView::GetThemeProviderForProfile() const {
237 // Because the frame's accessor reads the ThemeProvider from the profile and 231 // Because the frame's accessor reads the ThemeProvider from the profile and
238 // not the widget, it can be called even before we're in a view hierarchy. 232 // not the widget, it can be called even before we're in a view hierarchy.
239 return frame_->GetThemeProvider(); 233 return frame_->GetThemeProvider();
240 } 234 }
241 235
242 void BrowserNonClientFrameView::UpdateTaskbarDecoration() { 236 void BrowserNonClientFrameView::UpdateTaskbarDecoration() {
243 gfx::Image avatar; 237 #if defined(OS_WIN)
244 gfx::Image taskbar_badge_avatar; 238 if (browser_view()->browser()->profile()->IsGuestSession() ||
245 bool is_rectangle; 239 // Browser process and profile manager may be null in tests.
246 // Only need to update the taskbar overlay here. If GetAvatarImages() 240 (g_browser_process && g_browser_process->profile_manager() &&
247 // returns false, don't bother trying to update the taskbar decoration since 241 g_browser_process->profile_manager()
248 // the returned images are not initialized. This can happen if the user 242 ->GetProfileAttributesStorage()
249 // deletes the current profile. 243 .GetNumberOfProfiles() <= 1)) {
250 if (AvatarMenuButton::GetAvatarImages( 244 chrome::DrawTaskbarDecoration(frame_->GetNativeWindow(), nullptr);
251 this, AvatarMenu::ShouldShowAvatarMenu(), &avatar, 245 return;
252 &taskbar_badge_avatar, &is_rectangle)) {
253 // For popups and panels which don't have the avatar button, we still
254 // need to draw the taskbar decoration. Even though we have an icon on the
255 // window's relaunch details, we draw over it because the user may have
256 // pinned the badge-less Chrome shortcut which will cause windows to ignore
257 // the relaunch details.
258 // TODO(calamity): ideally this should not be necessary but due to issues
259 // with the default shortcut being pinned, we add the runtime badge for
260 // safety. See crbug.com/313800.
261 bool show_decoration = AvatarMenu::ShouldShowAvatarMenu() &&
262 !browser_view_->browser()->profile()->IsGuestSession();
263 // In tests, make sure that the browser process and profile manager are
264 // valid before using.
265 if (g_browser_process && g_browser_process->profile_manager()) {
266 const ProfileAttributesStorage& storage =
267 g_browser_process->profile_manager()->GetProfileAttributesStorage();
268 show_decoration = show_decoration && storage.GetNumberOfProfiles() > 1;
269 }
270 chrome::DrawTaskbarDecoration(frame_->GetNativeWindow(),
271 show_decoration
272 ? (taskbar_badge_avatar.IsEmpty() ? &avatar : &taskbar_badge_avatar)
273 : nullptr);
274 } 246 }
247
248 // For popups and panels which don't have the avatar button, we still
249 // need to draw the taskbar decoration. Even though we have an icon on the
250 // window's relaunch details, we draw over it because the user may have
251 // pinned the badge-less Chrome shortcut which will cause Windows to ignore
252 // the relaunch details.
253 // TODO(calamity): ideally this should not be necessary but due to issues
254 // with the default shortcut being pinned, we add the runtime badge for
255 // safety. See crbug.com/313800.
256 gfx::Image decoration;
257 AvatarMenu::GetImageForMenuButton(
258 browser_view()->browser()->profile()->GetPath(), &decoration);
259 // This can happen if the user deletes the current profile.
260 if (decoration.IsEmpty())
261 return;
262 chrome::DrawTaskbarDecoration(frame_->GetNativeWindow(), &decoration);
263 #endif
275 } 264 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698