- add sources.
[platform/framework/web/crosswalk.git] / src / ui / app_list / app_list_folder_item.cc
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.
4
5 #include "ui/app_list/app_list_folder_item.h"
6
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"
11
12 namespace app_list {
13
14 namespace {
15
16 const int kIconDimension = 48;
17 const size_t kNumTopApps = 4;
18 const int kItemIconDimension = 16;
19
20 // Generates the folder icon with the top 4 child item icons laid in 2x2 tile.
21 class FolderImageSource : public gfx::CanvasImageSource {
22  public:
23   typedef std::vector<gfx::ImageSkia> Icons;
24
25   FolderImageSource(const Icons& icons, const gfx::Size& size)
26       : gfx::CanvasImageSource(size, false),
27         icons_(icons),
28         size_(size) {
29     DCHECK(icons.size() <= kNumTopApps);
30   }
31
32   virtual ~FolderImageSource() {}
33
34  private:
35   void DrawIcon(gfx::Canvas* canvas,
36                 const gfx::ImageSkia& icon,
37                 const gfx::Size icon_size,
38                 int x, int y) {
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);
44   }
45
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);
51     SkPaint paint;
52     paint.setStyle(SkPaint::kFill_Style);
53     paint.setAntiAlias(true);
54     paint.setColor(kCircleColor);
55     canvas->DrawCircle(center, size().width() / 2, paint);
56
57     if (icons_.size() == 0)
58       return;
59
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;
68
69     // top left icon
70     size_t i = 0;
71     DrawIcon(canvas, icons_[i++], item_icon_size, left_x, top_y);
72
73     // top right icon
74     if (i < icons_.size())
75       DrawIcon(canvas, icons_[i++], item_icon_size, right_x, top_y);
76
77     // left bottm icon
78     if (i < icons_.size())
79       DrawIcon(canvas, icons_[i++], item_icon_size, left_x, bottom_y);
80
81     // right bottom icon
82     if (i < icons_.size())
83       DrawIcon(canvas, icons_[i], item_icon_size, right_x, bottom_y);
84   }
85
86   Icons icons_;
87   gfx::Size size_;
88
89   DISALLOW_COPY_AND_ASSIGN(FolderImageSource);
90 };
91
92 }  // namespace
93
94 AppListFolderItem::AppListFolderItem(const std::string& id)
95     : AppListItemModel(id),
96       item_list_(new AppListItemList) {
97   item_list_->AddObserver(this);
98 }
99
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);
104 }
105
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());
110
111   const gfx::Size icon_size = gfx::Size(kIconDimension, kIconDimension);
112   gfx::ImageSkia icon = gfx::ImageSkia(
113       new FolderImageSource(top_icons, icon_size),
114       icon_size);
115   SetIcon(icon, false);
116 }
117
118 void AppListFolderItem::Activate(int event_flags) {
119   // Folder handling is implemented by the View, so do nothing.
120 }
121
122 // static
123 const char AppListFolderItem::kAppType[] = "FolderItem";
124
125 const char* AppListFolderItem::GetAppType() const {
126   return AppListFolderItem::kAppType;
127 }
128
129 ui::MenuModel* AppListFolderItem::GetContextMenuModel() {
130   // TODO(stevenjb/jennyz): Implement.
131   return NULL;
132 }
133
134 void AppListFolderItem::ItemIconChanged() {
135   UpdateIcon();
136 }
137
138 void AppListFolderItem::ItemTitleChanged() {
139 }
140
141 void AppListFolderItem::ItemHighlightedChanged() {
142 }
143
144 void AppListFolderItem::ItemIsInstallingChanged() {
145 }
146
147 void AppListFolderItem::ItemPercentDownloadedChanged() {
148 }
149
150 void AppListFolderItem::OnListItemAdded(size_t index,
151                                         AppListItemModel* item) {
152   if (index <= kNumTopApps)
153     UpdateTopItems();
154 }
155
156 void AppListFolderItem::OnListItemRemoved(size_t index,
157                                           AppListItemModel* item) {
158   if (index <= kNumTopApps)
159     UpdateTopItems();
160 }
161
162 void AppListFolderItem::OnListItemMoved(size_t from_index,
163                                         size_t to_index,
164                                         AppListItemModel* item) {
165   if (from_index <= kNumTopApps || to_index <= kNumTopApps)
166     UpdateTopItems();
167 }
168
169 void AppListFolderItem::UpdateTopItems() {
170   for (size_t i = 0; i < top_items_.size(); ++i)
171     top_items_[i]->RemoveObserver(this);
172   top_items_.clear();
173
174   for (size_t i = 0;
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);
179   }
180   UpdateIcon();
181 }
182
183 }  // namespace app_list