-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Dialogs.kt
71 lines (66 loc) · 2 KB
/
Dialogs.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package util
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.AwtWindow
import androidx.compose.ui.window.FrameWindowScope
import androidx.compose.ui.window.WindowScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.swing.Swing
import java.awt.FileDialog
import java.io.File
import java.nio.file.Path
import javax.swing.JOptionPane
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun FrameWindowScope.FileDialog(
title: String,
isLoad: Boolean,
onResult: (result: Path?) -> Unit
) = AwtWindow(
create = {
object : FileDialog(window, "Choose a file", if (isLoad) LOAD else SAVE) {
override fun setVisible(value: Boolean) {
super.setVisible(value)
if (value) {
if (file != null) {
onResult(File(directory).resolve(file).toPath())
} else {
onResult(null)
}
}
}
}.apply {
this.title = title
}
},
dispose = FileDialog::dispose
)
@Composable
fun WindowScope.YesNoCancelDialog(
title: String,
message: String,
onResult: (result: AlertDialogResult) -> Unit
) {
DisposableEffect(Unit) {
val job = GlobalScope.launch(Dispatchers.Swing) {
val resultInt = JOptionPane.showConfirmDialog(
window, message, title, JOptionPane.YES_NO_CANCEL_OPTION
)
val result = when (resultInt) {
JOptionPane.YES_OPTION -> AlertDialogResult.Yes
JOptionPane.NO_OPTION -> AlertDialogResult.No
else -> AlertDialogResult.Cancel
}
onResult(result)
}
onDispose {
job.cancel()
}
}
}
enum class AlertDialogResult {
Yes, No, Cancel
}