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