|
1 |
| -// extension View { |
2 |
| -// /// Adds a specific padding amount to each edge of this view. |
3 |
| -// public func padding(_ amount: Int) -> some View { |
4 |
| -// return PaddingModifierView(body: VariadicView1(self), edges: .all, padding: amount) |
5 |
| -// } |
6 |
| - |
7 |
| -// /// Adds an equal padding amount to specific edges of this view. |
8 |
| -// public func padding(_ edges: Edge.Set, _ amount: Int) -> some View { |
9 |
| -// return PaddingModifierView(body: VariadicView1(self), edges: edges, padding: amount) |
10 |
| -// } |
11 |
| -// } |
12 |
| - |
13 |
| -// /// The implementation for the ``View/padding(_:_:)`` and ``View/padding(_:)`` view modifiers. |
14 |
| -// struct PaddingModifierView<Child: View>: TypeSafeView { |
15 |
| -// var body: VariadicView1<Child> |
16 |
| - |
17 |
| -// /// The edges on which to apply padding. |
18 |
| -// var edges: Edge.Set |
19 |
| -// /// The amount of padding to apply to the child view. |
20 |
| -// var padding: Int |
21 |
| - |
22 |
| -// func asWidget<Backend: AppBackend>( |
23 |
| -// _ children: ViewGraphNodeChildren1<Child>, |
24 |
| -// backend: Backend |
25 |
| -// ) -> Backend.Widget { |
26 |
| -// return backend.createPaddingContainer(for: children.child0.widget.into()) |
27 |
| -// } |
28 |
| - |
29 |
| -// func update<Backend: AppBackend>( |
30 |
| -// _ container: Backend.Widget, |
31 |
| -// children: ViewGraphNodeChildren1<Child>, |
32 |
| -// backend: Backend |
33 |
| -// ) { |
34 |
| -// backend.setPadding( |
35 |
| -// ofPaddingContainer: container, |
36 |
| -// top: edges.contains(.top) ? padding : 0, |
37 |
| -// bottom: edges.contains(.bottom) ? padding : 0, |
38 |
| -// leading: edges.contains(.leading) ? padding : 0, |
39 |
| -// trailing: edges.contains(.trailing) ? padding : 0 |
40 |
| -// ) |
41 |
| -// } |
42 |
| -// } |
| 1 | +extension View { |
| 2 | + /// Adds a specific padding amount to each edge of this view. |
| 3 | + public func padding(_ amount: Int) -> some View { |
| 4 | + return PaddingModifierView(body: VariadicView1(self), edges: .all, padding: amount) |
| 5 | + } |
| 6 | + |
| 7 | + /// Adds an equal padding amount to specific edges of this view. |
| 8 | + public func padding(_ edges: Edge.Set, _ amount: Int) -> some View { |
| 9 | + return PaddingModifierView(body: VariadicView1(self), edges: edges, padding: amount) |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +/// The implementation for the ``View/padding(_:_:)`` and ``View/padding(_:)`` view modifiers. |
| 14 | +struct PaddingModifierView<Child: View>: TypeSafeView { |
| 15 | + var body: VariadicView1<Child> |
| 16 | + |
| 17 | + /// The edges on which to apply padding. |
| 18 | + var edges: Edge.Set |
| 19 | + /// The amount of padding to apply to the child view. |
| 20 | + var padding: Int |
| 21 | + |
| 22 | + public var flexibility: Int { |
| 23 | + body.flexibility - 10 |
| 24 | + } |
| 25 | + |
| 26 | + func children<Backend: AppBackend>( |
| 27 | + backend: Backend, |
| 28 | + snapshots: [ViewGraphSnapshotter.NodeSnapshot]?, |
| 29 | + environment: Environment |
| 30 | + ) -> ViewGraphNodeChildren1<Child> { |
| 31 | + body.children(backend: backend, snapshots: snapshots, environment: environment) |
| 32 | + } |
| 33 | + |
| 34 | + func asWidget<Backend: AppBackend>( |
| 35 | + _ children: ViewGraphNodeChildren1<Child>, |
| 36 | + backend: Backend |
| 37 | + ) -> Backend.Widget { |
| 38 | + let container = backend.createContainer() |
| 39 | + backend.addChild(children.child0.widget.into(), to: container) |
| 40 | + return container |
| 41 | + } |
| 42 | + |
| 43 | + func update<Backend: AppBackend>( |
| 44 | + _ container: Backend.Widget, |
| 45 | + children: ViewGraphNodeChildren1<Child>, |
| 46 | + proposedSize: SIMD2<Int>, |
| 47 | + environment: Environment, |
| 48 | + backend: Backend |
| 49 | + ) -> SIMD2<Int> { |
| 50 | + let topPadding = edges.contains(.top) ? padding : 0 |
| 51 | + let bottomPadding = edges.contains(.bottom) ? padding : 0 |
| 52 | + let leadingPadding = edges.contains(.leading) ? padding : 0 |
| 53 | + let trailingPadding = edges.contains(.trailing) ? padding : 0 |
| 54 | + |
| 55 | + let childSize = children.child0.update( |
| 56 | + with: body.view0, |
| 57 | + proposedSize: SIMD2( |
| 58 | + proposedSize.x - leadingPadding - trailingPadding, |
| 59 | + proposedSize.y - topPadding - bottomPadding |
| 60 | + ), |
| 61 | + environment: environment |
| 62 | + ) |
| 63 | + |
| 64 | + let size = SIMD2( |
| 65 | + childSize.x + leadingPadding + trailingPadding, |
| 66 | + childSize.y + topPadding + bottomPadding |
| 67 | + ) |
| 68 | + backend.setSize(of: container, to: size) |
| 69 | + backend.setPosition(ofChildAt: 0, in: container, to: SIMD2(topPadding, leadingPadding)) |
| 70 | + |
| 71 | + return size |
| 72 | + } |
| 73 | +} |
0 commit comments