Although not specifically compose-desktop specific...
# compose-desktop
y
Although not specifically compose-desktop specific, wanted to share how I build the
.icns
file that you can use in your dmg image on macOS (because it is NOT trivial), using a svg file as an input and google chrome for the rendering (handle transparency properly)
Copy code
// render a single icon with the size
fun renderIcon(size: Int, filename: String) {
  val html = """
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body style="margin:0;overflow:hidden">
      <img style="padding; margin:0" src="${rootDir}/src/main/resources/images/logo.svg" width="$size" height="$size"></img>
      </body>
      </html>
    """.trimIndent()
  val inputFile = file("$buildDir/icon.html")
  inputFile.writeText(html)
  println(inputFile)
  exec {
    commandLine(
      "/Applications/Google <http://Chrome.app/Contents/MacOS/Google|Chrome.app/Contents/MacOS/Google> Chrome",
      "--headless",
      "--screenshot=$filename",
      "--window-size=$size,$size",
      "--default-background-color=0",
      inputFile.toString()
    )
  }
}

val iconsetDir = "${buildDir}/app-icon.iconset"

// render all icons required for iconset
tasks.register("iconset") {
  doLast {
    // make sure the folder exists
    mkdir(iconsetDir)

    // generate each icon
    arrayOf(512, 256, 128, 32, 16).forEach { size ->
      renderIcon(size * 2, "$iconsetDir/icon_${size}x${size}@2x.png")
      renderIcon(size, "$iconsetDir/icon_${size}x${size}.png")
    }
  }
}

// render the .icns file needed for macOS
val icnsTask = tasks.register("icns") {
  doLast {
    exec {
      commandLine("iconutil", "-c", "icns", iconsetDir)
      workingDir = buildDir
    }
  }
  dependsOn("iconset")
}
after running the
icns
task you end up with
app-icon.icns
file which you can then use in your app distribution
👍 1
t
If you want you could get rid of the chrome dependency (e.g. to be able to do it on a build server)
Copy code
def resizeImageWithJava(File infile, File outfile, int size) {
        def IMG_WIDTH = size
        def IMG_HEIGHT = size
        println "convert:" + infile.name + " size:" + size
        def BufferedImage originalImage = ImageIO.read(infile)
        def BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, originalImage.getType())
        println "  (${originalImage.width}x${originalImage.height}) -> (${resizedImage.width}x${resizedImage.height})"
        Graphics2D g = resizedImage.createGraphics()
        g.setComposite(AlphaComposite.Src)
        g.drawImage(originalImage.getScaledInstance(IMG_WIDTH, IMG_HEIGHT, Image.SCALE_AREA_AVERAGING), 0, 0, null)
        g.finalize()
        ImageIO.write(resizedImage, "png", outfile)
    }
But i am not sure if it can read SVG files. And of course you still need the iconutil tool.
y
As far as I can tell, you cannot read svg files like this. I guess there are libraries (like Apache batik) that can but then the build script would have to depend on this too...