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 void NinePatchResource::UpdateNinePatchLayer( | |
mdjones
2017/03/16 21:01:48
This feels like something that should either be st
Khushal
2017/03/17 23:02:58
The getter is already public. This was supposed to
| |
27 cc::NinePatchLayer* layer, | |
28 const gfx::Rect& content_location) const { | |
29 // The effective drawing size of the layer includes the size of the content | |
30 // (fit inside the expanded padding area) and the size of the margins on each | |
31 // side. | |
32 gfx::Size draw_size( | |
33 content_location.width() + size().width() - padding_.width(), | |
34 content_location.height() + size().height() - padding_.height()); | |
35 gfx::Rect border(Border(draw_size)); | |
36 // Offset the location of the layer by the amount taken by the left and top | |
37 // margin. | |
38 gfx::PointF position(content_location.x() - padding_.x(), | |
39 content_location.y() - padding_.y()); | |
40 | |
41 layer->SetBounds(draw_size); | |
42 layer->SetPosition(position); | |
43 layer->SetBorder(border); | |
44 layer->SetAperture(aperture_); | |
45 layer->SetUIResourceId(ui_resource()->id()); | |
46 } | |
47 | |
24 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const { | 48 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds) const { |
25 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); | 49 return Border(bounds, gfx::InsetsF(1.f, 1.f, 1.f, 1.f)); |
26 } | 50 } |
27 | 51 |
28 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds, | 52 gfx::Rect NinePatchResource::Border(const gfx::Size& bounds, |
29 const gfx::InsetsF& scale) const { | 53 const gfx::InsetsF& scale) const { |
30 // Calculate whether or not we need to scale down the border if the bounds of | 54 // 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. | 55 // the layer are going to be smaller than the aperture padding. |
32 float x_scale = std::min((float)bounds.width() / size().width(), 1.f); | 56 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); | 57 float y_scale = std::min((float)bounds.height() / size().height(), 1.f); |
34 | 58 |
35 float left_scale = std::min(x_scale * scale.left(), 1.f); | 59 float left_scale = std::min(x_scale * scale.left(), 1.f); |
36 float right_scale = std::min(x_scale * scale.right(), 1.f); | 60 float right_scale = std::min(x_scale * scale.right(), 1.f); |
37 float top_scale = std::min(y_scale * scale.top(), 1.f); | 61 float top_scale = std::min(y_scale * scale.top(), 1.f); |
38 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); | 62 float bottom_scale = std::min(y_scale * scale.bottom(), 1.f); |
39 | 63 |
40 return gfx::Rect(aperture_.x() * left_scale, aperture_.y() * top_scale, | 64 return gfx::Rect(aperture_.x() * left_scale, aperture_.y() * top_scale, |
41 (size().width() - aperture_.width()) * right_scale, | 65 (size().width() - aperture_.width()) * right_scale, |
42 (size().height() - aperture_.height()) * bottom_scale); | 66 (size().height() - aperture_.height()) * bottom_scale); |
43 } | 67 } |
44 | 68 |
45 std::unique_ptr<Resource> NinePatchResource::CreateForCopy() { | 69 std::unique_ptr<Resource> NinePatchResource::CreateForCopy() { |
46 return base::MakeUnique<NinePatchResource>(padding_, aperture_); | 70 return base::MakeUnique<NinePatchResource>(padding_, aperture_); |
47 } | 71 } |
48 | 72 |
49 } // namespace ui | 73 } // namespace ui |
OLD | NEW |