[go: nahoru, domu]

Add IntBounds. Use it in Menus and ui-tooling.

Relnote: An IntBounds unit class has been added, representing integer pixel bounds from layout. The API of PopupPositionProvider has been updated to use it.
Bug: 159596546
Test: IntBoundsTest, PopupTest
Change-Id: I0d8d03c5535c80c6808d8f9ca7a210408890e6e7
diff --git a/ui/ui-unit/api/0.1.0-dev15.txt b/ui/ui-unit/api/0.1.0-dev15.txt
index ddc351f..7fe4172 100644
--- a/ui/ui-unit/api/0.1.0-dev15.txt
+++ b/ui/ui-unit/api/0.1.0-dev15.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/api/current.txt b/ui/ui-unit/api/current.txt
index ddc351f..7fe4172 100644
--- a/ui/ui-unit/api/current.txt
+++ b/ui/ui-unit/api/current.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/api/public_plus_experimental_0.1.0-dev15.txt b/ui/ui-unit/api/public_plus_experimental_0.1.0-dev15.txt
index ddc351f..7fe4172 100644
--- a/ui/ui-unit/api/public_plus_experimental_0.1.0-dev15.txt
+++ b/ui/ui-unit/api/public_plus_experimental_0.1.0-dev15.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/api/public_plus_experimental_current.txt b/ui/ui-unit/api/public_plus_experimental_current.txt
index ddc351f..7fe4172 100644
--- a/ui/ui-unit/api/public_plus_experimental_current.txt
+++ b/ui/ui-unit/api/public_plus_experimental_current.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/api/restricted_0.1.0-dev15.txt b/ui/ui-unit/api/restricted_0.1.0-dev15.txt
index 293c6ed..a9c734a 100644
--- a/ui/ui-unit/api/restricted_0.1.0-dev15.txt
+++ b/ui/ui-unit/api/restricted_0.1.0-dev15.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/api/restricted_current.txt b/ui/ui-unit/api/restricted_current.txt
index 293c6ed..a9c734a 100644
--- a/ui/ui-unit/api/restricted_current.txt
+++ b/ui/ui-unit/api/restricted_current.txt
@@ -259,6 +259,29 @@
     field public static final long SecondsPerMinute = 60L; // 0x3cL
   }
 
+  @androidx.compose.Immutable public final class IntBounds {
+    ctor public IntBounds(int left, int top, int right, int bottom);
+    method public int component1();
+    method public int component2();
+    method public int component3();
+    method public int component4();
+    method @androidx.compose.Immutable public androidx.ui.unit.IntBounds copy(int left, int top, int right, int bottom);
+    method public int getBottom();
+    method public int getLeft();
+    method public int getRight();
+    method public int getTop();
+  }
+
+  public final class IntBoundsKt {
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntBounds IntBounds(androidx.ui.unit.IntOffset topLeft, androidx.ui.unit.IntSize size);
+    method @androidx.compose.Stable public static inline androidx.ui.unit.IntOffset center(androidx.ui.unit.IntBounds);
+    method public static inline int getHeight(androidx.ui.unit.IntBounds);
+    method public static inline int getWidth(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntBounds toBounds(androidx.ui.unit.IntSize);
+    method @androidx.compose.Stable public static androidx.ui.geometry.Rect toRect(androidx.ui.unit.IntBounds);
+    method @androidx.compose.Stable public static androidx.ui.unit.IntSize toSize(androidx.ui.unit.IntBounds);
+  }
+
   @androidx.compose.Immutable public final class IntOffset {
     ctor public IntOffset(internal long value);
     method @androidx.compose.Stable public inline operator int component1();
diff --git a/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/IntBounds.kt b/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/IntBounds.kt
new file mode 100644
index 0000000..f2dd669
--- /dev/null
+++ b/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/IntBounds.kt
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+@file:Suppress("NOTHING_TO_INLINE")
+
+package androidx.ui.unit
+
+import androidx.compose.Immutable
+import androidx.compose.Stable
+import androidx.ui.geometry.Rect
+
+/**
+ * A four dimensional bounds holder defined by integer pixels.
+ */
+@Immutable
+data class IntBounds(
+    val left: Int,
+    val top: Int,
+    val right: Int,
+    val bottom: Int
+)
+
+@Stable
+inline fun IntBounds(topLeft: IntOffset, size: IntSize) =
+    IntBounds(
+        left = topLeft.x,
+        top = topLeft.y,
+        right = topLeft.x + size.width,
+        bottom = topLeft.y + size.height
+    )
+
+/**
+ * The width of this IntBounds in integer pixels.
+ */
+@Stable
+inline val IntBounds.width: Int get() = right - left
+
+/**
+ * The height of this IntBounds in integer pixels.
+ */
+@Stable
+inline val IntBounds.height: Int get() = bottom - top
+
+/**
+ * Returns the [IntOffset] of the center of the [IntBounds].
+ */
+@Stable
+inline fun IntBounds.center(): IntOffset {
+    return IntOffset((left + right) / 2, (top + bottom) / 2)
+}
+
+/**
+ * Convert an [IntBounds] to an [IntSize].
+ */
+@Stable
+fun IntBounds.toSize(): IntSize {
+    return IntSize(width, height)
+}
+
+/**
+ * Convert an [IntSize] to an [IntBounds]. The left and top are 0 and the right and bottom
+ * are the width and height, respectively.
+ */
+@Stable
+fun IntSize.toBounds(): IntBounds {
+    return IntBounds(0, 0, width, height)
+}
+
+/**
+ * Convert an [IntBounds] to a [Rect].
+ */
+@Stable
+fun IntBounds.toRect(): Rect {
+    return Rect(
+        left.toFloat(),
+        top.toFloat(),
+        right.toFloat(),
+        bottom.toFloat()
+    )
+}
\ No newline at end of file
diff --git a/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/Px.kt b/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/Px.kt
index 9029a7c..58d04fa 100644
--- a/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/Px.kt
+++ b/ui/ui-unit/src/commonMain/kotlin/androidx/ui/unit/Px.kt
@@ -259,7 +259,7 @@
  */
 @Stable
 inline fun PxBounds.center(): Offset {
-    return Offset(left + width / 2f, top + height / 2f)
+    return Offset((left + right) / 2f, (top + bottom) / 2f)
 }
 
 /**
diff --git a/ui/ui-unit/src/test/kotlin/androidx/ui/unit/IntBoundsTest.kt b/ui/ui-unit/src/test/kotlin/androidx/ui/unit/IntBoundsTest.kt
new file mode 100644
index 0000000..e8c5d74
--- /dev/null
+++ b/ui/ui-unit/src/test/kotlin/androidx/ui/unit/IntBoundsTest.kt
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package androidx.ui.unit
+
+import org.junit.Assert
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(JUnit4::class)
+class IntBoundsTest {
+    @Test
+    fun boundsWidth() {
+        val bounds = IntBounds(10, 5, 25, 15)
+        Assert.assertEquals(15, bounds.width)
+    }
+
+    @Test
+    fun boundsHeight() {
+        val bounds = IntBounds(10, 5, 25, 15)
+        Assert.assertEquals(10, bounds.height)
+    }
+
+    @Test
+    fun toBounds() {
+        val size = IntSize(15, 10)
+        val bounds = IntBounds(0, 0, 15, 10)
+        Assert.assertEquals(bounds, size.toBounds())
+    }
+
+    @Test
+    fun toSize() {
+        val size = IntSize(15, 10)
+        val bounds = IntBounds(10, 5, 25, 15)
+        Assert.assertEquals(size, bounds.toSize())
+    }
+}
\ No newline at end of file