Index: ui/app_list/folder_image.cc |
diff --git a/ui/app_list/folder_image.cc b/ui/app_list/folder_image.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..71a01299d20f957a3e30a564110185d55956f753 |
--- /dev/null |
+++ b/ui/app_list/folder_image.cc |
@@ -0,0 +1,104 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/app_list/folder_image.h" |
+ |
+#include "ui/app_list/app_list_constants.h" |
+#include "ui/app_list/app_list_item.h" |
+#include "ui/app_list/app_list_item_list.h" |
+#include "ui/app_list/folder_image_source.h" |
+#include "ui/app_list/views/contents_view.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/gfx/geometry/rect.h" |
+#include "ui/strings/grit/ui_strings.h" |
+ |
+namespace app_list { |
+ |
+FolderImage::FolderImage(AppListItemList* item_list) : item_list_(item_list) { |
+ item_list_->AddObserver(this); |
+} |
+ |
+FolderImage::~FolderImage() { |
+ for (size_t i = 0; i < top_items_.size(); ++i) |
+ top_items_[i]->RemoveObserver(this); |
+ item_list_->RemoveObserver(this); |
+} |
+ |
+void FolderImage::UpdateIcon() { |
+ for (size_t i = 0; i < top_items_.size(); ++i) |
+ top_items_[i]->RemoveObserver(this); |
+ top_items_.clear(); |
+ |
+ for (size_t i = 0; i < kNumFolderTopItems && i < item_list_->item_count(); |
+ ++i) { |
+ AppListItem* item = item_list_->item_at(i); |
+ item->AddObserver(this); |
+ top_items_.push_back(item); |
+ } |
+ UpdateImageOnly(); |
+} |
+ |
+const gfx::ImageSkia& FolderImage::GetTopIcon(size_t item_index) { |
+ DCHECK(item_index <= top_items_.size()); |
+ return top_items_[item_index]->icon(); |
+} |
+ |
+gfx::Rect FolderImage::GetTargetIconRectInFolderForItem( |
+ AppListItem* item, |
+ const gfx::Rect& folder_icon_bounds) { |
+ for (size_t i = 0; i < top_items_.size(); ++i) { |
+ if (item->id() == top_items_[i]->id()) { |
+ std::vector<gfx::Rect> rects = |
+ FolderImageSource::GetTopIconsBounds(folder_icon_bounds); |
+ return rects[i]; |
+ } |
+ } |
+ |
+ gfx::Rect target_rect(folder_icon_bounds); |
+ target_rect.ClampToCenteredSize(FolderImageSource::ItemIconSize()); |
+ return target_rect; |
+} |
+ |
+void FolderImage::AddObserver(FolderImageObserver* observer) { |
+ observers_.AddObserver(observer); |
+} |
+ |
+void FolderImage::RemoveObserver(FolderImageObserver* observer) { |
+ observers_.RemoveObserver(observer); |
+} |
+ |
+void FolderImage::ItemIconChanged() { |
+ UpdateImageOnly(); |
+} |
+ |
+void FolderImage::OnListItemAdded(size_t index, AppListItem* item) { |
+ if (index < kNumFolderTopItems) |
+ UpdateIcon(); |
+} |
+ |
+void FolderImage::OnListItemRemoved(size_t index, AppListItem* item) { |
+ if (index < kNumFolderTopItems) |
+ UpdateIcon(); |
+} |
+ |
+void FolderImage::OnListItemMoved(size_t from_index, |
+ size_t to_index, |
+ AppListItem* item) { |
+ if (from_index < kNumFolderTopItems || to_index < kNumFolderTopItems) |
+ UpdateIcon(); |
+} |
+ |
+void FolderImage::UpdateImageOnly() { |
+ FolderImageSource::Icons top_icons; |
+ for (size_t i = 0; i < top_items_.size(); ++i) |
+ top_icons.push_back(top_items_[i]->icon()); |
+ |
+ const gfx::Size icon_size = gfx::Size(kGridIconDimension, kGridIconDimension); |
+ icon_ = |
+ gfx::ImageSkia(new FolderImageSource(top_icons, icon_size), icon_size); |
+ |
+ FOR_EACH_OBSERVER(FolderImageObserver, observers_, OnFolderImageUpdated()); |
+} |
+ |
+} // namespace app_list |