Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / views / app_list_item_view.cc
blob340bc5d93101202d89f0827d5db0e66fe3b3f3e4
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
3 // found in the LICENSE file.
5 #include "ui/app_list/views/app_list_item_view.h"
7 #include <algorithm>
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/accessibility/ax_view_state.h"
11 #include "ui/app_list/app_list_constants.h"
12 #include "ui/app_list/app_list_folder_item.h"
13 #include "ui/app_list/app_list_item.h"
14 #include "ui/app_list/app_list_switches.h"
15 #include "ui/app_list/views/apps_grid_view.h"
16 #include "ui/app_list/views/cached_label.h"
17 #include "ui/app_list/views/progress_bar_view.h"
18 #include "ui/base/dragdrop/drag_utils.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_switches_util.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/compositor/scoped_layer_animation_settings.h"
24 #include "ui/gfx/animation/throb_animation.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/font_list.h"
27 #include "ui/gfx/geometry/point.h"
28 #include "ui/gfx/geometry/vector2d.h"
29 #include "ui/gfx/image/image_skia_operations.h"
30 #include "ui/gfx/shadow_value.h"
31 #include "ui/gfx/transform_util.h"
32 #include "ui/strings/grit/ui_strings.h"
33 #include "ui/views/background.h"
34 #include "ui/views/controls/image_view.h"
35 #include "ui/views/controls/label.h"
36 #include "ui/views/controls/menu/menu_runner.h"
37 #include "ui/views/drag_controller.h"
39 namespace app_list {
41 namespace {
43 const int kTopPadding = 18;
44 const int kIconTitleSpacing = 6;
46 // Radius of the folder dropping preview circle.
47 const int kFolderPreviewRadius = 40;
49 const int kLeftRightPaddingChars = 1;
51 #if !defined(OS_WIN)
52 // Scale to transform the icon when a drag starts.
53 const float kDraggingIconScale = 1.5f;
54 #endif
56 // Delay in milliseconds of when the dragging UI should be shown for mouse drag.
57 const int kMouseDragUIDelayInMs = 200;
59 gfx::FontList GetFontList() {
60 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
61 const gfx::FontList& font_list = rb.GetFontList(kItemTextFontStyle);
62 // The font is different on each platform. The font size is adjusted on some
63 // platforms to keep a consistent look.
64 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
65 // Reducing the font size by 2 makes it the same as the Windows font size.
66 const int kFontSizeDelta = -2;
67 return font_list.DeriveWithSizeDelta(kFontSizeDelta);
68 #else
69 return font_list;
70 #endif
73 } // namespace
75 // static
76 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView";
78 AppListItemView::AppListItemView(AppsGridView* apps_grid_view,
79 AppListItem* item)
80 : CustomButton(apps_grid_view),
81 is_folder_(item->GetItemType() == AppListFolderItem::kItemType),
82 is_in_folder_(item->IsInFolder()),
83 item_weak_(item),
84 apps_grid_view_(apps_grid_view),
85 icon_(new views::ImageView),
86 title_(new CachedLabel),
87 progress_bar_(new ProgressBarView),
88 ui_state_(UI_STATE_NORMAL),
89 touch_dragging_(false),
90 shadow_animator_(this),
91 is_installing_(false),
92 is_highlighted_(false) {
93 shadow_animator_.animation()->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
94 shadow_animator_.SetStartAndEndShadows(IconStartShadows(), IconEndShadows());
96 icon_->set_interactive(false);
97 icon_->SetVerticalAlignment(views::ImageView::LEADING);
99 title_->SetBackgroundColor(0);
100 title_->SetAutoColorReadabilityEnabled(false);
101 title_->SetEnabledColor(kGridTitleColor);
102 title_->SetHandlesTooltips(false);
104 static const gfx::FontList font_list = GetFontList();
105 title_->SetFontList(font_list);
106 title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
107 title_->Invalidate();
108 SetTitleSubpixelAA();
110 AddChildView(icon_);
111 AddChildView(title_);
112 AddChildView(progress_bar_);
114 SetIcon(item->icon());
115 SetItemName(base::UTF8ToUTF16(item->GetDisplayName()),
116 base::UTF8ToUTF16(item->name()));
117 SetItemIsInstalling(item->is_installing());
118 SetItemIsHighlighted(item->highlighted());
119 item->AddObserver(this);
121 set_context_menu_controller(this);
122 set_request_focus_on_press(false);
124 SetAnimationDuration(0);
127 AppListItemView::~AppListItemView() {
128 if (item_weak_)
129 item_weak_->RemoveObserver(this);
132 void AppListItemView::SetIcon(const gfx::ImageSkia& icon) {
133 // Clear icon and bail out if item icon is empty.
134 if (icon.isNull()) {
135 icon_->SetImage(NULL);
136 return;
139 gfx::ImageSkia resized(gfx::ImageSkiaOperations::CreateResizedImage(
140 icon,
141 skia::ImageOperations::RESIZE_BEST,
142 gfx::Size(kGridIconDimension, kGridIconDimension)));
143 shadow_animator_.SetOriginalImage(resized);
146 void AppListItemView::SetUIState(UIState state) {
147 if (ui_state_ == state)
148 return;
150 ui_state_ = state;
152 switch (ui_state_) {
153 case UI_STATE_NORMAL:
154 title_->SetVisible(!is_installing_);
155 progress_bar_->SetVisible(is_installing_);
156 break;
157 case UI_STATE_DRAGGING:
158 title_->SetVisible(false);
159 progress_bar_->SetVisible(false);
160 break;
161 case UI_STATE_DROPPING_IN_FOLDER:
162 break;
164 #if !defined(OS_WIN)
165 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
166 switch (ui_state_) {
167 case UI_STATE_NORMAL:
168 layer()->SetTransform(gfx::Transform());
169 break;
170 case UI_STATE_DRAGGING: {
171 const gfx::Rect bounds(layer()->bounds().size());
172 layer()->SetTransform(gfx::GetScaleTransform(
173 bounds.CenterPoint(),
174 kDraggingIconScale));
175 break;
177 case UI_STATE_DROPPING_IN_FOLDER:
178 break;
180 #endif // !OS_WIN
182 SetTitleSubpixelAA();
183 SchedulePaint();
186 void AppListItemView::SetTouchDragging(bool touch_dragging) {
187 if (touch_dragging_ == touch_dragging)
188 return;
190 touch_dragging_ = touch_dragging;
191 SetState(STATE_NORMAL);
192 SetUIState(touch_dragging_ ? UI_STATE_DRAGGING : UI_STATE_NORMAL);
195 void AppListItemView::OnMouseDragTimer() {
196 DCHECK(apps_grid_view_->IsDraggedView(this));
197 SetUIState(UI_STATE_DRAGGING);
200 void AppListItemView::Prerender() {
201 title_->PaintToBackingImage();
204 void AppListItemView::CancelContextMenu() {
205 if (context_menu_runner_)
206 context_menu_runner_->Cancel();
209 gfx::ImageSkia AppListItemView::GetDragImage() {
210 return icon_->GetImage();
213 void AppListItemView::OnDragEnded() {
214 mouse_drag_timer_.Stop();
215 SetUIState(UI_STATE_NORMAL);
218 gfx::Point AppListItemView::GetDragImageOffset() {
219 gfx::Point image = icon_->GetImageBounds().origin();
220 return gfx::Point(icon_->x() + image.x(), icon_->y() + image.y());
223 void AppListItemView::SetAsAttemptedFolderTarget(bool is_target_folder) {
224 if (is_target_folder)
225 SetUIState(UI_STATE_DROPPING_IN_FOLDER);
226 else
227 SetUIState(UI_STATE_NORMAL);
230 void AppListItemView::SetItemName(const base::string16& display_name,
231 const base::string16& full_name) {
232 title_->SetText(display_name);
233 title_->Invalidate();
235 tooltip_text_ = display_name == full_name ? base::string16() : full_name;
237 // Use full name for accessibility.
238 SetAccessibleName(
239 is_folder_ ? l10n_util::GetStringFUTF16(
240 IDS_APP_LIST_FOLDER_BUTTON_ACCESSIBILE_NAME, full_name)
241 : full_name);
242 Layout();
245 void AppListItemView::SetItemIsHighlighted(bool is_highlighted) {
246 is_highlighted_ = is_highlighted;
247 SetTitleSubpixelAA();
248 SchedulePaint();
251 void AppListItemView::SetItemIsInstalling(bool is_installing) {
252 is_installing_ = is_installing;
253 if (ui_state_ == UI_STATE_NORMAL) {
254 title_->SetVisible(!is_installing);
255 progress_bar_->SetVisible(is_installing);
257 SetTitleSubpixelAA();
258 SchedulePaint();
261 void AppListItemView::SetItemPercentDownloaded(int percent_downloaded) {
262 // A percent_downloaded() of -1 can mean it's not known how much percent is
263 // completed, or the download hasn't been marked complete, as is the case
264 // while an extension is being installed after being downloaded.
265 if (percent_downloaded == -1)
266 return;
267 progress_bar_->SetValue(percent_downloaded / 100.0);
270 const char* AppListItemView::GetClassName() const {
271 return kViewClassName;
274 void AppListItemView::Layout() {
275 gfx::Rect rect(GetContentsBounds());
277 const int left_right_padding =
278 title_->font_list().GetExpectedTextWidth(kLeftRightPaddingChars);
279 rect.Inset(left_right_padding, kTopPadding, left_right_padding, 0);
280 const int y = rect.y();
282 icon_->SetBoundsRect(GetIconBoundsForTargetViewBounds(GetContentsBounds()));
284 const gfx::Size title_size = title_->GetPreferredSize();
285 gfx::Rect title_bounds(rect.x() + (rect.width() - title_size.width()) / 2,
286 y + kGridIconDimension + kIconTitleSpacing,
287 title_size.width(),
288 title_size.height());
289 title_bounds.Intersect(rect);
290 title_->SetBoundsRect(title_bounds);
291 SetTitleSubpixelAA();
293 gfx::Rect progress_bar_bounds(progress_bar_->GetPreferredSize());
294 progress_bar_bounds.set_x(
295 (GetContentsBounds().width() - progress_bar_bounds.width()) / 2);
296 progress_bar_bounds.set_y(title_bounds.y());
297 progress_bar_->SetBoundsRect(progress_bar_bounds);
300 void AppListItemView::OnPaint(gfx::Canvas* canvas) {
301 if (apps_grid_view_->IsDraggedView(this))
302 return;
304 gfx::Rect rect(GetContentsBounds());
305 if (apps_grid_view_->IsSelectedView(this)) {
306 canvas->FillRect(rect, kSelectedColor);
307 } else if (is_highlighted_ && !is_installing_ &&
308 !app_list::switches::IsExperimentalAppListEnabled()) {
309 canvas->FillRect(rect, kHighlightedColor);
310 return;
313 if (ui_state_ == UI_STATE_DROPPING_IN_FOLDER) {
314 DCHECK(apps_grid_view_->model()->folders_enabled());
316 // Draw folder dropping preview circle.
317 gfx::Point center = gfx::Point(icon_->x() + icon_->size().width() / 2,
318 icon_->y() + icon_->size().height() / 2);
319 SkPaint paint;
320 paint.setStyle(SkPaint::kFill_Style);
321 paint.setAntiAlias(true);
322 paint.setColor(kFolderBubbleColor);
323 canvas->DrawCircle(center, kFolderPreviewRadius, paint);
327 void AppListItemView::ShowContextMenuForView(views::View* source,
328 const gfx::Point& point,
329 ui::MenuSourceType source_type) {
330 ui::MenuModel* menu_model =
331 item_weak_ ? item_weak_->GetContextMenuModel() : NULL;
332 if (!menu_model)
333 return;
335 if (!apps_grid_view_->IsSelectedView(this))
336 apps_grid_view_->ClearAnySelectedView();
337 context_menu_runner_.reset(
338 new views::MenuRunner(menu_model, views::MenuRunner::HAS_MNEMONICS));
339 if (context_menu_runner_->RunMenuAt(GetWidget(),
340 NULL,
341 gfx::Rect(point, gfx::Size()),
342 views::MENU_ANCHOR_TOPLEFT,
343 source_type) ==
344 views::MenuRunner::MENU_DELETED) {
345 return;
349 void AppListItemView::StateChanged() {
350 if (app_list::switches::IsExperimentalAppListEnabled()) {
351 if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
352 shadow_animator_.animation()->Show();
353 } else {
354 shadow_animator_.animation()->Hide();
358 if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
359 // Show the hover/tap highlight: for tap, lighter highlight replaces darker
360 // keyboard selection; for mouse hover, keyboard selection takes precedence.
361 if (!apps_grid_view_->IsSelectedView(this) || state() == STATE_PRESSED)
362 SetItemIsHighlighted(true);
363 } else {
364 SetItemIsHighlighted(false);
365 if (item_weak_)
366 item_weak_->set_highlighted(false);
368 SetTitleSubpixelAA();
371 bool AppListItemView::ShouldEnterPushedState(const ui::Event& event) {
372 // Don't enter pushed state for ET_GESTURE_TAP_DOWN so that hover gray
373 // background does not show up during scroll.
374 if (event.type() == ui::ET_GESTURE_TAP_DOWN)
375 return false;
377 return views::CustomButton::ShouldEnterPushedState(event);
380 bool AppListItemView::OnMousePressed(const ui::MouseEvent& event) {
381 CustomButton::OnMousePressed(event);
383 if (!ShouldEnterPushedState(event))
384 return true;
386 apps_grid_view_->InitiateDrag(this, AppsGridView::MOUSE, event);
388 if (apps_grid_view_->IsDraggedView(this)) {
389 mouse_drag_timer_.Start(FROM_HERE,
390 base::TimeDelta::FromMilliseconds(kMouseDragUIDelayInMs),
391 this, &AppListItemView::OnMouseDragTimer);
393 return true;
396 bool AppListItemView::OnKeyPressed(const ui::KeyEvent& event) {
397 // Disable space key to press the button. The keyboard events received
398 // by this view are forwarded from a Textfield (SearchBoxView) and key
399 // released events are not forwarded. This leaves the button in pressed
400 // state.
401 if (event.key_code() == ui::VKEY_SPACE)
402 return false;
404 return CustomButton::OnKeyPressed(event);
407 void AppListItemView::OnMouseReleased(const ui::MouseEvent& event) {
408 CustomButton::OnMouseReleased(event);
409 apps_grid_view_->EndDrag(false);
412 void AppListItemView::OnMouseCaptureLost() {
413 // We don't cancel the dag on mouse capture lost for windows as entering a
414 // synchronous drag causes mouse capture to be lost and pressing escape
415 // dismisses the app list anyway.
416 #if !defined(OS_WIN)
417 CustomButton::OnMouseCaptureLost();
418 apps_grid_view_->EndDrag(true);
419 #endif
422 bool AppListItemView::OnMouseDragged(const ui::MouseEvent& event) {
423 CustomButton::OnMouseDragged(event);
424 if (apps_grid_view_->IsDraggedView(this)) {
425 // If the drag is no longer happening, it could be because this item
426 // got removed, in which case this item has been destroyed. So, bail out
427 // now as there will be nothing else to do anyway as
428 // apps_grid_view_->dragging() will be false.
429 if (!apps_grid_view_->UpdateDragFromItem(AppsGridView::MOUSE, event))
430 return true;
433 if (!apps_grid_view_->IsSelectedView(this))
434 apps_grid_view_->ClearAnySelectedView();
436 // Shows dragging UI when it's confirmed without waiting for the timer.
437 if (ui_state_ != UI_STATE_DRAGGING &&
438 apps_grid_view_->dragging() &&
439 apps_grid_view_->IsDraggedView(this)) {
440 mouse_drag_timer_.Stop();
441 SetUIState(UI_STATE_DRAGGING);
443 return true;
446 void AppListItemView::OnGestureEvent(ui::GestureEvent* event) {
447 switch (event->type()) {
448 case ui::ET_GESTURE_SCROLL_BEGIN:
449 if (touch_dragging_) {
450 apps_grid_view_->InitiateDrag(this, AppsGridView::TOUCH, *event);
451 event->SetHandled();
453 break;
454 case ui::ET_GESTURE_SCROLL_UPDATE:
455 if (touch_dragging_ && apps_grid_view_->IsDraggedView(this)) {
456 apps_grid_view_->UpdateDragFromItem(AppsGridView::TOUCH, *event);
457 event->SetHandled();
459 break;
460 case ui::ET_GESTURE_SCROLL_END:
461 case ui::ET_SCROLL_FLING_START:
462 if (touch_dragging_) {
463 SetTouchDragging(false);
464 apps_grid_view_->EndDrag(false);
465 event->SetHandled();
467 break;
468 case ui::ET_GESTURE_TAP_DOWN:
469 if (::switches::IsTouchFeedbackEnabled() && state_ != STATE_DISABLED) {
470 SetState(STATE_PRESSED);
471 event->SetHandled();
473 break;
474 case ui::ET_GESTURE_TAP:
475 case ui::ET_GESTURE_TAP_CANCEL:
476 if (::switches::IsTouchFeedbackEnabled() && state_ != STATE_DISABLED)
477 SetState(STATE_NORMAL);
478 break;
479 case ui::ET_GESTURE_LONG_PRESS:
480 if (!apps_grid_view_->has_dragged_view())
481 SetTouchDragging(true);
482 event->SetHandled();
483 break;
484 case ui::ET_GESTURE_LONG_TAP:
485 case ui::ET_GESTURE_END:
486 if (touch_dragging_)
487 SetTouchDragging(false);
488 break;
489 default:
490 break;
492 if (!event->handled())
493 CustomButton::OnGestureEvent(event);
496 bool AppListItemView::GetTooltipText(const gfx::Point& p,
497 base::string16* tooltip) const {
498 // Use the label to generate a tooltip, so that it will consider its text
499 // truncation in making the tooltip. We do not want the label itself to have a
500 // tooltip, so we only temporarily enable it to get the tooltip text from the
501 // label, then disable it again.
502 title_->SetHandlesTooltips(true);
503 title_->SetTooltipText(tooltip_text_);
504 bool handled = title_->GetTooltipText(p, tooltip);
505 title_->SetHandlesTooltips(false);
506 return handled;
509 void AppListItemView::ImageShadowAnimationProgressed(
510 ImageShadowAnimator* animator) {
511 icon_->SetImage(animator->shadow_image());
512 Layout();
515 void AppListItemView::OnSyncDragEnd() {
516 SetUIState(UI_STATE_NORMAL);
519 const gfx::Rect& AppListItemView::GetIconBounds() const {
520 return icon_->bounds();
523 void AppListItemView::SetDragUIState() {
524 SetUIState(UI_STATE_DRAGGING);
527 gfx::Rect AppListItemView::GetIconBoundsForTargetViewBounds(
528 const gfx::Rect& target_bounds) {
529 gfx::Rect rect(target_bounds);
530 rect.Inset(0, kTopPadding, 0, 0);
531 rect.set_height(icon_->GetImage().height());
532 rect.ClampToCenteredSize(icon_->GetImage().size());
533 return rect;
536 void AppListItemView::SetTitleSubpixelAA() {
537 // TODO(tapted): Enable AA for folders as well, taking care to play nice with
538 // the folder bubble animation.
539 bool enable_aa = !is_in_folder_ && ui_state_ == UI_STATE_NORMAL &&
540 !is_highlighted_ && !apps_grid_view_->IsSelectedView(this) &&
541 !apps_grid_view_->IsAnimatingView(this);
543 title_->SetSubpixelRenderingEnabled(enable_aa);
544 if (enable_aa) {
545 title_->SetBackgroundColor(app_list::kLabelBackgroundColor);
546 title_->set_background(views::Background::CreateSolidBackground(
547 app_list::kLabelBackgroundColor));
548 } else {
549 // In other cases, keep the background transparent to ensure correct
550 // interactions with animations. This will temporarily disable subpixel AA.
551 title_->SetBackgroundColor(0);
552 title_->set_background(NULL);
554 title_->Invalidate();
555 title_->SchedulePaint();
558 void AppListItemView::ItemIconChanged() {
559 SetIcon(item_weak_->icon());
562 void AppListItemView::ItemNameChanged() {
563 SetItemName(base::UTF8ToUTF16(item_weak_->GetDisplayName()),
564 base::UTF8ToUTF16(item_weak_->name()));
567 void AppListItemView::ItemIsInstallingChanged() {
568 SetItemIsInstalling(item_weak_->is_installing());
571 void AppListItemView::ItemPercentDownloadedChanged() {
572 SetItemPercentDownloaded(item_weak_->percent_downloaded());
575 void AppListItemView::ItemBeingDestroyed() {
576 DCHECK(item_weak_);
577 item_weak_->RemoveObserver(this);
578 item_weak_ = NULL;
581 } // namespace app_list