Skip to content

Commit b5d275d

Browse files
committed
Improve initial dialog positioning logic in TrayApp
- Introduced `initialPosition` state for better positioning management. - Refined calculation of tray window position during initialization. - Enhanced window visibility handling by leveraging pre-calculated positions.
1 parent 0b9a61e commit b5d275d

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

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

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ fun ApplicationScope.TrayApp(
287287
// Track if window has been created at least once
288288
var windowCreated by remember { mutableStateOf(false) }
289289

290+
// Initial position for the dialog state, calculated for initial visible
291+
var initialPosition by remember { mutableStateOf(WindowPosition(-10000.dp, -10000.dp)) }
292+
290293
// System information
291294
val isDark = isMenuBarInDarkMode()
292295
val os = getOperatingSystem()
@@ -393,7 +396,9 @@ fun ApplicationScope.TrayApp(
393396
if (isVisible) {
394397
// Show window immediately
395398
shouldShowWindow = true
396-
windowCreated = true
399+
if (!windowCreated) {
400+
windowCreated = true
401+
}
397402
lastShownAt = System.currentTimeMillis()
398403
} else {
399404
// Hide window after fade animation completes
@@ -440,6 +445,15 @@ fun ApplicationScope.TrayApp(
440445
autoHideEnabledAt = System.currentTimeMillis() + 1000
441446
}
442447

448+
// Calculate initial position after delays
449+
val widthPx = windowSize.width.value.toInt()
450+
val heightPx = windowSize.height.value.toInt()
451+
initialPosition = getTrayWindowPositionForInstance(
452+
tray.instanceKey(),
453+
widthPx,
454+
heightPx
455+
) as WindowPosition.Absolute
456+
443457
shouldShowWindow = true
444458
windowCreated = true
445459
lastShownAt = System.currentTimeMillis()
@@ -468,9 +482,9 @@ fun ApplicationScope.TrayApp(
468482
// Main popup window - always mounted once created to preserve states
469483
// Uses hybrid approach: moves off-screen instead of unmounting
470484
if (windowCreated) {
471-
// Start off-screen
485+
// Use the pre-calculated initial position
472486
val dialogState = rememberDialogState(
473-
position = WindowPosition(-10000.dp, -10000.dp),
487+
position = initialPosition,
474488
size = windowSize
475489
)
476490

0 commit comments

Comments
 (0)