Skip to content

Commit e19d9ad

Browse files
author
Pratham Gahlout
committed
Revert "Add plus and minus operators for PaddingValues"
This reverts commit 85189b7. Reason for revert: A few tests were failing because the composables under test were having very specific implementations of `plus`/`minus` operators as extension functions for `PaddingValues`. Since the original change introduced these operators as member functions to `PaddingValues`, the compiler prioritized the member functions over their specific implementations. Test: Revert Bug: b/449245052, b/449225230 Relnote: "`PaddingValues` now support addition and subtraction to simplify common padding calculations" Change-Id: I03d85ca9efa633ec0b8d6119590cbfc203337469
1 parent 693028e commit e19d9ad

File tree

5 files changed

+1
-163
lines changed

5 files changed

+1
-163
lines changed

compose/foundation/foundation-layout/api/current.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,6 @@ package androidx.compose.foundation.layout {
332332
method @BytecodeOnly public float calculateRightPadding-u2uoSUM(androidx.compose.ui.unit.LayoutDirection);
333333
method @KotlinOnly public androidx.compose.ui.unit.Dp calculateTopPadding();
334334
method @BytecodeOnly public float calculateTopPadding-D9Ej5fM();
335-
method @androidx.compose.runtime.Stable public default operator androidx.compose.foundation.layout.PaddingValues minus(androidx.compose.foundation.layout.PaddingValues other);
336-
method @androidx.compose.runtime.Stable public default operator androidx.compose.foundation.layout.PaddingValues plus(androidx.compose.foundation.layout.PaddingValues other);
337335
field public static final androidx.compose.foundation.layout.PaddingValues.Companion Companion;
338336
}
339337

compose/foundation/foundation-layout/api/restricted_current.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,6 @@ package androidx.compose.foundation.layout {
343343
method @BytecodeOnly public float calculateRightPadding-u2uoSUM(androidx.compose.ui.unit.LayoutDirection);
344344
method @KotlinOnly public androidx.compose.ui.unit.Dp calculateTopPadding();
345345
method @BytecodeOnly public float calculateTopPadding-D9Ej5fM();
346-
method @androidx.compose.runtime.Stable public default operator androidx.compose.foundation.layout.PaddingValues minus(androidx.compose.foundation.layout.PaddingValues other);
347-
method @androidx.compose.runtime.Stable public default operator androidx.compose.foundation.layout.PaddingValues plus(androidx.compose.foundation.layout.PaddingValues other);
348346
field public static final androidx.compose.foundation.layout.PaddingValues.Companion Companion;
349347
}
350348

