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 "ui/app_list/app_list_item_list.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/image/canvas_image_source.h"
10 #include "ui/gfx/image/image_skia_operations.h"
16 const int kIconDimension = 48;
17 const size_t kNumTopApps = 4;
18 const int kItemIconDimension = 16;
20 // Generates the folder icon with the top 4 child item icons laid in 2x2 tile.
21 class FolderImageSource : public gfx::CanvasImageSource {
23 typedef std::vector<gfx::ImageSkia> Icons;
25 FolderImageSource(const Icons& icons, const gfx::Size& size)
26 : gfx::CanvasImageSource(size, false),
29 DCHECK(icons.size() <= kNumTopApps);
32 virtual ~FolderImageSource() {}
35 void DrawIcon(gfx::Canvas* canvas,
36 const gfx::ImageSkia& icon,
37 const gfx::Size icon_size,
39 gfx::ImageSkia resized(
40 gfx::ImageSkiaOperations::CreateResizedImage(
41 icon, skia::ImageOperations::RESIZE_BEST, icon_size));
42 canvas->DrawImageInt(resized, 0, 0, resized.width(), resized.height(),
43 x, y, resized.width(), resized.height(), true);
46 // gfx::CanvasImageSource overrides:
47 virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
48 // Draw folder circle.
49 gfx::Point center = gfx::Point(size().width() / 2 , size().height() / 2);
50 const SkColor kCircleColor = SkColorSetRGB(0xE1, 0xE1, 0xE1);
52 paint.setStyle(SkPaint::kFill_Style);
53 paint.setAntiAlias(true);
54 paint.setColor(kCircleColor);
55 canvas->DrawCircle(center, size().width() / 2, paint);
57 if (icons_.size() == 0)
60 // Tiled icon coordinates.
61 const int delta_to_center = 1;
62 const gfx::Size item_icon_size =
63 gfx::Size(kItemIconDimension, kItemIconDimension);
64 int left_x = center.x() - item_icon_size.width() - delta_to_center;
65 int top_y = center.y() - item_icon_size.height() - delta_to_center;
66 int right_x = center.x() + delta_to_center;
67 int bottom_y = center.y() + delta_to_center;
71 DrawIcon(canvas, icons_[i++], item_icon_size, left_x, top_y);
74 if (i < icons_.size())
75 DrawIcon(canvas, icons_[i++], item_icon_size, right_x, top_y);
78 if (i < icons_.size())
79 DrawIcon(canvas, icons_[i++], item_icon_size, left_x, bottom_y);
82 if (i < icons_.size())
83 DrawIcon(canvas, icons_[i], item_icon_size, right_x, bottom_y);
89 DISALLOW_COPY_AND_ASSIGN(FolderImageSource);
94 AppListFolderItem::AppListFolderItem(const std::string& id)
95 : AppListItemModel(id),
96 item_list_(new AppListItemList) {
97 item_list_->AddObserver(this);
100 AppListFolderItem::~AppListFolderItem() {
101 for (size_t i = 0; i < top_items_.size(); ++i)
102 top_items_[i]->RemoveObserver(this);
103 item_list_->RemoveObserver(this);
106 void AppListFolderItem::UpdateIcon() {
107 FolderImageSource::Icons top_icons;
108 for (size_t i = 0; i < top_items_.size(); ++i)
109 top_icons.push_back(top_items_[i]->icon());
111 const gfx::Size icon_size = gfx::Size(kIconDimension, kIconDimension);
112 gfx::ImageSkia icon = gfx::ImageSkia(
113 new FolderImageSource(top_icons, icon_size),
115 SetIcon(icon, false);
118 void AppListFolderItem::Activate(int event_flags) {
119 // Folder handling is implemented by the View, so do nothing.
123 const char AppListFolderItem::kAppType[] = "FolderItem";
125 const char* AppListFolderItem::GetAppType() const {
126 return AppListFolderItem::kAppType;
129 ui::MenuModel* AppListFolderItem::GetContextMenuModel() {
130 // TODO(stevenjb/jennyz): Implement.
134 void AppListFolderItem::ItemIconChanged() {
138 void AppListFolderItem::ItemTitleChanged() {
141 void AppListFolderItem::ItemHighlightedChanged() {
144 void AppListFolderItem::ItemIsInstallingChanged() {
147 void AppListFolderItem::ItemPercentDownloadedChanged() {
150 void AppListFolderItem::OnListItemAdded(size_t index,
151 AppListItemModel* item) {
152 if (index <= kNumTopApps)
156 void AppListFolderItem::OnListItemRemoved(size_t index,
157 AppListItemModel* item) {
158 if (index <= kNumTopApps)
162 void AppListFolderItem::OnListItemMoved(size_t from_index,
164 AppListItemModel* item) {
165 if (from_index <= kNumTopApps || to_index <= kNumTopApps)
169 void AppListFolderItem::UpdateTopItems() {
170 for (size_t i = 0; i < top_items_.size(); ++i)
171 top_items_[i]->RemoveObserver(this);
175 i < kNumTopApps && i < item_list_->item_count(); ++i) {
176 AppListItemModel* item = item_list_->item_at(i);
177 item->AddObserver(this);
178 top_items_.push_back(item);
183 } // namespace app_list