-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of joptionspane for not supported tray notification
- Loading branch information
1 parent
66e0e96
commit 05155dc
Showing
10 changed files
with
129 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/DesktopNotifier.kt
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/DesktopNotifierFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mmk.kmpnotifier.notification | ||
|
||
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration | ||
import com.mmk.kmpnotifier.notification.impl.JOptionPaneNotifier | ||
import com.mmk.kmpnotifier.notification.impl.TrayNotifier | ||
|
||
internal object DesktopNotifierFactory { | ||
fun getNotifier(configuration: NotificationPlatformConfiguration.Desktop): Notifier { | ||
return when { | ||
TrayNotifier.isSupported -> TrayNotifier(configuration = configuration) | ||
//TODO for now return JOptionPaneNotifier for not supported platforms | ||
else -> JOptionPaneNotifier(configuration = configuration) | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/impl/JOptionPaneNotifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mmk.kmpnotifier.notification.impl | ||
|
||
import com.mmk.kmpnotifier.notification.Notifier | ||
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration | ||
import javax.swing.ImageIcon | ||
import javax.swing.JOptionPane | ||
|
||
internal class JOptionPaneNotifier(private val configuration: NotificationPlatformConfiguration.Desktop) : | ||
Notifier { | ||
|
||
override fun notify(title: String, body: String, payloadData: Map<String, String>): Int { | ||
val id = -1 | ||
notify(id = id, title = title, body = body, payloadData) | ||
return id | ||
} | ||
|
||
override fun notify(id: Int, title: String, body: String, payloadData: Map<String, String>) { | ||
JOptionPane.showMessageDialog( | ||
null, | ||
body, | ||
title, | ||
JOptionPane.INFORMATION_MESSAGE, | ||
ImageIcon(configuration.notificationIconPath) | ||
) | ||
} | ||
|
||
override fun remove(id: Int) { | ||
println("No remove functionality") | ||
} | ||
|
||
override fun removeAll() { | ||
println("No removeAll functionality") | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
kmpnotifier/src/jvmMain/kotlin/com/mmk/kmpnotifier/notification/impl/TrayNotifier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.mmk.kmpnotifier.notification.impl | ||
|
||
import com.mmk.kmpnotifier.notification.Notifier | ||
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration | ||
import java.awt.SystemTray | ||
import java.awt.Toolkit | ||
import java.awt.TrayIcon | ||
import kotlin.random.Random | ||
|
||
internal class TrayNotifier(private val configuration: NotificationPlatformConfiguration.Desktop) : | ||
Notifier { | ||
|
||
private val trayIcons: MutableMap<Int, TrayIcon> = mutableMapOf() | ||
|
||
companion object { | ||
val isSupported by lazy { | ||
SystemTray.isSupported().also { | ||
if (it.not()) System.err.println( | ||
"Tray is not supported on the current platform. " | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun notify(title: String, body: String, payloadData: Map<String, String>): Int { | ||
if (isSupported.not()) return -1 | ||
val notificationID = Random.nextInt(0, Int.MAX_VALUE) | ||
notify(notificationID, title, body, payloadData) | ||
return notificationID | ||
} | ||
|
||
override fun notify( | ||
id: Int, | ||
title: String, | ||
body: String, | ||
payloadData: Map<String, String> | ||
) { | ||
if (isSupported.not()) return | ||
val icon = Toolkit.getDefaultToolkit().getImage(configuration.notificationIconPath) | ||
val trayIcon = TrayIcon(icon).apply { | ||
isImageAutoSize = true | ||
} | ||
SystemTray.getSystemTray().add(trayIcon) | ||
.also { trayIcons[id] = trayIcon } | ||
trayIcon.displayMessage(title, body, TrayIcon.MessageType.INFO) | ||
} | ||
|
||
override fun remove(id: Int) { | ||
val systemTray = SystemTray.getSystemTray() | ||
val trayIcon = trayIcons.getOrDefault(id, null) | ||
trayIcon?.let { systemTray.remove(it) } | ||
} | ||
|
||
override fun removeAll() { | ||
val systemTray = SystemTray.getSystemTray() | ||
systemTray.trayIcons.forEach { systemTray.remove(it) } | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.94 KB
(24%)
sample/src/commonMain/composeResources/drawable/ic_notification.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion
2
sample/src/desktopMain/kotlin/com/mmk/kmpnotifier/sample/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
sample/src/desktopMain/kotlin/com/mmk/kmpnotifier/sample/Platform.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package com.mmk.kmpnotifier.sample | ||
|
||
import com.mmk.kmpnotifier.extensions.composeDesktopResourcesPath | ||
import com.mmk.kmpnotifier.notification.NotifierManager | ||
import com.mmk.kmpnotifier.notification.configuration.NotificationPlatformConfiguration | ||
import java.io.File | ||
|
||
actual fun onApplicationStartPlatformSpecific() { | ||
println("Desktop app is initialized") | ||
NotifierManager.initialize( | ||
NotificationPlatformConfiguration.Desktop(showPushNotification = true, notificationIconPath = "ic_notification.png") | ||
NotificationPlatformConfiguration.Desktop( | ||
showPushNotification = true, | ||
notificationIconPath = composeDesktopResourcesPath() + File.separator + "ic_notification.png" | ||
) | ||
) | ||
} |