OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "ui/android/resources/nine_patch_resource.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 |
| 9 namespace ui { |
| 10 |
| 11 // static |
| 12 NinePatchResource* NinePatchResource::From(Resource* resource) { |
| 13 DCHECK_EQ(Type::NINE_PATCH_BITMAP, resource->type()); |
| 14 return static_cast<NinePatchResource*>(resource); |
| 15 } |
| 16 |
| 17 NinePatchResource::NinePatchResource(gfx::Rect padding, gfx::Rect aperture) |
| 18 : Resource(Type::NINE_PATCH_BITMAP), |
| 19 padding_(padding), |
| 20 aperture_(aperture) {} |
| 21 |
| 22 NinePatchResource::~NinePatchResource() = default; |
| 23 |
| 24 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const { |
| 25 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); |
| 26 } |
| 27 |
| 28 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds, |
| 29 const gfx::InsetsF& scale) const { |
| 30 // Calculate whether or not we need to scale down the border if the bounds of |
| 31 // the layer are going to be smaller than the aperture padding. |
| 32 float x_scale = std::min((float)bounds.width() / size().width(), 1.f); |
| 33 float y_scale = std::min((float)bounds.height() / size().height(), 1.f); |
| 34 |
| 35 float left_scale = std::min(x_scale * scale.left(), 1.f); |
| 36 float right_scale = std::min(x_scale * scale.right(), 1.f); |
| 37 float top_scale = std::min(y_scale * scale.top(), 1.f); |
| 38 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); |
| 39 |
| 40 return gfx::Rect(aperture_.x() * left_scale, aperture_.y() * top_scale, |
| 41 (size().width() - aperture_.width()) * right_scale, |
| 42 (size().height() - aperture_.height()) * bottom_scale); |
| 43 } |
| 44 |
| 45 std::unique_ptr<Resource> NinePatchResource::CreateForCopy() { |
| 46 return base::MakeUnique<NinePatchResource>(padding_, aperture_); |
| 47 } |
| 48 |
| 49 } // namespace ui |
OLD | NEW |