OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/android/resources/nine_patch_resource.h" | 5 #include "ui/android/resources/nine_patch_resource.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "cc/layers/nine_patch_layer.h" |
| 9 #include "ui/gfx/geometry/point_f.h" |
8 | 10 |
9 namespace ui { | 11 namespace ui { |
10 | 12 |
11 // static | 13 // static |
12 NinePatchResource* NinePatchResource::From(Resource* resource) { | 14 NinePatchResource* NinePatchResource::From(Resource* resource) { |
13 DCHECK_EQ(Type::NINE_PATCH_BITMAP, resource->type()); | 15 DCHECK_EQ(Type::NINE_PATCH_BITMAP, resource->type()); |
14 return static_cast<NinePatchResource*>(resource); | 16 return static_cast<NinePatchResource*>(resource); |
15 } | 17 } |
16 | 18 |
17 NinePatchResource::NinePatchResource(gfx::Rect padding, gfx::Rect aperture) | 19 NinePatchResource::NinePatchResource(gfx::Rect padding, gfx::Rect aperture) |
18 : Resource(Type::NINE_PATCH_BITMAP), | 20 : Resource(Type::NINE_PATCH_BITMAP), |
19 padding_(padding), | 21 padding_(padding), |
20 aperture_(aperture) {} | 22 aperture_(aperture) {} |
21 | 23 |
22 NinePatchResource::~NinePatchResource() = default; | 24 NinePatchResource::~NinePatchResource() = default; |
23 | 25 |
| 26 gfx::Size NinePatchResource::DrawSize(const gfx::Size& content_size) const { |
| 27 // The effective drawing size of the resource includes the size of the content |
| 28 // (fit inside the expanded padding area) and the size of the margins on each |
| 29 // side. |
| 30 return gfx::Size(content_size.width() + size().width() - padding_.width(), |
| 31 content_size.height() + size().height() - padding_.height()); |
| 32 } |
| 33 |
| 34 gfx::PointF NinePatchResource::DrawPosition( |
| 35 const gfx::Point& content_position) const { |
| 36 // Offset the location of the layer by the amount taken by the left and top |
| 37 // margin. |
| 38 return gfx::PointF(content_position.x() - padding_.x(), |
| 39 content_position.y() - padding_.y()); |
| 40 } |
| 41 |
24 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const { | 42 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const { |
25 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); | 43 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); |
26 } | 44 } |
27 | 45 |
28 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds, | 46 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds, |
29 const gfx::InsetsF& scale) const { | 47 const gfx::InsetsF& scale) const { |
30 // Calculate whether or not we need to scale down the border if the bounds of | 48 // 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. | 49 // the layer are going to be smaller than the aperture padding. |
32 float x_scale = std::min((float)bounds.width() / size().width(), 1.f); | 50 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); | 51 float y_scale = std::min((float)bounds.height() / size().height(), 1.f); |
34 | 52 |
35 float left_scale = std::min(x_scale * scale.left(), 1.f); | 53 float left_scale = std::min(x_scale * scale.left(), 1.f); |
36 float right_scale = std::min(x_scale * scale.right(), 1.f); | 54 float right_scale = std::min(x_scale * scale.right(), 1.f); |
37 float top_scale = std::min(y_scale * scale.top(), 1.f); | 55 float top_scale = std::min(y_scale * scale.top(), 1.f); |
38 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); | 56 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); |
39 | 57 |
40 return gfx::Rect(aperture_.x() * left_scale, aperture_.y() * top_scale, | 58 return gfx::Rect(aperture_.x() * left_scale, aperture_.y() * top_scale, |
41 (size().width() - aperture_.width()) * right_scale, | 59 (size().width() - aperture_.width()) * right_scale, |
42 (size().height() - aperture_.height()) * bottom_scale); | 60 (size().height() - aperture_.height()) * bottom_scale); |
43 } | 61 } |
44 | 62 |
45 std::unique_ptr<Resource> NinePatchResource::CreateForCopy() { | 63 std::unique_ptr<Resource> NinePatchResource::CreateForCopy() { |
46 return base::MakeUnique<NinePatchResource>(padding_, aperture_); | 64 return base::MakeUnique<NinePatchResource>(padding_, aperture_); |
47 } | 65 } |
48 | 66 |
49 } // namespace ui | 67 } // namespace ui |
OLD | NEW |