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