Skip to content

Commit

Permalink
🔀 Merge pull request #65 from Snd-R/main
Browse files Browse the repository at this point in the history
Xdg Desktop FileChooser Portal
  • Loading branch information
vinceglb authored Jul 18, 2024
2 parents 877b7b3 + 3aa9df0 commit 7c1f9e3
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 40 deletions.
2 changes: 2 additions & 0 deletions filekit-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ kotlin {

jvmMain.dependencies {
implementation(libs.jna)
implementation(libs.dbus.java.core)
implementation(libs.dbus.java.transport.native.unixsocket)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.github.vinceglb.filekit.core

import io.github.vinceglb.filekit.core.platform.PlatformFilePicker
import io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver
import io.github.vinceglb.filekit.core.platform.util.Platform
import io.github.vinceglb.filekit.core.platform.util.PlatformUtil
import io.github.vinceglb.filekit.core.platform.xdg.XdgFilePickerPortal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

Expand Down Expand Up @@ -63,7 +63,7 @@ public actual object FileKit {
public actual fun isDirectoryPickerSupported(): Boolean = when (PlatformUtil.current) {
Platform.MacOS -> true
Platform.Windows -> true
Platform.Linux -> false
Platform.Linux -> PlatformFilePicker.current is XdgFilePickerPortal
}

public actual suspend fun saveFile(
Expand All @@ -73,13 +73,14 @@ public actual object FileKit {
initialDirectory: String?,
platformSettings: FileKitPlatformSettings?,
): PlatformFile? = withContext(Dispatchers.IO) {
AwtFileSaver.saveFile(
val result = PlatformFilePicker.current.saveFile(
bytes = bytes,
baseName = baseName,
extension = extension,
initialDirectory = initialDirectory,
parentWindow = platformSettings?.parentWindow,
)
result?.let { PlatformFile(result) }
}

public actual suspend fun isSaveFileWithoutBytesSupported(): Boolean = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
package io.github.vinceglb.filekit.core.platform

import io.github.vinceglb.filekit.core.platform.awt.AwtFilePicker
import io.github.vinceglb.filekit.core.platform.awt.AwtFileSaver
import io.github.vinceglb.filekit.core.platform.mac.MacOSFilePicker
import io.github.vinceglb.filekit.core.platform.util.Platform
import io.github.vinceglb.filekit.core.platform.util.PlatformUtil
import io.github.vinceglb.filekit.core.platform.windows.WindowsFilePicker
import io.github.vinceglb.filekit.core.platform.xdg.XdgFilePickerPortal
import java.awt.Window
import java.io.File

internal interface PlatformFilePicker {
suspend fun pickFile(
initialDirectory: String?,
fileExtensions: List<String>?,
title: String?,
parentWindow: Window?,
): File?

suspend fun pickFiles(
initialDirectory: String?,
fileExtensions: List<String>?,
title: String?,
parentWindow: Window?,
): List<File>?

fun pickDirectory(
initialDirectory: String?,
title: String?,
parentWindow: Window?,
): File?

companion object {
val current: PlatformFilePicker by lazy { createPlatformFilePicker() }

private fun createPlatformFilePicker(): PlatformFilePicker {
return when (PlatformUtil.current) {
Platform.MacOS -> MacOSFilePicker()
Platform.Windows -> WindowsFilePicker()
Platform.Linux -> AwtFilePicker()
}
}
}
suspend fun pickFile(
initialDirectory: String?,
fileExtensions: List<String>?,
title: String?,
parentWindow: Window?,
): File?

suspend fun pickFiles(
initialDirectory: String?,
fileExtensions: List<String>?,
title: String?,
parentWindow: Window?,
): List<File>?

suspend fun pickDirectory(
initialDirectory: String?,
title: String?,
parentWindow: Window?,
): File?

suspend fun saveFile(
bytes: ByteArray?,
baseName: String,
extension: String,
initialDirectory: String?,
parentWindow: Window?,
): File? = AwtFileSaver.saveFile(
bytes = bytes,
baseName = baseName,
extension = extension,
initialDirectory = initialDirectory,
parentWindow = parentWindow,
)


companion object {
val current: PlatformFilePicker by lazy { createPlatformFilePicker() }

private fun createPlatformFilePicker(): PlatformFilePicker {
return when (PlatformUtil.current) {
Platform.MacOS -> MacOSFilePicker()
Platform.Windows -> WindowsFilePicker()
Platform.Linux -> {
val xdgPortalFilePicker = XdgFilePickerPortal()
if (xdgPortalFilePicker.isAvailable()) xdgPortalFilePicker
else AwtFilePicker()

}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal class AwtFilePicker : PlatformFilePicker {
parentWindow = parentWindow
)

override fun pickDirectory(
override suspend fun pickDirectory(
initialDirectory: String?,
title: String?,
parentWindow: Window?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.vinceglb.filekit.core.platform.awt

import io.github.vinceglb.filekit.core.PlatformFile
import kotlinx.coroutines.suspendCancellableCoroutine
import java.awt.Dialog
import java.awt.FileDialog
Expand All @@ -16,13 +15,13 @@ internal object AwtFileSaver {
extension: String,
initialDirectory: String?,
parentWindow: Window?,
): PlatformFile? = suspendCancellableCoroutine { continuation ->
): File? = suspendCancellableCoroutine { continuation ->
fun handleResult(value: Boolean, files: Array<File>?) {
if (value) {
val file = files?.firstOrNull()?.let { file ->
// Write bytes to file, or create a new file
bytes?.let { file.writeBytes(bytes) } ?: file.createNewFile()
PlatformFile(file)
file
}
continuation.resume(file)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal class MacOSFilePicker : PlatformFilePicker {
)
}

override fun pickDirectory(
override suspend fun pickDirectory(
initialDirectory: String?,
title: String?,
parentWindow: Window?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class WindowsFilePicker : PlatformFilePicker {
.ifEmpty { null }
}

override fun pickDirectory(
override suspend fun pickDirectory(
initialDirectory: String?,
title: String?,
parentWindow: Window?,
Expand Down
Loading

0 comments on commit 7c1f9e3

Please sign in to comment.