1 // Copyright 2014 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/folder_image_source.h"
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/gfx/canvas.h"
9 #include "ui/gfx/geometry/point.h"
10 #include "ui/gfx/geometry/rect.h"
11 #include "ui/gfx/geometry/size.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/gfx/image/image_skia_operations.h"
17 const int kItemIconDimension
= 16;
23 FolderImageSource::FolderImageSource(const Icons
& icons
, const gfx::Size
& size
)
24 : gfx::CanvasImageSource(size
, false), icons_(icons
), size_(size
) {
25 DCHECK(icons
.size() <= kNumFolderTopItems
);
28 FolderImageSource::~FolderImageSource() {
32 gfx::Size
FolderImageSource::ItemIconSize() {
33 return gfx::Size(kItemIconDimension
, kItemIconDimension
);
37 std::vector
<gfx::Rect
> FolderImageSource::GetTopIconsBounds(
38 const gfx::Rect
& folder_icon_bounds
) {
39 const int delta_to_center
= 1;
40 gfx::Point icon_center
= folder_icon_bounds
.CenterPoint();
41 std::vector
<gfx::Rect
> top_icon_bounds
;
43 // Get the top left icon bounds.
44 int left_x
= icon_center
.x() - kItemIconDimension
- delta_to_center
;
45 int top_y
= icon_center
.y() - kItemIconDimension
- delta_to_center
;
46 gfx::Rect
top_left(left_x
, top_y
, kItemIconDimension
, kItemIconDimension
);
47 top_icon_bounds
.push_back(top_left
);
49 // Get the top right icon bounds.
50 int right_x
= icon_center
.x() + delta_to_center
;
51 gfx::Rect
top_right(right_x
, top_y
, kItemIconDimension
, kItemIconDimension
);
52 top_icon_bounds
.push_back(top_right
);
54 // Get the bottom left icon bounds.
55 int bottom_y
= icon_center
.y() + delta_to_center
;
56 gfx::Rect
bottom_left(
57 left_x
, bottom_y
, kItemIconDimension
, kItemIconDimension
);
58 top_icon_bounds
.push_back(bottom_left
);
60 // Get the bottom right icon bounds.
61 gfx::Rect
bottom_right(
62 right_x
, bottom_y
, kItemIconDimension
, kItemIconDimension
);
63 top_icon_bounds
.push_back(bottom_right
);
65 return top_icon_bounds
;
68 void FolderImageSource::DrawIcon(gfx::Canvas
* canvas
,
69 const gfx::ImageSkia
& icon
,
70 const gfx::Size icon_size
,
76 gfx::ImageSkia
resized(gfx::ImageSkiaOperations::CreateResizedImage(
77 icon
, skia::ImageOperations::RESIZE_BEST
, icon_size
));
78 canvas
->DrawImageInt(resized
,
90 void FolderImageSource::Draw(gfx::Canvas
* canvas
) {
91 // Draw circle for folder shadow.
92 gfx::PointF
shadow_center(size().width() / 2, size().height() / 2);
94 paint
.setStyle(SkPaint::kFill_Style
);
95 paint
.setAntiAlias(true);
96 paint
.setColor(kFolderShadowColor
);
97 canvas
->sk_canvas()->drawCircle(
98 shadow_center
.x(), shadow_center
.y(), kFolderShadowRadius
, paint
);
99 // Draw circle for folder bubble.
100 gfx::PointF
bubble_center(shadow_center
);
101 bubble_center
.Offset(0, -kFolderShadowOffsetY
);
102 paint
.setColor(kFolderBubbleColor
);
103 canvas
->sk_canvas()->drawCircle(
104 bubble_center
.x(), bubble_center
.y(), kFolderBubbleRadius
, paint
);
106 if (icons_
.size() == 0)
109 // Draw top items' icons.
110 const gfx::Size
item_icon_size(ItemIconSize());
111 std::vector
<gfx::Rect
> top_icon_bounds
= GetTopIconsBounds(gfx::Rect(size()));
113 for (size_t i
= 0; i
< kNumFolderTopItems
&& i
< icons_
.size(); ++i
) {
117 top_icon_bounds
[i
].x(),
118 top_icon_bounds
[i
].y());
122 } // namespace app_list