Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / views / tile_item_view.cc
blob944daefee584350364f6f6f3d7f3459058d3a00f
1 // Copyright 2014 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/tile_item_view.h"
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/app_list/views/app_list_main_view.h"
9 #include "ui/views/background.h"
10 #include "ui/views/controls/image_view.h"
11 #include "ui/views/controls/label.h"
12 #include "ui/views/layout/box_layout.h"
14 namespace {
16 const int kTopPadding = 5;
17 const int kTileSize = 90;
18 const int kIconTitleSpacing = 6;
20 } // namespace
22 namespace app_list {
24 TileItemView::TileItemView()
25 : views::CustomButton(this),
26 parent_background_color_(SK_ColorTRANSPARENT),
27 icon_(new views::ImageView),
28 title_(new views::Label),
29 selected_(false) {
30 // Prevent the icon view from interfering with our mouse events.
31 icon_->set_interactive(false);
32 icon_->SetVerticalAlignment(views::ImageView::LEADING);
34 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
35 title_->SetAutoColorReadabilityEnabled(false);
36 title_->SetEnabledColor(kGridTitleColor);
37 title_->SetFontList(rb.GetFontList(kItemTextFontStyle));
38 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
39 title_->SetHandlesTooltips(false);
41 AddChildView(icon_);
42 AddChildView(title_);
45 TileItemView::~TileItemView() {
48 void TileItemView::SetSelected(bool selected) {
49 if (selected == selected_)
50 return;
52 selected_ = selected;
53 UpdateBackgroundColor();
55 if (selected)
56 NotifyAccessibilityEvent(ui::AX_EVENT_FOCUS, true);
59 void TileItemView::SetParentBackgroundColor(SkColor color) {
60 parent_background_color_ = color;
61 UpdateBackgroundColor();
64 void TileItemView::SetHoverStyle(HoverStyle hover_style) {
65 if (hover_style == HOVER_STYLE_DARKEN_BACKGROUND) {
66 image_shadow_animator_.reset();
67 return;
70 image_shadow_animator_.reset(new ImageShadowAnimator(this));
71 image_shadow_animator_->animation()->SetTweenType(
72 gfx::Tween::FAST_OUT_SLOW_IN);
73 image_shadow_animator_->SetStartAndEndShadows(IconStartShadows(),
74 IconEndShadows());
77 void TileItemView::SetIcon(const gfx::ImageSkia& icon) {
78 if (image_shadow_animator_) {
79 // Will call icon_->SetImage synchronously.
80 image_shadow_animator_->SetOriginalImage(icon);
81 return;
84 icon_->SetImage(icon);
87 void TileItemView::SetTitle(const base::string16& title) {
88 title_->SetText(title);
89 SetAccessibleName(title);
92 void TileItemView::StateChanged() {
93 UpdateBackgroundColor();
96 void TileItemView::Layout() {
97 gfx::Rect rect(GetContentsBounds());
99 rect.Inset(0, kTopPadding, 0, 0);
100 icon_->SetBoundsRect(rect);
102 rect.Inset(0, kGridIconDimension + kIconTitleSpacing, 0, 0);
103 rect.set_height(title_->GetPreferredSize().height());
104 title_->SetBoundsRect(rect);
107 void TileItemView::ImageShadowAnimationProgressed(
108 ImageShadowAnimator* animator) {
109 icon_->SetImage(animator->shadow_image());
112 void TileItemView::UpdateBackgroundColor() {
113 views::Background* background = nullptr;
114 SkColor background_color = parent_background_color_;
116 if (selected_) {
117 background_color = kSelectedColor;
118 background = views::Background::CreateSolidBackground(background_color);
119 } else if (image_shadow_animator_) {
120 if (state() == STATE_HOVERED || state() == STATE_PRESSED)
121 image_shadow_animator_->animation()->Show();
122 else
123 image_shadow_animator_->animation()->Hide();
124 } else if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
125 background_color = kHighlightedColor;
126 background = views::Background::CreateSolidBackground(background_color);
129 // Tell the label what color it will be drawn onto. It will use whether the
130 // background color is opaque or transparent to decide whether to use subpixel
131 // rendering. Does not actually set the label's background color.
132 title_->SetBackgroundColor(background_color);
134 set_background(background);
135 SchedulePaint();
138 gfx::Size TileItemView::GetPreferredSize() const {
139 return gfx::Size(kTileSize, kTileSize);
142 bool TileItemView::GetTooltipText(const gfx::Point& p,
143 base::string16* tooltip) const {
144 // Use the label to generate a tooltip, so that it will consider its text
145 // truncation in making the tooltip. We do not want the label itself to have a
146 // tooltip, so we only temporarily enable it to get the tooltip text from the
147 // label, then disable it again.
148 title_->SetHandlesTooltips(true);
149 bool handled = title_->GetTooltipText(p, tooltip);
150 title_->SetHandlesTooltips(false);
151 return handled;
154 } // namespace app_list