Skip to content

Commit

Permalink
✨ Add canCreateDirectories option on macOS and JVM macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceglb committed Dec 11, 2024
1 parent 1ea13d1 commit b355568
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public actual suspend fun <Out> FileKit.pickFile(
mode: PickerMode<Out>,
title: String?,
initialDirectory: String?,
platformSettings: FileKitDialogSettings?,
platformSettings: FileKitDialogSettings,
): Out? = callPicker(
mode = when (mode) {
is PickerMode.Single -> Mode.Single
Expand All @@ -27,25 +27,27 @@ public actual suspend fun <Out> FileKit.pickFile(
PickerType.ImageAndVideo -> imageExtensions + videoExtensions
is PickerType.File -> type.extensions
},
platformSettings = platformSettings,
)?.map { PlatformFile(it) }?.let { mode.parseResult(it) }

public actual suspend fun FileKit.pickDirectory(
title: String?,
initialDirectory: String?,
platformSettings: FileKitDialogSettings?,
platformSettings: FileKitDialogSettings,
): PlatformFile? = callPicker(
mode = Mode.Directory,
title = title,
initialDirectory = initialDirectory,
fileExtensions = null
fileExtensions = null,
platformSettings = platformSettings,
)?.firstOrNull()?.let { PlatformFile(it) }

public actual suspend fun FileKit.saveFile(
bytes: ByteArray?,
baseName: String,
extension: String,
initialDirectory: String?,
platformSettings: FileKitDialogSettings?,
platformSettings: FileKitDialogSettings,
): PlatformFile? {
// Create an NSSavePanel
val nsSavePanel = NSSavePanel()
Expand All @@ -58,7 +60,7 @@ public actual suspend fun FileKit.saveFile(
nsSavePanel.allowedFileTypes = listOf(extension)

// Accept the creation of directories
nsSavePanel.canCreateDirectories = true
nsSavePanel.canCreateDirectories = platformSettings.canCreateDirectories

// Run the NSSavePanel
val result = nsSavePanel.runModal()
Expand All @@ -85,13 +87,17 @@ private fun callPicker(
title: String?,
initialDirectory: String?,
fileExtensions: List<String>?,
platformSettings: FileKitDialogSettings,
): List<NSURL>? {
// Create an NSOpenPanel
val nsOpenPanel = NSOpenPanel()

// Configure the NSOpenPanel
nsOpenPanel.configure(mode, title, fileExtensions, initialDirectory)

// Accept the creation of directories
nsOpenPanel.canCreateDirectories = platformSettings.canCreateDirectories

// Run the NSOpenPanel
val result = nsOpenPanel.runModal()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package io.github.vinceglb.filekit.dialog

public actual class FileKitDialogSettings
public actual class FileKitDialogSettings {
public actual companion object {
public actual fun createDefault(): FileKitDialogSettings = FileKitDialogSettings()
}
}

0 comments on commit b355568

Please sign in to comment.