OLD | NEW |
(Empty) | |
| 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 SkColor kTileBorderColor = SkColorSetARGB(0x30, 0xD0, 0xD0, 0xD0); |
| 29 const int kTileColorStripHeight = 2; |
| 30 const SkAlpha kTileColorStripOpacity = 0X5F; |
| 31 const int kTileCornerRadius = 2; |
| 32 |
| 33 } // namespace |
| 34 |
| 35 namespace app_list { |
| 36 |
| 37 // A background for the start page item view which consists of a rounded rect |
| 38 // with a dominant color strip at the bottom. |
| 39 class TileItemView::TileItemBackground : public views::Background { |
| 40 public: |
| 41 TileItemBackground() : strip_color_(SK_ColorBLACK) {} |
| 42 virtual ~TileItemBackground() {} |
| 43 |
| 44 void set_strip_color(SkColor strip_color) { strip_color_ = strip_color; } |
| 45 |
| 46 // Overridden from views::Background: |
| 47 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE { |
| 48 SkPaint paint; |
| 49 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 50 |
| 51 // Paint the border. |
| 52 paint.setColor(kTileBorderColor); |
| 53 canvas->DrawRoundRect(view->GetContentsBounds(), kTileCornerRadius, paint); |
| 54 |
| 55 // Paint a rectangle for the color strip. |
| 56 gfx::Rect color_strip_rect(view->GetContentsBounds()); |
| 57 color_strip_rect.Inset(1, 1, 1, 1); |
| 58 paint.setColor(SkColorSetA(strip_color_, kTileColorStripOpacity)); |
| 59 canvas->DrawRoundRect(color_strip_rect, kTileCornerRadius, paint); |
| 60 |
| 61 // Paint the main background rectangle, leaving part of the color strip |
| 62 // unobscured. |
| 63 gfx::Rect static_background_rect(color_strip_rect); |
| 64 static_background_rect.Inset(0, 0, 0, kTileColorStripHeight); |
| 65 paint.setColor(kTileBackgroundColor); |
| 66 canvas->DrawRoundRect(static_background_rect, kTileCornerRadius, paint); |
| 67 } |
| 68 |
| 69 private: |
| 70 SkColor strip_color_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(TileItemBackground); |
| 73 }; |
| 74 |
| 75 TileItemView::TileItemView() |
| 76 : views::CustomButton(this), |
| 77 item_(NULL), |
| 78 icon_(new views::ImageView), |
| 79 title_(new views::Label), |
| 80 background_(new TileItemBackground()) { |
| 81 set_background(background_); |
| 82 |
| 83 views::BoxLayout* layout_manager = new views::BoxLayout( |
| 84 views::BoxLayout::kVertical, kTileHorizontalPadding, 0, 0); |
| 85 layout_manager->set_main_axis_alignment( |
| 86 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 87 SetLayoutManager(layout_manager); |
| 88 |
| 89 icon_->SetImageSize(gfx::Size(kTileImageSize, kTileImageSize)); |
| 90 |
| 91 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 92 title_->SetAutoColorReadabilityEnabled(false); |
| 93 title_->SetEnabledColor(kGridTitleColor); |
| 94 title_->SetFontList(rb.GetFontList(kItemTextFontStyle)); |
| 95 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 96 |
| 97 AddChildView(icon_); |
| 98 AddChildView(title_); |
| 99 } |
| 100 |
| 101 TileItemView::~TileItemView() { |
| 102 } |
| 103 |
| 104 void TileItemView::SetAppListItem(AppListItem* item) { |
| 105 item_ = item; |
| 106 if (!item) { |
| 107 SetVisible(false); |
| 108 icon_->SetImage(NULL); |
| 109 title_->SetText(base::string16()); |
| 110 return; |
| 111 } |
| 112 |
| 113 SetVisible(true); |
| 114 icon_->SetImage(item_->icon()); |
| 115 title_->SetText(base::UTF8ToUTF16(item_->name())); |
| 116 |
| 117 background_->set_strip_color( |
| 118 color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap())); |
| 119 } |
| 120 |
| 121 gfx::Size TileItemView::GetPreferredSize() const { |
| 122 return gfx::Size(kTileSize, kTileSize); |
| 123 } |
| 124 |
| 125 void TileItemView::ButtonPressed(views::Button* sender, |
| 126 const ui::Event& event) { |
| 127 item_->Activate(event.flags()); |
| 128 } |
| 129 |
| 130 } // namespace app_list |
OLD | NEW |