-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
122 additions
and
47 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
64 changes: 64 additions & 0 deletions
64
src/main/kotlin/github/leavesczy/xlog/decode/ui/FileLoadDialog.kt
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package github.leavesczy.xlog.decode.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.window.AwtWindow | ||
import androidx.compose.ui.window.FrameWindowScope | ||
import kotlinx.coroutines.CompletableDeferred | ||
import java.awt.FileDialog | ||
import java.io.File | ||
import java.nio.file.Path | ||
|
||
@Composable | ||
fun FrameWindowScope.FileLoadDialog( | ||
title: String = "Choose a file", | ||
fileExtension: String? = null, | ||
isMultipleMode: Boolean = false, | ||
onResult: (result: Path?) -> Unit | ||
) = AwtWindow( | ||
create = { | ||
object : FileDialog(window, title, LOAD) { | ||
override fun setVisible(value: Boolean) { | ||
super.setVisible(value) | ||
if (value) { | ||
if (file != null) { | ||
onResult(File(directory).resolve(file).toPath()) | ||
} else { | ||
onResult(null) | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
update = { | ||
it.title = title | ||
it.isMultipleMode = isMultipleMode | ||
it.setFilenameFilter { _, name -> | ||
if (fileExtension.isNullOrBlank()) { | ||
true | ||
} else { | ||
name.endsWith(suffix = fileExtension) | ||
} | ||
} | ||
}, | ||
dispose = FileDialog::dispose | ||
) | ||
|
||
class DialogState<T> { | ||
|
||
private var onResult: CompletableDeferred<T>? by mutableStateOf(null) | ||
|
||
val isAwaiting get() = onResult != null | ||
|
||
suspend fun awaitResult(): T { | ||
onResult = CompletableDeferred() | ||
val result = onResult!!.await() | ||
onResult = null | ||
return result | ||
} | ||
|
||
fun onResult(result: T) = onResult!!.complete(result) | ||
|
||
} |
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