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