• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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"
20 
21 namespace {
22 
23 const int kTileSize = 90;
24 const int kTileHorizontalPadding = 10;
25 const int kTileImageSize = 48;
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:
TileItemBackground()40   TileItemBackground() : strip_color_(SK_ColorBLACK) {}
~TileItemBackground()41   virtual ~TileItemBackground() {}
42 
set_strip_color(SkColor strip_color)43   void set_strip_color(SkColor strip_color) { strip_color_ = strip_color; }
44 
45   // Overridden from views::Background:
Paint(gfx::Canvas * canvas,views::View * view) const46   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 
TileItemView()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(kTileImageSize, kTileImageSize));
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 SetAppListItem with a
97   // non-NULL item makes the tile visible.
98   SetVisible(false);
99 
100   AddChildView(icon_);
101   AddChildView(title_);
102 }
103 
~TileItemView()104 TileItemView::~TileItemView() {
105 }
106 
SetAppListItem(AppListItem * item)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
110   // item changes.
111   if (item == item_)
112     return;
113 
114   item_ = item;
115   if (!item) {
116     SetVisible(false);
117     icon_->SetImage(NULL);
118     title_->SetText(base::string16());
119     return;
120   }
121 
122   SetVisible(true);
123   icon_->SetImage(item_->icon());
124   title_->SetText(base::UTF8ToUTF16(item_->name()));
125 
126   background_->set_strip_color(
127       color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap()));
128 }
129 
GetPreferredSize() const130 gfx::Size TileItemView::GetPreferredSize() const {
131   return gfx::Size(kTileSize, kTileSize);
132 }
133 
ButtonPressed(views::Button * sender,const ui::Event & event)134 void TileItemView::ButtonPressed(views::Button* sender,
135                                  const ui::Event& event) {
136   item_->Activate(event.flags());
137 }
138 
139 }  // namespace app_list
140