Created
August 3, 2025 08:53
-
-
Save zerofancy/fbdb081d14ed32be338ecaec4763338d to your computer and use it in GitHub Desktop.
在Compose Desktop中弹出Toast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package top.ntutn.sevenzip.toast | |
| import androidx.compose.runtime.mutableStateListOf | |
| import kotlinx.coroutines.CoroutineScope | |
| import kotlinx.coroutines.delay | |
| import kotlinx.coroutines.launch | |
| class DefaultToastController(private val coroutineScope: CoroutineScope): IToastController { | |
| private val messages = mutableStateListOf<ToastMessage>() | |
| override fun show(text: String, duration: Long) { | |
| coroutineScope.launch { | |
| val toastMessage = ToastMessage(text, duration) | |
| messages.add(toastMessage) | |
| delay(duration) | |
| messages.remove(toastMessage) | |
| } | |
| } | |
| override fun getMessages(): List<ToastMessage> = messages.toList() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package top.ntutn.sevenzip.toast | |
| interface IToastController { | |
| fun show(text: String, duration: Long = 3000) | |
| fun getMessages(): List<ToastMessage> | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ToastHost { | |
| App() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package top.ntutn.sevenzip.toast | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.material3.MaterialTheme | |
| import androidx.compose.material3.Surface | |
| import androidx.compose.material3.Text | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.CompositionLocalProvider | |
| import androidx.compose.runtime.derivedStateOf | |
| import androidx.compose.runtime.getValue | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.runtime.rememberCoroutineScope | |
| import androidx.compose.runtime.staticCompositionLocalOf | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.unit.dp | |
| val LocalToastController = staticCompositionLocalOf<IToastController> { | |
| error("No ToastController Provided. Make sure to wrap your app with ToastHost.") | |
| } | |
| @Composable | |
| fun ToastHost(content: @Composable () -> Unit) { | |
| val coroutineScope = rememberCoroutineScope() | |
| val controller = remember(coroutineScope) { | |
| DefaultToastController(coroutineScope) | |
| } | |
| CompositionLocalProvider(LocalToastController provides controller) { | |
| content() | |
| val messages by remember { derivedStateOf { controller.getMessages() }} | |
| Box( | |
| modifier = Modifier.fillMaxSize(), | |
| contentAlignment = Alignment.BottomCenter | |
| ) { | |
| messages.forEachIndexed { index, message -> | |
| Surface( | |
| shape = MaterialTheme.shapes.medium, | |
| color = Color.Black.copy(alpha = 0.7f), | |
| tonalElevation = 16.dp, | |
| shadowElevation = 16.dp, | |
| modifier = Modifier | |
| .padding(bottom = 24.dp + 48.dp * index) | |
| ) { | |
| Text( | |
| text = message.text, | |
| color = Color.White, | |
| style = MaterialTheme.typography.bodySmall, | |
| modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp) | |
| ) | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package top.ntutn.sevenzip.toast | |
| data class ToastMessage(val text: String, val duration: Long) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Composable | |
| fun DemoUsage(modifier: Modifier) { | |
| val toastController = LocalToastController.current | |
| Button(onClick = { | |
| toastController.show("Hello World!") | |
| }) { | |
| Text("show toast") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment