A Kotlin/Java wrapper around the small C library by Bernard Teo (btzy/nativefiledialog-extended). As a fallback it uses default AWT file picker.
A library is a perfect fit for the Jetpack Compose for Desktop.
Supported platforms:
- Windows;
- MacOS;
- Linux (GTK).
repositories {
mavenCentral()
}
dependencies {
implementation("tv.wunderbox:nativefiledialog:$nativeFileDialogVersion")
}
interface FileDialog {
fun save(
filters: List<Filter> = emptyList(),
defaultPath: String? = null,
defaultName: String? = null,
): FileDialogResult<File>
fun pickFile(
filters: List<Filter> = emptyList(),
defaultPath: String? = null,
): FileDialogResult<File>
fun pickFileMany(
filters: List<Filter> = emptyList(),
defaultPath: String? = null,
): FileDialogResult<List<File>>
fun pickDirectory(
defaultPath: String? = null,
): FileDialogResult<File>
}
val fileDialog = FileDialog.default(awtComponent)
val result = fileDialog.pickFile() // returns FileDialogResult<File>
when (result) {
is FileDialogResult.Success -> {
val file = result.value
// Handle success
}
is FileDialogResult.Failure -> {
// Handle failure
}
}
Internally there are two different implementations available,
instead of using the default NfdFileDialog
with AwtFileDialog
as
a fallback, you can use only one:
val nfdFileDialog = NfdFileDialog()
val awtFileDialog = AwtFileDialog(awtComponent)
When using Kotlin Compose Multiplatform the awtComponent
variable is a ComposeWindow
object that might be obtained from androidx.compose.ui.window.WindowScope
.