OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/new_avatar_button.h" | 5 #include "chrome/browser/ui/views/new_avatar_button.h" |
6 | 6 |
7 #include "base/win/windows_version.h" | 7 #include "base/win/windows_version.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
10 #include "chrome/browser/profiles/profiles_state.h" | 10 #include "chrome/browser/profiles/profiles_state.h" |
| 11 #include "chrome/browser/ui/browser.h" |
11 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
12 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
15 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/color_utils.h" | 17 #include "ui/gfx/color_utils.h" |
17 #include "ui/gfx/font_list.h" | 18 #include "ui/gfx/font_list.h" |
18 #include "ui/gfx/text_elider.h" | 19 #include "ui/gfx/text_elider.h" |
19 #include "ui/views/border.h" | 20 #include "ui/views/border.h" |
20 #include "ui/views/painter.h" | 21 #include "ui/views/painter.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } else { | 91 } else { |
91 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); | 92 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_NORMAL); |
92 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); | 93 const int kHotImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_HOVER); |
93 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); | 94 const int kPushedImageSet[] = IMAGE_GRID(IDR_AVATAR_GLASS_BUTTON_PRESSED); |
94 | 95 |
95 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); | 96 SetBorder(CreateBorder(kNormalImageSet, kHotImageSet, kPushedImageSet)); |
96 set_menu_marker( | 97 set_menu_marker( |
97 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); | 98 rb->GetImageNamed(IDR_AVATAR_GLASS_BUTTON_DROPARROW).ToImageSkia()); |
98 } | 99 } |
99 | 100 |
100 avatar_menu_.reset(new AvatarMenu( | 101 g_browser_process->profile_manager()->GetProfileInfoCache().AddObserver(this); |
101 &g_browser_process->profile_manager()->GetProfileInfoCache(), | |
102 this, | |
103 browser_)); | |
104 avatar_menu_->RebuildMenu(); | |
105 | |
106 SchedulePaint(); | 102 SchedulePaint(); |
107 } | 103 } |
108 | 104 |
109 NewAvatarButton::~NewAvatarButton() { | 105 NewAvatarButton::~NewAvatarButton() { |
| 106 g_browser_process->profile_manager()-> |
| 107 GetProfileInfoCache().RemoveObserver(this); |
110 } | 108 } |
111 | 109 |
112 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { | 110 void NewAvatarButton::OnPaint(gfx::Canvas* canvas) { |
113 // From TextButton::PaintButton, draw everything but the text. | 111 // From TextButton::PaintButton, draw everything but the text. |
114 OnPaintBackground(canvas); | 112 OnPaintBackground(canvas); |
115 OnPaintBorder(canvas); | 113 OnPaintBorder(canvas); |
116 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); | 114 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); |
117 | 115 |
118 gfx::Rect rect; | 116 gfx::Rect rect; |
119 // In RTL languages the marker gets drawn leftmost, so account for its offset. | 117 // In RTL languages the marker gets drawn leftmost, so account for its offset. |
120 if (base::i18n::IsRTL()) | 118 if (base::i18n::IsRTL()) |
121 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); | 119 rect = gfx::Rect(-kInset, 0, size().width(), size().height()); |
122 else | 120 else |
123 rect = gfx::Rect(kInset, 0, size().width(), size().height()); | 121 rect = gfx::Rect(kInset, 0, size().width(), size().height()); |
124 | 122 |
125 canvas->DrawStringRectWithHalo( | 123 canvas->DrawStringRectWithHalo( |
126 text(), | 124 text(), |
127 gfx::FontList(), | 125 gfx::FontList(), |
128 SK_ColorWHITE, | 126 SK_ColorWHITE, |
129 SK_ColorDKGRAY, | 127 SK_ColorDKGRAY, |
130 rect, | 128 rect, |
131 gfx::Canvas::NO_SUBPIXEL_RENDERING); | 129 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
132 | 130 |
133 // From MenuButton::PaintButton, paint the marker | 131 // From MenuButton::PaintButton, paint the marker |
134 PaintMenuMarker(canvas); | 132 PaintMenuMarker(canvas); |
135 } | 133 } |
136 | 134 |
137 void NewAvatarButton::OnAvatarMenuChanged(AvatarMenu* avatar_menu) { | 135 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
| 136 UpdateAvatarButtonAndRelayoutParent(); |
| 137 } |
| 138 |
| 139 void NewAvatarButton::OnProfileWasRemoved( |
| 140 const base::FilePath& profile_path, |
| 141 const base::string16& profile_name) { |
| 142 UpdateAvatarButtonAndRelayoutParent(); |
| 143 } |
| 144 |
| 145 void NewAvatarButton::OnProfileNameChanged( |
| 146 const base::FilePath& profile_path, |
| 147 const base::string16& old_profile_name) { |
| 148 UpdateAvatarButtonAndRelayoutParent(); |
| 149 } |
| 150 |
| 151 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
138 // We want the button to resize if the new text is shorter. | 152 // We want the button to resize if the new text is shorter. |
139 ClearMaxTextSize(); | 153 ClearMaxTextSize(); |
140 SetText(GetElidedText(profiles::GetActiveProfileDisplayName(browser_))); | 154 SetText(GetElidedText( |
| 155 profiles::GetAvatarNameForProfile(browser_->profile()))); |
141 | 156 |
142 // Because the width of the button might have changed, the parent browser | 157 // Because the width of the button might have changed, the parent browser |
143 // frame needs to recalculate the button bounds and redraw it. | 158 // frame needs to recalculate the button bounds and redraw it. |
144 parent()->Layout(); | 159 parent()->Layout(); |
145 } | 160 } |
OLD | NEW |