| 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" | |
| 25 #include "ui/views/controls/image_view.h" | 23 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/menu/menu_item_view.h" | 24 #include "ui/views/controls/menu/menu_item_view.h" |
| 27 #include "ui/views/controls/menu/menu_model_adapter.h" | 25 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 28 #include "ui/views/controls/menu/menu_runner.h" | 26 #include "ui/views/controls/menu/menu_runner.h" |
| 29 | 27 |
| 30 namespace app_list { | 28 namespace app_list { |
| 31 | 29 |
| 32 namespace { | 30 namespace { |
| 33 | 31 |
| 34 const int kTopBottomPadding = 10; | 32 const int kTopBottomPadding = 10; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 76 |
| 79 } // namespace | 77 } // namespace |
| 80 | 78 |
| 81 // static | 79 // static |
| 82 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView"; | 80 const char AppListItemView::kViewClassName[] = "ui/app_list/AppListItemView"; |
| 83 | 81 |
| 84 // AppListItemView::IconOperation wraps background icon processing. | 82 // AppListItemView::IconOperation wraps background icon processing. |
| 85 class AppListItemView::IconOperation | 83 class AppListItemView::IconOperation |
| 86 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> { | 84 : public base::RefCountedThreadSafe<AppListItemView::IconOperation> { |
| 87 public: | 85 public: |
| 88 IconOperation(const SkBitmap& bitmap, | 86 IconOperation(const gfx::ImageSkia& image, |
| 89 const gfx::Size& size, | 87 const gfx::Size& size, |
| 90 const gfx::ShadowValues& shadows) | 88 const gfx::ShadowValues& shadows) |
| 91 : bitmap_(bitmap), | 89 : image_(image), |
| 92 size_(size), | 90 size_(size), |
| 93 shadows_(shadows) { | 91 shadows_(shadows) { |
| 94 } | 92 } |
| 95 | 93 |
| 96 static void Run(scoped_refptr<IconOperation> op) { | 94 static void Run(scoped_refptr<IconOperation> op) { |
| 97 op->ResizeAndGenerateShadow(); | 95 op->ResizeAndGenerateShadow(); |
| 98 } | 96 } |
| 99 | 97 |
| 100 void ResizeAndGenerateShadow() { | 98 void ResizeAndGenerateShadow() { |
| 101 if (cancel_flag_.IsSet()) | 99 if (cancel_flag_.IsSet()) |
| 102 return; | 100 return; |
| 103 | 101 |
| 104 if (size_ != gfx::Size(bitmap_.width(), bitmap_.height())) { | 102 if (size_ != gfx::Size(image_.width(), image_.height())) |
| 105 bitmap_ = skia::ImageOperations::Resize(bitmap_, | 103 image_ = image_.Resize(size_); |
| 106 skia::ImageOperations::RESIZE_BEST, size_.width(), size_.height()); | |
| 107 } | |
| 108 | 104 |
| 109 if (cancel_flag_.IsSet()) | 105 if (cancel_flag_.IsSet()) |
| 110 return; | 106 return; |
| 111 | 107 |
| 112 bitmap_ = SkBitmapOperations::CreateDropShadow(bitmap_, shadows_); | 108 image_ = image_.CreateDropShadow(shadows_); |
| 113 } | 109 } |
| 114 | 110 |
| 115 void Cancel() { | 111 void Cancel() { |
| 116 cancel_flag_.Set(); | 112 cancel_flag_.Set(); |
| 117 } | 113 } |
| 118 | 114 |
| 119 const SkBitmap& bitmap() const { | 115 const gfx::ImageSkia& image() const { |
| 120 return bitmap_; | 116 return image_; |
| 121 } | 117 } |
| 122 | 118 |
| 123 private: | 119 private: |
| 124 friend class base::RefCountedThreadSafe<AppListItemView::IconOperation>; | 120 friend class base::RefCountedThreadSafe<AppListItemView::IconOperation>; |
| 125 ~IconOperation() {} | 121 ~IconOperation() {} |
| 126 | 122 |
| 127 base::CancellationFlag cancel_flag_; | 123 base::CancellationFlag cancel_flag_; |
| 128 | 124 |
| 129 SkBitmap bitmap_; | 125 gfx::ImageSkia image_; |
| 130 const gfx::Size size_; | 126 const gfx::Size size_; |
| 131 const gfx::ShadowValues shadows_; | 127 const gfx::ShadowValues shadows_; |
| 132 | 128 |
| 133 DISALLOW_COPY_AND_ASSIGN(IconOperation); | 129 DISALLOW_COPY_AND_ASSIGN(IconOperation); |
| 134 }; | 130 }; |
| 135 | 131 |
| 136 AppListItemView::AppListItemView(AppsGridView* apps_grid_view, | 132 AppListItemView::AppListItemView(AppsGridView* apps_grid_view, |
| 137 AppListItemModel* model, | 133 AppListItemModel* model, |
| 138 views::ButtonListener* listener) | 134 views::ButtonListener* listener) |
| 139 : CustomButton(listener), | 135 : CustomButton(listener), |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 169 |
| 174 icon_size_ = size; | 170 icon_size_ = size; |
| 175 UpdateIcon(); | 171 UpdateIcon(); |
| 176 } | 172 } |
| 177 | 173 |
| 178 void AppListItemView::UpdateIcon() { | 174 void AppListItemView::UpdateIcon() { |
| 179 // Skip if |icon_size_| has not been determined. | 175 // Skip if |icon_size_| has not been determined. |
| 180 if (icon_size_.IsEmpty()) | 176 if (icon_size_.IsEmpty()) |
| 181 return; | 177 return; |
| 182 | 178 |
| 183 SkBitmap icon = model_->icon(); | 179 gfx::ImageSkia icon = model_->icon(); |
| 184 // Clear icon and bail out if model icon is empty. | 180 // Clear icon and bail out if model icon is empty. |
| 185 if (icon.empty()) { | 181 if (icon.empty()) { |
| 186 icon_->SetImage(NULL); | 182 icon_->SetImage(NULL); |
| 187 return; | 183 return; |
| 188 } | 184 } |
| 189 | 185 |
| 190 CancelPendingIconOperation(); | 186 CancelPendingIconOperation(); |
| 191 | 187 |
| 192 SkBitmap shadow; | 188 gfx::ImageSkia shadow; |
| 193 if (IconCache::GetInstance()->Get(icon, icon_size_, &shadow)) { | 189 if (IconCache::GetInstance()->Get(icon, icon_size_, &shadow)) { |
| 194 icon_->SetImage(shadow); | 190 icon_->SetImage(shadow); |
| 195 } else { | 191 } else { |
| 196 // Schedule resize and shadow generation. | 192 // Schedule resize and shadow generation. |
| 197 icon_op_ = new IconOperation(icon, icon_size_, icon_shadows_); | 193 icon_op_ = new IconOperation(icon, icon_size_, icon_shadows_); |
| 198 base::WorkerPool::PostTaskAndReply( | 194 base::WorkerPool::PostTaskAndReply( |
| 199 FROM_HERE, | 195 FROM_HERE, |
| 200 base::Bind(&IconOperation::Run, icon_op_), | 196 base::Bind(&IconOperation::Run, icon_op_), |
| 201 base::Bind(&AppListItemView::ApplyShadow, | 197 base::Bind(&AppListItemView::ApplyShadow, |
| 202 apply_shadow_factory_.GetWeakPtr(), | 198 apply_shadow_factory_.GetWeakPtr(), |
| 203 icon_op_), | 199 icon_op_), |
| 204 true /* task_is_slow */); | 200 true /* task_is_slow */); |
| 205 } | 201 } |
| 206 } | 202 } |
| 207 | 203 |
| 208 void AppListItemView::CancelPendingIconOperation() { | 204 void AppListItemView::CancelPendingIconOperation() { |
| 209 // Set canceled flag of previous request to skip unneeded processing. | 205 // Set canceled flag of previous request to skip unneeded processing. |
| 210 if (icon_op_.get()) | 206 if (icon_op_.get()) |
| 211 icon_op_->Cancel(); | 207 icon_op_->Cancel(); |
| 212 | 208 |
| 213 // Cancel reply callback for previous request. | 209 // Cancel reply callback for previous request. |
| 214 apply_shadow_factory_.InvalidateWeakPtrs(); | 210 apply_shadow_factory_.InvalidateWeakPtrs(); |
| 215 } | 211 } |
| 216 | 212 |
| 217 void AppListItemView::ApplyShadow(scoped_refptr<IconOperation> op) { | 213 void AppListItemView::ApplyShadow(scoped_refptr<IconOperation> op) { |
| 218 icon_->SetImage(op->bitmap()); | 214 icon_->SetImage(op->image()); |
| 219 IconCache::GetInstance()->Put(model_->icon(), icon_size_, op->bitmap()); | 215 IconCache::GetInstance()->Put(model_->icon(), icon_size_, op->image()); |
| 220 | 216 |
| 221 DCHECK(op.get() == icon_op_.get()); | 217 DCHECK(op.get() == icon_op_.get()); |
| 222 icon_op_ = NULL; | 218 icon_op_ = NULL; |
| 223 } | 219 } |
| 224 | 220 |
| 225 void AppListItemView::ItemIconChanged() { | 221 void AppListItemView::ItemIconChanged() { |
| 226 UpdateIcon(); | 222 UpdateIcon(); |
| 227 } | 223 } |
| 228 | 224 |
| 229 void AppListItemView::ItemTitleChanged() { | 225 void AppListItemView::ItemTitleChanged() { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 apps_grid_view_->SetSelectedItem(this); | 300 apps_grid_view_->SetSelectedItem(this); |
| 305 title_->SetEnabledColor(kTitleHoverColor); | 301 title_->SetEnabledColor(kTitleHoverColor); |
| 306 } else { | 302 } else { |
| 307 apps_grid_view_->ClearSelectedItem(this); | 303 apps_grid_view_->ClearSelectedItem(this); |
| 308 model_->SetHighlighted(false); | 304 model_->SetHighlighted(false); |
| 309 title_->SetEnabledColor(kTitleColor); | 305 title_->SetEnabledColor(kTitleColor); |
| 310 } | 306 } |
| 311 } | 307 } |
| 312 | 308 |
| 313 } // namespace app_list | 309 } // namespace app_list |
| OLD | NEW |