Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make coerceEachDimensionAtLeast() public API #240

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions insets/api/insets.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public final class com/google/accompanist/insets/ComposeInsets {
public static final fun ProvideWindowInsets (ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun ProvideWindowInsets (ZZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun coerceEachDimensionAtLeast (Lcom/google/accompanist/insets/Insets;Lcom/google/accompanist/insets/Insets;)Lcom/google/accompanist/insets/Insets;
public static final fun getLocalWindowInsets ()Landroidx/compose/runtime/ProvidableCompositionLocal;
public static final fun imePadding (Landroidx/compose/ui/Modifier;)Landroidx/compose/ui/Modifier;
public static final fun navigationBarsHeight-3ABfNKs (Landroidx/compose/ui/Modifier;F)Landroidx/compose/ui/Modifier;
Expand Down
24 changes: 17 additions & 7 deletions insets/src/main/java/com/google/accompanist/insets/Insets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -503,16 +503,26 @@ private fun Insets.updateFrom(windowInsets: WindowInsetsCompat, type: Int) {
isVisible = windowInsets.isVisible(type)
}

internal fun Insets.coerceEachDimensionAtLeast(other: Insets): Insets {
// Fast path, no need to copy if `this` >= `other`
if (left >= other.left && top >= other.top && right >= other.right && bottom >= other.bottom) {
/**
* Ensures that each dimension is not less than corresponding dimension in the
* specified [minimumValue].
*
* @return this if every dimension is greater than or equal to the corresponding
* dimension value in [minimumValue], otherwise a copy of this with each dimension coerced with the
* corresponding dimension value in [minimumValue].
*/
fun Insets.coerceEachDimensionAtLeast(minimumValue: Insets): Insets {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coerce "each" or "all"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was 50/50 on this tbh, but leaned towards 'each' to indicate that each dimension is coerced seperately.

// Fast path, no need to copy if: this >= minimumValue
if (left >= minimumValue.left && top >= minimumValue.top &&
right >= minimumValue.right && bottom >= minimumValue.bottom
) {
return this
}
return copy(
left = left.coerceAtLeast(other.left),
top = top.coerceAtLeast(other.top),
right = right.coerceAtLeast(other.right),
bottom = bottom.coerceAtLeast(other.bottom),
left = left.coerceAtLeast(minimumValue.left),
top = top.coerceAtLeast(minimumValue.top),
right = right.coerceAtLeast(minimumValue.right),
bottom = bottom.coerceAtLeast(minimumValue.bottom),
)
}

Expand Down