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 static inline int Round(double x) { |
20 const SkColor kTextHover = 0xFFDDDDDD; | 16 return static_cast<int>(floor(x + 0.5)); |
21 const SkColor kTextEnabled = SK_ColorWHITE; | |
22 const SkColor kTextHighlighted = SK_ColorWHITE; | |
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 } | 17 } |
43 | 18 |
44 ProfileMenuButton::~ProfileMenuButton() {} | 19 AvatarMenuButton::AvatarMenuButton(const std::wstring& text, |
20 ui::MenuModel* menu_model) | |
21 : MenuButton(NULL, text, this, false), | |
22 menu_model_(menu_model) { | |
23 // In RTL mode, the avatar icon should be looking the opposite direction. | |
24 EnableCanvasFlippingForRTLUI(true); | |
25 } | |
45 | 26 |
46 void ProfileMenuButton::SetText(const std::wstring& text) { | 27 AvatarMenuButton::~AvatarMenuButton() {} |
47 MenuButton::SetText(UTF16ToWideHack(ui::ElideText(WideToUTF16Hack(text), | 28 |
48 font(), kMaxTextWidth, false))); | 29 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) { |
30 const SkBitmap& icon = GetImageToPaint(); | |
31 if (!icon.isNull()) { | |
32 // Scale the image to fit the width of the button. | |
33 int src_width = icon.width(); | |
34 int src_x = 0; | |
35 int dst_width = std::min(src_width, width()); | |
36 int dst_x = Round((width() - dst_width) / 2.0); | |
37 | |
38 float scale = | |
sky
2011/06/10 15:08:08
Don't you need to also worry about the height not
sail
2011/06/10 17:10:35
If the height doesn't then it just gets cropped. T
| |
39 static_cast<float>(dst_width) / static_cast<float>(icon.width()); | |
40 int scaled_height = Round(height() / scale); | |
41 int src_height = std::min(scaled_height, icon.height()); | |
42 int src_y = Round((icon.height() - src_height) / 2.0); | |
43 int dst_height = src_height * scale; | |
44 int dst_y = Round((height() - dst_height) / 2.0); | |
45 | |
46 canvas->DrawBitmapInt(icon, src_x, src_y, src_width, src_height, | |
47 dst_x, dst_y, dst_width, dst_height, false); | |
48 } | |
49 } | |
50 | |
51 gfx::Size AvatarMenuButton::GetPreferredAvatarSize() { | |
52 return gfx::Size(38, 31); | |
49 } | 53 } |
50 | 54 |
51 // views::ViewMenuDelegate implementation | 55 // views::ViewMenuDelegate implementation |
52 void ProfileMenuButton::RunMenu(views::View* source, const gfx::Point &pt) { | 56 void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
53 views::MenuModelAdapter menu_model_adapter(profile_menu_model_.get()); | 57 if (!menu_model_.get()) |
58 return; | |
59 | |
60 views::MenuModelAdapter menu_model_adapter(menu_model_.get()); | |
54 views::MenuItemView menu(&menu_model_adapter); | 61 views::MenuItemView menu(&menu_model_adapter); |
55 menu_model_adapter.BuildMenu(&menu); | 62 menu_model_adapter.BuildMenu(&menu); |
56 | 63 |
57 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); | 64 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); |
58 menu.RunMenuAt(source->GetWindow()->GetNativeWindow(), NULL, | 65 menu.RunMenuAt(source->GetWindow()->GetNativeWindow(), NULL, |
59 gfx::Rect(pt, gfx::Size(0, 0)), | 66 gfx::Rect(pt, gfx::Size(0, 0)), |
60 views::MenuItemView::TOPRIGHT, | 67 views::MenuItemView::TOPRIGHT, |
61 true); | 68 true); |
62 } | 69 } |
OLD | NEW |