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/search_result_tile_item_view.h" | 5 #include "ui/app_list/views/search_result_tile_item_view.h" |
6 | 6 |
| 7 #include "base/i18n/number_formatting.h" |
7 #include "ui/app_list/app_list_constants.h" | 8 #include "ui/app_list/app_list_constants.h" |
8 #include "ui/app_list/app_list_features.h" | 9 #include "ui/app_list/app_list_features.h" |
9 #include "ui/app_list/app_list_view_delegate.h" | 10 #include "ui/app_list/app_list_view_delegate.h" |
10 #include "ui/app_list/search_result.h" | 11 #include "ui/app_list/search_result.h" |
11 #include "ui/app_list/views/search_result_container_view.h" | 12 #include "ui/app_list/views/search_result_container_view.h" |
12 #include "ui/views/controls/image_view.h" | 13 #include "ui/views/controls/image_view.h" |
13 #include "ui/views/controls/label.h" | 14 #include "ui/views/controls/label.h" |
14 #include "ui/views/controls/menu/menu_runner.h" | 15 #include "ui/views/controls/menu/menu_runner.h" |
15 | 16 |
16 namespace app_list { | 17 namespace app_list { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 constexpr int kRecommendationTileWidth = 96; | 21 constexpr int kRecommendationTileWidth = 96; |
21 constexpr int kRecommendationTileHeight = 99; | 22 constexpr int kRecommendationTileHeight = 99; |
22 constexpr int kRecommendationIconTopPadding = 24; | 23 constexpr int kRecommendationIconTopPadding = 24; |
23 constexpr int kRecommendationTitleSpacing = 10; | 24 constexpr int kRecommendationTitleSpacing = 10; |
24 constexpr int kRecommendationTileMaxWidth = 80; | 25 constexpr int kRecommendationTileMaxWidth = 80; |
25 | 26 |
| 27 constexpr int kSearchTileWidth = 80; |
| 28 constexpr int kSearchTileHeight = 92; |
26 constexpr int kSearchTileTopPadding = 4; | 29 constexpr int kSearchTileTopPadding = 4; |
27 constexpr int kSearchTitleSpacing = 6; | 30 constexpr int kSearchTitleSpacing = 6; |
28 | 31 |
29 constexpr SkColor kRecommendationTileColor = SK_ColorWHITE; | 32 constexpr SkColor kRecommendationTileColor = SK_ColorWHITE; |
30 | 33 |
| 34 constexpr SkColor kSearchTitleColor = SkColorSetA(SK_ColorBLACK, 223); |
| 35 constexpr SkColor kSearchAppRatingColor = SkColorSetA(SK_ColorBLACK, 143); |
| 36 constexpr SkColor kSearchAppPriceColor = SkColorSetRGB(0x0F, 0x9D, 0x58); |
| 37 |
31 } // namespace | 38 } // namespace |
32 | 39 |
33 SearchResultTileItemView::SearchResultTileItemView( | 40 SearchResultTileItemView::SearchResultTileItemView( |
34 SearchResultContainerView* result_container, | 41 SearchResultContainerView* result_container, |
35 AppListViewDelegate* view_delegate) | 42 AppListViewDelegate* view_delegate) |
36 : result_container_(result_container), | 43 : result_container_(result_container), |
37 item_(nullptr), | |
38 view_delegate_(view_delegate), | 44 view_delegate_(view_delegate), |
39 is_fullscreen_app_list_enabled_(features::IsFullscreenAppListEnabled()) { | 45 is_fullscreen_app_list_enabled_(features::IsFullscreenAppListEnabled()) { |
40 // When |item_| is null, the tile is invisible. Calling SetSearchResult with a | 46 // When |item_| is null, the tile is invisible. Calling SetSearchResult with a |
41 // non-null item makes the tile visible. | 47 // non-null item makes the tile visible. |
42 SetVisible(false); | 48 SetVisible(false); |
43 | 49 |
| 50 if (is_fullscreen_app_list_enabled_) { |
| 51 const gfx::FontList& base_font = |
| 52 ui::ResourceBundle::GetSharedInstance().GetFontList( |
| 53 ui::ResourceBundle::BaseFont); |
| 54 |
| 55 rating_ = new views::Label; |
| 56 rating_->SetEnabledColor(kSearchAppRatingColor); |
| 57 rating_->SetFontList(base_font); |
| 58 rating_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 59 rating_->SetVisible(false); |
| 60 AddChildView(rating_); |
| 61 |
| 62 price_ = new views::Label; |
| 63 price_->SetEnabledColor(kSearchAppPriceColor); |
| 64 price_->SetFontList(base_font); |
| 65 price_->SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| 66 price_->SetVisible(false); |
| 67 AddChildView(price_); |
| 68 } |
| 69 |
44 set_context_menu_controller(this); | 70 set_context_menu_controller(this); |
45 } | 71 } |
46 | 72 |
47 SearchResultTileItemView::~SearchResultTileItemView() { | 73 SearchResultTileItemView::~SearchResultTileItemView() { |
48 if (item_) | 74 if (item_) |
49 item_->RemoveObserver(this); | 75 item_->RemoveObserver(this); |
50 } | 76 } |
51 | 77 |
52 void SearchResultTileItemView::SetSearchResult(SearchResult* item) { | 78 void SearchResultTileItemView::SetSearchResult(SearchResult* item) { |
53 // Handle the case where this may be called from a nested run loop while its | 79 // Handle the case where this may be called from a nested run loop while its |
54 // context menu is showing. This cancels the menu (it's for the old item). | 80 // context menu is showing. This cancels the menu (it's for the old item). |
55 context_menu_runner_.reset(); | 81 context_menu_runner_.reset(); |
56 | 82 |
57 SetVisible(!!item); | 83 SetVisible(!!item); |
58 | 84 |
59 SearchResult* old_item = item_; | 85 SearchResult* old_item = item_; |
60 if (old_item) | 86 if (old_item) |
61 old_item->RemoveObserver(this); | 87 old_item->RemoveObserver(this); |
62 | 88 |
63 item_ = item; | 89 item_ = item; |
64 | 90 |
65 if (!item) | 91 if (!item) |
66 return; | 92 return; |
67 | 93 |
68 item_->AddObserver(this); | 94 item_->AddObserver(this); |
69 | 95 |
70 SetTitle(item_->title()); | 96 SetTitle(item_->title()); |
| 97 SetRating(item_->rating()); |
| 98 SetPrice(item_->formatted_price()); |
71 | 99 |
72 // Customize title UI | 100 if (is_fullscreen_app_list_enabled_) { |
73 gfx::FontList base_font = ui::ResourceBundle::GetSharedInstance().GetFontList( | 101 const gfx::FontList& base_font = |
74 ui::ResourceBundle::BaseFont); | 102 ui::ResourceBundle::GetSharedInstance().GetFontList( |
75 if (is_fullscreen_app_list_enabled_ && | 103 ui::ResourceBundle::BaseFont); |
76 item_->display_type() == SearchResult::DISPLAY_RECOMMENDATION) { | 104 |
77 title()->SetFontList(base_font.DeriveWithSizeDelta(1)); | 105 // Customize title UI |
78 title()->SetEnabledColor(kRecommendationTileColor); | 106 if (item_->display_type() == SearchResult::DISPLAY_RECOMMENDATION) { |
| 107 title()->SetFontList(base_font.DeriveWithSizeDelta(1)); |
| 108 title()->SetEnabledColor(kRecommendationTileColor); |
| 109 } else if (item_->display_type() == SearchResult::DISPLAY_TILE) { |
| 110 title()->SetFontList(base_font.DeriveWithSizeDelta(1)); |
| 111 title()->SetEnabledColor(kSearchTitleColor); |
| 112 } |
79 } | 113 } |
80 | 114 |
81 // Only refresh the icon if it's different from the old one. This prevents | 115 // Only refresh the icon if it's different from the old one. This prevents |
82 // flickering. | 116 // flickering. |
83 if (!old_item || !item->icon().BackedBySameObjectAs(old_item->icon())) { | 117 if (!old_item || !item->icon().BackedBySameObjectAs(old_item->icon())) { |
84 OnIconChanged(); | 118 OnIconChanged(); |
85 } | 119 } |
86 } | 120 } |
87 | 121 |
| 122 void SearchResultTileItemView::SetRating(float rating) { |
| 123 if (!rating_) |
| 124 return; |
| 125 |
| 126 if (rating < 0) { |
| 127 rating_->SetVisible(false); |
| 128 return; |
| 129 } |
| 130 |
| 131 rating_->SetText(base::FormatDouble(rating, 1)); |
| 132 rating_->SetVisible(true); |
| 133 } |
| 134 |
| 135 void SearchResultTileItemView::SetPrice(const base::string16& price) { |
| 136 if (!price_) |
| 137 return; |
| 138 |
| 139 if (price.empty()) { |
| 140 price_->SetVisible(false); |
| 141 return; |
| 142 } |
| 143 |
| 144 price_->SetText(price); |
| 145 price_->SetVisible(true); |
| 146 } |
| 147 |
88 void SearchResultTileItemView::ButtonPressed(views::Button* sender, | 148 void SearchResultTileItemView::ButtonPressed(views::Button* sender, |
89 const ui::Event& event) { | 149 const ui::Event& event) { |
90 view_delegate_->OpenSearchResult(item_, false, event.flags()); | 150 view_delegate_->OpenSearchResult(item_, false, event.flags()); |
91 } | 151 } |
92 | 152 |
93 bool SearchResultTileItemView::OnKeyPressed(const ui::KeyEvent& event) { | 153 bool SearchResultTileItemView::OnKeyPressed(const ui::KeyEvent& event) { |
94 if (event.key_code() == ui::VKEY_RETURN) { | 154 if (event.key_code() == ui::VKEY_RETURN) { |
95 view_delegate_->OpenSearchResult(item_, false, event.flags()); | 155 view_delegate_->OpenSearchResult(item_, false, event.flags()); |
96 return true; | 156 return true; |
97 } | 157 } |
98 | 158 |
99 return false; | 159 return false; |
100 } | 160 } |
101 | 161 |
102 void SearchResultTileItemView::OnIconChanged() { | 162 void SearchResultTileItemView::OnIconChanged() { |
103 SetIcon(item_->icon()); | 163 SetIcon(item_->icon()); |
104 } | 164 } |
105 | 165 |
106 void SearchResultTileItemView::OnBadgeIconChanged() { | 166 void SearchResultTileItemView::OnBadgeIconChanged() { |
107 SetBadgeIcon(item_->badge_icon()); | 167 SetBadgeIcon(item_->badge_icon()); |
108 } | 168 } |
109 | 169 |
| 170 void SearchResultTileItemView::OnRatingChanged() { |
| 171 SetRating(item_->rating()); |
| 172 } |
| 173 |
| 174 void SearchResultTileItemView::OnPriceChanged() { |
| 175 SetPrice(item_->formatted_price()); |
| 176 } |
| 177 |
110 void SearchResultTileItemView::OnResultDestroying() { | 178 void SearchResultTileItemView::OnResultDestroying() { |
111 // The menu comes from |item_|. If we're showing a menu we need to cancel it. | 179 // The menu comes from |item_|. If we're showing a menu we need to cancel it. |
112 context_menu_runner_.reset(); | 180 context_menu_runner_.reset(); |
113 | 181 |
114 if (item_) | 182 if (item_) |
115 item_->RemoveObserver(this); | 183 item_->RemoveObserver(this); |
116 | 184 |
117 SetSearchResult(nullptr); | 185 SetSearchResult(nullptr); |
118 } | 186 } |
119 | 187 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 gfx::Size icon_size = icon()->GetImage().size(); | 234 gfx::Size icon_size = icon()->GetImage().size(); |
167 badge_rect.Offset( | 235 badge_rect.Offset( |
168 (icon_size.width() - kAppBadgeIconSize) / 2, | 236 (icon_size.width() - kAppBadgeIconSize) / 2, |
169 icon_size.height() - kBadgeBackgroundRadius - kAppBadgeIconSize / 2); | 237 icon_size.height() - kBadgeBackgroundRadius - kAppBadgeIconSize / 2); |
170 badge()->SetBoundsRect(badge_rect); | 238 badge()->SetBoundsRect(badge_rect); |
171 } | 239 } |
172 | 240 |
173 rect.Inset(0, kGridIconDimension + kSearchTitleSpacing, 0, 0); | 241 rect.Inset(0, kGridIconDimension + kSearchTitleSpacing, 0, 0); |
174 rect.set_height(title()->GetPreferredSize().height()); | 242 rect.set_height(title()->GetPreferredSize().height()); |
175 title()->SetBoundsRect(rect); | 243 title()->SetBoundsRect(rect); |
| 244 |
| 245 if (rating_) { |
| 246 gfx::Rect rating_rect(rect); |
| 247 rating_rect.Inset(0, title()->GetPreferredSize().height(), 0, 0); |
| 248 rating_rect.set_height(rating_->GetPreferredSize().height()); |
| 249 rating_->SetBoundsRect(rating_rect); |
| 250 } |
| 251 |
| 252 if (price_) { |
| 253 gfx::Rect price_rect(rect); |
| 254 price_rect.Inset(0, title()->GetPreferredSize().height(), 0, 0); |
| 255 price_rect.set_height(price_->GetPreferredSize().height()); |
| 256 price_->SetBoundsRect(price_rect); |
| 257 } |
176 } else { | 258 } else { |
177 TileItemView::Layout(); | 259 TileItemView::Layout(); |
178 } | 260 } |
179 } | 261 } |
180 | 262 |
181 gfx::Size SearchResultTileItemView::CalculatePreferredSize() const { | 263 gfx::Size SearchResultTileItemView::CalculatePreferredSize() const { |
182 if (is_fullscreen_app_list_enabled_ && item_ && | 264 if (is_fullscreen_app_list_enabled_ && item_) { |
183 item_->display_type() == SearchResult::DISPLAY_RECOMMENDATION) { | 265 if (item_->display_type() == SearchResult::DISPLAY_RECOMMENDATION) |
184 return gfx::Size(kRecommendationTileWidth, kRecommendationTileHeight); | 266 return gfx::Size(kRecommendationTileWidth, kRecommendationTileHeight); |
| 267 if (item_->display_type() == SearchResult::DISPLAY_TILE) |
| 268 return gfx::Size(kSearchTileWidth, kSearchTileHeight); |
185 } | 269 } |
186 | 270 |
187 return TileItemView::CalculatePreferredSize(); | 271 return TileItemView::CalculatePreferredSize(); |
188 } | 272 } |
189 | 273 |
190 } // namespace app_list | 274 } // namespace app_list |
OLD | NEW |