Commit acb76576 authored by Andrew Xu's avatar Andrew Xu Committed by Commit Bot

Provide an alignment option in RoundedImageView

This CL provides an alignment option in RoundedImageView to specify
which portion of the image should be drawn. It's useful when the image's
size is greater than the view's.

Bug: 1133020
Change-Id: I923f4fcb763b8a1c17a18471148264ef21f7ed73
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2443492
Commit-Queue: Andrew Xu <andrewxu@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812914}
parent a6c02ab0
...@@ -57,7 +57,8 @@ class FadeImageView : public RoundedImageView, ...@@ -57,7 +57,8 @@ class FadeImageView : public RoundedImageView,
const ClipboardHistoryItem* clipboard_history_item, const ClipboardHistoryItem* clipboard_history_item,
const ClipboardHistoryResourceManager* resource_manager, const ClipboardHistoryResourceManager* resource_manager,
float opacity) float opacity)
: RoundedImageView(kRoundedCornerRadius), : RoundedImageView(kRoundedCornerRadius,
RoundedImageView::Alignment::kCenter),
bitmap_item_view_(bitmap_item_view), bitmap_item_view_(bitmap_item_view),
resource_manager_(resource_manager), resource_manager_(resource_manager),
clipboard_history_item_(*clipboard_history_item), clipboard_history_item_(*clipboard_history_item),
...@@ -260,8 +261,8 @@ ClipboardHistoryBitmapItemView::BuildImageView() { ...@@ -260,8 +261,8 @@ ClipboardHistoryBitmapItemView::BuildImageView() {
resource_manager_, resource_manager_,
GetContentsOpacity()); GetContentsOpacity());
case ui::ClipboardInternalFormat::kBitmap: { case ui::ClipboardInternalFormat::kBitmap: {
auto image_view = auto image_view = std::make_unique<RoundedImageView>(
std::make_unique<RoundedImageView>(kRoundedCornerRadius); kRoundedCornerRadius, RoundedImageView::Alignment::kCenter);
gfx::ImageSkia bitmap_image = gfx::ImageSkia::CreateFrom1xBitmap( gfx::ImageSkia bitmap_image = gfx::ImageSkia::CreateFrom1xBitmap(
clipboard_history_item()->data().bitmap()); clipboard_history_item()->data().bitmap());
if (GetContentsOpacity() != 1.f) { if (GetContentsOpacity() != 1.f) {
......
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
namespace ash { namespace ash {
RoundedImageView::RoundedImageView(int corner_radius) { RoundedImageView::RoundedImageView(int corner_radius, Alignment alignment)
: alignment_(alignment) {
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
corner_radius_[i] = corner_radius; corner_radius_[i] = corner_radius;
} }
...@@ -72,7 +73,21 @@ void RoundedImageView::OnPaint(gfx::Canvas* canvas) { ...@@ -72,7 +73,21 @@ void RoundedImageView::OnPaint(gfx::Canvas* canvas) {
path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius); path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius);
cc::PaintFlags flags; cc::PaintFlags flags;
flags.setAntiAlias(true); flags.setAntiAlias(true);
canvas->DrawImageInPath(resized_image_, image_bounds.x(), image_bounds.y(),
gfx::ImageSkia image_to_draw;
switch (alignment_) {
case Alignment::kLeading:
image_to_draw = resized_image_;
break;
case Alignment::kCenter:
gfx::Rect preferred_size(GetImageSize());
preferred_size.ClampToCenteredSize(image_bounds.size());
image_to_draw = gfx::ImageSkiaOperations::ExtractSubset(resized_image_,
preferred_size);
break;
}
canvas->DrawImageInPath(image_to_draw, image_bounds.x(), image_bounds.y(),
path, flags); path, flags);
} }
......
...@@ -16,9 +16,18 @@ namespace ash { ...@@ -16,9 +16,18 @@ namespace ash {
// A custom image view with rounded edges. // A custom image view with rounded edges.
class ASH_PUBLIC_EXPORT RoundedImageView : public views::View { class ASH_PUBLIC_EXPORT RoundedImageView : public views::View {
public: public:
enum class Alignment {
// The image's drawn portion always contains the image's origin.
kLeading,
// If the image's size is greater than the view's, only the portion around
// the image's center shows.
kCenter
};
// Constructs a new rounded image view with rounded corners of radius // Constructs a new rounded image view with rounded corners of radius
// |corner_radius|. // |corner_radius|.
explicit RoundedImageView(int corner_radius); RoundedImageView(int corner_radius, Alignment alignment);
~RoundedImageView() override; ~RoundedImageView() override;
// Set the image that should be displayed. The image contents is copied to the // Set the image that should be displayed. The image contents is copied to the
...@@ -49,6 +58,8 @@ class ASH_PUBLIC_EXPORT RoundedImageView : public views::View { ...@@ -49,6 +58,8 @@ class ASH_PUBLIC_EXPORT RoundedImageView : public views::View {
gfx::ImageSkia resized_image_; gfx::ImageSkia resized_image_;
int corner_radius_[4]; int corner_radius_[4];
const Alignment alignment_;
DISALLOW_COPY_AND_ASSIGN(RoundedImageView); DISALLOW_COPY_AND_ASSIGN(RoundedImageView);
}; };
......
...@@ -29,8 +29,8 @@ HoldingSpaceItemChipView::HoldingSpaceItemChipView( ...@@ -29,8 +29,8 @@ HoldingSpaceItemChipView::HoldingSpaceItemChipView(
SetPreferredSize(gfx::Size(kHoldingSpaceChipWidth, kHoldingSpaceChipHeight)); SetPreferredSize(gfx::Size(kHoldingSpaceChipWidth, kHoldingSpaceChipHeight));
image_ = AddChildView( image_ = AddChildView(std::make_unique<RoundedImageView>(
std::make_unique<RoundedImageView>(kHoldingSpaceChipIconSize / 2)); kHoldingSpaceChipIconSize / 2, RoundedImageView::Alignment::kLeading));
label_ = AddChildView(std::make_unique<views::Label>(item->text())); label_ = AddChildView(std::make_unique<views::Label>(item->text()));
label_->SetElideBehavior(gfx::ELIDE_MIDDLE); label_->SetElideBehavior(gfx::ELIDE_MIDDLE);
......
...@@ -23,8 +23,8 @@ HoldingSpaceItemScreenshotView::HoldingSpaceItemScreenshotView( ...@@ -23,8 +23,8 @@ HoldingSpaceItemScreenshotView::HoldingSpaceItemScreenshotView(
SetPreferredSize(kHoldingSpaceScreenshotSize); SetPreferredSize(kHoldingSpaceScreenshotSize);
SetLayoutManager(std::make_unique<views::FillLayout>()); SetLayoutManager(std::make_unique<views::FillLayout>());
image_ = AddChildView( image_ = AddChildView(std::make_unique<RoundedImageView>(
std::make_unique<RoundedImageView>(kHoldingSpaceCornerRadius)); kHoldingSpaceCornerRadius, RoundedImageView::Alignment::kLeading));
// Subscribe to be notified of changes to `item_`'s image. // Subscribe to be notified of changes to `item_`'s image.
image_subscription_ = item->image().AddImageSkiaChangedCallback( image_subscription_ = item->image().AddImageSkiaChangedCallback(
......
...@@ -159,7 +159,8 @@ views::View* CreateUserAvatarView(int user_index) { ...@@ -159,7 +159,8 @@ views::View* CreateUserAvatarView(int user_index) {
return new TopShortcutButton(kSystemMenuGuestIcon, return new TopShortcutButton(kSystemMenuGuestIcon,
IDS_ASH_STATUS_TRAY_GUEST_LABEL); IDS_ASH_STATUS_TRAY_GUEST_LABEL);
} else { } else {
auto* image_view = new RoundedImageView(kTrayItemSize / 2); auto* image_view = new RoundedImageView(
kTrayItemSize / 2, RoundedImageView::Alignment::kLeading);
image_view->SetCanProcessEventsWithinSubtree(false); image_view->SetCanProcessEventsWithinSubtree(false);
image_view->SetImage(user_session->user_info.avatar.image, image_view->SetImage(user_session->user_info.avatar.image,
gfx::Size(kTrayItemSize, kTrayItemSize)); gfx::Size(kTrayItemSize, kTrayItemSize));
......
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