
Bridges the gap between multiplatform and platform-specific libraries, offering unified APIs for AlertDialog and DropdownMenu, enabling consistent code across platforms without modifying stable APIs.
compose-mpp is a Kotlin Multiplatform library that fills the gap between MPP and platform-specific Jetpack Compose + Compose for Desktop JVM libraries. Namely, compose-mpp provides the following Compose API:
Short answer: no.
A bit more details: Android Jetpack Compose provides the Android implementation, and JetBrains provides the Desktop implementation. But they are incompatible. So if we'd like to write the same code in commonMain, we have to create by our own.
Full details: Okay, it's quite a lot of source code that you might want to skip to the next section, but anyways. Here are how AlertDialog() composables are defined on each platform (Android, Desktop):
| Android | Desktop |
|---|---|
|
|
In Android version, properties argument is of androidx.compose.ui.window.DialogProperties type, which is defined in androidx.compose.ui.window package.
@Immutable
class DialogProperties @ExperimentalComposeUiApi constructor(
val dismissOnBackPress: Boolean = true,
val dismissOnClickOutside: Boolean = true,
val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
@Suppress("EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET")
@get:ExperimentalComposeUiApi
val usePlatformDefaultWidth: Boolean = true
)
Those dismissOnBackPress, dismissOnClickOutside, securePolicy properties do not make sense on Desktop. Therefore it is not even imported in its desktopMain.
What JetBrains uses on Desktop implementation instead is dialogProvider argument of AlertDialogProvider interface, which is its own type:
@ExperimentalMaterialApi
interface AlertDialogProvider {
@Composable
fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
)
}
@ExperimentalMaterialApi
object PopupAlertDialogProvider : AlertDialogProvider {
@Composable
override fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
) {
Popup(
alignment = Alignment.Center,
focusable = true,
onDismissRequest = onDismissRequest,
) {
Surface(elevation = 24.dp) {
content()
}
}
}
}
They are however only the details in the optional argument for each platform that we would quite often like to skip.
DropdownMenu is under similar status, but I would not focus on it now. You can investigate it by yourselves.
Recap: we have to create by our own AlertDialog.
There is an existing GitHub issue for this.
I don't think it is fixable within Compose for Desktop.
First of all, it is not about defining a new AlertDialog() composable in commonMain. It is usually done as in expect/actual implementation switches, but Android version is not defined as such. And since Jetpack Compose (Android) API is stable, it cannot be changed. It is not defined by JetBrains anyways.
Therefore, we cannot define the same AlertDialog() composables in commonMain.
IF the future Kotlin compilers did not require actual keyword and automatically propagate references to platform specific implementation, then it might be possible to make it MPP-ready. But it's not at the moment (and I'm quite unsure if it is doable or desirable either).
It is doable in an arbitrary external Maven artifact, but not in androidx compose packages.
The API is so far almost identical to Jetpack Compose, except that you have to change the package in the type references in your code from androidx.compose.material to dev.atsushieno.composempp.material etc.
Here is the build.gradle.kts (or build.gradle) change to make (most likely within kotlin { soruceSets { val commonMain by getting { dependencies { ... } } } } section):
implementation("dev.atsushieno:compose-mpp:+") // replace with specific version
The undocumented API reference is available at: https://atsushieno.github.io/compose-mpp/
compose-mpp is available under the Apache V2 License.
compose-mpp is a Kotlin Multiplatform library that fills the gap between MPP and platform-specific Jetpack Compose + Compose for Desktop JVM libraries. Namely, compose-mpp provides the following Compose API:
Short answer: no.
A bit more details: Android Jetpack Compose provides the Android implementation, and JetBrains provides the Desktop implementation. But they are incompatible. So if we'd like to write the same code in commonMain, we have to create by our own.
Full details: Okay, it's quite a lot of source code that you might want to skip to the next section, but anyways. Here are how AlertDialog() composables are defined on each platform (Android, Desktop):
| Android | Desktop |
|---|---|
|
|
In Android version, properties argument is of androidx.compose.ui.window.DialogProperties type, which is defined in androidx.compose.ui.window package.
@Immutable
class DialogProperties @ExperimentalComposeUiApi constructor(
val dismissOnBackPress: Boolean = true,
val dismissOnClickOutside: Boolean = true,
val securePolicy: SecureFlagPolicy = SecureFlagPolicy.Inherit,
@Suppress("EXPERIMENTAL_ANNOTATION_ON_WRONG_TARGET")
@get:ExperimentalComposeUiApi
val usePlatformDefaultWidth: Boolean = true
)
Those dismissOnBackPress, dismissOnClickOutside, securePolicy properties do not make sense on Desktop. Therefore it is not even imported in its desktopMain.
What JetBrains uses on Desktop implementation instead is dialogProvider argument of AlertDialogProvider interface, which is its own type:
@ExperimentalMaterialApi
interface AlertDialogProvider {
@Composable
fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
)
}
@ExperimentalMaterialApi
object PopupAlertDialogProvider : AlertDialogProvider {
@Composable
override fun AlertDialog(
onDismissRequest: () -> Unit,
content: @Composable () -> Unit
) {
Popup(
alignment = Alignment.Center,
focusable = true,
onDismissRequest = onDismissRequest,
) {
Surface(elevation = 24.dp) {
content()
}
}
}
}
They are however only the details in the optional argument for each platform that we would quite often like to skip.
DropdownMenu is under similar status, but I would not focus on it now. You can investigate it by yourselves.
Recap: we have to create by our own AlertDialog.
There is an existing GitHub issue for this.
I don't think it is fixable within Compose for Desktop.
First of all, it is not about defining a new AlertDialog() composable in commonMain. It is usually done as in expect/actual implementation switches, but Android version is not defined as such. And since Jetpack Compose (Android) API is stable, it cannot be changed. It is not defined by JetBrains anyways.
Therefore, we cannot define the same AlertDialog() composables in commonMain.
IF the future Kotlin compilers did not require actual keyword and automatically propagate references to platform specific implementation, then it might be possible to make it MPP-ready. But it's not at the moment (and I'm quite unsure if it is doable or desirable either).
It is doable in an arbitrary external Maven artifact, but not in androidx compose packages.
The API is so far almost identical to Jetpack Compose, except that you have to change the package in the type references in your code from androidx.compose.material to dev.atsushieno.composempp.material etc.
Here is the build.gradle.kts (or build.gradle) change to make (most likely within kotlin { soruceSets { val commonMain by getting { dependencies { ... } } } } section):
implementation("dev.atsushieno:compose-mpp:+") // replace with specific version
The undocumented API reference is available at: https://atsushieno.github.io/compose-mpp/
compose-mpp is available under the Apache V2 License.