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" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "ui/app_list/app_list_constants.h" | 8 #include "ui/app_list/app_list_constants.h" |
9 #include "ui/app_list/app_list_item.h" | 9 #include "ui/app_list/app_list_item.h" |
10 #include "ui/app_list/app_list_model.h" | 10 #include "ui/app_list/app_list_model.h" |
11 #include "ui/app_list/app_list_view_delegate.h" | 11 #include "ui/app_list/app_list_view_delegate.h" |
12 #include "ui/app_list/search_result.h" | |
12 #include "ui/app_list/views/app_list_main_view.h" | 13 #include "ui/app_list/views/app_list_main_view.h" |
13 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/color_analysis.h" | 15 #include "ui/gfx/color_analysis.h" |
15 #include "ui/gfx/color_utils.h" | 16 #include "ui/gfx/color_utils.h" |
16 #include "ui/views/background.h" | 17 #include "ui/views/background.h" |
17 #include "ui/views/controls/image_view.h" | 18 #include "ui/views/controls/image_view.h" |
18 #include "ui/views/controls/label.h" | 19 #include "ui/views/controls/label.h" |
19 #include "ui/views/layout/box_layout.h" | 20 #include "ui/views/layout/box_layout.h" |
20 | 21 |
21 namespace { | 22 namespace { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 SetLayoutManager(layout_manager); | 87 SetLayoutManager(layout_manager); |
87 | 88 |
88 icon_->SetImageSize(gfx::Size(kTileImageSize, kTileImageSize)); | 89 icon_->SetImageSize(gfx::Size(kTileImageSize, kTileImageSize)); |
89 | 90 |
90 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 91 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
91 title_->SetAutoColorReadabilityEnabled(false); | 92 title_->SetAutoColorReadabilityEnabled(false); |
92 title_->SetEnabledColor(kGridTitleColor); | 93 title_->SetEnabledColor(kGridTitleColor); |
93 title_->SetFontList(rb.GetFontList(kItemTextFontStyle)); | 94 title_->SetFontList(rb.GetFontList(kItemTextFontStyle)); |
94 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 95 title_->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
95 | 96 |
96 // When |item_| is NULL, the tile is invisible. Calling SetAppListItem with a | 97 // When |item_| is NULL, the tile is invisible. Calling SetSearchResult with a |
97 // non-NULL item makes the tile visible. | 98 // non-NULL item makes the tile visible. |
98 SetVisible(false); | 99 SetVisible(false); |
99 | 100 |
100 AddChildView(icon_); | 101 AddChildView(icon_); |
101 AddChildView(title_); | 102 AddChildView(title_); |
102 } | 103 } |
103 | 104 |
104 TileItemView::~TileItemView() { | 105 TileItemView::~TileItemView() { |
106 if (item_) | |
107 item_->RemoveObserver(this); | |
105 } | 108 } |
106 | 109 |
107 void TileItemView::SetAppListItem(AppListItem* item) { | 110 void TileItemView::SetSearchResult(SearchResult* item) { |
108 // TODO(calamity): This will not update if the contents of |item_| have | 111 SetVisible(item != NULL); |
109 // changed since it was last assigned. Add an observer to refresh when the | 112 |
110 // item changes. | 113 SearchResult* old_item = item_; |
111 if (item == item_) | 114 if (old_item) |
115 old_item->RemoveObserver(this); | |
116 | |
117 item_ = item; | |
118 | |
119 if (!item) | |
112 return; | 120 return; |
113 | 121 |
114 item_ = item; | 122 item_->AddObserver(this); |
115 if (!item) { | 123 |
116 SetVisible(false); | 124 title_->SetText(item_->title()); |
117 icon_->SetImage(NULL); | 125 |
118 title_->SetText(base::string16()); | 126 // Only refresh the icon if it's different from the old one. This prevents |
119 return; | 127 // flickering. |
128 if (old_item == NULL || | |
129 !item->icon().BackedBySameObjectAs(old_item->icon())) { | |
Matt Giuca
2014/08/06 08:54:53
Do this for AppListItem in a separate patch. (You
calamity
2014/08/12 05:03:37
As discussed, doing this for AppListItems could ha
| |
130 OnIconChanged(); | |
120 } | 131 } |
121 | |
122 SetVisible(true); | |
123 icon_->SetImage(item_->icon()); | |
124 title_->SetText(base::UTF8ToUTF16(item_->name())); | |
125 | |
126 background_->set_strip_color( | |
127 color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap())); | |
128 } | 132 } |
129 | 133 |
130 gfx::Size TileItemView::GetPreferredSize() const { | 134 gfx::Size TileItemView::GetPreferredSize() const { |
131 return gfx::Size(kTileSize, kTileSize); | 135 return gfx::Size(kTileSize, kTileSize); |
132 } | 136 } |
133 | 137 |
134 void TileItemView::ButtonPressed(views::Button* sender, | 138 void TileItemView::ButtonPressed(views::Button* sender, |
135 const ui::Event& event) { | 139 const ui::Event& event) { |
136 item_->Activate(event.flags()); | 140 item_->Open(event.flags()); |
141 } | |
142 | |
143 void TileItemView::OnIconChanged() { | |
144 icon_->SetImage(item_->icon()); | |
145 background_->set_strip_color( | |
146 color_utils::CalculateKMeanColorOfBitmap(*item_->icon().bitmap())); | |
147 SchedulePaint(); | |
148 } | |
149 | |
150 void TileItemView::OnResultDestroying() { | |
151 if (item_) | |
152 item_->RemoveObserver(this); | |
153 item_ = NULL; | |
137 } | 154 } |
138 | 155 |
139 } // namespace app_list | 156 } // namespace app_list |
OLD | NEW |