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. 4 5#include "ash/system/user/rounded_image_view.h" 6#include "skia/ext/image_operations.h" 7#include "third_party/skia/include/core/SkPaint.h" 8#include "third_party/skia/include/core/SkPath.h" 9#include "ui/gfx/canvas.h" 10#include "ui/gfx/image/image_skia_operations.h" 11#include "ui/gfx/skia_util.h" 12 13namespace ash { 14namespace tray { 15 16RoundedImageView::RoundedImageView(int corner_radius, bool active_user) 17 : active_user_(active_user) { 18 for (int i = 0; i < 4; ++i) 19 corner_radius_[i] = corner_radius; 20} 21 22RoundedImageView::~RoundedImageView() {} 23 24void RoundedImageView::SetImage(const gfx::ImageSkia& img, 25 const gfx::Size& size) { 26 image_ = img; 27 image_size_ = size; 28 29 // Try to get the best image quality for the avatar. 30 resized_ = gfx::ImageSkiaOperations::CreateResizedImage( 31 image_, skia::ImageOperations::RESIZE_BEST, size); 32 if (GetWidget() && visible()) { 33 PreferredSizeChanged(); 34 SchedulePaint(); 35 } 36} 37 38void RoundedImageView::SetCornerRadii(int top_left, 39 int top_right, 40 int bottom_right, 41 int bottom_left) { 42 corner_radius_[0] = top_left; 43 corner_radius_[1] = top_right; 44 corner_radius_[2] = bottom_right; 45 corner_radius_[3] = bottom_left; 46} 47 48gfx::Size RoundedImageView::GetPreferredSize() const { 49 return gfx::Size(image_size_.width() + GetInsets().width(), 50 image_size_.height() + GetInsets().height()); 51} 52 53void RoundedImageView::OnPaint(gfx::Canvas* canvas) { 54 View::OnPaint(canvas); 55 gfx::Rect image_bounds(size()); 56 image_bounds.ClampToCenteredSize(GetPreferredSize()); 57 image_bounds.Inset(GetInsets()); 58 const SkScalar kRadius[8] = { 59 SkIntToScalar(corner_radius_[0]), 60 SkIntToScalar(corner_radius_[0]), 61 SkIntToScalar(corner_radius_[1]), 62 SkIntToScalar(corner_radius_[1]), 63 SkIntToScalar(corner_radius_[2]), 64 SkIntToScalar(corner_radius_[2]), 65 SkIntToScalar(corner_radius_[3]), 66 SkIntToScalar(corner_radius_[3]) 67 }; 68 SkPath path; 69 path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius); 70 SkPaint paint; 71 paint.setAntiAlias(true); 72 paint.setXfermodeMode(active_user_ ? SkXfermode::kSrcOver_Mode 73 : SkXfermode::kLuminosity_Mode); 74 canvas->DrawImageInPath( 75 resized_, image_bounds.x(), image_bounds.y(), path, paint); 76} 77 78} // namespace tray 79} // namespace ash 80