Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: ui/app_list/app_list_item_view.cc

Issue 10533032: cleanup: Remove applist v1 code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/app_list/app_list_item_view.h ('k') | ui/app_list/app_list_view.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "ui/app_list/app_list_item_view.h" 5 #include "ui/app_list/app_list_item_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 14 matching lines...) Expand all
25 #include "ui/views/controls/image_view.h" 25 #include "ui/views/controls/image_view.h"
26 #include "ui/views/controls/menu/menu_item_view.h" 26 #include "ui/views/controls/menu/menu_item_view.h"
27 #include "ui/views/controls/menu/menu_model_adapter.h" 27 #include "ui/views/controls/menu/menu_model_adapter.h"
28 #include "ui/views/controls/menu/menu_runner.h" 28 #include "ui/views/controls/menu/menu_runner.h"
29 29
30 namespace app_list { 30 namespace app_list {
31 31
32 namespace { 32 namespace {
33 33
34 const int kTopBottomPadding = 10; 34 const int kTopBottomPadding = 10;
35 const int kTopPaddingV2 = 20; 35 const int kTopPadding = 20;
36 const int kIconTitleSpacing = 7; 36 const int kIconTitleSpacing = 7;
37 37
38 const SkColor kTitleColor = SK_ColorWHITE; 38 const SkColor kTitleColor = SkColorSetRGB(0x5A, 0x5A, 0x5A);
39 const SkColor kTitleColorV2 = SkColorSetRGB(0x5A, 0x5A, 0x5A); 39 const SkColor kTitleHoverColor = SkColorSetRGB(0x3C, 0x3C, 0x3C);
40 const SkColor kTitleColorHoverV2 = SkColorSetRGB(0x3C, 0x3C, 0x3C); 40 const SkColor kTitleBackgroundColor = SkColorSetRGB(0xFC, 0xFC, 0xFC);
41 const SkColor kTitleBackgroundColorV2 = SkColorSetRGB(0xFC, 0xFC, 0xFC);
42 41
43 const SkColor kHoverAndPushedColor = SkColorSetARGB(0x55, 0, 0, 0); 42 const SkColor kHoverAndPushedColor = SkColorSetARGB(0x19, 0, 0, 0);
44 const SkColor kHoverAndPushedColorV2 = SkColorSetARGB(0x19, 0, 0, 0); 43 const SkColor kSelectedColor = SkColorSetARGB(0x0D, 0, 0, 0);
44 const SkColor kHighlightedColor = kHoverAndPushedColor;
45 45
46 const SkColor kSelectedColor = SkColorSetARGB(0x2A, 0, 0, 0); 46 const int kTitleFontSize = 11;
47 const SkColor kSelectedColorV2 = SkColorSetARGB(0x0D, 0, 0, 0);
48
49 const SkColor kHighlightedColor = kHoverAndPushedColor;
50 const SkColor kHighlightedColorV2 = kHoverAndPushedColorV2;
51
52 // FontSize/IconSize ratio = 24 / 128, which means we should get 24 font size
53 // when icon size is 128.
54 const float kFontSizeToIconSizeRatio = 0.1875f;
55
56 // Font smaller than kBoldFontSize needs to be bold.
57 const int kBoldFontSize = 14;
58
59 const int kMinFontSize = 12;
60
61 const int kFixedFontSize = 11; // Font size for fixed layout.
62
63 const int kMinTitleChars = 15;
64
65 const int kLeftRightPaddingChars = 1; 47 const int kLeftRightPaddingChars = 1;
66 48
67 const gfx::Font& GetTitleFontForIconSize(const gfx::Size& size, bool fixed) { 49 const gfx::Font& GetTitleFont() {
68 static int icon_height;
69 static gfx::Font* font = NULL; 50 static gfx::Font* font = NULL;
70 51
71 // Reuses current font for fixed layout or icon height is the same. 52 // Reuses current font.
72 if (font && (fixed || icon_height == size.height())) 53 if (font)
73 return *font; 54 return *font;
74 55
75 delete font;
76
77 icon_height = size.height();
78 int font_size = fixed ? kFixedFontSize :
79 std::max(static_cast<int>(icon_height * kFontSizeToIconSizeRatio),
80 kMinFontSize);
81
82 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 56 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
83 gfx::Font title_font(rb.GetFont(ui::ResourceBundle::BaseFont).GetFontName(), 57 gfx::Font title_font(rb.GetFont(ui::ResourceBundle::BaseFont).GetFontName(),
84 font_size); 58 kTitleFontSize);
85 if (font_size <= kBoldFontSize) 59 title_font = title_font.DeriveFont(0, gfx::Font::BOLD);
86 title_font = title_font.DeriveFont(0, gfx::Font::BOLD);
87 font = new gfx::Font(title_font); 60 font = new gfx::Font(title_font);
88 return *font; 61 return *font;
89 } 62 }
90 63
91 // An image view that is not interactive. 64 // An image view that is not interactive.
92 class StaticImageView : public views::ImageView { 65 class StaticImageView : public views::ImageView {
93 public: 66 public:
94 StaticImageView() : ImageView() { 67 StaticImageView() : ImageView() {
95 } 68 }
96 69
97 private: 70 private:
98 // views::View overrides: 71 // views::View overrides:
99 virtual bool HitTest(const gfx::Point& l) const OVERRIDE { 72 virtual bool HitTest(const gfx::Point& l) const OVERRIDE {
100 return false; 73 return false;
101 } 74 }
102 75
103 DISALLOW_COPY_AND_ASSIGN(StaticImageView); 76 DISALLOW_COPY_AND_ASSIGN(StaticImageView);
104 }; 77 };
105 78
106 // A minimum title width set by test to override the default logic that derives
107 // the min width from font.
108 int g_min_title_width = 0;
109
110 } // namespace 79 } // namespace
111 80
112 // static 81 // static
113 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView"; 82 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView";
114 83
115 // AppListItemView::IconOperation wraps background icon processing. 84 // AppListItemView::IconOperation wraps background icon processing.
116 class AppListItemView::IconOperation 85 class AppListItemView::IconOperation
117 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> { 86 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> {
118 public: 87 public:
119 IconOperation(const SkBitmap& bitmap, 88 IconOperation(const SkBitmap& bitmap,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 }; 133 };
165 134
166 AppListItemView::AppListItemView(AppsGridView* apps_grid_view, 135 AppListItemView::AppListItemView(AppsGridView* apps_grid_view,
167 AppListItemModel* model, 136 AppListItemModel* model,
168 views::ButtonListener* listener) 137 views::ButtonListener* listener)
169 : CustomButton(listener), 138 : CustomButton(listener),
170 model_(model), 139 model_(model),
171 apps_grid_view_(apps_grid_view), 140 apps_grid_view_(apps_grid_view),
172 icon_(new StaticImageView), 141 icon_(new StaticImageView),
173 title_(new DropShadowLabel), 142 title_(new DropShadowLabel),
174 selected_(false),
175 ALLOW_THIS_IN_INITIALIZER_LIST(apply_shadow_factory_(this)) { 143 ALLOW_THIS_IN_INITIALIZER_LIST(apply_shadow_factory_(this)) {
144 title_->SetBackgroundColor(kTitleBackgroundColor);
145 title_->SetEnabledColor(kTitleColor);
146 title_->SetFont(GetTitleFont());
176 147
177 if (IsV2()) { 148 const gfx::ShadowValue kIconShadows[] = {
178 title_->SetBackgroundColor(kTitleBackgroundColorV2); 149 gfx::ShadowValue(gfx::Point(0, 2), 2, SkColorSetARGB(0x24, 0, 0, 0)),
179 title_->SetEnabledColor(kTitleColorV2); 150 };
180 151 icon_shadows_.assign(kIconShadows, kIconShadows + arraysize(kIconShadows));
181 const gfx::ShadowValue kIconShadows[] = {
182 gfx::ShadowValue(gfx::Point(0, 2), 2, SkColorSetARGB(0x24, 0, 0, 0)),
183 };
184 icon_shadows_.assign(kIconShadows, kIconShadows + arraysize(kIconShadows));
185 } else {
186 title_->SetBackgroundColor(0);
187 title_->SetEnabledColor(kTitleColor);
188 const gfx::ShadowValue kTitleShadows[] = {
189 gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x66, 0, 0, 0)),
190 gfx::ShadowValue(gfx::Point(0, 0), 10, SkColorSetARGB(0x66, 0, 0, 0)),
191 gfx::ShadowValue(gfx::Point(0, 2), 2, SkColorSetARGB(0x66, 0, 0, 0)),
192 gfx::ShadowValue(gfx::Point(0, 2), 4, SkColorSetARGB(0x66, 0, 0, 0)),
193 };
194 title_->SetTextShadows(arraysize(kTitleShadows), kTitleShadows);
195
196 const gfx::ShadowValue kIconShadows[] = {
197 gfx::ShadowValue(gfx::Point(0, 0), 2, SkColorSetARGB(0xCC, 0, 0, 0)),
198 gfx::ShadowValue(gfx::Point(0, 4), 4, SkColorSetARGB(0x33, 0, 0, 0)),
199 gfx::ShadowValue(gfx::Point(0, 5), 10, SkColorSetARGB(0x4C, 0, 0, 0)),
200 };
201 icon_shadows_.assign(kIconShadows, kIconShadows + arraysize(kIconShadows));
202 }
203
204 152
205 AddChildView(icon_); 153 AddChildView(icon_);
206 AddChildView(title_); 154 AddChildView(title_);
207 155
208 ItemIconChanged(); 156 ItemIconChanged();
209 ItemTitleChanged(); 157 ItemTitleChanged();
210 model_->AddObserver(this); 158 model_->AddObserver(this);
211 159
212 set_context_menu_controller(this); 160 set_context_menu_controller(this);
213 set_request_focus_on_press(false); 161 set_request_focus_on_press(false);
214
215 // Don't take focus for v2 so that focus stays with the search box.
216 if (!IsV2())
217 set_focusable(true);
218 } 162 }
219 163
220 AppListItemView::~AppListItemView() { 164 AppListItemView::~AppListItemView() {
221 model_->RemoveObserver(this); 165 model_->RemoveObserver(this);
222 CancelPendingIconOperation(); 166 CancelPendingIconOperation();
223 } 167 }
224 168
225 // static
226 gfx::Size AppListItemView::GetPreferredSizeForIconSize(
227 const gfx::Size& icon_size) {
228 int min_title_width = g_min_title_width;
229 // Fixed 20px is used for left/right padding before switching to padding
230 // based on number of chars. It is also a number used for test case
231 // AppList.ModelViewCalculateLayout.
232 int left_right_padding = 20;
233 if (min_title_width == 0) {
234 // Assumes fixed layout is false since this function should only be called
235 // for dynamic layout.
236 const gfx::Font& title_font = GetTitleFontForIconSize(icon_size,
237 false /* fixed */);
238 // Use big char such as 'G' to calculate min title width.
239 min_title_width = kMinTitleChars *
240 title_font.GetStringWidth(ASCIIToUTF16("G"));
241 left_right_padding = kLeftRightPaddingChars *
242 title_font.GetAverageCharacterWidth();
243 }
244
245 int dimension = std::max(icon_size.width() * 2, min_title_width);
246 gfx::Size size(dimension, dimension);
247 size.Enlarge(left_right_padding, kTopBottomPadding);
248 return size;
249 }
250
251 // static
252 void AppListItemView::SetMinTitleWidth(int width) {
253 g_min_title_width = width;
254 }
255
256 void AppListItemView::SetIconSize(const gfx::Size& size) { 169 void AppListItemView::SetIconSize(const gfx::Size& size) {
257 if (icon_size_ == size) 170 if (icon_size_ == size)
258 return; 171 return;
259 172
260 icon_size_ = size; 173 icon_size_ = size;
261 title_->SetFont(GetTitleFontForIconSize(size, IsV2()));
262 UpdateIcon(); 174 UpdateIcon();
263 } 175 }
264 176
265 void AppListItemView::SetSelected(bool selected) {
266 if (selected == selected_)
267 return;
268
269 if (focusable())
270 RequestFocus();
271 selected_ = selected;
272 SchedulePaint();
273 }
274
275 bool AppListItemView::IsV2() const {
276 return apps_grid_view_->fixed_layout();
277 }
278
279 void AppListItemView::UpdateIcon() { 177 void AppListItemView::UpdateIcon() {
280 // Skip if |icon_size_| has not been determined. 178 // Skip if |icon_size_| has not been determined.
281 if (icon_size_.IsEmpty()) 179 if (icon_size_.IsEmpty())
282 return; 180 return;
283 181
284 SkBitmap icon = model_->icon(); 182 SkBitmap icon = model_->icon();
285 // Clear icon and bail out if model icon is empty. 183 // Clear icon and bail out if model icon is empty.
286 if (icon.empty()) { 184 if (icon.empty()) {
287 icon_->SetImage(NULL); 185 icon_->SetImage(NULL);
288 return; 186 return;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 223
326 void AppListItemView::ItemIconChanged() { 224 void AppListItemView::ItemIconChanged() {
327 UpdateIcon(); 225 UpdateIcon();
328 } 226 }
329 227
330 void AppListItemView::ItemTitleChanged() { 228 void AppListItemView::ItemTitleChanged() {
331 title_->SetText(UTF8ToUTF16(model_->title())); 229 title_->SetText(UTF8ToUTF16(model_->title()));
332 } 230 }
333 231
334 void AppListItemView::ItemHighlightedChanged() { 232 void AppListItemView::ItemHighlightedChanged() {
233 apps_grid_view_->EnsureItemVisible(this);
335 SchedulePaint(); 234 SchedulePaint();
336 } 235 }
337 236
338 std::string AppListItemView::GetClassName() const { 237 std::string AppListItemView::GetClassName() const {
339 return kViewClassName; 238 return kViewClassName;
340 } 239 }
341 240
342 gfx::Size AppListItemView::GetPreferredSize() {
343 return GetPreferredSizeForIconSize(icon_size_);
344 }
345
346 void AppListItemView::Layout() { 241 void AppListItemView::Layout() {
347 gfx::Rect rect(GetContentsBounds()); 242 gfx::Rect rect(GetContentsBounds());
348 243
349 int left_right_padding = kLeftRightPaddingChars * 244 int left_right_padding = kLeftRightPaddingChars *
350 title_->font().GetAverageCharacterWidth(); 245 title_->font().GetAverageCharacterWidth();
351 gfx::Size title_size = title_->GetPreferredSize(); 246 gfx::Size title_size = title_->GetPreferredSize();
352 247
353 int y = 0; 248 rect.Inset(left_right_padding, kTopPadding);
354 if (IsV2()) { 249 int y = rect.y();
355 // Layout for v2, starting at kTopPaddingV2.
356 rect.Inset(left_right_padding, kTopPaddingV2);
357 y = rect.y();
358 } else {
359 // Layout for v1, vertically centered.
360 rect.Inset(left_right_padding, kTopBottomPadding);
361
362 int height = icon_size_.height() + kIconTitleSpacing +
363 title_size.height();
364 y = rect.y() + (rect.height() - height) / 2;
365 }
366 250
367 gfx::Rect icon_bounds(rect.x(), y, rect.width(), icon_size_.height()); 251 gfx::Rect icon_bounds(rect.x(), y, rect.width(), icon_size_.height());
368 icon_bounds.Inset(gfx::ShadowValue::GetMargin(icon_shadows_)); 252 icon_bounds.Inset(gfx::ShadowValue::GetMargin(icon_shadows_));
369 icon_->SetBoundsRect(icon_bounds); 253 icon_->SetBoundsRect(icon_bounds);
370 254
371 title_->SetBounds(rect.x(), 255 title_->SetBounds(rect.x(),
372 y + icon_size_.height() + kIconTitleSpacing, 256 y + icon_size_.height() + kIconTitleSpacing,
373 rect.width(), 257 rect.width(),
374 title_size.height()); 258 title_size.height());
375 } 259 }
376 260
377 void AppListItemView::OnPaint(gfx::Canvas* canvas) { 261 void AppListItemView::OnPaint(gfx::Canvas* canvas) {
378 gfx::Rect rect(GetContentsBounds()); 262 gfx::Rect rect(GetContentsBounds());
379 263
380 const SkColor highlighted = IsV2() ? kHighlightedColorV2 : kHighlightedColor; 264 bool selected = apps_grid_view_->IsSelectedItem(this);
381 const SkColor hover_push = IsV2() ? kHoverAndPushedColorV2 :
382 kHoverAndPushedColor;
383 const SkColor selected = IsV2() ? kSelectedColorV2 : kSelectedColor;
384 265
385 if (model_->highlighted()) { 266 if (model_->highlighted()) {
386 canvas->FillRect(rect, highlighted); 267 canvas->FillRect(rect, kHighlightedColor);
387 } else if (hover_animation_->is_animating()) { 268 } else if (hover_animation_->is_animating()) {
388 int alpha = SkColorGetA(hover_push) * 269 int alpha = SkColorGetA(kHoverAndPushedColor) *
389 hover_animation_->GetCurrentValue(); 270 hover_animation_->GetCurrentValue();
390 canvas->FillRect(rect, SkColorSetA(hover_push, alpha)); 271 canvas->FillRect(rect, SkColorSetA(kHoverAndPushedColor, alpha));
391 } else if (state() == BS_HOT || state() == BS_PUSHED) { 272 } else if (state() == BS_HOT || state() == BS_PUSHED) {
392 canvas->FillRect(rect, hover_push); 273 canvas->FillRect(rect, kHoverAndPushedColor);
393 } else if (selected_) { 274 } else if (selected) {
394 canvas->FillRect(rect, selected); 275 canvas->FillRect(rect, kSelectedColor);
395 } 276 }
396 } 277 }
397 278
398 void AppListItemView::GetAccessibleState(ui::AccessibleViewState* state) { 279 void AppListItemView::GetAccessibleState(ui::AccessibleViewState* state) {
399 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 280 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
400 state->name = UTF8ToUTF16(model_->title()); 281 state->name = UTF8ToUTF16(model_->title());
401 } 282 }
402 283
403 void AppListItemView::ShowContextMenuForView(views::View* source, 284 void AppListItemView::ShowContextMenuForView(views::View* source,
404 const gfx::Point& point) { 285 const gfx::Point& point) {
405 ui::MenuModel* menu_model = model_->GetContextMenuModel(); 286 ui::MenuModel* menu_model = model_->GetContextMenuModel();
406 if (!menu_model) 287 if (!menu_model)
407 return; 288 return;
408 289
409 views::MenuModelAdapter menu_adapter(menu_model); 290 views::MenuModelAdapter menu_adapter(menu_model);
410 context_menu_runner_.reset( 291 context_menu_runner_.reset(
411 new views::MenuRunner(new views::MenuItemView(&menu_adapter))); 292 new views::MenuRunner(new views::MenuItemView(&menu_adapter)));
412 menu_adapter.BuildMenu(context_menu_runner_->GetMenu()); 293 menu_adapter.BuildMenu(context_menu_runner_->GetMenu());
413 if (context_menu_runner_->RunMenuAt( 294 if (context_menu_runner_->RunMenuAt(
414 GetWidget(), NULL, gfx::Rect(point, gfx::Size()), 295 GetWidget(), NULL, gfx::Rect(point, gfx::Size()),
415 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == 296 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
416 views::MenuRunner::MENU_DELETED) 297 views::MenuRunner::MENU_DELETED)
417 return; 298 return;
418 } 299 }
419 300
420 void AppListItemView::StateChanged() { 301 void AppListItemView::StateChanged() {
421 if (state() == BS_HOT || state() == BS_PUSHED) { 302 if (state() == BS_HOT || state() == BS_PUSHED) {
422 apps_grid_view_->SetSelectedItem(this); 303 apps_grid_view_->SetSelectedItem(this);
423 if (IsV2()) 304 title_->SetEnabledColor(kTitleHoverColor);
424 title_->SetEnabledColor(kTitleColorHoverV2);
425 } else { 305 } else {
426 apps_grid_view_->ClearSelectedItem(this); 306 apps_grid_view_->ClearSelectedItem(this);
427 model_->SetHighlighted(false); 307 model_->SetHighlighted(false);
428 if (IsV2()) 308 title_->SetEnabledColor(kTitleColor);
429 title_->SetEnabledColor(kTitleColorV2);
430 } 309 }
431 } 310 }
432 311
433 } // namespace app_list 312 } // namespace app_list
OLDNEW
« no previous file with comments | « ui/app_list/app_list_item_view.h ('k') | ui/app_list/app_list_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698