From 845b021432bc98734372b9bada292c85f35d0ee7 Mon Sep 17 00:00:00 2001 From: denis0001-dev Date: Sun, 18 Oct 2026 14:04:52 +0300 Subject: [PATCH] Flip the tray context menu below the cursor when there is no room above On GNOME the tray icon sits in the top bar, so the menu could only ever be placed by clamping: the old code always asked for anchor.y - height, and when that fell outside the working area coerceIn pinned it to usable.y regardless of where the click was. That is what made the menu look offset from the click. Placement now tries up-and-left first and flips to below (and to the right when there is no room on the left) instead of clamping, with a small 4px gap from the cursor. --- .../ru/fromchat/desktop/DesktopTrayMenu.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/desktop/src/main/kotlin/ru/fromchat/desktop/DesktopTrayMenu.kt b/app/desktop/src/main/kotlin/ru/fromchat/desktop/DesktopTrayMenu.kt index 1b312c0..418a48d 100644 --- a/app/desktop/src/main/kotlin/ru/fromchat/desktop/DesktopTrayMenu.kt +++ b/app/desktop/src/main/kotlin/ru/fromchat/desktop/DesktopTrayMenu.kt @@ -93,6 +93,9 @@ private val trayWindowTranslucencySupported: Boolean by lazy { }.getOrDefault(false) } +/** Gap between the cursor and the flipped menu edge, in logical pixels. */ +private const val trayMenuCursorGapPx = 4 + private fun applyMenuOpacity(window: java.awt.Window, alpha: Float) { if (!trayWindowTranslucencySupported) return runCatching { window.opacity = alpha }.onFailure { @@ -335,8 +338,22 @@ internal fun DesktopTrayHost( val usable = usableScreenBoundsFor(anchor) val maxX = (usable.x + usable.width - awtWidth).coerceAtLeast(usable.x) val maxY = (usable.y + usable.height - awtHeight).coerceAtLeast(usable.y) - val x = (anchor.x - awtWidth).coerceIn(usable.x, maxX) - val y = (anchor.y - awtHeight).coerceIn(usable.y, maxY) + // Prefer up-and-left of the cursor, but a top-bar tray has no room above it, so + // clamping alone would shove the menu into an odd spot. Flip to below the cursor + // when there is not enough space above, and to the right when there is not enough + // space to the left. + val aboveY = anchor.y - awtHeight + val y = if (aboveY >= usable.y) { + aboveY + } else { + (anchor.y + trayMenuCursorGapPx).coerceAtMost(maxY) + } + val leftX = anchor.x - awtWidth + val x = if (leftX >= usable.x) { + leftX + } else { + (anchor.x + trayMenuCursorGapPx).coerceAtMost(maxX) + } awtWindow.setSize(awtWidth, awtHeight) awtWindow.setLocation(x, y) with(density) {