Skip to content

Instantly share code, notes, and snippets.

@zach-klippenstein
Last active November 5, 2024 18:40
Show Gist options
  • Save zach-klippenstein/07a77c643ecf71be11c8fa3fab14e351 to your computer and use it in GitHub Desktop.
Save zach-klippenstein/07a77c643ecf71be11c8fa3fab14e351 to your computer and use it in GitHub Desktop.
Some fake modifiers to teach Compose's centering idiom.
@file:Suppress("NOTHING_TO_INLINE", "UnusedReceiverParameter")
@file:SuppressLint("ModifierFactoryUnreferencedReceiver")
import android.annotation.SuppressLint
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.ui.Modifier
import kotlin.DeprecationLevel.ERROR
/**
* Fake modifier to teach Compose's centering idiom. In Compose, the way you center something is to
* use [fillMaxSize] to fill the maximum incoming constraints, and then [wrapContentSize] to allow the
* content to be as small as it wants and center it in the available space.
*/
@Deprecated(
"Use fillMaxSize().wrapContentSize() instead.",
ReplaceWith(
"fillMaxSize().wrapContentSize()",
"androidx.compose.foundation.layout.fillMaxSize",
"androidx.compose.foundation.layout.wrapContentSize"
),
level = ERROR
)
public inline fun Modifier.center(): Modifier =
error("Use fillMaxSize().wrapContentSize() instead.")
/**
* Fake modifier to teach Compose's centering idiom. In Compose, the way you center something is to
* use [fillMaxWidth] to fill the maximum incoming constraints, and then [wrapContentWidth] to allow
* the content to be as small as it wants and center it in the available space.
*/
@Deprecated(
"Use fillMaxHeight().wrapContentHeight() instead.",
ReplaceWith(
"fillMaxHeight().wrapContentHeight()",
"androidx.compose.foundation.layout.fillMaxHeight",
"androidx.compose.foundation.layout.wrapContentHeight"
),
level = ERROR
)
public inline fun Modifier.centerVertically(): Modifier =
error("Use fillMaxHeight().wrapContentHeight() instead.")
/**
* Fake modifier to teach Compose's centering idiom. In Compose, the way you center something is to
* use [fillMaxHeight] to fill the maximum incoming constraints, and then [wrapContentHeight] to allow
* the content to be as small as it wants and center it in the available space.
*/
@Deprecated(
"Use fillMaxWidth().wrapContentWidth() instead.",
ReplaceWith(
"fillMaxWidth().wrapContentWidth()",
"androidx.compose.foundation.layout.fillMaxWidth",
"androidx.compose.foundation.layout.wrapContentWidth"
),
level = ERROR
)
public inline fun Modifier.centerHorizontally(): Modifier =
error("Use fillMaxWidth().wrapContentWidth() instead.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment