| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/app_list/views/tile_item_view.h" | 5 #include "ui/app_list/views/tile_item_view.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "ui/app_list/app_list_constants.h" | 7 #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" | 8 #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" | 9 #include "ui/views/background.h" |
| 18 #include "ui/views/controls/image_view.h" | 10 #include "ui/views/controls/image_view.h" |
| 19 #include "ui/views/controls/label.h" | 11 #include "ui/views/controls/label.h" |
| 20 #include "ui/views/layout/box_layout.h" | 12 #include "ui/views/layout/box_layout.h" |
| 21 | 13 |
| 22 namespace { | 14 namespace { |
| 23 | 15 |
| 24 const int kTileSize = 90; | 16 const int kTileSize = 90; |
| 25 const int kTileHorizontalPadding = 10; | 17 const int kTileHorizontalPadding = 10; |
| 26 | 18 |
| 27 } // namespace | 19 } // namespace |
| 28 | 20 |
| 29 namespace app_list { | 21 namespace app_list { |
| 30 | 22 |
| 31 TileItemView::TileItemView() | 23 TileItemView::TileItemView() |
| 32 : views::CustomButton(this), | 24 : views::CustomButton(this), |
| 33 item_(NULL), | |
| 34 icon_(new views::ImageView), | 25 icon_(new views::ImageView), |
| 35 title_(new views::Label) { | 26 title_(new views::Label) { |
| 36 views::BoxLayout* layout_manager = new views::BoxLayout( | 27 views::BoxLayout* layout_manager = new views::BoxLayout( |
| 37 views::BoxLayout::kVertical, kTileHorizontalPadding, 0, 0); | 28 views::BoxLayout::kVertical, kTileHorizontalPadding, 0, 0); |
| 38 layout_manager->set_main_axis_alignment( | 29 layout_manager->set_main_axis_alignment( |
| 39 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 30 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 40 SetLayoutManager(layout_manager); | 31 SetLayoutManager(layout_manager); |
| 41 | 32 |
| 42 icon_->SetImageSize(gfx::Size(kTileIconSize, kTileIconSize)); | 33 icon_->SetImageSize(gfx::Size(kTileIconSize, kTileIconSize)); |
| 43 | 34 |
| 44 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 35 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 45 title_->SetAutoColorReadabilityEnabled(false); | 36 title_->SetAutoColorReadabilityEnabled(false); |
| 46 title_->SetEnabledColor(kGridTitleColor); | 37 title_->SetEnabledColor(kGridTitleColor); |
| 47 title_->set_background(views::Background::CreateSolidBackground( | 38 title_->set_background(views::Background::CreateSolidBackground( |
| 48 kLabelBackgroundColor)); | 39 kLabelBackgroundColor)); |
| 49 title_->SetFontList(rb.GetFontList(kItemTextFontStyle)); | 40 title_->SetFontList(rb.GetFontList(kItemTextFontStyle)); |
| 50 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 41 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| 51 | 42 |
| 52 // When |item_| is NULL, the tile is invisible. Calling SetSearchResult with a | 43 // When |item_| is NULL, the tile is invisible. Calling SetSearchResult with a |
| 53 // non-NULL item makes the tile visible. | 44 // non-NULL item makes the tile visible. |
| 54 SetVisible(false); | 45 SetVisible(false); |
| 55 | 46 |
| 56 AddChildView(icon_); | 47 AddChildView(icon_); |
| 57 AddChildView(title_); | 48 AddChildView(title_); |
| 58 } | 49 } |
| 59 | 50 |
| 60 TileItemView::~TileItemView() { | 51 TileItemView::~TileItemView() { |
| 61 if (item_) | |
| 62 item_->RemoveObserver(this); | |
| 63 } | 52 } |
| 64 | 53 |
| 65 void TileItemView::SetSearchResult(SearchResult* item) { | 54 void TileItemView::SetIcon(const gfx::ImageSkia& icon) { |
| 66 SetVisible(item != NULL); | 55 icon_->SetImage(icon); |
| 56 } |
| 67 | 57 |
| 68 SearchResult* old_item = item_; | 58 void TileItemView::SetTitle(const base::string16& title) { |
| 69 if (old_item) | 59 title_->SetText(title); |
| 70 old_item->RemoveObserver(this); | |
| 71 | |
| 72 item_ = item; | |
| 73 | |
| 74 if (!item) | |
| 75 return; | |
| 76 | |
| 77 item_->AddObserver(this); | |
| 78 | |
| 79 title_->SetText(item_->title()); | |
| 80 | |
| 81 // Only refresh the icon if it's different from the old one. This prevents | |
| 82 // flickering. | |
| 83 if (old_item == NULL || | |
| 84 !item->icon().BackedBySameObjectAs(old_item->icon())) { | |
| 85 OnIconChanged(); | |
| 86 } | |
| 87 } | 60 } |
| 88 | 61 |
| 89 gfx::Size TileItemView::GetPreferredSize() const { | 62 gfx::Size TileItemView::GetPreferredSize() const { |
| 90 return gfx::Size(kTileSize, kTileSize); | 63 return gfx::Size(kTileSize, kTileSize); |
| 91 } | 64 } |
| 92 | 65 |
| 93 void TileItemView::ButtonPressed(views::Button* sender, | |
| 94 const ui::Event& event) { | |
| 95 item_->Open(event.flags()); | |
| 96 } | |
| 97 | |
| 98 void TileItemView::OnIconChanged() { | |
| 99 icon_->SetImage(item_->icon()); | |
| 100 } | |
| 101 | |
| 102 void TileItemView::OnResultDestroying() { | |
| 103 if (item_) | |
| 104 item_->RemoveObserver(this); | |
| 105 item_ = NULL; | |
| 106 } | |
| 107 | |
| 108 } // namespace app_list | 66 } // namespace app_list |
| OLD | NEW |