Skip to content

Commit

Permalink
fixup! Add file associations support
Browse files Browse the repository at this point in the history
[Desktop: Add support for file associations](#773)
  • Loading branch information
zhelenskiy committed Jun 22, 2024
1 parent 811739d commit 0fd4ebe
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ abstract class JvmApplicationDistributions : AbstractDistributions() {
fn.execute(windows)
}

@JvmOverloads
fun fileAssociation(
mimeType: String, extension: String, description: String,
linuxIcon: File? = null, windowsIcon: File? = null, macOSIcon: File? = null,
linuxIconFile: File? = null, windowsIconFile: File? = null, macOSIconFile: File? = null,
) {
linux.fileAssociation(mimeType, extension, description, linuxIcon)
windows.fileAssociation(mimeType, extension, description, windowsIcon)
macOS.fileAssociation(mimeType, extension, description, macOSIcon)
linux.fileAssociation(mimeType, extension, description, linuxIconFile)
windows.fileAssociation(mimeType, extension, description, windowsIconFile)
macOS.fileAssociation(mimeType, extension, description, macOSIconFile)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ abstract class AbstractPlatformSettings {
var installationPath: String? = null

internal val fileAssociations: MutableSet<FileAssociation> = mutableSetOf()

@JvmOverloads
fun fileAssociation(mimeType: String, extension: String, description: String, iconFile: File? = null) {
fileAssociations.add(FileAssociation(mimeType, extension, description, iconFile))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,33 @@ abstract class AbstractJPackageTask @Inject constructor(
internal val fileAssociations: SetProperty<FileAssociation> = objects.setProperty(FileAssociation::class.java)

private val iconMapping by lazy {
val icons = fileAssociations.orNull.orEmpty().mapNotNull { it.iconFile }
val icons = fileAssociations.orNull.orEmpty().mapNotNull { it.iconFile }.distinct()
if (icons.isEmpty()) return@lazy emptyMap()
val iconTempNames = generateSequence {
icons.mapTo(mutableSetOf()) { String(CharArray(10) { ('a'..'z').random() }) }
}.first { it.size == icons.size }
val iconTempNames: List<String> = mutableListOf<String>().apply {
val usedNames = mutableSetOf("${packageName.get()}.icns")
for (icon in icons) {
if (!icon.exists()) continue
if (usedNames.add(icon.name)) {
add(icon.name)
continue
}
val nameWithoutExtension = icon.nameWithoutExtension
val extension = icon.extension
for (n in 1UL..ULong.MAX_VALUE) {
val newName = "$nameWithoutExtension ($n).$extension"
if (usedNames.add(newName)) {
add(newName)
break
}
}
}
}
val appDir = destinationDir.ioFile.resolve("${packageName.get()}.app")
val iconsDir = appDir.resolve("Contents").resolve("Resources")
if (iconsDir.exists()) {
iconsDir.deleteRecursively()
}
icons.zip(iconTempNames) { icon, newName ->
icon to iconsDir.resolve(newName + icon.name.drop(icon.nameWithoutExtension.length))
}.toMap()
icons.zip(iconTempNames) { icon, newName -> icon to iconsDir.resolve(newName) }.toMap()
}

private lateinit var jvmRuntimeInfo: JvmRuntimeProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,87 @@
<string>true</string>
<key>NSHighResolutionCapable</key>
<string>true</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>kot</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>Kotlin_icon_big.icns</string>
<key>CFBundleTypeMIMETypes</key>
<string>text/kotlin</string>
<key>CFBundleTypeName</key>
<string>Kotlin Source File0</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>****</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>kot1</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>TestPackage.icns</string>
<key>CFBundleTypeMIMETypes</key>
<string>text/kotlin</string>
<key>CFBundleTypeName</key>
<string>Kotlin Source File1</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>****</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>kott</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>Kotlin_icon_big (1).icns</string>
<key>CFBundleTypeMIMETypes</key>
<string>text/kotlin</string>
<key>CFBundleTypeName</key>
<string>Kotlin Source File2</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>****</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleTypeExtensions</key>
<array>
<string>kott1</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>TestPackage.icns</string>
<key>CFBundleTypeMIMETypes</key>
<string>text/kotlin</string>
<key>CFBundleTypeName</key>
<string>Kotlin Source File3</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>****</string>
</array>
</dict>
</array>

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Exameple URL</string>
<string>Example URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>exampleUrl</string>
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def extraInfoPlistKeys = """
<array>
<dict>
<key>CFBundleURLName</key>
<string>Exameple URL</string>
<string>Example URL</string>
<key>CFBundleURLSchemes</key>
<array>
<string>exampleUrl</string>
Expand All @@ -28,12 +28,36 @@ compose.desktop {
mainClass = "MainKt"
nativeDistributions {
packageName = "TestPackage"
fileAssociation(
"text/kotlin",
"kot",
"Kotlin Source File0",
project.file("Kotlin_icon_big.png"),
project.file("Kotlin_icon_big.ico"),
project.file("Kotlin_icon_big.icns"),
)
fileAssociation(
"text/kotlin",
"kot1",
"Kotlin Source File1",
)
macOS {
dockName = "CustomDockName"
minimumSystemVersion = "12.0"
infoPlist {
extraKeysRawXml = extraInfoPlistKeys
}
fileAssociation(
"text/kotlin",
"kott",
"Kotlin Source File2",
project.file("subdir/Kotlin_icon_big.icns"),
)
fileAssociation(
"text/kotlin",
"kott1",
"Kotlin Source File3",
)
}
}
}
Expand Down
Binary file not shown.
54 changes: 54 additions & 0 deletions tutorials/Native_distributions_and_local_execution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,60 @@ compose.desktop {
}
```

## File associations

File associations can be added with `fileAssociation` function.
These associations can optionally include custom icons.
Similar to [app icon](#app-icon), these custom icons also have OS-specific formats: `.icns` for macOS, `.ico` for Windows and `.png` for Linux.

It is possible to specify both OS-specific and OS-agnostic file associations.
For OS-agnostic you can specify icon files for all three OSes in-place,
while for OS-specific there is a shorthand with a single icon file parameter.

```kotlin
compose.desktop {
application {
mainClass = "MainKt"

nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)

packageName = "file-associations-demo"
packageVersion = "1.0.0"

macOS {
fileAssociation(
mimeType = "text/kotlin",
extension = "kott",
description = "Kotlin Source File",
iconFile = project.file("Kotlin_icon_big.icns"),
)
fileAssociation(
mimeType = "text/kotlin",
extension = "ko",
description = "Kotlin Source File",
)
}

fileAssociation(
mimeType = "text/kotlin",
extension = "kot",
description = "Kotlin Source File",
macOSIconFile = project.file("Kotlin_icon_big.icns"),
windowsIconFile = project.file("Kotlin_icon_big.ico"),
linuxIconFile = project.file("Kotlin_icon_big.png"),
)

fileAssociation(
mimeType = "text/kotlin",
extension = "kottt",
description = "Kotlin Source File",
)
}
}
}
```

## Customizing Info.plist on macOS

We aim to support important platform-specific customization use-cases via declarative DSL.
Expand Down

0 comments on commit 0fd4ebe

Please sign in to comment.