compose/foundation/foundation-layout/bcv/native/current.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ abstract interface androidx.compose.foundation.layout/PaddingValues { // android
5959
abstract fun calculateLeftPadding(androidx.compose.ui.unit/LayoutDirection): androidx.compose.ui.unit/Dp // androidx.compose.foundation.layout/PaddingValues.calculateLeftPadding|calculateLeftPadding(androidx.compose.ui.unit.LayoutDirection){}[0]
6060
abstract fun calculateRightPadding(androidx.compose.ui.unit/LayoutDirection): androidx.compose.ui.unit/Dp // androidx.compose.foundation.layout/PaddingValues.calculateRightPadding|calculateRightPadding(androidx.compose.ui.unit.LayoutDirection){}[0]
6161
abstract fun calculateTopPadding(): androidx.compose.ui.unit/Dp // androidx.compose.foundation.layout/PaddingValues.calculateTopPadding|calculateTopPadding(){}[0]
62-
open fun minus(androidx.compose.foundation.layout/PaddingValues): androidx.compose.foundation.layout/PaddingValues // androidx.compose.foundation.layout/PaddingValues.minus|minus(androidx.compose.foundation.layout.PaddingValues){}[0]
63-
open fun plus(androidx.compose.foundation.layout/PaddingValues): androidx.compose.foundation.layout/PaddingValues // androidx.compose.foundation.layout/PaddingValues.plus|plus(androidx.compose.foundation.layout.PaddingValues){}[0]
6462

6563
final class Absolute : androidx.compose.foundation.layout/PaddingValues { // androidx.compose.foundation.layout/PaddingValues.Absolute|null[0]
6664
constructor <init>(androidx.compose.ui.unit/Dp = ..., androidx.compose.ui.unit/Dp = ..., androidx.compose.ui.unit/Dp = ..., androidx.compose.ui.unit/Dp = ...) // androidx.compose.foundation.layout/PaddingValues.Absolute.<init>|<init>(androidx.compose.ui.unit.Dp;androidx.compose.ui.unit.Dp;androidx.compose.ui.unit.Dp;androidx.compose.ui.unit.Dp){}[0]

compose/foundation/foundation-layout/src/androidDeviceTest/kotlin/androidx/compose/foundation/layout/PaddingValuesTest.kt

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import androidx.compose.ui.Modifier
1919
import androidx.compose.ui.layout.onPlaced
2020
import androidx.compose.ui.platform.LocalDensity
2121
import androidx.compose.ui.test.junit4.createComposeRule
22-
import androidx.compose.ui.unit.LayoutDirection
23-
import androidx.compose.ui.unit.dp
2422
import com.google.common.truth.Truth.assertThat
2523
import kotlinx.coroutines.test.StandardTestDispatcher
2624
import org.junit.Rule
@@ -53,75 +51,4 @@ class PaddingValuesTest {
5351
assertThat(height).isEqualTo(100)
5452
}
5553
}
56-
57-
@Test
58-
fun paddingValuesAdditionOperation() {
59-
val p1 = PaddingValues(start = 1.dp, top = 2.dp, end = 3.dp, bottom = 4.dp)
60-
val p2 = PaddingValues(start = 5.dp, top = 6.dp, end = 7.dp, bottom = 8.dp)
61-
val result = p1 + p2
62-
63-
assertThat(result.calculateLeftPadding(LayoutDirection.Ltr)).isEqualTo(1.dp + 5.dp)
64-
assertThat(result.calculateRightPadding(LayoutDirection.Ltr)).isEqualTo(3.dp + 7.dp)
65-
assertThat(result.calculateTopPadding()).isEqualTo(2.dp + 6.dp)
66-
assertThat(result.calculateBottomPadding()).isEqualTo(4.dp + 8.dp)
67-
assertThat(result.calculateLeftPadding(LayoutDirection.Rtl)).isEqualTo(3.dp + 7.dp)
68-
assertThat(result.calculateRightPadding(LayoutDirection.Rtl)).isEqualTo(1.dp + 5.dp)
69-
}
70-
71-
@Test
72-
fun paddingValuesSubtractionOperation() {
73-
val p1 = PaddingValues(start = 5.dp, top = 6.dp, end = 7.dp, bottom = 8.dp)
74-
val p2 = PaddingValues(start = 1.dp, top = 2.dp, end = 3.dp, bottom = 4.dp)
75-
val result = p1 - p2
76-
77-
assertThat(result.calculateLeftPadding(LayoutDirection.Ltr)).isEqualTo(5.dp - 1.dp)
78-
assertThat(result.calculateRightPadding(LayoutDirection.Ltr)).isEqualTo(7.dp - 3.dp)
79-
assertThat(result.calculateTopPadding()).isEqualTo(6.dp - 2.dp)
80-
assertThat(result.calculateBottomPadding()).isEqualTo(8.dp - 4.dp)
81-
assertThat(result.calculateLeftPadding(LayoutDirection.Rtl)).isEqualTo(7.dp - 3.dp)
82-
assertThat(result.calculateRightPadding(LayoutDirection.Rtl)).isEqualTo(5.dp - 1.dp)
83-
}
84-
85-
@Test
86-
fun paddingValuesSubtraction_coercesToZero() {
87-
val p1 = PaddingValues(start = 1.dp, top = 2.dp, end = 3.dp, bottom = 4.dp)
88-
val p2 = PaddingValues(start = 5.dp, top = 6.dp, end = 7.dp, bottom = 8.dp)
89-
val result = p1 - p2
90-
91-
// Since all paddings of p1 are > than p2, the resultant paddings should be zero
92-
assertThat(result.calculateLeftPadding(LayoutDirection.Ltr)).isEqualTo(0.dp)
93-
assertThat(result.calculateRightPadding(LayoutDirection.Ltr)).isEqualTo(0.dp)
94-
assertThat(result.calculateTopPadding()).isEqualTo(0.dp)
95-
assertThat(result.calculateBottomPadding()).isEqualTo(0.dp)
96-
assertThat(result.calculateLeftPadding(LayoutDirection.Rtl)).isEqualTo(0.dp)
97-
assertThat(result.calculateRightPadding(LayoutDirection.Rtl)).isEqualTo(0.dp)
98-
}
99-
100-
@Test
101-
fun absolutePaddingValuesAddedToPaddingValues() {
102-
val p1 = PaddingValues(start = 10.dp, top = 20.dp, end = 30.dp, bottom = 40.dp)
103-
val p2 = PaddingValues.Absolute(left = 1.dp, top = 2.dp, right = 3.dp, bottom = 4.dp)
104-
val result = p1 + p2
105-
106-
assertThat(result.calculateLeftPadding(LayoutDirection.Ltr)).isEqualTo(10.dp + 1.dp)
107-
assertThat(result.calculateRightPadding(LayoutDirection.Ltr)).isEqualTo(30.dp + 3.dp)
108-
assertThat(result.calculateTopPadding()).isEqualTo(20.dp + 2.dp)
109-
assertThat(result.calculateBottomPadding()).isEqualTo(40.dp + 4.dp)
110-
assertThat(result.calculateLeftPadding(LayoutDirection.Rtl)).isEqualTo(30.dp + 1.dp)
111-
assertThat(result.calculateRightPadding(LayoutDirection.Rtl)).isEqualTo(10.dp + 3.dp)
112-
}
113-
114-
@Test
115-
fun absolutePaddingValuesSubtractedFromPaddingValues() {
116-
val p1 = PaddingValues(start = 10.dp, top = 20.dp, end = 30.dp, bottom = 40.dp)
117-
val p2 = PaddingValues.Absolute(left = 1.dp, top = 2.dp, right = 3.dp, bottom = 4.dp)
118-
val result = p1 - p2
119-
120-
assertThat(result.calculateLeftPadding(LayoutDirection.Ltr)).isEqualTo(10.dp - 1.dp)
121-
assertThat(result.calculateRightPadding(LayoutDirection.Ltr)).isEqualTo(30.dp - 3.dp)
122-
assertThat(result.calculateTopPadding()).isEqualTo(20.dp - 2.dp)
123-
assertThat(result.calculateBottomPadding()).isEqualTo(40.dp - 4.dp)
124-
assertThat(result.calculateLeftPadding(LayoutDirection.Rtl)).isEqualTo(30.dp - 1.dp)
125-
assertThat(result.calculateRightPadding(LayoutDirection.Rtl)).isEqualTo(10.dp - 3.dp)
126-
}
12754
}

