Skip to content

Commit e51d700

Browse files
committed
fix: show JFileChooser dialogs on the Swing EDT
1 parent c77d72d commit e51d700

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

composeApp/src/jvmMain/kotlin/io/github/smiling_pixel/sync/DiaryEntryExport.jvm.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import kotlinx.coroutines.Dispatchers
44
import kotlinx.coroutines.withContext
55
import java.io.File
66
import javax.swing.JFileChooser
7+
import javax.swing.SwingUtilities
78

89
internal actual suspend fun writeDiaryEntryExportFiles(
910
files: List<DiaryEntryExportFile>
@@ -14,12 +15,24 @@ internal actual suspend fun writeDiaryEntryExportFiles(
1415
fileSelectionMode = JFileChooser.DIRECTORIES_ONLY
1516
isAcceptAllFileFilterUsed = false
1617
}
17-
val result = chooser.showSaveDialog(null)
18-
if (result != JFileChooser.APPROVE_OPTION) {
18+
var selectedDirectory: File? = null
19+
var result = JFileChooser.CANCEL_OPTION
20+
val showDialog = Runnable {
21+
result = chooser.showSaveDialog(null)
22+
if (result == JFileChooser.APPROVE_OPTION) {
23+
selectedDirectory = chooser.selectedFile
24+
}
25+
}
26+
if (SwingUtilities.isEventDispatchThread()) {
27+
showDialog.run()
28+
} else {
29+
SwingUtilities.invokeAndWait(showDialog)
30+
}
31+
if (result != JFileChooser.APPROVE_OPTION || selectedDirectory == null) {
1932
return DiaryEntryExportResult.Failure("Diary entry export was cancelled.")
2033
}
2134

22-
val directory: File = chooser.selectedFile
35+
val directory: File = selectedDirectory!!
2336
if (!directory.exists()) {
2437
directory.mkdirs()
2538
}

0 commit comments

Comments
 (0)