-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Use FileKit for the
FileDialog
The fixes the file dialog to actually work on Linux with Wayland. Also see a similar discussion at [1]. For now, only the load dialog is implemented, as saving would require another callback anyway. Fixes #276. [1]: gradle/gradle-client#10 Signed-off-by: Sebastian Schuberth <[email protected]>
- Loading branch information
1 parent
b8b9140
commit afa6bb6
Showing
3 changed files
with
15 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,26 @@ | ||
package org.ossreviewtoolkit.workbench.composables | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.awt.ComposeWindow | ||
import androidx.compose.ui.window.AwtWindow | ||
|
||
import java.awt.FileDialog | ||
import java.io.File | ||
import java.io.FilenameFilter | ||
import io.github.vinceglb.filekit.core.FileKit | ||
import io.github.vinceglb.filekit.core.PickerMode | ||
import io.github.vinceglb.filekit.core.PickerType | ||
|
||
import java.nio.file.Path | ||
|
||
import kotlinx.coroutines.runBlocking | ||
|
||
@Composable | ||
fun FileDialog( | ||
title: String, | ||
isLoad: Boolean, | ||
fileExtensionFilter: List<String> = emptyList(), | ||
onResult: (result: Path?) -> Unit | ||
) = AwtWindow( | ||
create = { | ||
object : FileDialog(ComposeWindow(), title, if (isLoad) LOAD else SAVE) { | ||
override fun setVisible(value: Boolean) { | ||
super.setVisible(value) | ||
) { | ||
require(isLoad) | ||
|
||
if (value && directory != null && file != null) { | ||
onResult(File(directory).resolve(file).toPath()) | ||
} | ||
} | ||
}.apply { | ||
this.title = title | ||
val fileType = PickerType.File(fileExtensionFilter) | ||
val pickedFile = runBlocking { FileKit.pickFile(fileType, PickerMode.Single, title) } | ||
|
||
if (fileExtensionFilter.isNotEmpty()) { | ||
file = fileExtensionFilter.joinToString(";") { "*.$it" } | ||
filenameFilter = FilenameFilter { _, name -> name.substringAfterLast(".") in fileExtensionFilter } | ||
} | ||
} | ||
}, | ||
dispose = FileDialog::dispose | ||
) | ||
pickedFile?.run { onResult(file.toPath()) } | ||
} |