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"
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"
15 AppListFolderItem::AppListFolderItem(const std::string
& id
,
16 FolderType folder_type
)
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
),
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(
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
);
57 gfx::Rect
target_rect(folder_icon_bounds
);
58 target_rect
.ClampToCenteredSize(FolderImageSource::ItemIconSize());
62 void AppListFolderItem::Activate(int event_flags
) {
63 // Folder handling is implemented by the View, so do nothing.
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.
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
))
94 const AppListFolderItem
* other_folder
=
95 static_cast<const AppListFolderItem
*>(other
);
96 if (other_folder
->item_list()->item_count() != item_list_
->item_count())
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
)))
106 std::string
AppListFolderItem::GenerateId() {
107 return base::GenerateGUID();
110 void AppListFolderItem::ItemIconChanged() {
114 void AppListFolderItem::OnListItemAdded(size_t index
,
116 if (index
< kNumFolderTopItems
)
120 void AppListFolderItem::OnListItemRemoved(size_t index
,
122 if (index
< kNumFolderTopItems
)
126 void AppListFolderItem::OnListItemMoved(size_t from_index
,
129 if (from_index
< kNumFolderTopItems
|| to_index
< kNumFolderTopItems
)
133 void AppListFolderItem::UpdateTopItems() {
134 for (size_t i
= 0; i
< top_items_
.size(); ++i
)
135 top_items_
[i
]->RemoveObserver(this);
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
);
147 } // namespace app_list