Add testing/scripts/OWNERS
[chromium-blink-merge.git] / ui / app_list / app_list_folder_item.cc
blobe5b75a540518d13e705caf7d73abbd638e1c2a54
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/app_list/app_list_folder_item.h"
7 #include "base/guid.h"
8 #include "ui/app_list/app_list_constants.h"
9 #include "ui/app_list/app_list_item_list.h"
10 #include "ui/app_list/folder_image_source.h"
11 #include "ui/gfx/geometry/rect.h"
13 namespace app_list {
15 AppListFolderItem::AppListFolderItem(const std::string& id,
16 FolderType folder_type)
17 : AppListItem(id),
18 folder_type_(folder_type),
19 item_list_(new AppListItemList) {
20 item_list_->AddObserver(this);
23 AppListFolderItem::~AppListFolderItem() {
24 for (size_t i = 0; i < top_items_.size(); ++i)
25 top_items_[i]->RemoveObserver(this);
26 item_list_->RemoveObserver(this);
29 void AppListFolderItem::UpdateIcon() {
30 FolderImageSource::Icons top_icons;
31 for (size_t i = 0; i < top_items_.size(); ++i)
32 top_icons.push_back(top_items_[i]->icon());
34 const gfx::Size icon_size = gfx::Size(kGridIconDimension, kGridIconDimension);
35 gfx::ImageSkia icon = gfx::ImageSkia(
36 new FolderImageSource(top_icons, icon_size),
37 icon_size);
38 SetIcon(icon, false);
41 const gfx::ImageSkia& AppListFolderItem::GetTopIcon(size_t item_index) {
42 CHECK_LT(item_index, top_items_.size());
43 return top_items_[item_index]->icon();
46 gfx::Rect AppListFolderItem::GetTargetIconRectInFolderForItem(
47 AppListItem* item,
48 const gfx::Rect& folder_icon_bounds) {
49 for (size_t i = 0; i < top_items_.size(); ++i) {
50 if (item->id() == top_items_[i]->id()) {
51 std::vector<gfx::Rect> rects =
52 FolderImageSource::GetTopIconsBounds(folder_icon_bounds);
53 return rects[i];
57 gfx::Rect target_rect(folder_icon_bounds);
58 target_rect.ClampToCenteredSize(FolderImageSource::ItemIconSize());
59 return target_rect;
62 void AppListFolderItem::Activate(int event_flags) {
63 // Folder handling is implemented by the View, so do nothing.
66 // static
67 const char AppListFolderItem::kItemType[] = "FolderItem";
69 const char* AppListFolderItem::GetItemType() const {
70 return AppListFolderItem::kItemType;
73 ui::MenuModel* AppListFolderItem::GetContextMenuModel() {
74 // TODO(stevenjb/jennyz): Implement.
75 return NULL;
78 AppListItem* AppListFolderItem::FindChildItem(const std::string& id) {
79 return item_list_->FindItem(id);
82 size_t AppListFolderItem::ChildItemCount() const {
83 return item_list_->item_count();
86 void AppListFolderItem::OnExtensionPreferenceChanged() {
87 for (size_t i = 0; i < item_list_->item_count(); ++i)
88 item_list_->item_at(i)->OnExtensionPreferenceChanged();
91 bool AppListFolderItem::CompareForTest(const AppListItem* other) const {
92 if (!AppListItem::CompareForTest(other))
93 return false;
94 const AppListFolderItem* other_folder =
95 static_cast<const AppListFolderItem*>(other);
96 if (other_folder->item_list()->item_count() != item_list_->item_count())
97 return false;
98 for (size_t i = 0; i < item_list_->item_count(); ++i) {
99 if (!item_list()->item_at(i)->CompareForTest(
100 other_folder->item_list()->item_at(i)))
101 return false;
103 return true;
106 std::string AppListFolderItem::GenerateId() {
107 return base::GenerateGUID();
110 void AppListFolderItem::ItemIconChanged() {
111 UpdateIcon();
114 void AppListFolderItem::OnListItemAdded(size_t index,
115 AppListItem* item) {
116 if (index < kNumFolderTopItems)
117 UpdateTopItems();
120 void AppListFolderItem::OnListItemRemoved(size_t index,
121 AppListItem* item) {
122 if (index < kNumFolderTopItems)
123 UpdateTopItems();
126 void AppListFolderItem::OnListItemMoved(size_t from_index,
127 size_t to_index,
128 AppListItem* item) {
129 if (from_index < kNumFolderTopItems || to_index < kNumFolderTopItems)
130 UpdateTopItems();
133 void AppListFolderItem::UpdateTopItems() {
134 for (size_t i = 0; i < top_items_.size(); ++i)
135 top_items_[i]->RemoveObserver(this);
136 top_items_.clear();
138 for (size_t i = 0;
139 i < kNumFolderTopItems && i < item_list_->item_count(); ++i) {
140 AppListItem* item = item_list_->item_at(i);
141 item->AddObserver(this);
142 top_items_.push_back(item);
144 UpdateIcon();
147 } // namespace app_list