Chromium Code Reviews| 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_bubble_view.h" | |
| 6 | |
| 7 #include "chrome/browser/browser_process.h" | |
| 8 #include "chrome/browser/ui/browser.h" | |
| 9 #include "chrome/browser/profiles/avatar_menu_model.h" | |
| 10 #include "chrome/browser/profiles/profile_info_cache.h" | |
| 11 #include "chrome/browser/profiles/profile_manager.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 #include "grit/theme_resources.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/gfx/canvas.h" | |
| 17 #include "ui/gfx/font.h" | |
| 18 #include "ui/gfx/image/image.h" | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int ITEM_WIDTH = 210; | |
|
Robert Sesek
2011/08/05 00:44:03
Shouldn't constants be in kCamelCase? That's what
sail
2011/08/05 01:28:21
Done.
| |
| 23 const int ITEM_HEIGHT = 32; | |
| 24 const int ITEM_MARGIN_Y = 8; | |
| 25 const int ICON_WIDTH = 38; | |
| 26 const int ICON_MARGIN_X = 6; | |
| 27 const int ADD_USER_BUTTON_HEIGHT = 20; | |
| 28 const int EDIT_USER_BUTTON_WIDTH = 19; | |
| 29 const int EDIT_USER_BUTTON_MARGIN_X = 3; | |
| 30 | |
| 31 inline int Round(double x) { | |
| 32 return static_cast<int>(x + 0.5); | |
| 33 } | |
| 34 | |
| 35 gfx::Rect GetCenteredAndScaledRect(int src_width, int src_height, | |
| 36 int dst_x, int dst_y, | |
| 37 int dst_width, int dst_height) { | |
| 38 int scaled_width; | |
| 39 int scaled_height; | |
| 40 if (src_width > src_height) { | |
| 41 scaled_width = std::min(src_width, dst_width); | |
| 42 float scale = static_cast<float>(scaled_width) / | |
| 43 static_cast<float>(src_width); | |
| 44 scaled_height = Round(src_height * scale); | |
| 45 } else { | |
| 46 scaled_height = std::min(src_height, dst_height); | |
| 47 float scale = static_cast<float>(scaled_height) / | |
| 48 static_cast<float>(src_height); | |
| 49 scaled_width = Round(src_width * scale); | |
| 50 } | |
| 51 int x = dst_x + (dst_width - scaled_width) / 2; | |
| 52 int y = dst_y + (dst_height - scaled_height) / 2; | |
| 53 return gfx::Rect(x, y, scaled_width, scaled_height); | |
| 54 } | |
| 55 | |
| 56 class AddUserButton : public views::ImageButton { | |
| 57 public: | |
| 58 explicit AddUserButton(views::ButtonListener* listener) | |
| 59 : views::ImageButton(listener) { | |
|
Robert Sesek
2011/08/05 00:44:03
nit: indent 4
sail
2011/08/05 01:28:21
Done.
| |
| 60 } | |
| 61 | |
| 62 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 63 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 64 int x = ICON_WIDTH + ICON_MARGIN_X; | |
| 65 int w = width() - x; | |
| 66 | |
| 67 gfx::Font font(rb.GetFont(ResourceBundle::BaseFont)); | |
| 68 font = font.DeriveFont(0, gfx::Font::UNDERLINED); | |
| 69 | |
| 70 canvas->DrawStringInt( | |
| 71 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK), font, | |
| 72 SkColorSetRGB(0, 0x79, 0xda), x, 0, w, height()); | |
| 73 } | |
| 74 }; | |
| 75 | |
| 76 class ProfileItemView : public views::ImageButton { | |
| 77 public: | |
| 78 ProfileItemView(const AvatarMenuModel::Item& item, | |
| 79 views::ButtonListener* listener) | |
| 80 : views::ImageButton(listener), | |
| 81 item_(item) { | |
| 82 } | |
| 83 | |
| 84 void OnPaint(gfx::Canvas* canvas) { | |
| 85 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 86 | |
| 87 // Draw the profile icon on the left. | |
| 88 SkBitmap profile_icon = item_.icon; | |
| 89 gfx::Rect profile_icon_rect = GetCenteredAndScaledRect( | |
| 90 profile_icon.width(), profile_icon.height(), | |
| 91 0, 0, ICON_WIDTH, height()); | |
| 92 canvas->DrawBitmapInt(profile_icon, 0, 0, profile_icon.width(), | |
| 93 profile_icon.height(), profile_icon_rect.x(), | |
| 94 profile_icon_rect.y(), profile_icon_rect.width(), | |
| 95 profile_icon_rect.height(), false); | |
| 96 | |
| 97 // If this profile is selected then draw a check mark on the bottom right | |
| 98 // of the profile icon. | |
| 99 if (item_.active) { | |
| 100 SkBitmap check_icon = *rb.GetBitmapNamed(IDR_PROFILE_SELECTED); | |
|
Robert Sesek
2011/08/05 00:44:03
Use GetImageNamed()
sail
2011/08/05 01:28:21
Done.
| |
| 101 int y = profile_icon_rect.bottom() - check_icon.height(); | |
| 102 int x = profile_icon_rect.right() - check_icon.width() + 2; | |
| 103 canvas->DrawBitmapInt(check_icon, 0, 0, check_icon.width(), | |
| 104 check_icon.height(), x, y, check_icon.width(), | |
| 105 check_icon.height(), false); | |
| 106 } | |
| 107 | |
| 108 // Draw the profile name to the right of the profile icon. | |
| 109 int name_x = profile_icon_rect.right() + ICON_MARGIN_X; | |
| 110 canvas->DrawStringInt(item_.name, rb.GetFont(ResourceBundle::BaseFont), | |
| 111 GetNameColor(), name_x, 0, width() - name_x, | |
| 112 height()); | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 SkColor GetNameColor() { | |
| 117 bool normal = state() != views::CustomButton::BS_PUSHED && | |
| 118 state() != views::CustomButton::BS_HOT; | |
| 119 if (item_.active) | |
| 120 return normal ? SkColorSetRGB(30, 30, 30) : SkColorSetRGB(0, 0, 0); | |
| 121 else | |
| 122 return normal ? SkColorSetRGB(128, 128, 128) : SkColorSetRGB(64, 64, 64); | |
| 123 } | |
| 124 | |
| 125 AvatarMenuModel::Item item_; | |
| 126 }; | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 class EditUserButton : public views::ImageButton { | |
| 131 public: | |
| 132 EditUserButton(size_t profile_index, views::ButtonListener* listener) | |
| 133 : views::ImageButton(listener), | |
| 134 profile_index_(profile_index) { | |
| 135 } | |
| 136 | |
| 137 size_t GetProfileIndex() { | |
|
Robert Sesek
2011/08/05 00:44:03
profile_index()
sail
2011/08/05 01:28:21
Done.
| |
| 138 return profile_index_; | |
| 139 } | |
| 140 | |
| 141 private: | |
| 142 size_t profile_index_; | |
| 143 }; | |
| 144 | |
| 145 AvatarMenuBubbleView::AvatarMenuBubbleView(Browser* browser) | |
| 146 : add_user_button_(NULL), | |
| 147 browser_(browser), | |
| 148 edit_user_button_(NULL) { | |
| 149 avatar_menu_model_.reset(new AvatarMenuModel( | |
| 150 &g_browser_process->profile_manager()->GetProfileInfoCache(), | |
| 151 this, browser_)); | |
| 152 | |
| 153 size_t active_profile_index = 0; | |
|
Robert Sesek
2011/08/05 00:44:03
Move this into OnAvatarMenuModelChanged(). That wi
sail
2011/08/05 01:28:21
Done.
sail
2011/08/05 01:28:21
Done.
| |
| 154 for (size_t i = 0; i < avatar_menu_model_->GetNumberOfItems(); ++i) { | |
| 155 const AvatarMenuModel::Item& item = avatar_menu_model_->GetItemAt(i); | |
| 156 ProfileItemView* item_view = new ProfileItemView(item, this); | |
| 157 AddChildView(item_view); | |
| 158 item_views_.push_back(item_view); | |
| 159 | |
| 160 if (item.active) | |
| 161 active_profile_index = i; | |
| 162 } | |
| 163 | |
| 164 add_user_button_ = new AddUserButton(this); | |
| 165 AddChildView(add_user_button_); | |
| 166 | |
| 167 edit_user_button_ = new EditUserButton(active_profile_index, this); | |
| 168 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 169 edit_user_button_->SetImage(views::CustomButton::BS_NORMAL, | |
| 170 rb.GetBitmapNamed(IDR_PROFILE_EDIT)); | |
|
Robert Sesek
2011/08/05 00:44:03
GetImageNamed() here and throughout
sail
2011/08/05 01:28:21
Done.
| |
| 171 edit_user_button_->SetImage(views::CustomButton::BS_HOT, | |
| 172 rb.GetBitmapNamed(IDR_PROFILE_EDIT_HOVER)); | |
| 173 edit_user_button_->SetImage(views::CustomButton::BS_PUSHED, | |
| 174 rb.GetBitmapNamed(IDR_PROFILE_EDIT_PRESSED)); | |
| 175 edit_user_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
| 176 views::ImageButton::ALIGN_MIDDLE); | |
| 177 AddChildView(edit_user_button_); | |
| 178 } | |
| 179 | |
| 180 AvatarMenuBubbleView::~AvatarMenuBubbleView() { | |
| 181 } | |
| 182 | |
| 183 gfx::Size AvatarMenuBubbleView::GetPreferredSize() { | |
| 184 return gfx::Size(ITEM_WIDTH, (ITEM_HEIGHT + ITEM_MARGIN_Y) * | |
| 185 item_views_.size() + ADD_USER_BUTTON_HEIGHT); | |
| 186 } | |
| 187 | |
| 188 void AvatarMenuBubbleView::Layout() { | |
| 189 int item_width = width() - EDIT_USER_BUTTON_WIDTH - EDIT_USER_BUTTON_MARGIN_X; | |
| 190 | |
| 191 for (size_t i = 0; i < item_views_.size(); ++i) { | |
| 192 views::ImageButton* item_view = item_views_[i]; | |
| 193 int y = (ITEM_HEIGHT + ITEM_MARGIN_Y) * i; | |
| 194 item_view->SetBounds(0, y, item_width, ITEM_HEIGHT); | |
| 195 } | |
| 196 | |
| 197 int y = (ITEM_HEIGHT + ITEM_MARGIN_Y) * item_views_.size(); | |
| 198 add_user_button_->SetBounds(0, y, width(), ADD_USER_BUTTON_HEIGHT); | |
| 199 | |
| 200 y = (ITEM_HEIGHT + ITEM_MARGIN_Y) * edit_user_button_->GetProfileIndex(); | |
| 201 edit_user_button_->SetBounds(width() - EDIT_USER_BUTTON_WIDTH, y, | |
| 202 EDIT_USER_BUTTON_WIDTH, ITEM_HEIGHT); | |
| 203 } | |
| 204 | |
| 205 void AvatarMenuBubbleView::ButtonPressed(views::Button* sender, | |
| 206 const views::Event& event) { | |
| 207 if (sender == add_user_button_) { | |
| 208 avatar_menu_model_->AddNewProfile(); | |
| 209 } else if (sender == edit_user_button_) { | |
| 210 avatar_menu_model_->EditProfile(edit_user_button_->GetProfileIndex()); | |
| 211 } else { | |
| 212 for (size_t i = 0; i < item_views_.size(); ++i) { | |
| 213 if (sender == item_views_[i]) { | |
| 214 avatar_menu_model_->SwitchToProfile(i); | |
| 215 break; | |
| 216 } | |
| 217 } | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 void AvatarMenuBubbleView::BubbleClosing(Bubble* bubble, | |
| 222 bool closed_by_escape) { | |
| 223 } | |
| 224 | |
| 225 bool AvatarMenuBubbleView::CloseOnEscape() { | |
| 226 return true; | |
| 227 } | |
| 228 | |
| 229 bool AvatarMenuBubbleView::FadeInOnShow() { | |
| 230 return false; | |
| 231 } | |
| 232 | |
| 233 void AvatarMenuBubbleView::OnAvatarMenuModelChanged() { | |
| 234 } | |
| OLD | NEW |