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 "base/strings/utf_string_conversions.h"
8 #include "ui/app_list/app_list_constants.h"
9 #include "ui/app_list/app_list_item.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/app_list_view_delegate.h"
12 #include "ui/app_list/views/app_list_main_view.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/color_analysis.h"
15 #include "ui/gfx/color_utils.h"
16 #include "ui/views/background.h"
17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/box_layout.h"
23 const int kTileSize
= 90;
24 const int kTileHorizontalPadding
= 10;
25 const int kTileImageSize
= 48;
27 const SkColor kTileBackgroundColor
= SK_ColorWHITE
;
28 const int kTileColorStripHeight
= 2;
29 const SkAlpha kTileColorStripOpacity
= 0X5F;
30 const int kTileCornerRadius
= 2;
36 // A background for the start page item view which consists of a rounded rect
37 // with a dominant color strip at the bottom.
38 class TileItemView::TileItemBackground
: public views::Background
{
40 TileItemBackground() : strip_color_(SK_ColorBLACK
) {}
41 virtual ~TileItemBackground() {}
43 void set_strip_color(SkColor strip_color
) { strip_color_
= strip_color
; }
45 // Overridden from views::Background:
46 virtual void Paint(gfx::Canvas
* canvas
, views::View
* view
) const OVERRIDE
{
48 paint
.setFlags(SkPaint::kAntiAlias_Flag
);
51 paint
.setColor(kStartPageBorderColor
);
52 canvas
->DrawRoundRect(view
->GetContentsBounds(), kTileCornerRadius
, paint
);
54 // Paint a rectangle for the color strip.
55 gfx::Rect
color_strip_rect(view
->GetContentsBounds());
56 color_strip_rect
.Inset(1, 1, 1, 1);
57 paint
.setColor(SkColorSetA(strip_color_
, kTileColorStripOpacity
));
58 canvas
->DrawRoundRect(color_strip_rect
, kTileCornerRadius
, paint
);
60 // Paint the main background rectangle, leaving part of the color strip
62 gfx::Rect
static_background_rect(color_strip_rect
);
63 static_background_rect
.Inset(0, 0, 0, kTileColorStripHeight
);
64 paint
.setColor(kTileBackgroundColor
);
65 canvas
->DrawRoundRect(static_background_rect
, kTileCornerRadius
, paint
);
71 DISALLOW_COPY_AND_ASSIGN(TileItemBackground
);
74 TileItemView::TileItemView()
75 : views::CustomButton(this),
77 icon_(new views::ImageView
),
78 title_(new views::Label
),
79 background_(new TileItemBackground()) {
80 set_background(background_
);
82 views::BoxLayout
* layout_manager
= new views::BoxLayout(
83 views::BoxLayout::kVertical
, kTileHorizontalPadding
, 0, 0);
84 layout_manager
->set_main_axis_alignment(
85 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
86 SetLayoutManager(layout_manager
);
88 icon_
->SetImageSize(gfx::Size(kTileImageSize
, kTileImageSize
));
90 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
91 title_
->SetAutoColorReadabilityEnabled(false);
92 title_
->SetEnabledColor(kGridTitleColor
);
93 title_
->SetFontList(rb
.GetFontList(kItemTextFontStyle
));
94 title_
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
96 // When |item_| is NULL, the tile is invisible. Calling SetAppListItem with a
97 // non-NULL item makes the tile visible.
101 AddChildView(title_
);
104 TileItemView::~TileItemView() {
107 void TileItemView::SetAppListItem(AppListItem
* item
) {
108 // TODO(calamity): This will not update if the contents of |item_| have
109 // changed since it was last assigned. Add an observer to refresh when the
117 icon_
->SetImage(NULL
);
118 title_
->SetText(base::string16());
123 icon_
->SetImage(item_
->icon());
124 title_
->SetText(base::UTF8ToUTF16(item_
->name()));
126 background_
->set_strip_color(
127 color_utils::CalculateKMeanColorOfBitmap(*item_
->icon().bitmap()));
130 gfx::Size
TileItemView::GetPreferredSize() const {
131 return gfx::Size(kTileSize
, kTileSize
);
134 void TileItemView::ButtonPressed(views::Button
* sender
,
135 const ui::Event
& event
) {
136 item_
->Activate(event
.flags());
139 } // namespace app_list