Skip to content

Commit bf437de

Browse files
committed
Fix macOS window not appearing when switching between Spaces
When the tray window is visible and the user switches to a different Space (desktop), clicking the tray icon now brings the window to the current Space instead of hiding it. This is achieved by checking if the window has focus before toggling visibility - if unfocused (likely on another Space), the window is brought to front on the active Space.
1 parent 9df769d commit bf437de

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

  • src/commonMain/kotlin/com/kdroid/composetray/tray/api

src/commonMain/kotlin/com/kdroid/composetray/tray/api/TrayApp.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,9 @@ private fun ApplicationScope.TrayAppImplOriginal(
511511
// Position pre-computed at click time so the LaunchedEffect can use it immediately.
512512
var pendingPosition by remember { mutableStateOf<WindowPosition?>(null) }
513513

514+
// Store window reference for macOS Space detection
515+
var windowRef by remember { mutableStateOf<java.awt.Window?>(null) }
516+
514517
val dialogState = rememberDialogState(size = currentWindowSize)
515518
LaunchedEffect(currentWindowSize) { dialogState.size = currentWindowSize }
516519

@@ -538,6 +541,24 @@ private fun ApplicationScope.TrayAppImplOriginal(
538541
if (now - lastPrimaryActionAt >= toggleDebounceMs) {
539542
lastPrimaryActionAt = now
540543
if (isVisible) {
544+
// On macOS, check if window has focus before hiding
545+
// If it doesn't have focus (e.g., on another Space), bring it to front instead
546+
if (getOperatingSystem() == MACOS && windowRef != null) {
547+
val hasFocus = runCatching { windowRef!!.isFocused() }.getOrElse { false }
548+
if (!hasFocus) {
549+
// Window is not focused (likely on another Space), bring it to current Space
550+
invokeLater {
551+
runCatching { MacTrayLoader.lib.tray_set_windows_move_to_active_space() }
552+
runCatching { MacOSWindowManager().setMoveToActiveSpace(windowRef!!) }
553+
runCatching {
554+
windowRef!!.toFront()
555+
windowRef!!.requestFocus()
556+
windowRef!!.requestFocusInWindow()
557+
}
558+
}
559+
return@internalPrimaryAction
560+
}
561+
}
541562
requestHideExplicit()
542563
} else {
543564
if (now - lastHiddenAt >= minHiddenDurationMs) {
@@ -638,6 +659,9 @@ private fun ApplicationScope.TrayAppImplOriginal(
638659
DisposableEffect(shouldShowWindow, dismissMode) {
639660
if (!shouldShowWindow) return@DisposableEffect onDispose { }
640661

662+
// Store window reference for Space detection on macOS
663+
windowRef = window
664+
641665
try { window.name = WindowVisibilityMonitor.TRAY_DIALOG_NAME } catch (_: Throwable) {}
642666
runCatching { WindowVisibilityMonitor.recompute() }
643667

@@ -690,6 +714,7 @@ private fun ApplicationScope.TrayAppImplOriginal(
690714
window.removeWindowFocusListener(focusListener)
691715
macWatcher?.stop(); linuxWatcher?.stop(); windowsWatcher?.stop()
692716
runCatching { WindowVisibilityMonitor.recompute() }
717+
windowRef = null
693718
}
694719
}
695720

0 commit comments

Comments
 (0)