Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / tile_item_view.cc
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.
4
5 #include "ui/app_list/views/tile_item_view.h"
6
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"
21
22 namespace {
23
24 const int kTileSize = 90;
25 const int kTileHorizontalPadding = 10;
26
27 const SkColor kTileBackgroundColor = SK_ColorWHITE;
28 const int kTileColorStripHeight = 2;
29 const SkAlpha kTileColorStripOpacity = 0X5F;
30 const int kTileCornerRadius = 2;
31
32 }  // namespace
33
34 namespace app_list {
35
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 {
39  public:
40   TileItemBackground() : strip_color_(SK_ColorBLACK) {}
41   virtual ~TileItemBackground() {}
42
43   void set_strip_color(SkColor strip_color) { strip_color_ = strip_color; }
44
45   // Overridden from views::Background:
46   virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
47     SkPaint paint;
48     paint.setFlags(SkPaint::kAntiAlias_Flag);
49
50     // Paint the border.
51     paint.setColor(kStartPageBorderColor);
52     canvas->DrawRoundRect(view->GetContentsBounds(), kTileCornerRadius, paint);
53
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);
59
60     // Paint the main background rectangle, leaving part of the color strip
61     // unobscured.
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);
66   }
67
68  private:
69   SkColor strip_color_;
70
71   DISALLOW_COPY_AND_ASSIGN(TileItemBackground);
72 };
73
74 TileItemView::TileItemView()
75     : views::CustomButton(this),
76       item_(NULL),
77       icon_(new views::ImageView),
78       title_(new views::Label),
79       background_(new TileItemBackground()) {
80   set_background(background_);
81
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);
87
88   icon_->SetImageSize(gfx::Size(kTileIconSize, kTileIconSize));
89
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);
95
96   // When |item_| is NULL, the tile is invisible. Calling SetSearchResult with a
97   // non-NULL item makes the tile visible.
98   SetVisible(false);
99
100   AddChildView(icon_);
101   AddChildView(title_);
102 }
103
104 TileItemView::~TileItemView() {
105   if (item_)
106     item_->RemoveObserver(this);
107 }
108
109 void TileItemView::SetSearchResult(SearchResult* item) {
110   SetVisible(item != NULL);
111
112   SearchResult* old_item = item_;
113   if (old_item)
114     old_item->RemoveObserver(this);
115
116   item_ = item;
117
118   if (!item)
119     return;
120
121   item_->AddObserver(this);
122
123   title_->SetText(item_->title());
124
125   // Only refresh the icon if it's different from the old one. This prevents
126   // flickering.
127   if (old_item == NULL ||
128       !item->icon().BackedBySameObjectAs(old_item->icon())) {
129     OnIconChanged();
130   }
131 }
132
133 gfx::Size TileItemView::GetPreferredSize() const {
134   return gfx::Size(kTileSize, kTileSize);
135 }
136
137 void TileItemView::ButtonPressed(views::Button* sender,
138                                  const ui::Event& event) {
139   item_->Open(event.flags());
140 }
141
142 void TileItemView::OnIconChanged() {
143   icon_->SetImage(item_->icon());
144   background_->set_strip_color(
145       color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap()));
146   SchedulePaint();
147 }
148
149 void TileItemView::OnResultDestroying() {
150   if (item_)
151     item_->RemoveObserver(this);
152   item_ = NULL;
153 }
154
155 }  // namespace app_list