From 7fcaa2ddf70247be82e0a2151c83581675110896 Mon Sep 17 00:00:00 2001 From: Daniel Wolf Date: Thu, 8 Feb 2018 10:07:52 +0100 Subject: [PATCH] Setting dock icon on OS X --- .../rhubarb_for_spine/MainApp.kt | 39 ++++++++++++++++++- .../rhubarb_for_spine/MainView.kt | 6 --- 2 files changed, 38 insertions(+), 7 deletions(-) mode change 100644 => 100755 extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainApp.kt diff --git a/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainApp.kt b/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainApp.kt old mode 100644 new mode 100755 index 0d21765..d82c65e --- a/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainApp.kt +++ b/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainApp.kt @@ -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 + } + } + +} diff --git a/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainView.kt b/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainView.kt index b797d13..56bf74e 100644 --- a/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainView.kt +++ b/extras/rhubarb-for-spine/src/main/kotlin/com/rhubarb_lip_sync/rhubarb_for_spine/MainView.kt @@ -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 {