| 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/avatar_menu_button.h" | 5 #include "chrome/browser/ui/views/avatar_menu_button.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/profile_menu_model.h" |
| 9 #include "chrome/browser/ui/views/avatar_menu.h" |
| 7 #include "ui/gfx/canvas_skia.h" | 10 #include "ui/gfx/canvas_skia.h" |
| 8 #include "views/controls/menu/menu_model_adapter.h" | |
| 9 #include "views/widget/widget.h" | 11 #include "views/widget/widget.h" |
| 10 | 12 |
| 11 // Menu should display below the image on the frame. This | |
| 12 // offset size depends on whether the frame is in glass or opaque mode. | |
| 13 const int kMenuDisplayOffset = 5; | |
| 14 | |
| 15 static inline int Round(double x) { | 13 static inline int Round(double x) { |
| 16 return static_cast<int>(x + 0.5); | 14 return static_cast<int>(x + 0.5); |
| 17 } | 15 } |
| 18 | 16 |
| 19 AvatarMenuButton::AvatarMenuButton(const std::wstring& text, | 17 AvatarMenuButton::AvatarMenuButton(Browser* browser, bool has_menu) |
| 20 ui::MenuModel* menu_model) | 18 : MenuButton(NULL, std::wstring(), this, false), |
| 21 : MenuButton(NULL, text, this, false), | 19 browser_(browser), |
| 22 menu_model_(menu_model) { | 20 has_menu_(has_menu) { |
| 23 // In RTL mode, the avatar icon should be looking the opposite direction. | 21 // In RTL mode, the avatar icon should be looking the opposite direction. |
| 24 EnableCanvasFlippingForRTLUI(true); | 22 EnableCanvasFlippingForRTLUI(true); |
| 25 } | 23 } |
| 26 | 24 |
| 27 AvatarMenuButton::~AvatarMenuButton() {} | 25 AvatarMenuButton::~AvatarMenuButton() {} |
| 28 | 26 |
| 29 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) { | 27 void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) { |
| 30 const SkBitmap& icon = GetImageToPaint(); | 28 const SkBitmap& icon = GetImageToPaint(); |
| 31 if (icon.isNull()) | 29 if (icon.isNull()) |
| 32 return; | 30 return; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 46 // Round rather than truncating, so that for odd heights we select an extra | 44 // Round rather than truncating, so that for odd heights we select an extra |
| 47 // pixel below the image center rather than above. This is because the | 45 // pixel below the image center rather than above. This is because the |
| 48 // incognito image has shadows at the top that make the apparent center below | 46 // incognito image has shadows at the top that make the apparent center below |
| 49 // the real center. | 47 // the real center. |
| 50 int dst_y = Round((height() - dst_height) / 2.0); | 48 int dst_y = Round((height() - dst_height) / 2.0); |
| 51 | 49 |
| 52 canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), | 50 canvas->DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), |
| 53 dst_x, dst_y, dst_width, dst_height, false); | 51 dst_x, dst_y, dst_width, dst_height, false); |
| 54 } | 52 } |
| 55 | 53 |
| 54 bool AvatarMenuButton::HitTest(const gfx::Point& point) const { |
| 55 if (!has_menu_) |
| 56 return false; |
| 57 return views::MenuButton::HitTest(point); |
| 58 } |
| 59 |
| 56 // views::ViewMenuDelegate implementation | 60 // views::ViewMenuDelegate implementation |
| 57 void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | 61 void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { |
| 58 if (!menu_model_.get()) | 62 if (!has_menu_) |
| 59 return; | 63 return; |
| 60 | 64 |
| 61 views::MenuModelAdapter menu_model_adapter(menu_model_.get()); | 65 menu_model_.reset(new ProfileMenuModel(browser_)); |
| 62 views::MenuItemView menu(&menu_model_adapter); | 66 // The avatar menu will automatically delete itself when done. |
| 63 menu_model_adapter.BuildMenu(&menu); | 67 AvatarMenu* avatar_menu = |
| 64 | 68 new AvatarMenu(menu_model_.get(), browser_->profile()); |
| 65 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); | 69 avatar_menu->RunMenu(this); |
| 66 menu.RunMenuAt(source->GetWidget()->GetNativeWindow(), NULL, | |
| 67 gfx::Rect(pt, gfx::Size(0, 0)), | |
| 68 views::MenuItemView::TOPRIGHT, | |
| 69 true); | |
| 70 } | 70 } |
| OLD | NEW |