compose/foundation/foundation-layout/src/commonMain/kotlin/androidx/compose/foundation/layout/Padding.kt

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package androidx.compose.foundation.layout
1818

19+
import androidx.compose.foundation.layout.PaddingValues.Absolute
1920
import androidx.compose.foundation.layout.internal.requirePrecondition
2021
import androidx.compose.runtime.Immutable
2122
import androidx.compose.runtime.Stable
@@ -29,7 +30,6 @@ import androidx.compose.ui.platform.InspectorInfo
2930
import androidx.compose.ui.unit.Constraints
3031
import androidx.compose.ui.unit.Dp
3132
import androidx.compose.ui.unit.LayoutDirection
32-
import androidx.compose.ui.unit.coerceAtLeast
3333
import androidx.compose.ui.unit.constrainHeight
3434
import androidx.compose.ui.unit.constrainWidth
3535
import androidx.compose.ui.unit.dp
@@ -194,16 +194,6 @@ interface PaddingValues {
194194
/** The padding to be applied along the bottom edge inside a box. */
195195
fun calculateBottomPadding(): Dp
196196

197-
/** Returns a new [PaddingValues] with the sum of this and [other] */
198-
@Stable operator fun plus(other: PaddingValues): PaddingValues = AddedPaddingValues(this, other)
199-
200-
/**
201-
* Returns a new [PaddingValues] with the difference of this and [other], coercing negative
202-
* values to `0.dp`.
203-
*/
204-
@Stable
205-
operator fun minus(other: PaddingValues): PaddingValues = SubtractedPaddingValues(this, other)
206-
207197
/** Describes an absolute (RTL unaware) padding to be applied along the edges inside a box. */
208198
@Immutable
209199
class Absolute(
@@ -342,79 +332,6 @@ internal class PaddingValuesImpl(
342332
override fun toString() = "PaddingValues(start=$start, top=$top, end=$end, bottom=$bottom)"
343333
}
344334

345-
/**
346-
* A [PaddingValues] that is the sum of [first] and [second] as returned after [PaddingValues.plus].
347-
*/
348-
@Immutable
349-
private class AddedPaddingValues(val first: PaddingValues, val second: PaddingValues) :
350-
PaddingValues {
351-
override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp {
352-
return first.calculateLeftPadding(layoutDirection) +
353-
second.calculateLeftPadding(layoutDirection)
354-
}
355-
356-
override fun calculateTopPadding(): Dp {
357-
return first.calculateTopPadding() + second.calculateTopPadding()
358-
}
359-
360-
override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp {
361-
return first.calculateRightPadding(layoutDirection) +
362-
second.calculateRightPadding(layoutDirection)
363-
}
364-
365-
override fun calculateBottomPadding(): Dp {
366-
return first.calculateBottomPadding() + second.calculateBottomPadding()
367-
}
368-
369-
override fun equals(other: Any?): Boolean {
370-
if (other !is AddedPaddingValues) return false
371-
return first == other.first && second == other.second
372-
}
373-
374-
override fun hashCode() = first.hashCode() * 31 + second.hashCode()
375-
376-
override fun toString() = "($first + $second)"
377-
}
378-
379-
/**
380-
* A [PaddingValues] that is the difference of [first] and [second] as returned after
381-
* [PaddingValues.minus]. Paddings will be coerced to 0 if the result is negative.
382-
*/
383-
@Immutable
384-
private class SubtractedPaddingValues(val first: PaddingValues, val second: PaddingValues) :
385-
PaddingValues {
386-
override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp {
387-
return (first.calculateLeftPadding(layoutDirection) -
388-
second.calculateLeftPadding(layoutDirection))
389-
.coerceAtLeast(0.dp)
390-
}
391-
392-
override fun calculateTopPadding(): Dp {
393-
return (first.calculateTopPadding() - second.calculateTopPadding()).coerceAtLeast(0.dp)
394-
}
395-
396-
override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp {
397-
return (first.calculateRightPadding(layoutDirection) -
398-
second.calculateRightPadding(layoutDirection))
399-
.coerceAtLeast(0.dp)
400-
}
401-
402-
override fun calculateBottomPadding(): Dp {
403-
return (first.calculateBottomPadding() - second.calculateBottomPadding()).coerceAtLeast(
404-
0.dp
405-
)
406-
}
407-
408-
override fun equals(other: Any?): Boolean {
409-
if (other !is SubtractedPaddingValues) return false
410-
return first == other.first && second == other.second
411-
}
412-
413-
override fun hashCode() = first.hashCode() * 31 + second.hashCode()
414-
415-
override fun toString() = "($first - $second)"
416-
}
417-
418335
private class PaddingElement(
419336
var start: Dp = 0.dp,
420337
var top: Dp = 0.dp,

0 commit comments

Comments
 (0)