-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from BioforestChain/nativeFileChooser
feat: add native file chooser
- Loading branch information
Showing
11 changed files
with
119 additions
and
62 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
42 changes: 42 additions & 0 deletions
42
...bview/src/desktopMain/kotlin/org/dweb_browser/dwebview/engine/setupFileChooser.desktop.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,42 @@ | ||
package org.dweb_browser.dwebview.engine | ||
|
||
import com.teamdev.jxbrowser.browser.callback.OpenFileCallback | ||
import com.teamdev.jxbrowser.browser.callback.OpenFilesCallback | ||
import com.teamdev.jxbrowser.browser.callback.OpenFolderCallback | ||
import kotlinx.coroutines.launch | ||
import org.dweb_browser.sys.filechooser.FileChooserManage | ||
import java.nio.file.Path | ||
|
||
fun setupFileChooser(engine: DWebViewEngine) { | ||
val fileChooserManage = FileChooserManage() | ||
engine.browser.set(OpenFileCallback::class.java, OpenFileCallback { params, tell -> | ||
val acceptableExtensions: List<String> = params.acceptableExtensions() | ||
|
||
engine.lifecycleScope.launch { | ||
val pickerFiles = | ||
fileChooserManage.openFileChooser(engine.remoteMM, acceptableExtensions, false) | ||
pickerFiles.firstOrNull()?.let { | ||
tell.open(Path.of(it)) | ||
} ?: tell.cancel() | ||
} | ||
}) | ||
|
||
engine.browser.set(OpenFilesCallback::class.java, OpenFilesCallback { params, tell -> | ||
val acceptableExtensions: List<String> = params.acceptableExtensions() | ||
engine.lifecycleScope.launch { | ||
val pickerFiles = | ||
fileChooserManage.openFileChooser(engine.remoteMM, acceptableExtensions, true) | ||
|
||
if (pickerFiles.isEmpty()) tell.cancel() else | ||
tell.open(*pickerFiles.map { Path.of(it) }.toTypedArray()) | ||
} | ||
}) | ||
|
||
engine.browser.set(OpenFolderCallback::class.java, OpenFolderCallback { params, tell -> | ||
engine.lifecycleScope.launch { | ||
val folder = fileChooserManage.openFolderChooser(params.suggestedDirectory()) | ||
|
||
if (folder.isEmpty()) tell.cancel() else tell.open(Path.of(folder)) | ||
} | ||
}) | ||
} |
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
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
73 changes: 44 additions & 29 deletions
73
.../sys/src/desktopMain/kotlin/org/dweb_browser/sys/filechooser/FileChooserManage.desktop.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 |
---|---|---|
@@ -1,41 +1,56 @@ | ||
package org.dweb_browser.sys.filechooser | ||
|
||
import io.github.vinceglb.filekit.core.FileKit | ||
import io.github.vinceglb.filekit.core.PickerMode | ||
import io.github.vinceglb.filekit.core.PickerType | ||
import io.github.vinceglb.filekit.core.pickFile | ||
import org.dweb_browser.core.module.MicroModule | ||
import org.dweb_browser.helper.platform.awaitComposeWindow | ||
import java.io.File | ||
import javax.swing.JFileChooser | ||
import javax.swing.filechooser.FileFilter | ||
|
||
actual class FileChooserManage actual constructor() { | ||
actual suspend fun openFileChooser( | ||
microModule: MicroModule.Runtime, | ||
accept: String, | ||
multiple: Boolean, | ||
limit: Int | ||
multiple: Boolean | ||
): List<String> { | ||
return when (val composeWindow = microModule.awaitComposeWindow()) { | ||
null -> emptyList() | ||
else -> { | ||
val fc = JFileChooser(); | ||
fc.isMultiSelectionEnabled = multiple | ||
val fileNameFilter = acceptToNameFilter(accept) | ||
fc.addChoosableFileFilter(object : FileFilter() { | ||
override fun accept(file: File): Boolean { | ||
return fileNameFilter(file.name) | ||
} | ||
|
||
override fun getDescription(): String { | ||
return accept | ||
} | ||
}) | ||
when (fc.showOpenDialog(composeWindow)) { | ||
JFileChooser.APPROVE_OPTION -> { | ||
fc.selectedFiles.map { it.absolutePath } | ||
} | ||
|
||
else -> emptyList() | ||
} | ||
} | ||
val pickerType = when (accept) { | ||
"image/*" -> PickerType.Image | ||
"video/*" -> PickerType.Video | ||
else -> PickerType.File() | ||
} | ||
|
||
if (multiple) { | ||
val pickFiles = FileKit.pickFile(pickerType, mode = PickerMode.Multiple()) | ||
|
||
return pickFiles?.map { | ||
it.file.absolutePath | ||
} ?: emptyList() | ||
} else { | ||
val pickFile = FileKit.pickFile(pickerType) | ||
|
||
return pickFile?.file?.absolutePath?.let { listOf(it) } ?: emptyList() | ||
} | ||
} | ||
|
||
suspend fun openFileChooser( | ||
microModule: MicroModule.Runtime, | ||
accept: List<String>, | ||
multiple: Boolean | ||
): List<String> { | ||
val pickerType = PickerType.File(extensions = accept) | ||
|
||
if (multiple) { | ||
val pickFiles = FileKit.pickFile(pickerType, mode = PickerMode.Multiple()) | ||
|
||
return pickFiles?.map { | ||
it.file.absolutePath | ||
} ?: emptyList() | ||
} else { | ||
val pickFile = FileKit.pickFile(pickerType) | ||
|
||
return pickFile?.file?.absolutePath?.let { listOf(it) } ?: emptyList() | ||
} | ||
} | ||
|
||
suspend fun openFolderChooser(initialDirectory: String?) = | ||
FileKit.pickDirectory(initialDirectory = initialDirectory)?.file?.absolutePath ?: "" | ||
} |
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