Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File associations #4957

Merged
merged 10 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
zhelenskiy marked this conversation as resolved.
Show resolved Hide resolved

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
zhelenskiy marked this conversation as resolved.
Show resolved Hide resolved
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"),
igordmn marked this conversation as resolved.
Show resolved Hide resolved
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
zhelenskiy marked this conversation as resolved.
Show resolved Hide resolved

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
Loading