Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/profile_menu_button.h" | 5 #include "chrome/browser/ui/views/avatar_menu_button.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/profile_menu_model.h" | 7 #include "ui/gfx/canvas_skia.h" |
| 8 #include "ui/base/text/text_elider.h" | |
| 9 #include "ui/gfx/color_utils.h" | |
| 10 #include "views/controls/button/button.h" | |
| 11 #include "views/controls/menu/menu_item_view.h" | |
| 12 #include "views/controls/menu/menu_model_adapter.h" | 8 #include "views/controls/menu/menu_model_adapter.h" |
| 13 #include "views/window/window.h" | 9 #include "views/window/window.h" |
| 14 | 10 |
| 15 // Menu should display below the profile button tag image on the frame. This | 11 // Menu should display below the image on the frame. This |
| 16 // offset size depends on whether the frame is in glass or opaque mode. | 12 // offset size depends on whether the frame is in glass or opaque mode. |
| 17 const int kMenuDisplayOffset = 7; | 13 const int kMenuDisplayOffset = 5; |
| 18 | 14 |
| 19 // TextHover is slightly darker than enabled color, for a subtle hover shift. | 15 AvatarMenuButton::AvatarMenuButton(const std::wstring& text, |
| 20 const SkColor kTextHover = 0xFFDDDDDD; | 16 ui::MenuModel* menu_model) |
| 21 const SkColor kTextEnabled = SK_ColorWHITE; | 17 : MenuButton(NULL, text, this, false), |
| 22 const SkColor kTextHighlighted = SK_ColorWHITE; | 18 menu_model_(menu_model) { |
| 23 | |
| 24 // Horizontal padding beside profile menu button, to center it in the | |
| 25 // underlying tag image. | |
| 26 const int kProfileButtonBorderSpacing = 10; | |
| 27 | |
| 28 // Maximum width for name string in pixels. | |
| 29 const int kMaxTextWidth = 200; | |
| 30 | |
| 31 ProfileMenuButton::ProfileMenuButton(const std::wstring& text, Profile* profile) | |
| 32 : MenuButton(NULL, text, this, true) { | |
| 33 // Turn off hover highlighting and position button in the center of the | |
| 34 // underlying profile tag image. | |
| 35 set_border(views::Border::CreateEmptyBorder( | |
| 36 0, kProfileButtonBorderSpacing, 0, kProfileButtonBorderSpacing)); | |
| 37 SetHoverColor(kTextHover); | |
| 38 SetEnabledColor(kTextEnabled); | |
| 39 SetHighlightColor(kTextHighlighted); | |
| 40 | |
| 41 profile_menu_model_.reset(new ProfileMenuModel); | |
| 42 } | 19 } |
| 43 | 20 |
| 44 ProfileMenuButton::~ProfileMenuButton() {} | 21 AvatarMenuButton::~AvatarMenuButton() {} |
| 45 | 22 |
| 46 void ProfileMenuButton::SetText(const std::wstring& text) { | 23 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) { |
| 47 MenuButton::SetText(UTF16ToWideHack(ui::ElideText(WideToUTF16Hack(text), | 24 // In RTL mode, the avatar icon should be looking the opposite direction. |
| 48 font(), kMaxTextWidth, false))); | 25 canvas->Save(); |
| 26 if (base::i18n::IsRTL()) { | |
|
Peter Kasting
2011/06/09 18:03:31
You should not need to do this manually. See View
sail
2011/06/10 00:56:20
Done.
| |
| 27 canvas->TranslateInt(width(), 0); | |
| 28 canvas->ScaleInt(-1, 1); | |
| 29 } | |
| 30 | |
| 31 SkBitmap icon = GetImageToPaint(); | |
| 32 if (!icon.isNull()) { | |
| 33 int w = icon.width(); | |
| 34 int h = icon.height(); | |
| 35 canvas->DrawBitmapInt(icon, 0, | |
| 36 // Bias the rounding to select a region that's lower rather than higher, | |
| 37 // as the shadows at the image top mean the apparent center is below the | |
| 38 // real center. | |
| 39 ((icon.height() - icon.height()) + 1) / 2, w, h, | |
|
Miranda Callahan
2011/06/09 13:23:07
Looks like you are calculating (x - x) here? (Als
Peter Kasting
2011/06/09 18:03:31
Yeah, I think one of these is supposed to be the b
sail
2011/06/10 00:56:20
Fixed.
I changed the code to be a little more gene
| |
| 40 0, 0, width(), height(), false); | |
| 41 } | |
| 42 | |
| 43 canvas->Restore(); | |
| 49 } | 44 } |
| 50 | 45 |
| 51 // views::ViewMenuDelegate implementation | 46 // views::ViewMenuDelegate implementation |
| 52 void ProfileMenuButton::RunMenu(views::View* source, const gfx::Point &pt) { | 47 void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point &pt) { |
|
Peter Kasting
2011/06/09 18:03:31
Nit: While here, fix the position of '&'
sail
2011/06/10 00:56:20
Done.
| |
| 53 views::MenuModelAdapter menu_model_adapter(profile_menu_model_.get()); | 48 if (!menu_model_.get()) |
| 49 return; | |
| 50 | |
| 51 views::MenuModelAdapter menu_model_adapter(menu_model_.get()); | |
| 54 views::MenuItemView menu(&menu_model_adapter); | 52 views::MenuItemView menu(&menu_model_adapter); |
| 55 menu_model_adapter.BuildMenu(&menu); | 53 menu_model_adapter.BuildMenu(&menu); |
| 56 | 54 |
| 57 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); | 55 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); |
| 58 menu.RunMenuAt(source->GetWindow()->GetNativeWindow(), NULL, | 56 menu.RunMenuAt(source->GetWindow()->GetNativeWindow(), NULL, |
| 59 gfx::Rect(pt, gfx::Size(0, 0)), | 57 gfx::Rect(pt, gfx::Size(0, 0)), |
| 60 views::MenuItemView::TOPRIGHT, | 58 views::MenuItemView::TOPRIGHT, |
| 61 true); | 59 true); |
| 62 } | 60 } |
| OLD | NEW |