| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/avatar_menu_button.h" | |
| 6 | |
| 7 #include "ui/gfx/canvas_skia.h" | |
| 8 #include "views/controls/menu/menu_model_adapter.h" | |
| 9 #include "views/window/window.h" | |
| 10 | |
| 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) { | |
| 16 return static_cast<int>(floor(x + 0.5)); | |
| 17 } | |
| 18 | |
| 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 } | |
| 26 | |
| 27 AvatarMenuButton::~AvatarMenuButton() {} | |
| 28 | |
| 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 // Scale the height and maintain aspect ratio. This means that the | |
| 39 // icon may not fit in the view. That's ok, we just center it vertically. | |
| 40 float scale = | |
| 41 static_cast<float>(dst_width) / static_cast<float>(icon.width()); | |
| 42 int scaled_height = Round(height() / scale); | |
| 43 int src_height = std::min(scaled_height, icon.height()); | |
| 44 int src_y = Round((icon.height() - src_height) / 2.0); | |
| 45 int dst_height = src_height * scale; | |
| 46 int dst_y = Round((height() - dst_height) / 2.0); | |
| 47 | |
| 48 canvas->DrawBitmapInt(icon, src_x, src_y, src_width, src_height, | |
| 49 dst_x, dst_y, dst_width, dst_height, false); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 gfx::Size AvatarMenuButton::GetPreferredAvatarSize() { | |
| 54 return gfx::Size(38, 31); | |
| 55 } | |
| 56 | |
| 57 // views::ViewMenuDelegate implementation | |
| 58 void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point& pt) { | |
| 59 if (!menu_model_.get()) | |
| 60 return; | |
| 61 | |
| 62 views::MenuModelAdapter menu_model_adapter(menu_model_.get()); | |
| 63 views::MenuItemView menu(&menu_model_adapter); | |
| 64 menu_model_adapter.BuildMenu(&menu); | |
| 65 | |
| 66 gfx::Point menu_point(pt.x(), pt.y() + kMenuDisplayOffset); | |
| 67 menu.RunMenuAt(source->GetWindow()->GetNativeWindow(), NULL, | |
| 68 gfx::Rect(pt, gfx::Size(0, 0)), | |
| 69 views::MenuItemView::TOPRIGHT, | |
| 70 true); | |
| 71 } | |
| OLD | NEW |