From 41a300051edffbd1cf99fc0d163f9d0591b4e0e1 Mon Sep 17 00:00:00 2001 From: AmirHossein Abdolmotallebi Date: Tue, 30 Jul 2024 01:37:03 +0330 Subject: [PATCH] Exception will not throw if System tray is not supported by the OS --- CHANGELOG.md | 2 + .../amirab/util/desktop/systemtray/AwtTray.kt | 42 ++++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38b0f10..7cf3502 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ ### Fixed +- Exception will not throw if System Tray is not supported by the OS + ### Security ## 1.0.5 diff --git a/desktop/tray/src/main/kotlin/ir/amirab/util/desktop/systemtray/AwtTray.kt b/desktop/tray/src/main/kotlin/ir/amirab/util/desktop/systemtray/AwtTray.kt index 1a8bf6d..146e671 100644 --- a/desktop/tray/src/main/kotlin/ir/amirab/util/desktop/systemtray/AwtTray.kt +++ b/desktop/tray/src/main/kotlin/ir/amirab/util/desktop/systemtray/AwtTray.kt @@ -31,16 +31,7 @@ fun AwtTray( val density by rememberUpdatedState(GlobalDensity) if (!isTraySupported) { DisposableEffect(Unit) { - // We should notify developer, but shouldn't throw an exception. - // If we would throw an exception, some application wouldn't work on some platforms at - // all, if developer doesn't check that application crashes. - // - // We can do this because we don't return anything in Tray function, and following - // code doesn't depend on something that is created/calculated in this function. - System.err.println( - "Tray is not supported on the current platform. " + - "Use the global property `isTraySupported` to check." - ) + System.err.println("System tray is not supported") onDispose {} } return @@ -58,11 +49,11 @@ fun AwtTray( icon.toAwtImage(GlobalDensity, GlobalLayoutDirection, trayIconSize) } - val tray = remember { + val trayIcon = remember { TrayIcon(awtIcon) } - DisposableEffect(tray) { - with(tray) { + DisposableEffect(trayIcon) { + with(trayIcon) { val doubleClickListener = object : MouseListener { override fun mouseClicked(p0: MouseEvent?) { when (p0?.button) { @@ -95,21 +86,32 @@ fun AwtTray( } } SideEffect { - if (tray.image != awtIcon) tray.image = awtIcon - if (tray.toolTip != tooltip) tray.toolTip = tooltip + if (trayIcon.image != awtIcon) trayIcon.image = awtIcon + if (trayIcon.toolTip != tooltip) trayIcon.toolTip = tooltip } val coroutineScope = rememberCoroutineScope() DisposableEffect(Unit) { - SystemTray.getSystemTray().add(tray) + val systemTray = kotlin.runCatching { + // some linux users reporting that system tray will not support by distro and throws exception after entering to lock screen + // I have to investigate why! + SystemTray.getSystemTray() + }.getOrNull() - state.notificationFlow - .onEach(tray::displayMessage) - .launchIn(coroutineScope) + if (systemTray!=null){ + systemTray.add(trayIcon) + state.notificationFlow + .onEach(trayIcon::displayMessage) + .launchIn(coroutineScope) + } onDispose { - SystemTray.getSystemTray().remove(tray) + // don't get hard! + // linux have some strange behaviors in system tray I have to improve this + runCatching { + systemTray?.remove(trayIcon) + } } } }