| Index: chrome/browser/ui/views/avatar_menu_button.cc
|
| ===================================================================
|
| --- chrome/browser/ui/views/avatar_menu_button.cc (revision 88024)
|
| +++ chrome/browser/ui/views/avatar_menu_button.cc (working copy)
|
| @@ -2,55 +2,64 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "chrome/browser/ui/views/profile_menu_button.h"
|
| +#include "chrome/browser/ui/views/avatar_menu_button.h"
|
|
|
| -#include "chrome/browser/ui/profile_menu_model.h"
|
| -#include "ui/base/text/text_elider.h"
|
| -#include "ui/gfx/color_utils.h"
|
| -#include "views/controls/button/button.h"
|
| -#include "views/controls/menu/menu_item_view.h"
|
| +#include "ui/gfx/canvas_skia.h"
|
| #include "views/controls/menu/menu_model_adapter.h"
|
| #include "views/window/window.h"
|
|
|
| -// Menu should display below the profile button tag image on the frame. This
|
| +// Menu should display below the image on the frame. This
|
| // offset size depends on whether the frame is in glass or opaque mode.
|
| -const int kMenuDisplayOffset = 7;
|
| +const int kMenuDisplayOffset = 5;
|
|
|
| -// TextHover is slightly darker than enabled color, for a subtle hover shift.
|
| -const SkColor kTextHover = 0xFFDDDDDD;
|
| -const SkColor kTextEnabled = SK_ColorWHITE;
|
| -const SkColor kTextHighlighted = SK_ColorWHITE;
|
| +static inline int Round(double x) {
|
| + return static_cast<int>(floor(x + 0.5));
|
| +}
|
|
|
| -// Horizontal padding beside profile menu button, to center it in the
|
| -// underlying tag image.
|
| -const int kProfileButtonBorderSpacing = 10;
|
| +AvatarMenuButton::AvatarMenuButton(const std::wstring& text,
|
| + ui::MenuModel* menu_model)
|
| + : MenuButton(NULL, text, this, false),
|
| + menu_model_(menu_model) {
|
| + // In RTL mode, the avatar icon should be looking the opposite direction.
|
| + EnableCanvasFlippingForRTLUI(true);
|
| +}
|
|
|
| -// Maximum width for name string in pixels.
|
| -const int kMaxTextWidth = 200;
|
| +AvatarMenuButton::~AvatarMenuButton() {}
|
|
|
| -ProfileMenuButton::ProfileMenuButton(const std::wstring& text, Profile* profile)
|
| - : MenuButton(NULL, text, this, true) {
|
| - // Turn off hover highlighting and position button in the center of the
|
| - // underlying profile tag image.
|
| - set_border(views::Border::CreateEmptyBorder(
|
| - 0, kProfileButtonBorderSpacing, 0, kProfileButtonBorderSpacing));
|
| - SetHoverColor(kTextHover);
|
| - SetEnabledColor(kTextEnabled);
|
| - SetHighlightColor(kTextHighlighted);
|
| +void AvatarMenuButton::OnPaint(gfx::Canvas* canvas) {
|
| + const SkBitmap& icon = GetImageToPaint();
|
| + if (!icon.isNull()) {
|
| + // Scale the image to fit the width of the button.
|
| + int src_width = icon.width();
|
| + int src_x = 0;
|
| + int dst_width = std::min(src_width, width());
|
| + int dst_x = Round((width() - dst_width) / 2.0);
|
|
|
| - profile_menu_model_.reset(new ProfileMenuModel);
|
| + // Scale the height and maintain aspect ratio. This means that the
|
| + // icon may not fit in the view. That's ok, we just center it vertically.
|
| + float scale =
|
| + static_cast<float>(dst_width) / static_cast<float>(icon.width());
|
| + int scaled_height = Round(height() / scale);
|
| + int src_height = std::min(scaled_height, icon.height());
|
| + int src_y = Round((icon.height() - src_height) / 2.0);
|
| + int dst_height = src_height * scale;
|
| + int dst_y = Round((height() - dst_height) / 2.0);
|
| +
|
| + canvas->DrawBitmapInt(icon, src_x, src_y, src_width, src_height,
|
| + dst_x, dst_y, dst_width, dst_height, false);
|
| + }
|
| }
|
|
|
| -ProfileMenuButton::~ProfileMenuButton() {}
|
| -
|
| -void ProfileMenuButton::SetText(const std::wstring& text) {
|
| - MenuButton::SetText(UTF16ToWideHack(ui::ElideText(WideToUTF16Hack(text),
|
| - font(), kMaxTextWidth, false)));
|
| +gfx::Size AvatarMenuButton::GetPreferredAvatarSize() {
|
| + return gfx::Size(38, 31);
|
| }
|
|
|
| // views::ViewMenuDelegate implementation
|
| -void ProfileMenuButton::RunMenu(views::View* source, const gfx::Point &pt) {
|
| - views::MenuModelAdapter menu_model_adapter(profile_menu_model_.get());
|
| +void AvatarMenuButton::RunMenu(views::View* source, const gfx::Point& pt) {
|
| + if (!menu_model_.get())
|
| + return;
|
| +
|
| + views::MenuModelAdapter menu_model_adapter(menu_model_.get());
|
| views::MenuItemView menu(&menu_model_adapter);
|
| menu_model_adapter.BuildMenu(&menu);
|
|
|
|
|