Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/app_list_item_view.h" | 5 #include "ui/app_list/app_list_item_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/synchronization/cancellation_flag.h" | 11 #include "base/synchronization/cancellation_flag.h" |
| 12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "skia/ext/image_operations.h" | |
| 15 #include "ui/app_list/app_list_item_model.h" | 14 #include "ui/app_list/app_list_item_model.h" |
| 16 #include "ui/app_list/apps_grid_view.h" | 15 #include "ui/app_list/apps_grid_view.h" |
| 17 #include "ui/app_list/drop_shadow_label.h" | 16 #include "ui/app_list/drop_shadow_label.h" |
| 18 #include "ui/app_list/icon_cache.h" | 17 #include "ui/app_list/icon_cache.h" |
| 19 #include "ui/base/accessibility/accessible_view_state.h" | 18 #include "ui/base/accessibility/accessible_view_state.h" |
| 20 #include "ui/base/animation/throb_animation.h" | 19 #include "ui/base/animation/throb_animation.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
| 24 #include "ui/gfx/skbitmap_operations.h" | 23 #include "ui/gfx/image/image_skia_operations.h" |
| 25 #include "ui/views/controls/image_view.h" | 24 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/menu/menu_item_view.h" | 25 #include "ui/views/controls/menu/menu_item_view.h" |
| 27 #include "ui/views/controls/menu/menu_model_adapter.h" | 26 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 28 #include "ui/views/controls/menu/menu_runner.h" | 27 #include "ui/views/controls/menu/menu_runner.h" |
| 29 | 28 |
| 30 namespace app_list { | 29 namespace app_list { |
| 31 | 30 |
| 32 namespace { | 31 namespace { |
| 33 | 32 |
| 34 const int kTopBottomPadding = 10; | 33 const int kTopBottomPadding = 10; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 | 77 |
| 79 } // namespace | 78 } // namespace |
| 80 | 79 |
| 81 // static | 80 // static |
| 82 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView"; | 81 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView"; |
| 83 | 82 |
| 84 // AppListItemView::IconOperation wraps background icon processing. | 83 // AppListItemView::IconOperation wraps background icon processing. |
| 85 class AppListItemView::IconOperation | 84 class AppListItemView::IconOperation |
| 86 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> { | 85 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> { |
| 87 public: | 86 public: |
| 88 IconOperation(const SkBitmap& bitmap, | 87 IconOperation(const gfx::ImageSkia& image, |
| 89 const gfx::Size& size, | 88 const gfx::Size& size, |
| 90 const gfx::ShadowValues& shadows) | 89 const gfx::ShadowValues& shadows) |
| 91 : bitmap_(bitmap), | 90 : image_(image), |
| 92 size_(size), | 91 size_(size), |
| 93 shadows_(shadows) { | 92 shadows_(shadows) { |
| 94 } | 93 } |
| 95 | 94 |
| 96 static void Run(scoped_refptr<IconOperation> op) { | 95 static void Run(scoped_refptr<IconOperation> op) { |
| 97 op->ResizeAndGenerateShadow(); | 96 op->ResizeAndGenerateShadow(); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void ResizeAndGenerateShadow() { | 99 void ResizeAndGenerateShadow() { |
| 101 if (cancel_flag_.IsSet()) | 100 if (cancel_flag_.IsSet()) |
| 102 return; | 101 return; |
| 103 | 102 |
| 104 if (size_ != gfx::Size(bitmap_.width(), bitmap_.height())) { | 103 gfx::ImageSkia resized( |
| 105 bitmap_ = skia::ImageOperations::Resize(bitmap_, | 104 gfx::ImageSkiaOperations::CreateResizedImage(image_, size_)); |
| 106 skia::ImageOperations::RESIZE_BEST, size_.width(), size_.height()); | 105 gfx::ImageSkia shadow( |
| 106 gfx::ImageSkiaOperations::CreateImageWithDropShadow(resized, shadows_)); | |
| 107 | |
| 108 // The following statement causes shadowed image being generated for all | |
| 109 // existing image reps in |image_|. This is needed so that expensive shadow | |
| 110 // generation does not run on UI thread. | |
| 111 gfx::ImageSkia::ImageSkiaReps image_reps = image_.image_reps(); | |
| 112 for (gfx::ImageSkia::ImageSkiaReps::const_iterator it = image_reps.begin(); | |
|
pkotwicz
2012/07/16 22:07:05
This will not work. image_reps() will return an em
xiyuan
2012/07/16 22:15:12
I am getting the existing image_reps of the origin
pkotwicz
2012/07/16 22:49:30
Sorry, my bad. This works.
image_reps() on shadow
| |
| 113 it != image_reps.end(); ++it) { | |
| 114 shadow.GetRepresentation(it->scale_factor()); | |
| 107 } | 115 } |
| 108 | 116 image_ = shadow; |
| 109 if (cancel_flag_.IsSet()) | |
| 110 return; | |
| 111 | |
| 112 bitmap_ = SkBitmapOperations::CreateDropShadow(bitmap_, shadows_); | |
| 113 } | 117 } |
| 114 | 118 |
| 115 void Cancel() { | 119 void Cancel() { |
| 116 cancel_flag_.Set(); | 120 cancel_flag_.Set(); |
| 117 } | 121 } |
| 118 | 122 |
| 119 const SkBitmap& bitmap() const { | 123 const gfx::ImageSkia& image() const { |
| 120 return bitmap_; | 124 return image_; |
| 121 } | 125 } |
| 122 | 126 |
| 123 private: | 127 private: |
| 124 friend class base::RefCountedThreadSafe<AppListItemView::IconOperation>; | 128 friend class base::RefCountedThreadSafe<AppListItemView::IconOperation>; |
| 125 ~IconOperation() {} | 129 ~IconOperation() {} |
| 126 | 130 |
| 127 base::CancellationFlag cancel_flag_; | 131 base::CancellationFlag cancel_flag_; |
| 128 | 132 |
| 129 SkBitmap bitmap_; | 133 gfx::ImageSkia image_; |
| 130 const gfx::Size size_; | 134 const gfx::Size size_; |
| 131 const gfx::ShadowValues shadows_; | 135 const gfx::ShadowValues shadows_; |
| 132 | 136 |
| 133 DISALLOW_COPY_AND_ASSIGN(IconOperation); | 137 DISALLOW_COPY_AND_ASSIGN(IconOperation); |
| 134 }; | 138 }; |
| 135 | 139 |
| 136 AppListItemView::AppListItemView(AppsGridView* apps_grid_view, | 140 AppListItemView::AppListItemView(AppsGridView* apps_grid_view, |
| 137 AppListItemModel* model, | 141 AppListItemModel* model, |
| 138 views::ButtonListener* listener) | 142 views::ButtonListener* listener) |
| 139 : CustomButton(listener), | 143 : CustomButton(listener), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 | 177 |
| 174 icon_size_ = size; | 178 icon_size_ = size; |
| 175 UpdateIcon(); | 179 UpdateIcon(); |
| 176 } | 180 } |
| 177 | 181 |
| 178 void AppListItemView::UpdateIcon() { | 182 void AppListItemView::UpdateIcon() { |
| 179 // Skip if |icon_size_| has not been determined. | 183 // Skip if |icon_size_| has not been determined. |
| 180 if (icon_size_.IsEmpty()) | 184 if (icon_size_.IsEmpty()) |
| 181 return; | 185 return; |
| 182 | 186 |
| 183 SkBitmap icon = model_->icon(); | 187 gfx::ImageSkia icon = model_->icon(); |
| 184 // Clear icon and bail out if model icon is empty. | 188 // Clear icon and bail out if model icon is empty. |
| 185 if (icon.empty()) { | 189 if (icon.empty()) { |
| 186 icon_->SetImage(NULL); | 190 icon_->SetImage(NULL); |
| 187 return; | 191 return; |
| 188 } | 192 } |
| 189 | 193 |
| 190 CancelPendingIconOperation(); | 194 CancelPendingIconOperation(); |
| 191 | 195 |
| 192 SkBitmap shadow; | 196 gfx::ImageSkia shadow; |
| 193 if (IconCache::GetInstance()->Get(icon, icon_size_, &shadow)) { | 197 if (IconCache::GetInstance()->Get(icon, icon_size_, &shadow)) { |
| 194 icon_->SetImage(shadow); | 198 icon_->SetImage(shadow); |
| 195 } else { | 199 } else { |
| 196 // Schedule resize and shadow generation. | 200 // Schedule resize and shadow generation. |
| 197 icon_op_ = new IconOperation(icon, icon_size_, icon_shadows_); | 201 icon_op_ = new IconOperation(icon, icon_size_, icon_shadows_); |
| 198 base::WorkerPool::PostTaskAndReply( | 202 base::WorkerPool::PostTaskAndReply( |
| 199 FROM_HERE, | 203 FROM_HERE, |
| 200 base::Bind(&IconOperation::Run, icon_op_), | 204 base::Bind(&IconOperation::Run, icon_op_), |
| 201 base::Bind(&AppListItemView::ApplyShadow, | 205 base::Bind(&AppListItemView::ApplyShadow, |
| 202 apply_shadow_factory_.GetWeakPtr(), | 206 apply_shadow_factory_.GetWeakPtr(), |
| 203 icon_op_), | 207 icon_op_), |
| 204 true /* task_is_slow */); | 208 true /* task_is_slow */); |
| 205 } | 209 } |
| 206 } | 210 } |
| 207 | 211 |
| 208 void AppListItemView::CancelPendingIconOperation() { | 212 void AppListItemView::CancelPendingIconOperation() { |
| 209 // Set canceled flag of previous request to skip unneeded processing. | 213 // Set canceled flag of previous request to skip unneeded processing. |
| 210 if (icon_op_.get()) | 214 if (icon_op_.get()) |
| 211 icon_op_->Cancel(); | 215 icon_op_->Cancel(); |
| 212 | 216 |
| 213 // Cancel reply callback for previous request. | 217 // Cancel reply callback for previous request. |
| 214 apply_shadow_factory_.InvalidateWeakPtrs(); | 218 apply_shadow_factory_.InvalidateWeakPtrs(); |
| 215 } | 219 } |
| 216 | 220 |
| 217 void AppListItemView::ApplyShadow(scoped_refptr<IconOperation> op) { | 221 void AppListItemView::ApplyShadow(scoped_refptr<IconOperation> op) { |
| 218 icon_->SetImage(op->bitmap()); | 222 icon_->SetImage(op->image()); |
| 219 IconCache::GetInstance()->Put(model_->icon(), icon_size_, op->bitmap()); | 223 IconCache::GetInstance()->Put(model_->icon(), icon_size_, op->image()); |
| 220 | 224 |
| 221 DCHECK(op.get() == icon_op_.get()); | 225 DCHECK(op.get() == icon_op_.get()); |
| 222 icon_op_ = NULL; | 226 icon_op_ = NULL; |
| 223 } | 227 } |
| 224 | 228 |
| 225 void AppListItemView::ItemIconChanged() { | 229 void AppListItemView::ItemIconChanged() { |
| 226 UpdateIcon(); | 230 UpdateIcon(); |
| 227 } | 231 } |
| 228 | 232 |
| 229 void AppListItemView::ItemTitleChanged() { | 233 void AppListItemView::ItemTitleChanged() { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 apps_grid_view_->SetSelectedItem(this); | 308 apps_grid_view_->SetSelectedItem(this); |
| 305 title_->SetEnabledColor(kTitleHoverColor); | 309 title_->SetEnabledColor(kTitleHoverColor); |
| 306 } else { | 310 } else { |
| 307 apps_grid_view_->ClearSelectedItem(this); | 311 apps_grid_view_->ClearSelectedItem(this); |
| 308 model_->SetHighlighted(false); | 312 model_->SetHighlighted(false); |
| 309 title_->SetEnabledColor(kTitleColor); | 313 title_->SetEnabledColor(kTitleColor); |
| 310 } | 314 } |
| 311 } | 315 } |
| 312 | 316 |
| 313 } // namespace app_list | 317 } // namespace app_list |
| OLD | NEW |