Skip to content

Commit

Permalink
refactor: Use FileKit for the FileDialog
Browse files Browse the repository at this point in the history
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
sschuberth committed Jul 20, 2024
1 parent b8b9140 commit afa6bb6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation(libs.bundles.ort)
implementation(libs.bundles.richtext)
implementation(libs.dataTableMaterial)
implementation(libs.fileKit)
implementation(libs.jacksonModuleKotlin)
implementation(libs.kotlinxCoroutinesSwing)
implementation(libs.log4jApiKotlin)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ kotlinPlugin = "2.0.0"
versionsPlugin = "0.51.0"

dataTableMaterial = "0.5.1"
fileKit = "0.7.0"
jackson = "2.17.2"
kotlinxCoroutines = "1.8.1"
log4jApi = "2.23.1"
Expand All @@ -24,6 +25,7 @@ versions = { id = "com.github.ben-manes.versions", version.ref = "versionsPlugin
[libraries]
dataTableMaterial = { module = "com.seanproctor:data-table-material", version.ref = "dataTableMaterial" }
detektFormatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detektPlugin" }
fileKit = { module = "io.github.vinceglb:filekit-core", version.ref = "fileKit" }
jacksonModuleKotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
kotlinxCoroutinesSwing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinxCoroutines" }
log4jApiKotlin = { module = "org.apache.logging.log4j:log4j-api-kotlin", version.ref = "log4jApiKotlin" }
Expand Down
36 changes: 12 additions & 24 deletions src/main/kotlin/composables/FileDialog.kt
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()) }
}

0 comments on commit afa6bb6

Please sign in to comment.