1
0
mirror of https://github.com/ACINQ/eclair.git synced 2024-11-20 10:39:19 +01:00

(gui) using system tray and os notifications only if os is Windows/MacOs

Prevent issues with AWT in Linux
This commit is contained in:
dpad85 2017-02-16 12:23:46 +01:00
parent c355dd4578
commit b4c0494d9f
2 changed files with 35 additions and 25 deletions

View File

@ -16,7 +16,7 @@ import fr.acinq.eclair.channel.ChannelEvent
import fr.acinq.eclair.gui.controllers.MainController
import fr.acinq.eclair.gui.stages.SplashStage
import fr.acinq.eclair.router.NetworkEvent
import fr.acinq.eclair.{Setup}
import fr.acinq.eclair.Setup
import grizzled.slf4j.Logging
/**
@ -26,6 +26,7 @@ class FxApp extends Application with Logging {
override def start(primaryStage: Stage): Unit = {
val icon = new Image("/gui/commons/images/eclair02.png", true)
primaryStage.getIcons().add(icon)
val splashStage = new SplashStage()
@ -37,20 +38,8 @@ class FxApp extends Application with Logging {
try {
val setup = new Setup()
// add icon in system tray
val awtIcon = Toolkit.getDefaultToolkit.createImage(getClass.getResource("/gui/commons/images/eclair02.png"))
val trayIcon = new TrayIcon(awtIcon, "Eclair")
trayIcon.setImageAutoSize(true)
if (SystemTray.isSupported()) {
try {
SystemTray.getSystemTray.add(trayIcon)
} catch {
case e: AWTException => logger.debug("Eclair could not be added to System Tray.")
}
}
val handlers = new Handlers(setup, trayIcon)
val systemTrayIcon = initSystemTrayIcon
val handlers = new Handlers(setup, systemTrayIcon)
val controller = new MainController(handlers, primaryStage, setup, getHostServices)
val guiUpdater = setup.system.actorOf(Props(classOf[GUIUpdater], primaryStage, controller, setup), "gui-updater")
setup.system.eventStream.subscribe(guiUpdater, classOf[ChannelEvent])
@ -84,18 +73,17 @@ class FxApp extends Application with Logging {
primaryStage.setTitle("Eclair")
primaryStage.setOnCloseRequest(new EventHandler[WindowEvent] {
override def handle(event: WindowEvent): Unit = {
SystemTray.getSystemTray.remove(trayIcon)
override def handle(event: WindowEvent) {
SystemTray.getSystemTray.remove(systemTrayIcon.orNull)
System.exit(0)
}
})
splashStage.close()
splashStage.close
primaryStage.setScene(scene)
primaryStage.show()
addTrayIconActions(trayIcon, primaryStage)
primaryStage.show
systemTrayIcon.map(addTrayIconActions(_, primaryStage))
}
})
} catch {
case con: ConnectException => {
logger.error(s"Error when connecting to bitcoin-core: ", con)
@ -115,9 +103,29 @@ class FxApp extends Application with Logging {
}
}
}
}).start
}
}).start()
/**
* Adds an icon to the system tray if the OS is Windows or MacOS.
* Returns the created tray icon as an Option.
*/
private def initSystemTrayIcon: Option[TrayIcon] = {
System.getProperty("os.name").toLowerCase match {
case os if SystemTray.isSupported && (os.startsWith("windows") || os.startsWith("macosx") || os.startsWith("osx")) =>
// add icon in system tray
val awtIcon = Toolkit.getDefaultToolkit.createImage(getClass.getResource("/gui/commons/images/eclair02.png"))
val trayIcon = new TrayIcon(awtIcon, "Eclair")
trayIcon.setImageAutoSize(true)
try {
SystemTray.getSystemTray.add(trayIcon)
Option(trayIcon)
} catch {
case e: AWTException => logger.debug("Eclair could not be added to System Tray.")
None
}
case _ => None
}
}
/**
@ -127,6 +135,7 @@ class FxApp extends Application with Logging {
* <li>Minimize the stage
* <li>Close the stage
* </ul>
*
* @param trayIcon the tray icon
* @param stage the main app stage
*/

View File

@ -22,7 +22,7 @@ import scala.util.{Failure, Success}
/**
* Created by PM on 16/08/2016.
*/
class Handlers(setup: Setup, trayIcon: TrayIcon) extends Logging {
class Handlers(setup: Setup, trayIcon: Option[TrayIcon]) extends Logging {
import setup._
@ -85,9 +85,10 @@ class Handlers(setup: Setup, trayIcon: TrayIcon) extends Logging {
* @param showAppName true if you want the notification title to be preceded by "Eclair - ". True by default
*/
def notification (title: String, message: String, messageType: TrayIcon.MessageType = MessageType.NONE, showAppName: Boolean = true) = {
if (SystemTray.isSupported) {
val smartTitle = if (showAppName) s"Eclair - $title" else title
trayIcon.displayMessage(smartTitle, message, messageType)
trayIcon.map(_.displayMessage(smartTitle, message, messageType))
}
}
}