1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/app_list/model/folder_image.h"
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "ash/app_list/model/app_list_item.h"
#include "ash/app_list/model/app_list_item_list.h"
#include "ash/public/cpp/app_list/app_list_config.h"
#include "ash/public/cpp/app_list/app_list_config_provider.h"
#include "base/i18n/rtl.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ref.h"
#include "ui/base/l10n/l10n_util.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/canvas_image_source.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/scoped_canvas.h"
namespace ash {
namespace {
// The shadow blur of icon.
constexpr int kIconShadowBlur = 5;
// The shadow color of icon.
constexpr SkColor kIconShadowColor = SkColorSetA(SK_ColorBLACK, 31);
// 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 AppListConfig& app_list_config,
const Icons& icons,
const gfx::Size& size);
FolderImageSource(const FolderImageSource&) = delete;
FolderImageSource& operator=(const FolderImageSource&) = delete;
~FolderImageSource() override;
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;
const raw_ref<const AppListConfig, DanglingUntriaged> app_list_config_;
Icons icons_;
gfx::Size size_;
};
FolderImageSource::FolderImageSource(const AppListConfig& app_list_config,
const Icons& icons,
const gfx::Size& size)
: gfx::CanvasImageSource(size),
app_list_config_(app_list_config),
icons_(icons),
size_(size) {
DCHECK(icons.size() <= FolderImage::kNumFolderTopItems);
}
FolderImageSource::~FolderImageSource() = default;
void FolderImageSource::DrawIcon(gfx::Canvas* canvas,
const gfx::ImageSkia& icon,
const gfx::Size& icon_size,
int x,
int y) {
if (icon.isNull())
return;
const gfx::ImageSkia resized(gfx::ImageSkiaOperations::CreateResizedImage(
icon, skia::ImageOperations::RESIZE_BEST, icon_size));
// Draw a shadowed icon on the specified location.
const gfx::ShadowValues shadow = {
gfx::ShadowValue(gfx::Vector2d(), kIconShadowBlur, kIconShadowColor)};
const gfx::ImageSkia shadowed(
gfx::ImageSkiaOperations::CreateImageWithDropShadow(resized, shadow));
// Offset the shadowed image so the actual image position matches its original
// bounds. The offset has to be calculated in pixels, as the shadow margin
// might not match the 1x margin scaled to the target scale factor (when the
// drawing the shadow, the shadow margin is first scaled then rounded, which
// might introduce different rounding error depending on the scale factor).
gfx::ScopedCanvas scoped_canvas(canvas);
const float scale = canvas->UndoDeviceScaleFactor();
gfx::Insets shadow_margin =
gfx::ShadowValue::GetMargin({shadow[0].Scale(scale)});
const gfx::ImageSkiaRep& shadowed_rep = shadowed.GetRepresentation(scale);
canvas->DrawImageIntInPixel(
shadowed_rep, x * scale + shadow_margin.left(),
y * scale + shadow_margin.top(), scale * shadowed.width(),
scale * shadowed.height(), true, cc::PaintFlags());
}
void FolderImageSource::Draw(gfx::Canvas* canvas) {
if (icons_.size() == 0)
return;
// Draw top items' icons.
const size_t num_items =
std::min(FolderImage::kNumFolderTopItems, icons_.size());
std::vector<gfx::Rect> top_icon_bounds = FolderImage::GetTopIconsBounds(
*app_list_config_, gfx::Rect(size()), num_items);
for (size_t i = 0; i < num_items; ++i) {
DrawIcon(canvas, icons_[i],
app_list_config_->item_icon_in_folder_icon_size(),
top_icon_bounds[i].x(), top_icon_bounds[i].y());
}
}
} // namespace
// static
const size_t FolderImage::kNumFolderTopItems = 4;
FolderImage::FolderImage(const AppListConfig* app_list_config,
AppListItemList* item_list)
: app_list_config_(app_list_config), item_list_(item_list) {
DCHECK(app_list_config_);
item_list_->AddObserver(this);
}
FolderImage::~FolderImage() {
for (ash::AppListItem* item : top_items_) {
item->RemoveObserver(this);
}
item_list_->RemoveObserver(this);
}
void FolderImage::UpdateIcon() {
for (ash::AppListItem* item : top_items_) {
item->RemoveObserver(this);
}
top_items_.clear();
for (size_t i = 0;
i < item_list_->item_count() && top_items_.size() < kNumFolderTopItems;
++i) {
AppListItem* item = item_list_->item_at(i);
// If this item is currently being dragged, pretend it has already left our
// folder
if (item == dragged_item_)
continue;
item->AddObserver(this);
top_items_.push_back(item);
}
RedrawIconAndNotify();
}
void FolderImage::UpdateDraggedItem(const AppListItem* dragged_item) {
DCHECK(dragged_item_ != dragged_item);
dragged_item_ = dragged_item;
UpdateIcon();
}
// static
std::vector<gfx::Rect> FolderImage::GetTopIconsBounds(
const AppListConfig& app_list_config,
const gfx::Rect& folder_icon_bounds,
size_t num_items) {
DCHECK_LE(num_items, kNumFolderTopItems);
std::vector<gfx::Rect> top_icon_bounds;
const AppListConfig& base_config =
*AppListConfigProvider::Get().GetConfigForType(app_list_config.type(),
true /*can_create*/);
// The folder icons are generated as unclipped icons for default app list
// config, and then scaled down to the required unclipped folder size as
// needed (if clipped icon is needed, the unclipped icon bounds are clipped to
// the target size).
// This method goes through a similar flow:
// 1. Calculate the top icon bounds in the default unclipped folder icon.
// 2. Scale the bounds to the target config unclipped folder icon size.
// 3. Translate the bound to adjust for clipped bounds size (expected to be
// the |folder_icon_bounds| size).
// 4. Translate to bounds to adjust for the clipped bounds origin (expected
// to be the |folder_icon_bounds| origin).
// Steps 2 - 4 are done using |scale_and_translate_bounds|.
const int item_icon_dimension =
base_config.item_icon_in_folder_icon_dimension();
const int folder_icon_dimension = base_config.folder_icon_dimension();
gfx::Point icon_center(folder_icon_dimension / 2, folder_icon_dimension / 2);
const gfx::Rect center_rect(icon_center.x() - item_icon_dimension / 2,
icon_center.y() - item_icon_dimension / 2,
item_icon_dimension, item_icon_dimension);
const int origin_offset =
(item_icon_dimension + base_config.item_icon_in_folder_icon_margin()) / 2;
const int scaled_folder_icon_dimension =
app_list_config.folder_icon_dimension();
auto scale_and_translate_bounds =
[folder_icon_bounds, folder_icon_dimension,
scaled_folder_icon_dimension](const gfx::Rect& original) {
const float scale = static_cast<float>(scaled_folder_icon_dimension) /
folder_icon_dimension;
gfx::Rect bounds = gfx::ScaleToRoundedRect(original, scale, scale);
const int clipped_image_offset =
(scaled_folder_icon_dimension - folder_icon_bounds.width()) / 2;
bounds.Offset(-clipped_image_offset, -clipped_image_offset);
bounds.Offset(folder_icon_bounds.x(), folder_icon_bounds.y());
return bounds;
};
if (num_items == 1) {
// Center icon bounds.
top_icon_bounds.emplace_back(scale_and_translate_bounds(center_rect));
return top_icon_bounds;
}
if (num_items == 2) {
// Left icon bounds.
gfx::Rect left_rect = center_rect;
left_rect.Offset(-origin_offset, 0);
// Right icon bounds.
gfx::Rect right_rect = center_rect;
right_rect.Offset(origin_offset, 0);
if (base::i18n::IsRTL())
std::swap(left_rect, right_rect);
top_icon_bounds.emplace_back(scale_and_translate_bounds(left_rect));
top_icon_bounds.emplace_back(scale_and_translate_bounds(right_rect));
return top_icon_bounds;
}
if (num_items == 3) {
// Top icon bounds.
gfx::Rect top_rect = center_rect;
top_rect.Offset(0, -origin_offset);
top_icon_bounds.emplace_back(scale_and_translate_bounds(top_rect));
}
if (num_items == 4) {
// Top left icon bounds.
gfx::Rect top_left_rect = center_rect;
top_left_rect.Offset(-origin_offset, -origin_offset);
// Top right icon bounds.
gfx::Rect top_right_rect = center_rect;
top_right_rect.Offset(origin_offset, -origin_offset);
if (base::i18n::IsRTL()) {
std::swap(top_left_rect, top_right_rect);
}
top_icon_bounds.emplace_back(scale_and_translate_bounds(top_left_rect));
top_icon_bounds.emplace_back(scale_and_translate_bounds(top_right_rect));
}
// Bottom left icon bounds.
gfx::Rect bottom_left_rect = center_rect;
bottom_left_rect.Offset(-origin_offset, origin_offset);
// Bottom right icon bounds.
gfx::Rect bottom_right_rect = center_rect;
bottom_right_rect.Offset(origin_offset, origin_offset);
if (base::i18n::IsRTL())
std::swap(bottom_left_rect, bottom_right_rect);
top_icon_bounds.emplace_back(scale_and_translate_bounds(bottom_left_rect));
top_icon_bounds.emplace_back(scale_and_translate_bounds(bottom_right_rect));
return top_icon_bounds;
}
gfx::Rect FolderImage::GetTargetIconRectInFolderForItem(
const AppListConfig& app_list_config,
AppListItem* item,
const gfx::Rect& folder_icon_bounds) const {
for (size_t i = 0; i < top_items_.size(); ++i) {
if (item->id() == top_items_[i]->id()) {
std::vector<gfx::Rect> rects = GetTopIconsBounds(
app_list_config, folder_icon_bounds, top_items_.size());
return rects[i];
}
}
gfx::Rect target_rect(folder_icon_bounds);
target_rect.ClampToCenteredSize(
app_list_config.item_icon_in_folder_icon_size());
return target_rect;
}
void FolderImage::AddObserver(FolderImageObserver* observer) {
observers_.AddObserver(observer);
}
void FolderImage::RemoveObserver(FolderImageObserver* observer) {
observers_.RemoveObserver(observer);
}
void FolderImage::ItemIconChanged(AppListConfigType config_type) {
if (config_type != app_list_config_->type())
return;
// Note: Must update the image only (cannot simply call UpdateIcon), because
// UpdateIcon removes and re-adds the FolderImage as an observer of the
// AppListItems, which causes the current iterator to call ItemIconChanged
// again, and goes into an infinite loop.
RedrawIconAndNotify();
}
void FolderImage::OnListItemAdded(size_t index, AppListItem* item) {
if (index < kNumFolderTopItems)
UpdateIcon();
}
void FolderImage::OnListItemRemoved(size_t index, AppListItem* item) {
if (index < kNumFolderTopItems)
UpdateIcon();
}
void FolderImage::OnListItemMoved(size_t from_index,
size_t to_index,
AppListItem* item) {
if (from_index < kNumFolderTopItems || to_index < kNumFolderTopItems)
UpdateIcon();
}
void FolderImage::RedrawIconAndNotify() {
FolderImageSource::Icons top_icons;
for (const ash::AppListItem* item : top_items_) {
top_icons.push_back(item->GetIcon(app_list_config_->type()));
}
const gfx::Size icon_size = app_list_config_->folder_icon_size();
icon_ = gfx::ImageSkia(std::make_unique<FolderImageSource>(
*app_list_config_, top_icons, icon_size),
icon_size);
for (auto& observer : observers_)
observer.OnFolderImageUpdated(app_list_config_->type());
}
} // namespace ash
|