Skip to content

Commit 0b9795b

Browse files
committed
Refactor Tray position calculation to handle synthesized clicks near tray corner
- Added fallback logic to generate synthetic click positions when no actual click data exists, ensuring proper window positioning at startup. - Implemented `syntheticClickFromCorner` helper for DPI-aware synthesized clicks. - Improved Windows-specific behavior to handle initial visibility scenarios seamlessly.
1 parent 290dd33 commit 0b9795b

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/commonMain/kotlin/com/kdroid/composetray/utils/TrayPosition.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,20 @@ fun getTrayWindowPosition(
211211

212212
if (getOperatingSystem() == OperatingSystem.WINDOWS) {
213213
val freshPos = TrayClickTracker.getLastClickPosition()
214-
val posToUse = freshPos ?: return fallbackCornerPosition(windowWidth, windowHeight, horizontalOffset, verticalOffset)
214+
?: loadTrayClickPosition()
215+
216+
val posToUse = freshPos ?: run {
217+
// No click yet (e.g., initiallyVisible = true). Synthesize one near the tray corner
218+
val corner = getTrayPosition()
219+
val (sx, sy) = syntheticClickFromCorner(corner, screenSize.width, screenSize.height)
220+
return calculateWindowPositionFromClick(
221+
sx, sy, corner,
222+
windowWidth, windowHeight,
223+
screenSize.width, screenSize.height,
224+
horizontalOffset, verticalOffset
225+
)
226+
}
227+
215228
return calculateWindowPositionFromClick(
216229
posToUse.x, posToUse.y, posToUse.position,
217230
windowWidth, windowHeight,
@@ -453,3 +466,18 @@ internal fun isPointWithinLinuxStatusItem(px: Int, py: Int): Boolean {
453466
val bottom = iy + half + fudge
454467
return px in left..right && py in top..bottom
455468
}
469+
470+
// TrayPosition.kt
471+
472+
private fun syntheticClickFromCorner(
473+
corner: TrayPosition,
474+
screenW: Int,
475+
screenH: Int
476+
): Pair<Int, Int> {
477+
val half = dpiAwareHalfIconOffset() // ~half icon in px, DPI-aware
478+
val x = if (corner == TrayPosition.TOP_RIGHT || corner == TrayPosition.BOTTOM_RIGHT)
479+
screenW - half else half
480+
val y = if (corner == TrayPosition.BOTTOM_LEFT || corner == TrayPosition.BOTTOM_RIGHT)
481+
screenH - half else half
482+
return x to y
483+
}

0 commit comments

Comments
 (0)