Commit 793ecbcd authored by mgiuca's avatar mgiuca Committed by Commit bot

Moved FolderImageSource class to a separate file.

Previously, this was a private class in app_list_folder_item.cc. It will
soon be required from multiple locations (to generate folder icons), so
it deserves its own file.

Also moved methods/constants relating to the tiny icon size/position
into static methods of this class (as needed by various clients).

BUG=425444

Review URL: https://codereview.chromium.org/657793004

Cr-Commit-Position: refs/heads/master@{#301028}
parent 4425863d
...@@ -26,6 +26,8 @@ component("app_list") { ...@@ -26,6 +26,8 @@ component("app_list") {
"app_list_switches.h", "app_list_switches.h",
"app_list_view_delegate.cc", "app_list_view_delegate.cc",
"app_list_view_delegate.h", "app_list_view_delegate.h",
"folder_image_source.cc",
"folder_image_source.h",
"pagination_controller.cc", "pagination_controller.cc",
"pagination_controller.h", "pagination_controller.h",
"pagination_model.cc", "pagination_model.cc",
......
...@@ -74,6 +74,8 @@ ...@@ -74,6 +74,8 @@
'cocoa/item_drag_controller.mm', 'cocoa/item_drag_controller.mm',
'cocoa/scroll_view_with_no_scrollbars.h', 'cocoa/scroll_view_with_no_scrollbars.h',
'cocoa/scroll_view_with_no_scrollbars.mm', 'cocoa/scroll_view_with_no_scrollbars.mm',
'folder_image_source.cc',
'folder_image_source.h',
'pagination_controller.cc', 'pagination_controller.cc',
'pagination_controller.h', 'pagination_controller.h',
'pagination_model.cc', 'pagination_model.cc',
......
...@@ -7,79 +7,11 @@ ...@@ -7,79 +7,11 @@
#include "base/guid.h" #include "base/guid.h"
#include "ui/app_list/app_list_constants.h" #include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_item_list.h" #include "ui/app_list/app_list_item_list.h"
#include "ui/gfx/canvas.h" #include "ui/app_list/folder_image_source.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace app_list { namespace app_list {
namespace {
const int kItemIconDimension = 16;
// Generates the folder icon with the top 4 child item icons laid in 2x2 tile.
class FolderImageSource : public gfx::CanvasImageSource {
public:
typedef std::vector<gfx::ImageSkia> Icons;
FolderImageSource(const Icons& icons, const gfx::Size& size)
: gfx::CanvasImageSource(size, false),
icons_(icons),
size_(size) {
DCHECK(icons.size() <= kNumFolderTopItems);
}
~FolderImageSource() override {}
private:
void DrawIcon(gfx::Canvas* canvas,
const gfx::ImageSkia& icon,
const gfx::Size icon_size,
int x, int y) {
if (icon.isNull())
return;
gfx::ImageSkia resized(
gfx::ImageSkiaOperations::CreateResizedImage(
icon, skia::ImageOperations::RESIZE_BEST, icon_size));
canvas->DrawImageInt(resized, 0, 0, resized.width(), resized.height(),
x, y, resized.width(), resized.height(), true);
}
// gfx::CanvasImageSource overrides:
void Draw(gfx::Canvas* canvas) override {
// Draw folder circle.
gfx::Point center = gfx::Point(size().width() / 2 , size().height() / 2);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setAntiAlias(true);
paint.setColor(kFolderBubbleColor);
canvas->DrawCircle(center, size().width() / 2, paint);
if (icons_.size() == 0)
return;
// Draw top items' icons.
const gfx::Size item_icon_size =
gfx::Size(kItemIconDimension, kItemIconDimension);
std::vector<gfx::Rect> top_icon_bounds =
AppListFolderItem::GetTopIconsBounds(gfx::Rect(size()));
for (size_t i= 0; i < kNumFolderTopItems && i < icons_.size(); ++i) {
DrawIcon(canvas, icons_[i], item_icon_size,
top_icon_bounds[i].x(), top_icon_bounds[i].y());
}
}
Icons icons_;
gfx::Size size_;
DISALLOW_COPY_AND_ASSIGN(FolderImageSource);
};
} // namespace
AppListFolderItem::AppListFolderItem(const std::string& id, AppListFolderItem::AppListFolderItem(const std::string& id,
FolderType folder_type) FolderType folder_type)
: AppListItem(id), : AppListItem(id),
...@@ -117,14 +49,13 @@ gfx::Rect AppListFolderItem::GetTargetIconRectInFolderForItem( ...@@ -117,14 +49,13 @@ gfx::Rect AppListFolderItem::GetTargetIconRectInFolderForItem(
for (size_t i = 0; i < top_items_.size(); ++i) { for (size_t i = 0; i < top_items_.size(); ++i) {
if (item->id() == top_items_[i]->id()) { if (item->id() == top_items_[i]->id()) {
std::vector<gfx::Rect> rects = std::vector<gfx::Rect> rects =
AppListFolderItem::GetTopIconsBounds(folder_icon_bounds); FolderImageSource::GetTopIconsBounds(folder_icon_bounds);
return rects[i]; return rects[i];
} }
} }
gfx::Rect target_rect(folder_icon_bounds); gfx::Rect target_rect(folder_icon_bounds);
target_rect.ClampToCenteredSize( target_rect.ClampToCenteredSize(FolderImageSource::ItemIconSize());
gfx::Size(kItemIconDimension, kItemIconDimension));
return target_rect; return target_rect;
} }
...@@ -135,38 +66,6 @@ void AppListFolderItem::Activate(int event_flags) { ...@@ -135,38 +66,6 @@ void AppListFolderItem::Activate(int event_flags) {
// static // static
const char AppListFolderItem::kItemType[] = "FolderItem"; const char AppListFolderItem::kItemType[] = "FolderItem";
// static
std::vector<gfx::Rect> AppListFolderItem::GetTopIconsBounds(
const gfx::Rect& folder_icon_bounds) {
const int delta_to_center = 1;
gfx::Point icon_center = folder_icon_bounds.CenterPoint();
std::vector<gfx::Rect> top_icon_bounds;
// Get the top left icon bounds.
int left_x = icon_center.x() - kItemIconDimension - delta_to_center;
int top_y = icon_center.y() - kItemIconDimension - delta_to_center;
gfx::Rect top_left(left_x, top_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(top_left);
// Get the top right icon bounds.
int right_x = icon_center.x() + delta_to_center;
gfx::Rect top_right(right_x, top_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(top_right);
// Get the bottom left icon bounds.
int bottom_y = icon_center.y() + delta_to_center;
gfx::Rect bottom_left(
left_x, bottom_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(bottom_left);
// Get the bottom right icon bounds.
gfx::Rect bottom_right(
right_x, bottom_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(bottom_right);
return top_icon_bounds;
}
const char* AppListFolderItem::GetItemType() const { const char* AppListFolderItem::GetItemType() const {
return AppListFolderItem::kItemType; return AppListFolderItem::kItemType;
} }
......
...@@ -68,12 +68,6 @@ class APP_LIST_EXPORT AppListFolderItem : public AppListItem, ...@@ -68,12 +68,6 @@ class APP_LIST_EXPORT AppListFolderItem : public AppListItem,
void OnExtensionPreferenceChanged() override; void OnExtensionPreferenceChanged() override;
bool CompareForTest(const AppListItem* other) const override; bool CompareForTest(const AppListItem* other) const override;
// Calculates the top item icons' bounds inside |folder_icon_bounds|.
// Returns the bounds of top item icons in sequence of top left, top right,
// bottom left, bottom right.
static std::vector<gfx::Rect> GetTopIconsBounds(
const gfx::Rect& folder_icon_bounds);
// Returns an id for a new folder. // Returns an id for a new folder.
static std::string GenerateId(); static std::string GenerateId();
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/app_list/folder_image_source.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
namespace {
const int kItemIconDimension = 16;
} // namespace
namespace app_list {
FolderImageSource::FolderImageSource(const Icons& icons, const gfx::Size& size)
: gfx::CanvasImageSource(size, false), icons_(icons), size_(size) {
DCHECK(icons.size() <= kNumFolderTopItems);
}
FolderImageSource::~FolderImageSource() {
}
// static
gfx::Size FolderImageSource::ItemIconSize() {
return gfx::Size(kItemIconDimension, kItemIconDimension);
}
// static
std::vector<gfx::Rect> FolderImageSource::GetTopIconsBounds(
const gfx::Rect& folder_icon_bounds) {
const int delta_to_center = 1;
gfx::Point icon_center = folder_icon_bounds.CenterPoint();
std::vector<gfx::Rect> top_icon_bounds;
// Get the top left icon bounds.
int left_x = icon_center.x() - kItemIconDimension - delta_to_center;
int top_y = icon_center.y() - kItemIconDimension - delta_to_center;
gfx::Rect top_left(left_x, top_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(top_left);
// Get the top right icon bounds.
int right_x = icon_center.x() + delta_to_center;
gfx::Rect top_right(right_x, top_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(top_right);
// Get the bottom left icon bounds.
int bottom_y = icon_center.y() + delta_to_center;
gfx::Rect bottom_left(
left_x, bottom_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(bottom_left);
// Get the bottom right icon bounds.
gfx::Rect bottom_right(
right_x, bottom_y, kItemIconDimension, kItemIconDimension);
top_icon_bounds.push_back(bottom_right);
return top_icon_bounds;
}
void FolderImageSource::DrawIcon(gfx::Canvas* canvas,
const gfx::ImageSkia& icon,
const gfx::Size icon_size,
int x,
int y) {
if (icon.isNull())
return;
gfx::ImageSkia resized(gfx::ImageSkiaOperations::CreateResizedImage(
icon, skia::ImageOperations::RESIZE_BEST, icon_size));
canvas->DrawImageInt(resized,
0,
0,
resized.width(),
resized.height(),
x,
y,
resized.width(),
resized.height(),
true);
}
void FolderImageSource::Draw(gfx::Canvas* canvas) {
// Draw folder circle.
gfx::Point center = gfx::Point(size().width() / 2, size().height() / 2);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setAntiAlias(true);
paint.setColor(kFolderBubbleColor);
canvas->DrawCircle(center, size().width() / 2, paint);
if (icons_.size() == 0)
return;
// Draw top items' icons.
const gfx::Size item_icon_size(ItemIconSize());
std::vector<gfx::Rect> top_icon_bounds = GetTopIconsBounds(gfx::Rect(size()));
for (size_t i = 0; i < kNumFolderTopItems && i < icons_.size(); ++i) {
DrawIcon(canvas,
icons_[i],
item_icon_size,
top_icon_bounds[i].x(),
top_icon_bounds[i].y());
}
}
} // namespace app_list
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_APP_LIST_APP_LIST_FOLDER_IMAGE_SOURCE_H_
#define UI_APP_LIST_APP_LIST_FOLDER_IMAGE_SOURCE_H_
#include <vector>
#include "ui/gfx/image/canvas_image_source.h"
namespace gfx {
class Canvas;
class ImageSkia;
class Rect;
class Size;
}
namespace app_list {
// Generates the folder icon with the top 4 child item icons laid in 2x2 tile.
class FolderImageSource : public gfx::CanvasImageSource {
public:
typedef std::vector<gfx::ImageSkia> Icons;
FolderImageSource(const Icons& icons, const gfx::Size& size);
~FolderImageSource() override;
// Gets the size of a small app icon inside the folder icon.
static gfx::Size ItemIconSize();
// Calculates the top item icons' bounds inside |folder_icon_bounds|.
// Returns the bounds of top item icons in sequence of top left, top right,
// bottom left, bottom right.
static std::vector<gfx::Rect> GetTopIconsBounds(
const gfx::Rect& folder_icon_bounds);
private:
void DrawIcon(gfx::Canvas* canvas,
const gfx::ImageSkia& icon,
const gfx::Size icon_size,
int x,
int y);
// gfx::CanvasImageSource overrides:
void Draw(gfx::Canvas* canvas) override;
Icons icons_;
gfx::Size size_;
DISALLOW_COPY_AND_ASSIGN(FolderImageSource);
};
} // namespace app_list
#endif // UI_APP_LIST_APP_LIST_FOLDER_ITEM_H_
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "ui/app_list/app_list_constants.h" #include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_folder_item.h" #include "ui/app_list/app_list_folder_item.h"
#include "ui/app_list/app_list_switches.h" #include "ui/app_list/app_list_switches.h"
#include "ui/app_list/folder_image_source.h"
#include "ui/app_list/views/app_list_folder_view.h" #include "ui/app_list/views/app_list_folder_view.h"
#include "ui/app_list/views/app_list_item_view.h" #include "ui/app_list/views/app_list_item_view.h"
#include "ui/app_list/views/app_list_main_view.h" #include "ui/app_list/views/app_list_main_view.h"
...@@ -202,7 +203,7 @@ std::vector<gfx::Rect> AppsContainerView::GetTopItemIconBoundsInActiveFolder() { ...@@ -202,7 +203,7 @@ std::vector<gfx::Rect> AppsContainerView::GetTopItemIconBoundsInActiveFolder() {
folder_item_view->GetIconBounds()); folder_item_view->GetIconBounds());
gfx::Rect to_container = apps_grid_view_->ConvertRectToParent(to_grid_view); gfx::Rect to_container = apps_grid_view_->ConvertRectToParent(to_grid_view);
return AppListFolderItem::GetTopIconsBounds(to_container); return FolderImageSource::GetTopIconsBounds(to_container);
} }
void AppsContainerView::CreateViewsForFolderTopItemsAnimation( void AppsContainerView::CreateViewsForFolderTopItemsAnimation(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment