Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [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.core.LayoutNode.LayoutState |
| 20 | import androidx.ui.core.LayoutNode.MeasureBlocks |
| 21 | import androidx.ui.core.LayoutNode.UsageByParent |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 22 | import androidx.ui.unit.IntOffset |
| 23 | import androidx.ui.unit.IntSize |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 24 | |
George Mount | 5469e25 | 2020-06-17 10:01:42 -0700 | [diff] [blame] | 25 | @OptIn(ExperimentalLayoutNodeApi::class) |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 26 | internal class OuterMeasurablePlaceable( |
| 27 | private val layoutNode: LayoutNode, |
| 28 | var outerWrapper: LayoutNodeWrapper |
| 29 | ) : Measurable, Placeable() { |
| 30 | |
| 31 | private var measuredOnce = false |
| 32 | val lastConstraints: Constraints? get() = if (measuredOnce) measurementConstraints else null |
| 33 | var lastLayoutDirection: LayoutDirection? = null |
| 34 | private set |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 35 | var lastPosition: IntOffset? = null |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 36 | private set |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 37 | private val lastProvidedAlignmentLines = mutableMapOf<AlignmentLine, Int>() |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * A local version of [Owner.measureIteration] to ensure that [MeasureBlocks.measure] |
| 41 | * is not called multiple times within a measure pass. |
| 42 | */ |
| 43 | var measureIteration = -1L |
| 44 | private set |
| 45 | |
| 46 | override val parentData: Any? get() = outerWrapper.parentData |
| 47 | |
| 48 | /** |
| 49 | * The function to be executed when the parent layout measures its children. |
| 50 | */ |
| 51 | override fun measure(constraints: Constraints, layoutDirection: LayoutDirection): Placeable { |
| 52 | // when we measure the root it is like the virtual parent is currently laying out |
| 53 | val parentState = layoutNode.parent?.layoutState ?: LayoutState.LayingOut |
| 54 | layoutNode.measuredByParent = when (parentState) { |
| 55 | LayoutState.Measuring -> UsageByParent.InMeasureBlock |
| 56 | LayoutState.LayingOut -> UsageByParent.InLayoutBlock |
| 57 | else -> throw IllegalStateException( |
| 58 | "Measurable could be only measured from the parent's measure or layout block." + |
| 59 | "Parents state is $parentState" |
| 60 | ) |
| 61 | } |
| 62 | remeasure(constraints, layoutDirection) |
| 63 | return this |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Return true if the measured size has been changed |
| 68 | */ |
| 69 | fun remeasure(constraints: Constraints, layoutDirection: LayoutDirection): Boolean { |
| 70 | val owner = layoutNode.requireOwner() |
| 71 | val iteration = owner.measureIteration |
| 72 | val parent = layoutNode.parent |
| 73 | @Suppress("Deprecation") |
| 74 | layoutNode.canMultiMeasure = layoutNode.canMultiMeasure || |
| 75 | (parent != null && parent.canMultiMeasure) |
| 76 | @Suppress("Deprecation") |
| 77 | check(measureIteration != iteration || layoutNode.canMultiMeasure) { |
| 78 | "measure() may not be called multiple times on the same Measurable" |
| 79 | } |
| 80 | measureIteration = owner.measureIteration |
| 81 | if (layoutNode.layoutState == LayoutState.NeedsRemeasure || |
| 82 | measurementConstraints != constraints || |
| 83 | lastLayoutDirection != layoutDirection |
| 84 | ) { |
| 85 | measuredOnce = true |
| 86 | layoutNode.layoutState = LayoutState.Measuring |
| 87 | measurementConstraints = constraints |
| 88 | lastLayoutDirection = layoutDirection |
| 89 | lastProvidedAlignmentLines.clear() |
| 90 | lastProvidedAlignmentLines.putAll(layoutNode.providedAlignmentLines) |
| 91 | owner.observeMeasureModelReads(layoutNode) { |
| 92 | outerWrapper.measure(constraints, layoutDirection) |
| 93 | } |
| 94 | layoutNode.layoutState = LayoutState.NeedsRelayout |
| 95 | if (layoutNode.providedAlignmentLines != lastProvidedAlignmentLines) { |
| 96 | layoutNode.onAlignmentsChanged() |
| 97 | } |
| 98 | val previousSize = measuredSize |
| 99 | val newWidth = outerWrapper.width |
| 100 | val newHeight = outerWrapper.height |
| 101 | if (newWidth != previousSize.width || |
| 102 | newHeight != previousSize.height |
| 103 | ) { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 104 | measuredSize = IntSize(newWidth, newHeight) |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 105 | return true |
| 106 | } |
| 107 | } |
| 108 | return false |
| 109 | } |
| 110 | |
Mihai Popa | 08c4795 | 2020-06-04 20:01:27 +0100 | [diff] [blame] | 111 | override fun get(line: AlignmentLine): Int = outerWrapper[line] |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 112 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 113 | override fun place(position: IntOffset) { |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 114 | lastPosition = position |
| 115 | with(InnerPlacementScope) { |
| 116 | outerWrapper.placeAbsolute(position) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Calls [place] with the same position used during the last [place] call |
| 122 | */ |
| 123 | fun replace() { |
| 124 | place(checkNotNull(lastPosition)) |
| 125 | } |
| 126 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 127 | override fun minIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int = |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 128 | outerWrapper.minIntrinsicWidth(height, layoutDirection) |
| 129 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 130 | override fun maxIntrinsicWidth(height: Int, layoutDirection: LayoutDirection): Int = |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 131 | outerWrapper.maxIntrinsicWidth(height, layoutDirection) |
| 132 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 133 | override fun minIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int = |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 134 | outerWrapper.minIntrinsicHeight(width, layoutDirection) |
| 135 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 136 | override fun maxIntrinsicHeight(width: Int, layoutDirection: LayoutDirection): Int = |
Andrey Kulikov | b33a380 | 2020-05-21 20:02:53 +0100 | [diff] [blame] | 137 | outerWrapper.maxIntrinsicHeight(width, layoutDirection) |
| 138 | } |