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 kItemWidth = 210; | |
| 23 const int kItemHeight = 32; | |
| 24 const int kItemMarginY = 8; | |
| 25 const int kIconWidth = 38; | |
| 26 const int kIconMarginX = 6; | |
| 27 const int kAddUserButtonHeight = 20; | |
| 28 const int kEditUserButtonWidth = 19; | |
| 29 const int kEditUserButtonMarginX = 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 { | |
|
Peter Kasting
2011/08/05 18:08:33
You should use a Link object for this, it will do
sail
2011/08/09 00:47:47
Done.
| |
| 57 public: | |
| 58 explicit AddUserButton(views::ButtonListener* listener) | |
| 59 : views::ImageButton(listener) { | |
| 60 } | |
| 61 | |
| 62 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 63 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 64 int x = kIconWidth + kIconMarginX; | |
| 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, kIconWidth, 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.GetImageNamed(IDR_PROFILE_SELECTED); | |
| 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() + kIconMarginX; | |
| 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 | |
|
Peter Kasting
2011/08/05 18:08:33
Nit: No else after return
sail
2011/08/09 00:47:47
Done.
| |
| 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 profile_index() { | |
| 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 | |
| 154 AvatarMenuBubbleView::~AvatarMenuBubbleView() { | |
| 155 } | |
| 156 | |
| 157 gfx::Size AvatarMenuBubbleView::GetPreferredSize() { | |
| 158 return gfx::Size(kItemWidth, (kItemHeight + kItemMarginY) * | |
|
Peter Kasting
2011/08/05 18:08:33
How come we hardcode the size and never check the
sail
2011/08/09 00:47:47
Done.
| |
| 159 item_views_.size() + kAddUserButtonHeight); | |
| 160 } | |
| 161 | |
| 162 void AvatarMenuBubbleView::Layout() { | |
|
Peter Kasting
2011/08/05 18:08:33
I think you can get rid of most of this code if yo
sail
2011/08/09 00:47:47
I spent half a day on this and I couldn't get a co
| |
| 163 int kItemWidth = width() - kEditUserButtonWidth - kEditUserButtonMarginX; | |
| 164 | |
| 165 for (size_t i = 0; i < item_views_.size(); ++i) { | |
| 166 views::ImageButton* item_view = item_views_[i]; | |
| 167 int y = (kItemHeight + kItemMarginY) * i; | |
| 168 item_view->SetBounds(0, y, kItemWidth, kItemHeight); | |
| 169 } | |
| 170 | |
| 171 int y = (kItemHeight + kItemMarginY) * item_views_.size(); | |
| 172 add_user_button_->SetBounds(0, y, width(), kAddUserButtonHeight); | |
| 173 | |
| 174 y = (kItemHeight + kItemMarginY) * edit_user_button_->profile_index(); | |
| 175 edit_user_button_->SetBounds(width() - kEditUserButtonWidth, y, | |
| 176 kEditUserButtonWidth, kItemHeight); | |
| 177 } | |
| 178 | |
| 179 void AvatarMenuBubbleView::ButtonPressed(views::Button* sender, | |
| 180 const views::Event& event) { | |
| 181 if (sender == add_user_button_) { | |
| 182 avatar_menu_model_->AddNewProfile(); | |
| 183 } else if (sender == edit_user_button_) { | |
| 184 avatar_menu_model_->EditProfile(edit_user_button_->profile_index()); | |
| 185 } else { | |
| 186 for (size_t i = 0; i < item_views_.size(); ++i) { | |
| 187 if (sender == item_views_[i]) { | |
| 188 avatar_menu_model_->SwitchToProfile(i); | |
| 189 break; | |
| 190 } | |
| 191 } | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 void AvatarMenuBubbleView::BubbleClosing(Bubble* bubble, | |
| 196 bool closed_by_escape) { | |
| 197 } | |
| 198 | |
| 199 bool AvatarMenuBubbleView::CloseOnEscape() { | |
| 200 return true; | |
| 201 } | |
| 202 | |
| 203 bool AvatarMenuBubbleView::FadeInOnShow() { | |
| 204 return false; | |
| 205 } | |
| 206 | |
| 207 void AvatarMenuBubbleView::OnAvatarMenuModelChanged( | |
| 208 AvatarMenuModel* avatar_menu_model) { | |
| 209 // Unset all our child view references and call RemoveAllChildViews() which | |
| 210 // will actually delete them. | |
| 211 add_user_button_ = NULL; | |
| 212 edit_user_button_ = NULL; | |
| 213 item_views_.clear(); | |
| 214 RemoveAllChildViews(true); | |
| 215 | |
| 216 size_t active_profile_index = 0; | |
| 217 for (size_t i = 0; i < avatar_menu_model->GetNumberOfItems(); ++i) { | |
| 218 const AvatarMenuModel::Item& item = avatar_menu_model->GetItemAt(i); | |
| 219 ProfileItemView* item_view = new ProfileItemView(item, this); | |
| 220 AddChildView(item_view); | |
| 221 item_views_.push_back(item_view); | |
| 222 | |
| 223 if (item.active) | |
| 224 active_profile_index = i; | |
| 225 } | |
| 226 | |
| 227 add_user_button_ = new AddUserButton(this); | |
| 228 AddChildView(add_user_button_); | |
| 229 | |
| 230 edit_user_button_ = new EditUserButton(active_profile_index, this); | |
| 231 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 232 edit_user_button_->SetImage(views::CustomButton::BS_NORMAL, | |
| 233 rb.GetImageNamed(IDR_PROFILE_EDIT)); | |
| 234 edit_user_button_->SetImage(views::CustomButton::BS_HOT, | |
| 235 rb.GetImageNamed(IDR_PROFILE_EDIT_HOVER)); | |
| 236 edit_user_button_->SetImage(views::CustomButton::BS_PUSHED, | |
| 237 rb.GetImageNamed(IDR_PROFILE_EDIT_PRESSED)); | |
| 238 edit_user_button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
| 239 views::ImageButton::ALIGN_MIDDLE); | |
| 240 AddChildView(edit_user_button_); | |
| 241 | |
| 242 PreferredSizeChanged(); | |
| 243 } | |
| OLD | NEW |