-
Notifications
You must be signed in to change notification settings - Fork 18
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 #65 from Snd-R/main
Xdg Desktop FileChooser Portal
- Loading branch information
Showing
9 changed files
with
292 additions
and
40 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
84 changes: 53 additions & 31 deletions
84
...it-core/src/jvmMain/kotlin/io/github/vinceglb/filekit/core/platform/PlatformFilePicker.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,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() | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.