From f3207b526f0f9fef2bcd688eab9c2cb5b90f4af1 Mon Sep 17 00:00:00 2001 From: ItsLemmy Date: Mon, 17 Nov 2025 22:10:58 -0500 Subject: [PATCH] Tooltip: Update position on text content change, take 2 --- Modules/Tooltip/Tooltip.qml | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/Modules/Tooltip/Tooltip.qml b/Modules/Tooltip/Tooltip.qml index 4be4ee94..2e3d1d34 100644 --- a/Modules/Tooltip/Tooltip.qml +++ b/Modules/Tooltip/Tooltip.qml @@ -335,18 +335,43 @@ PopupWindow { const tipHeight = tooltipText.implicitHeight + (padding * 2); root.implicitHeight = tipHeight; - // Reposition if necessary + // Reposition based on current direction var targetGlobal = targetItem.mapToItem(null, 0, 0); const targetWidth = targetItem.width; + const targetHeight = targetItem.height; + + // Recalculate base anchor position (center on target for top/bottom, etc.) + var newAnchorX = anchorX; + var newAnchorY = anchorY; + + // Determine which direction the tooltip is currently positioned + // and recalculate the centering for that direction + if (anchorY > targetHeight / 2) { + // Tooltip is below target + newAnchorX = (targetWidth - tipWidth) / 2; + } else if (anchorY < -tipHeight / 2) { + // Tooltip is above target + newAnchorX = (targetWidth - tipWidth) / 2; + } else if (anchorX > targetWidth / 2) { + // Tooltip is to the right + newAnchorY = (targetHeight - tipHeight) / 2; + } else if (anchorX < -tipWidth / 2) { + // Tooltip is to the left + newAnchorY = (targetHeight - tipHeight) / 2; + } // Adjust horizontal position to keep tooltip on screen if needed - const globalX = targetGlobal.x + anchorX; + const globalX = targetGlobal.x + newAnchorX; if (globalX < 0) { - anchorX = -targetGlobal.x + margin; + newAnchorX = -targetGlobal.x + margin; } else if (globalX + tipWidth > screenWidth) { - anchorX = screenWidth - targetGlobal.x - tipWidth - margin; + newAnchorX = screenWidth - targetGlobal.x - tipWidth - margin; } + // Apply the new anchor positions + anchorX = newAnchorX; + anchorY = newAnchorY; + // Force anchor update Qt.callLater(() => { if (root.anchor && root.visible) {