George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.ui.core |
| 18 | |
| 19 | import androidx.ui.graphics.Canvas |
| 20 | import androidx.ui.graphics.Color |
| 21 | import androidx.ui.graphics.Paint |
| 22 | import androidx.ui.graphics.PaintingStyle |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 23 | |
| 24 | @OptIn(ExperimentalLayoutNodeApi::class) |
Mihai Popa | 5f9e753 | 2020-04-21 01:19:23 +0100 | [diff] [blame] | 25 | internal class ModifiedLayoutNode( |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 26 | wrapped: LayoutNodeWrapper, |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 27 | modifier: LayoutModifier |
| 28 | ) : DelegatingLayoutNodeWrapper<LayoutModifier>(wrapped, modifier) { |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 29 | |
Mihai Popa | fc355b1 | 2020-04-15 17:34:17 +0100 | [diff] [blame] | 30 | override val measureScope = ModifierMeasureScope() |
| 31 | |
| 32 | override fun performMeasure( |
| 33 | constraints: Constraints, |
| 34 | layoutDirection: LayoutDirection |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 35 | ): Placeable = with(modifier) { |
Mihai Popa | fc355b1 | 2020-04-15 17:34:17 +0100 | [diff] [blame] | 36 | measureScope.layoutDirection = layoutDirection |
| 37 | measureResult = measureScope.measure(wrapped, constraints, layoutDirection) |
Mihai Popa | 5f9e753 | 2020-04-21 01:19:23 +0100 | [diff] [blame] | 38 | this@ModifiedLayoutNode |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 39 | } |
| 40 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 41 | override fun minIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int = |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 42 | with(modifier) { |
Mihai Popa | fe2f8fa | 2020-04-21 18:00:21 +0100 | [diff] [blame] | 43 | measureScope.layoutDirection = layoutDirection |
| 44 | measureScope.minIntrinsicWidth(wrapped, height, layoutDirection) |
| 45 | } |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 46 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 47 | override fun maxIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int = |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 48 | with(modifier) { |
Mihai Popa | fe2f8fa | 2020-04-21 18:00:21 +0100 | [diff] [blame] | 49 | measureScope.layoutDirection = layoutDirection |
| 50 | measureScope.maxIntrinsicWidth(wrapped, height, layoutDirection) |
| 51 | } |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 52 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 53 | override fun minIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int = |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 54 | with(modifier) { |
Mihai Popa | fe2f8fa | 2020-04-21 18:00:21 +0100 | [diff] [blame] | 55 | measureScope.layoutDirection = layoutDirection |
| 56 | measureScope.minIntrinsicHeight(wrapped, width, layoutDirection) |
| 57 | } |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 58 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 59 | override fun maxIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int = |
George Mount | a2e46b0 | 2020-06-15 10:33:15 -0700 | [diff] [blame] | 60 | with(modifier) { |
Mihai Popa | fe2f8fa | 2020-04-21 18:00:21 +0100 | [diff] [blame] | 61 | measureScope.layoutDirection = layoutDirection |
| 62 | measureScope.maxIntrinsicHeight(wrapped, width, layoutDirection) |
| 63 | } |
George Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 64 | |
Mihai Popa | 08c4795 | 2020-06-04 20:01:27 +0100 | [diff] [blame] | 65 | override operator fun get(line: AlignmentLine): Int { |
Mihai Popa | 39d8240 | 2020-05-05 13:35:50 +0100 | [diff] [blame] | 66 | if (measureResult.alignmentLines.containsKey(line)) { |
Mihai Popa | 08c4795 | 2020-06-04 20:01:27 +0100 | [diff] [blame] | 67 | return measureResult.alignmentLines[line] ?: AlignmentLine.Unspecified |
Mihai Popa | 39d8240 | 2020-05-05 13:35:50 +0100 | [diff] [blame] | 68 | } |
Mihai Popa | 08c4795 | 2020-06-04 20:01:27 +0100 | [diff] [blame] | 69 | val positionInWrapped = wrapped[line] |
| 70 | if (positionInWrapped == AlignmentLine.Unspecified) { |
| 71 | return AlignmentLine.Unspecified |
| 72 | } |
Mihai Popa | 39d8240 | 2020-05-05 13:35:50 +0100 | [diff] [blame] | 73 | // 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 Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 83 | |
| 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 Popa | fc355b1 | 2020-04-15 17:34:17 +0100 | [diff] [blame] | 101 | 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 Mount | 7b612b8 | 2020-04-02 12:56:09 -0700 | [diff] [blame] | 107 | } |
| 108 | } |