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;
31 TileItemView::TileItemView()
32 : views::CustomButton(this),
34 icon_(new views::ImageView
),
35 title_(new views::Label
) {
36 views::BoxLayout
* layout_manager
= new views::BoxLayout(
37 views::BoxLayout::kVertical
, kTileHorizontalPadding
, 0, 0);
38 layout_manager
->set_main_axis_alignment(
39 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
40 SetLayoutManager(layout_manager
);
42 icon_
->SetImageSize(gfx::Size(kTileIconSize
, kTileIconSize
));
44 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
45 title_
->SetAutoColorReadabilityEnabled(false);
46 title_
->SetEnabledColor(kGridTitleColor
);
47 title_
->set_background(views::Background::CreateSolidBackground(
48 app_list::kContentsBackgroundColor
));
49 title_
->SetFontList(rb
.GetFontList(kItemTextFontStyle
));
50 title_
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
52 // When |item_| is NULL, the tile is invisible. Calling SetSearchResult with a
53 // non-NULL item makes the tile visible.
60 TileItemView::~TileItemView() {
62 item_
->RemoveObserver(this);
65 void TileItemView::SetSearchResult(SearchResult
* item
) {
66 SetVisible(item
!= NULL
);
68 SearchResult
* old_item
= item_
;
70 old_item
->RemoveObserver(this);
77 item_
->AddObserver(this);
79 title_
->SetText(item_
->title());
81 // Only refresh the icon if it's different from the old one. This prevents
83 if (old_item
== NULL
||
84 !item
->icon().BackedBySameObjectAs(old_item
->icon())) {
89 gfx::Size
TileItemView::GetPreferredSize() const {
90 return gfx::Size(kTileSize
, kTileSize
);
93 void TileItemView::ButtonPressed(views::Button
* sender
,
94 const ui::Event
& event
) {
95 item_
->Open(event
.flags());
98 void TileItemView::OnIconChanged() {
99 icon_
->SetImage(item_
->icon());
102 void TileItemView::OnResultDestroying() {
104 item_
->RemoveObserver(this);
108 } // namespace app_list