Skip to content

Commit

Permalink
♻️ Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
vinceglb committed Dec 11, 2024
1 parent b355568 commit 6e3aa2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ internal class MacOSFilePicker : PlatformFilePicker {
val openPanel = Foundation.invoke("NSOpenPanel", "new")

// Setup single, multiple selection or directory mode
mode.setupPickerMode(openPanel)

// Set canCreateDirectories
Foundation.invoke(openPanel, "setCanCreateDirectories:", macOSSettings.canCreateDirectories)
mode.setupPickerMode(openPanel, macOSSettings.canCreateDirectories)

// Set the title
title?.let {
Expand Down Expand Up @@ -139,33 +136,36 @@ internal class MacOSFilePicker : PlatformFilePicker {
}

private sealed class MacOSFilePickerMode<T> {
abstract fun setupPickerMode(openPanel: ID)
abstract fun setupPickerMode(openPanel: ID, canCreateDirectories: Boolean)
abstract fun getResult(openPanel: ID): T?

data object SingleFile : MacOSFilePickerMode<File?>() {
override fun setupPickerMode(openPanel: ID) {
override fun setupPickerMode(openPanel: ID, canCreateDirectories: Boolean) {
Foundation.invoke(openPanel, "setCanChooseFiles:", true)
Foundation.invoke(openPanel, "setCanChooseDirectories:", false)
Foundation.invoke(openPanel, "setCanCreateDirectories:", canCreateDirectories)
}

override fun getResult(openPanel: ID): File? = singlePath(openPanel)
}

data object MultipleFiles : MacOSFilePickerMode<List<File>>() {
override fun setupPickerMode(openPanel: ID) {
override fun setupPickerMode(openPanel: ID, canCreateDirectories: Boolean) {
Foundation.invoke(openPanel, "setCanChooseFiles:", true)
Foundation.invoke(openPanel, "setCanChooseDirectories:", false)
Foundation.invoke(openPanel, "setAllowsMultipleSelection:", true)
Foundation.invoke(openPanel, "setCanCreateDirectories:", canCreateDirectories)
// MaxItems is not supported by MacOSFilePicker
}

override fun getResult(openPanel: ID): List<File>? = multiplePaths(openPanel)
}

data object Directories : MacOSFilePickerMode<File>() {
override fun setupPickerMode(openPanel: ID) {
override fun setupPickerMode(openPanel: ID, canCreateDirectories: Boolean) {
Foundation.invoke(openPanel, "setCanChooseFiles:", false)
Foundation.invoke(openPanel, "setCanChooseDirectories:", true)
Foundation.invoke(openPanel, "setCanCreateDirectories:", canCreateDirectories)
}

override fun getResult(openPanel: ID): File? = singlePath(openPanel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,13 @@ private fun callPicker(
val nsOpenPanel = NSOpenPanel()

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

// Accept the creation of directories
nsOpenPanel.canCreateDirectories = platformSettings.canCreateDirectories
nsOpenPanel.configure(
mode = mode,
title = title,
extensions = fileExtensions,
initialDirectory = initialDirectory,
canCreateDirectories = platformSettings.canCreateDirectories
)

// Run the NSOpenPanel
val result = nsOpenPanel.runModal()
Expand All @@ -115,6 +118,7 @@ private fun NSOpenPanel.configure(
title: String?,
extensions: List<String>?,
initialDirectory: String?,
canCreateDirectories: Boolean,
): NSOpenPanel {
// Set the title
title?.let { message = it }
Expand Down Expand Up @@ -146,6 +150,9 @@ private fun NSOpenPanel.configure(
}
}

// Accept the creation of directories
this.canCreateDirectories = canCreateDirectories

return this
}

Expand Down

0 comments on commit 6e3aa2d

Please sign in to comment.