Replies: 1 comment 7 replies
-
I would suggest using LWJGL and either use Java bindings to Native File Dialog or Tiny File Dialogs. These libraries can guarantee that you are getting the truly native apis provided by the operating systems themselves. Below is the code that I use in my Compose for Desktop application to open a native file dialog and return a file (using Tiny File Dialogs): /**
* Displays a file open dialog.
*
* @param title the dialog title or {@code NULL}
* @param defaultPathAndFile the default path and/or file or {@code NULL}
* @param filterPatterns an array of file type patterns ({@code NULL} or {"*.jpg","*.png"}
* @param singleFilterDescription {@code NULL} or "image files"
* @param allowMultipleSelects if true, multiple selections are allowed
*
* @return the file(s) selected or {@code NULL} on cancel. In case of multiple files, the separator is '|'.
*/
@NativeType("char const *")
fun openTinyFileDialog(
@NativeType("char const *") title: CharSequence?,
@NativeType("char const *") defaultPathAndFile: CharSequence?,
@NativeType("char const * const *") filterPatterns: PointerBuffer?,
@NativeType("char const *") singleFilterDescription: CharSequence?,
@NativeType("int") allowMultipleSelects: Boolean
): String? {
val stack = stackGet()
val stackPointer = stack.pointer
return try {
stack.nUTF8Safe(title, true)
val titleEncoded = if (title == null) NULL else stack.pointerAddress
stack.nUTF8Safe(defaultPathAndFile, true)
val defaultPathAndFileEncoded = if (defaultPathAndFile == null) NULL else stack.pointerAddress
stack.nUTF8Safe(singleFilterDescription, true)
val singleFilterDescriptionEncoded = if (singleFilterDescription == null) NULL else stack.pointerAddress
val result = ntinyfd_openFileDialog(
titleEncoded,
defaultPathAndFileEncoded,
remainingSafe(filterPatterns),
memAddressSafe(filterPatterns),
singleFilterDescriptionEncoded,
if (allowMultipleSelects) 1 else 0
)
memUTF8Safe(result)
} finally {
stack.pointer = stackPointer
}
}
fun openFileDialogAndGetResult(): File? {
openTinyFileDialog(
title = "Open",
defaultPathAndFile = null,
filterPatterns = null,
singleFilterDescription = null,
allowMultipleSelects = false
).also {
return if (it != null) File(it) else null
}
} |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I have just started to develop in Kotlin with this framework a software project that requires some native dialog boxes. After searching on Google and in the main README, I understand that this framework does not include system dialogs (messages, confirmations, file selection, ...).
So I'm here to suggest to integrate them, because for a desktop application, but even for a mobile application, it can be useful in many cases. In my case, to select files as input, then save the result to the output file, with a confirmation before overwriting an existing file.
Beta Was this translation helpful? Give feedback.
All reactions