[go: nahoru, domu]

blob: 622863ea6ae756ca0d30b074501c8b6faadcf70d [file] [log] [blame]
George Mount7b612b82020-04-02 12:56:09 -07001/*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.ui.core
18
19import androidx.ui.graphics.Canvas
20import androidx.ui.graphics.Color
21import androidx.ui.graphics.Paint
22import androidx.ui.graphics.PaintingStyle
George Mount5469e252020-06-17 10:01:42 -070023
24@OptIn(ExperimentalLayoutNodeApi::class)
Mihai Popa5f9e7532020-04-21 01:19:23 +010025internal class ModifiedLayoutNode(
George Mount7b612b82020-04-02 12:56:09 -070026 wrapped: LayoutNodeWrapper,
George Mounta2e46b02020-06-15 10:33:15 -070027 modifier: LayoutModifier
28) : DelegatingLayoutNodeWrapper<LayoutModifier>(wrapped, modifier) {
George Mount7b612b82020-04-02 12:56:09 -070029
Mihai Popafc355b12020-04-15 17:34:17 +010030 override val measureScope = ModifierMeasureScope()
31
32 override fun performMeasure(
33 constraints: Constraints,
34 layoutDirection: LayoutDirection
George Mounta2e46b02020-06-15 10:33:15 -070035 ): Placeable = with(modifier) {
Mihai Popafc355b12020-04-15 17:34:17 +010036 measureScope.layoutDirection = layoutDirection
37 measureResult = measureScope.measure(wrapped, constraints, layoutDirection)
Mihai Popa5f9e7532020-04-21 01:19:23 +010038 this@ModifiedLayoutNode
George Mount7b612b82020-04-02 12:56:09 -070039 }
40
George Mount8f237572020-04-30 12:08:30 -070041 override fun minIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int =
George Mounta2e46b02020-06-15 10:33:15 -070042 with(modifier) {
Mihai Popafe2f8fa2020-04-21 18:00:21 +010043 measureScope.layoutDirection = layoutDirection
44 measureScope.minIntrinsicWidth(wrapped, height, layoutDirection)
45 }
George Mount7b612b82020-04-02 12:56:09 -070046
George Mount8f237572020-04-30 12:08:30 -070047 override fun maxIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int =
George Mounta2e46b02020-06-15 10:33:15 -070048 with(modifier) {
Mihai Popafe2f8fa2020-04-21 18:00:21 +010049 measureScope.layoutDirection = layoutDirection
50 measureScope.maxIntrinsicWidth(wrapped, height, layoutDirection)
51 }
George Mount7b612b82020-04-02 12:56:09 -070052
George Mount8f237572020-04-30 12:08:30 -070053 override fun minIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int =
George Mounta2e46b02020-06-15 10:33:15 -070054 with(modifier) {
Mihai Popafe2f8fa2020-04-21 18:00:21 +010055 measureScope.layoutDirection = layoutDirection
56 measureScope.minIntrinsicHeight(wrapped, width, layoutDirection)
57 }
George Mount7b612b82020-04-02 12:56:09 -070058
George Mount8f237572020-04-30 12:08:30 -070059 override fun maxIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int =
George Mounta2e46b02020-06-15 10:33:15 -070060 with(modifier) {
Mihai Popafe2f8fa2020-04-21 18:00:21 +010061 measureScope.layoutDirection = layoutDirection
62 measureScope.maxIntrinsicHeight(wrapped, width, layoutDirection)
63 }
George Mount7b612b82020-04-02 12:56:09 -070064
Mihai Popa08c47952020-06-04 20:01:27 +010065 override operator fun get(line: AlignmentLine): Int {
Mihai Popa39d82402020-05-05 13:35:50 +010066 if (measureResult.alignmentLines.containsKey(line)) {
Mihai Popa08c47952020-06-04 20:01:27 +010067 return measureResult.alignmentLines[line] ?: AlignmentLine.Unspecified
Mihai Popa39d82402020-05-05 13:35:50 +010068 }
Mihai Popa08c47952020-06-04 20:01:27 +010069 val positionInWrapped = wrapped[line]
70 if (positionInWrapped == AlignmentLine.Unspecified) {
71 return AlignmentLine.Unspecified
72 }
Mihai Popa39d82402020-05-05 13:35:50 +010073 // Place our wrapped to obtain their position inside ourselves.
74 isShallowPlacing = true
75 place(this.position)
76 isShallowPlacing = false
77 return if (line is HorizontalAlignmentLine) {
78 positionInWrapped + wrapped.position.y
79 } else {
80 positionInWrapped + wrapped.position.x
81 }
82 }
George Mount7b612b82020-04-02 12:56:09 -070083
84 override fun draw(canvas: Canvas) {
85 withPositionTranslation(canvas) {
86 wrapped.draw(canvas)
87 if (layoutNode.requireOwner().showLayoutBounds) {
88 drawBorder(canvas, modifierBoundsPaint)
89 }
90 }
91 }
92
93 internal companion object {
94 val modifierBoundsPaint = Paint().also { paint ->
95 paint.color = Color.Blue
96 paint.strokeWidth = 1f
97 paint.style = PaintingStyle.stroke
98 }
99 }
100
Mihai Popafc355b12020-04-15 17:34:17 +0100101 inner class ModifierMeasureScope : MeasureScope() {
102 override var layoutDirection: LayoutDirection = LayoutDirection.Ltr
103 override val density: Float
104 get() = layoutNode.measureScope.density
105 override val fontScale: Float
106 get() = layoutNode.measureScope.fontScale
George Mount7b612b82020-04-02 12:56:09 -0700107 }
108}