Setting dock icon on OS X

This commit is contained in:
Daniel Wolf 2018-02-08 10:07:52 +01:00
parent 8f1056dc5f
commit 7fcaa2ddf7
2 changed files with 38 additions and 7 deletions

View File

@ -1,5 +1,42 @@
package com.rhubarb_lip_sync.rhubarb_for_spine
import javafx.scene.image.Image
import javafx.stage.Stage
import tornadofx.App
import tornadofx.addStageIcon
import java.lang.reflect.Method
import javax.swing.ImageIcon
class MainApp : App(MainView::class)
class MainApp : App(MainView::class) {
override fun start(stage: Stage) {
super.start(stage)
setIcon()
}
private fun setIcon() {
// Set icon for windows
for (iconSize in listOf(16, 32, 48, 256)) {
addStageIcon(Image(this.javaClass.getResourceAsStream("/icon-$iconSize.png")))
}
// OS X requires the dock icon to be changed separately.
// Not all JDKs contain the class com.apple.eawt.Application, so we have to use reflection.
val classLoader = this.javaClass.classLoader
try {
val iconURL = this.javaClass.getResource("/icon-256.png")
val image: java.awt.Image = ImageIcon(iconURL).image
// The following is reflection code for the line
// Application.getApplication().setDockIconImage(image)
val applicationClass: Class<*> = classLoader.loadClass("com.apple.eawt.Application")
val getApplicationMethod: Method = applicationClass.getMethod("getApplication")
val application: Any = getApplicationMethod.invoke(null)
val setDockIconImageMethod: Method =
applicationClass.getMethod("setDockIconImage", java.awt.Image::class.java)
setDockIconImageMethod.invoke(application, image);
} catch (e: Exception) {
// Works only on OS X
}
}
}

View File

@ -7,7 +7,6 @@ import javafx.beans.property.SimpleStringProperty
import javafx.event.ActionEvent
import javafx.event.EventHandler
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.input.DragEvent
import javafx.scene.input.TransferMode
import javafx.scene.text.Text
@ -22,11 +21,6 @@ class MainView : View() {
init {
title = "Rhubarb Lip Sync for Spine"
// Set icon
for (iconSize in listOf(16, 32, 48, 256)) {
addStageIcon(Image(this.javaClass.getResourceAsStream("/icon-$iconSize.png")))
}
}
override val root = form {