Skip to content

Commit

Permalink
library: Opt dialog & popup
Browse files Browse the repository at this point in the history
* Display dialog in the center when in landscape mode
* Add BackHandler to dialog & popup
  • Loading branch information
YuKongA committed Aug 31, 2024
1 parent 72e0718 commit f64767c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fun main() = application {
onCloseRequest = ::exitApplication,
title = "Miuix",
) {
WindowSizeProvider.init(window)
WindowProvider.init(window)
App()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package top.yukonga.miuix.kmp
import android.annotation.SuppressLint
import android.os.Build
import android.view.RoundedCorner
import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
Expand Down Expand Up @@ -35,4 +36,15 @@ fun getCornerRadiusBottom(): Int {
val context = LocalContext.current
val resourceId = context.resources.getIdentifier("rounded_corner_radius_bottom", "dimen", "android")
return if (resourceId > 0) context.resources.getDimensionPixelSize(resourceId) else 0
}

@Composable
actual fun BackHandler(
dismiss: () -> Unit,
onDismissRequest: () -> Unit
) {
BackHandler {
dismiss()
onDismissRequest()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import top.yukonga.miuix.kmp.basic.MiuixBasicComponent
import top.yukonga.miuix.kmp.basic.MiuixText
import top.yukonga.miuix.kmp.icon.icons.ArrowRight
import top.yukonga.miuix.kmp.icon.MiuixIcons
import top.yukonga.miuix.kmp.icon.icons.ArrowRight
import top.yukonga.miuix.kmp.theme.MiuixTheme

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
Expand All @@ -16,6 +18,7 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import getWindowSize
import top.yukonga.miuix.kmp.basic.MiuixBox
import top.yukonga.miuix.kmp.basic.MiuixText
import top.yukonga.miuix.kmp.theme.MiuixTheme
Expand All @@ -28,6 +31,12 @@ import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
@Composable
expect fun getRoundedCorner(): Dp

@Composable
expect fun BackHandler(
dismiss: () -> Unit,
onDismissRequest: () -> Unit
)

/**
* A dialog with a title, a summary, and a content.
*
Expand All @@ -45,10 +54,16 @@ fun MiuixSuperDialog(
insideMargin: DpSize = DpSize(14.dp, 14.dp),
content: @Composable () -> Unit
) {
val bottomCornerRadius = if (getRoundedCorner() != 0.dp) getRoundedCorner() - insideMargin.width else 32.dp
val bottomCornerRadius = rememberUpdatedState(if (getRoundedCorner() != 0.dp) getRoundedCorner() - insideMargin.width else 32.dp)
val getWindowSize = rememberUpdatedState(getWindowSize())
val contentAlignment = if (getWindowSize.value.width > getWindowSize.value.height) Alignment.Center else Alignment.BottomCenter

BackHandler(
dismiss = { dismissDialog() },
onDismissRequest = onDismissRequest
)

MiuixBox(
contentAlignment = Alignment.BottomCenter,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = insideMargin.width)
Expand All @@ -62,13 +77,14 @@ fun MiuixSuperDialog(
) {
Column(
modifier = Modifier
.then(if (contentAlignment != Alignment.Center) Modifier.fillMaxWidth() else Modifier.widthIn(max = 400.dp))
.pointerInput(Unit) {
detectTapGestures { /* Do nothing to consume the click */ }
}
.fillMaxWidth()
.align(contentAlignment)
.background(
color = MiuixTheme.colorScheme.dropdownBackground,
shape = SquircleShape(bottomCornerRadius)
shape = SquircleShape(bottomCornerRadius.value)
)
.padding(24.dp),
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,17 @@ fun MiuixSuperDropdown(
if (isDropdownExpanded.value) hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)

if (isDropdownExpanded.value) {
BackHandler(
dismiss = { dismissPopup() },
onDismissRequest = { isDropdownExpanded.value = false }
)
showPopup(
content = {
MiuixBox(
modifier = Modifier
.fillMaxSize()
.pointerInput(Unit) {
detectTapGestures(onPress = {
detectTapGestures(onTap = {
dismissPopup()
isDropdownExpanded.value = false
})
Expand Down
4 changes: 2 additions & 2 deletions miuix/src/desktopMain/kotlin/expectUtils.desktop.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.platform.LocalDensity
import kotlin.math.roundToInt

object WindowSizeProvider {
object WindowProvider {
private var composeWindow: ComposeWindow? = null

fun init(window: ComposeWindow) {
Expand All @@ -23,7 +23,7 @@ object WindowSizeProvider {
@Composable
actual fun getWindowSize(): WindowSize {
val density = LocalDensity.current.density
val windowSize = WindowSizeProvider.getWindowSize()
val windowSize = WindowProvider.getWindowSize()
return WindowSize(
width = (windowSize.width * density).roundToInt(),
height = (windowSize.height * density).roundToInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ import androidx.compose.ui.unit.dp

@Composable
actual fun getRoundedCorner(): Dp = 0.dp

@Composable
actual fun BackHandler(
dismiss: () -> Unit,
onDismissRequest: () -> Unit
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ import androidx.compose.ui.unit.dp

@Composable
actual fun getRoundedCorner(): Dp = 0.dp

@Composable
actual fun BackHandler(
dismiss: () -> Unit,
onDismissRequest: () -> Unit
) {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
package top.yukonga.miuix.kmp

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlinx.browser.window
import org.w3c.dom.events.KeyboardEvent

@Composable
actual fun getRoundedCorner(): Dp = 0.dp

@Composable
actual fun BackHandler(
dismiss: () -> Unit,
onDismissRequest: () -> Unit
) {
LaunchedEffect(Unit) {
window.addEventListener("keydown") { event ->
if ((event as KeyboardEvent).key == "Escape") {
dismiss()
onDismissRequest()
}
}
}
}

0 comments on commit f64767c

Please sign in to comment.