Skip to content

Commit f1bf340

Browse files
Andrew Wangfacebook-github-bot
authored andcommitted
Convert MeasureSpecUtils to Kotlin
Summary: as per title Reviewed By: pengj Differential Revision: D52999947 fbshipit-source-id: 061379ef6b43cc8df7910a2fbb0ed27f6b310a1d
1 parent e511d81 commit f1bf340

File tree

2 files changed

+106
-119
lines changed

2 files changed

+106
-119
lines changed

litho-rendercore/src/main/java/com/facebook/rendercore/utils/MeasureSpecUtils.java

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
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 com.facebook.rendercore.utils
18+
19+
import android.view.View.MeasureSpec
20+
import java.util.Locale
21+
22+
/**
23+
* An utility class to verify that an old measured size is still compatible to be used with a new
24+
* measureSpec.
25+
*/
26+
object MeasureSpecUtils {
27+
28+
private const val DELTA = 0.5f
29+
private val UNSPECIFIED = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
30+
31+
@JvmStatic fun unspecified(): Int = UNSPECIFIED
32+
33+
@JvmStatic fun atMost(px: Int): Int = MeasureSpec.makeMeasureSpec(px, MeasureSpec.AT_MOST)
34+
35+
@JvmStatic fun exactly(px: Int): Int = MeasureSpec.makeMeasureSpec(px, MeasureSpec.EXACTLY)
36+
37+
@JvmStatic fun getMode(spec: Int): Int = MeasureSpec.getMode(spec)
38+
39+
@JvmStatic fun getSize(spec: Int): Int = MeasureSpec.getSize(spec)
40+
41+
@JvmStatic
42+
fun areMeasureSpecsEquivalent(specA: Int, specB: Int): Boolean =
43+
specA == specB ||
44+
(MeasureSpec.getMode(specA) == UNSPECIFIED && MeasureSpec.getMode(specB) == UNSPECIFIED)
45+
46+
@JvmStatic
47+
fun isMeasureSpecCompatible(oldSizeSpec: Int, sizeSpec: Int, oldMeasuredSize: Int): Boolean {
48+
val newSpecMode = MeasureSpec.getMode(sizeSpec)
49+
val newSpecSize = MeasureSpec.getSize(sizeSpec)
50+
val oldSpecMode = MeasureSpec.getMode(oldSizeSpec)
51+
val oldSpecSize = MeasureSpec.getSize(oldSizeSpec)
52+
return (oldSizeSpec == sizeSpec) ||
53+
(oldSpecMode == UNSPECIFIED && newSpecMode == UNSPECIFIED) ||
54+
newSizeIsExactAndMatchesOldMeasuredSize(
55+
newSpecMode, newSpecSize, oldMeasuredSize.toFloat()) ||
56+
oldSizeIsUnspecifiedAndStillFits(
57+
oldSpecMode, newSpecMode, newSpecSize, oldMeasuredSize.toFloat()) ||
58+
newMeasureSizeIsStricterAndStillValid(
59+
oldSpecMode, newSpecMode, oldSpecSize, newSpecSize, oldMeasuredSize.toFloat())
60+
}
61+
62+
private fun newSizeIsExactAndMatchesOldMeasuredSize(
63+
newSizeSpecMode: Int,
64+
newSizeSpecSize: Int,
65+
oldMeasuredSize: Float
66+
): Boolean =
67+
newSizeSpecMode == MeasureSpec.EXACTLY && Math.abs(newSizeSpecSize - oldMeasuredSize) < DELTA
68+
69+
private fun oldSizeIsUnspecifiedAndStillFits(
70+
oldSizeSpecMode: Int,
71+
newSizeSpecMode: Int,
72+
newSizeSpecSize: Int,
73+
oldMeasuredSize: Float
74+
): Boolean =
75+
newSizeSpecMode == MeasureSpec.AT_MOST &&
76+
oldSizeSpecMode == UNSPECIFIED &&
77+
newSizeSpecSize >= oldMeasuredSize
78+
79+
private fun newMeasureSizeIsStricterAndStillValid(
80+
oldSizeSpecMode: Int,
81+
newSizeSpecMode: Int,
82+
oldSizeSpecSize: Int,
83+
newSizeSpecSize: Int,
84+
oldMeasuredSize: Float
85+
): Boolean =
86+
oldSizeSpecMode == MeasureSpec.AT_MOST &&
87+
newSizeSpecMode == MeasureSpec.AT_MOST &&
88+
oldSizeSpecSize > newSizeSpecSize &&
89+
oldMeasuredSize <= newSizeSpecSize
90+
91+
@JvmStatic
92+
fun getMeasureSpecDescription(measureSpec: Int): String {
93+
val value = getSize(measureSpec)
94+
val mode = getModeDescription(getMode(measureSpec))
95+
return String.format(Locale.US, "[%d, %s]", value, mode)
96+
}
97+
98+
@JvmStatic
99+
fun getModeDescription(mode: Int): String =
100+
when (mode) {
101+
MeasureSpec.AT_MOST -> "AT_MOST"
102+
MeasureSpec.EXACTLY -> "EXACTLY"
103+
UNSPECIFIED -> "UNSPECIFIED"
104+
else -> "INVALID"
105+
}
106+
}

0 commit comments

Comments
 (0)