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/search_result.h"
13 #include "ui/app_list/views/app_list_main_view.h"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/color_analysis.h"
16 #include "ui/gfx/color_utils.h"
17 #include "ui/views/background.h"
18 #include "ui/views/controls/image_view.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/layout/box_layout.h"
24 const int kTileSize = 90;
25 const int kTileHorizontalPadding = 10;
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(kTileIconSize, kTileIconSize));
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 SetSearchResult with a
97 // non-NULL item makes the tile visible.
101 AddChildView(title_);
104 TileItemView::~TileItemView() {
106 item_->RemoveObserver(this);
109 void TileItemView::SetSearchResult(SearchResult* item) {
110 SetVisible(item != NULL);
112 SearchResult* old_item = item_;
114 old_item->RemoveObserver(this);
121 item_->AddObserver(this);
123 title_->SetText(item_->title());
125 // Only refresh the icon if it's different from the old one. This prevents
127 if (old_item == NULL ||
128 !item->icon().BackedBySameObjectAs(old_item->icon())) {
133 gfx::Size TileItemView::GetPreferredSize() const {
134 return gfx::Size(kTileSize, kTileSize);
137 void TileItemView::ButtonPressed(views::Button* sender,
138 const ui::Event& event) {
139 item_->Open(event.flags());
142 void TileItemView::OnIconChanged() {
143 icon_->SetImage(item_->icon());
144 background_->set_strip_color(
145 color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap()));
149 void TileItemView::OnResultDestroying() {
151 item_->RemoveObserver(this);
155 } // namespace app_list