OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/app_list_folder_item.h" | 5 #include "ui/app_list/app_list_folder_item.h" |
6 | 6 |
7 #include "base/guid.h" | 7 #include "base/guid.h" |
8 #include "ui/app_list/app_list_constants.h" | 8 #include "ui/app_list/app_list_constants.h" |
9 #include "ui/app_list/app_list_item_list.h" | 9 #include "ui/app_list/app_list_item_list.h" |
10 #include "ui/gfx/canvas.h" | 10 #include "ui/app_list/folder_image_source.h" |
11 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
12 #include "ui/gfx/image/canvas_image_source.h" | |
13 #include "ui/gfx/image/image_skia_operations.h" | |
14 | 12 |
15 namespace app_list { | 13 namespace app_list { |
16 | 14 |
17 namespace { | |
18 | |
19 const int kItemIconDimension = 16; | |
20 | |
21 // Generates the folder icon with the top 4 child item icons laid in 2x2 tile. | |
22 class FolderImageSource : public gfx::CanvasImageSource { | |
23 public: | |
24 typedef std::vector<gfx::ImageSkia> Icons; | |
25 | |
26 FolderImageSource(const Icons& icons, const gfx::Size& size) | |
27 : gfx::CanvasImageSource(size, false), | |
28 icons_(icons), | |
29 size_(size) { | |
30 DCHECK(icons.size() <= kNumFolderTopItems); | |
31 } | |
32 | |
33 ~FolderImageSource() override {} | |
34 | |
35 private: | |
36 void DrawIcon(gfx::Canvas* canvas, | |
37 const gfx::ImageSkia& icon, | |
38 const gfx::Size icon_size, | |
39 int x, int y) { | |
40 if (icon.isNull()) | |
41 return; | |
42 | |
43 gfx::ImageSkia resized( | |
44 gfx::ImageSkiaOperations::CreateResizedImage( | |
45 icon, skia::ImageOperations::RESIZE_BEST, icon_size)); | |
46 canvas->DrawImageInt(resized, 0, 0, resized.width(), resized.height(), | |
47 x, y, resized.width(), resized.height(), true); | |
48 } | |
49 | |
50 // gfx::CanvasImageSource overrides: | |
51 void Draw(gfx::Canvas* canvas) override { | |
52 // Draw folder circle. | |
53 gfx::Point center = gfx::Point(size().width() / 2 , size().height() / 2); | |
54 SkPaint paint; | |
55 paint.setStyle(SkPaint::kFill_Style); | |
56 paint.setAntiAlias(true); | |
57 paint.setColor(kFolderBubbleColor); | |
58 canvas->DrawCircle(center, size().width() / 2, paint); | |
59 | |
60 if (icons_.size() == 0) | |
61 return; | |
62 | |
63 // Draw top items' icons. | |
64 const gfx::Size item_icon_size = | |
65 gfx::Size(kItemIconDimension, kItemIconDimension); | |
66 std::vector<gfx::Rect> top_icon_bounds = | |
67 AppListFolderItem::GetTopIconsBounds(gfx::Rect(size())); | |
68 | |
69 for (size_t i= 0; i < kNumFolderTopItems && i < icons_.size(); ++i) { | |
70 DrawIcon(canvas, icons_[i], item_icon_size, | |
71 top_icon_bounds[i].x(), top_icon_bounds[i].y()); | |
72 } | |
73 } | |
74 | |
75 Icons icons_; | |
76 gfx::Size size_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(FolderImageSource); | |
79 }; | |
80 | |
81 } // namespace | |
82 | |
83 AppListFolderItem::AppListFolderItem(const std::string& id, | 15 AppListFolderItem::AppListFolderItem(const std::string& id, |
84 FolderType folder_type) | 16 FolderType folder_type) |
85 : AppListItem(id), | 17 : AppListItem(id), |
86 folder_type_(folder_type), | 18 folder_type_(folder_type), |
87 item_list_(new AppListItemList) { | 19 item_list_(new AppListItemList) { |
88 item_list_->AddObserver(this); | 20 item_list_->AddObserver(this); |
89 } | 21 } |
90 | 22 |
91 AppListFolderItem::~AppListFolderItem() { | 23 AppListFolderItem::~AppListFolderItem() { |
92 for (size_t i = 0; i < top_items_.size(); ++i) | 24 for (size_t i = 0; i < top_items_.size(); ++i) |
(...skipping 17 matching lines...) Expand all Loading... |
110 DCHECK(item_index <= top_items_.size()); | 42 DCHECK(item_index <= top_items_.size()); |
111 return top_items_[item_index]->icon(); | 43 return top_items_[item_index]->icon(); |
112 } | 44 } |
113 | 45 |
114 gfx::Rect AppListFolderItem::GetTargetIconRectInFolderForItem( | 46 gfx::Rect AppListFolderItem::GetTargetIconRectInFolderForItem( |
115 AppListItem* item, | 47 AppListItem* item, |
116 const gfx::Rect& folder_icon_bounds) { | 48 const gfx::Rect& folder_icon_bounds) { |
117 for (size_t i = 0; i < top_items_.size(); ++i) { | 49 for (size_t i = 0; i < top_items_.size(); ++i) { |
118 if (item->id() == top_items_[i]->id()) { | 50 if (item->id() == top_items_[i]->id()) { |
119 std::vector<gfx::Rect> rects = | 51 std::vector<gfx::Rect> rects = |
120 AppListFolderItem::GetTopIconsBounds(folder_icon_bounds); | 52 FolderImageSource::GetTopIconsBounds(folder_icon_bounds); |
121 return rects[i]; | 53 return rects[i]; |
122 } | 54 } |
123 } | 55 } |
124 | 56 |
125 gfx::Rect target_rect(folder_icon_bounds); | 57 gfx::Rect target_rect(folder_icon_bounds); |
126 target_rect.ClampToCenteredSize( | 58 target_rect.ClampToCenteredSize(FolderImageSource::ItemIconSize()); |
127 gfx::Size(kItemIconDimension, kItemIconDimension)); | |
128 return target_rect; | 59 return target_rect; |
129 } | 60 } |
130 | 61 |
131 void AppListFolderItem::Activate(int event_flags) { | 62 void AppListFolderItem::Activate(int event_flags) { |
132 // Folder handling is implemented by the View, so do nothing. | 63 // Folder handling is implemented by the View, so do nothing. |
133 } | 64 } |
134 | 65 |
135 // static | 66 // static |
136 const char AppListFolderItem::kItemType[] = "FolderItem"; | 67 const char AppListFolderItem::kItemType[] = "FolderItem"; |
137 | 68 |
138 // static | |
139 std::vector<gfx::Rect> AppListFolderItem::GetTopIconsBounds( | |
140 const gfx::Rect& folder_icon_bounds) { | |
141 const int delta_to_center = 1; | |
142 gfx::Point icon_center = folder_icon_bounds.CenterPoint(); | |
143 std::vector<gfx::Rect> top_icon_bounds; | |
144 | |
145 // Get the top left icon bounds. | |
146 int left_x = icon_center.x() - kItemIconDimension - delta_to_center; | |
147 int top_y = icon_center.y() - kItemIconDimension - delta_to_center; | |
148 gfx::Rect top_left(left_x, top_y, kItemIconDimension, kItemIconDimension); | |
149 top_icon_bounds.push_back(top_left); | |
150 | |
151 // Get the top right icon bounds. | |
152 int right_x = icon_center.x() + delta_to_center; | |
153 gfx::Rect top_right(right_x, top_y, kItemIconDimension, kItemIconDimension); | |
154 top_icon_bounds.push_back(top_right); | |
155 | |
156 // Get the bottom left icon bounds. | |
157 int bottom_y = icon_center.y() + delta_to_center; | |
158 gfx::Rect bottom_left( | |
159 left_x, bottom_y, kItemIconDimension, kItemIconDimension); | |
160 top_icon_bounds.push_back(bottom_left); | |
161 | |
162 // Get the bottom right icon bounds. | |
163 gfx::Rect bottom_right( | |
164 right_x, bottom_y, kItemIconDimension, kItemIconDimension); | |
165 top_icon_bounds.push_back(bottom_right); | |
166 | |
167 return top_icon_bounds; | |
168 } | |
169 | |
170 const char* AppListFolderItem::GetItemType() const { | 69 const char* AppListFolderItem::GetItemType() const { |
171 return AppListFolderItem::kItemType; | 70 return AppListFolderItem::kItemType; |
172 } | 71 } |
173 | 72 |
174 ui::MenuModel* AppListFolderItem::GetContextMenuModel() { | 73 ui::MenuModel* AppListFolderItem::GetContextMenuModel() { |
175 // TODO(stevenjb/jennyz): Implement. | 74 // TODO(stevenjb/jennyz): Implement. |
176 return NULL; | 75 return NULL; |
177 } | 76 } |
178 | 77 |
179 AppListItem* AppListFolderItem::FindChildItem(const std::string& id) { | 78 AppListItem* AppListFolderItem::FindChildItem(const std::string& id) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 for (size_t i = 0; | 138 for (size_t i = 0; |
240 i < kNumFolderTopItems && i < item_list_->item_count(); ++i) { | 139 i < kNumFolderTopItems && i < item_list_->item_count(); ++i) { |
241 AppListItem* item = item_list_->item_at(i); | 140 AppListItem* item = item_list_->item_at(i); |
242 item->AddObserver(this); | 141 item->AddObserver(this); |
243 top_items_.push_back(item); | 142 top_items_.push_back(item); |
244 } | 143 } |
245 UpdateIcon(); | 144 UpdateIcon(); |
246 } | 145 } |
247 | 146 |
248 } // namespace app_list | 147 } // namespace app_list |
OLD | NEW |