fix(frontend): correct floating UI positioning under root zoom

The user font-size preference applies CSS `zoom` on `<html>`
(see `composables/useFont.ts`). This breaks every popover that
anchors itself with `getBoundingClientRect()` + `position: fixed/absolute`,
because the rect is in visual pixels while CSS lengths are then
multiplied by zoom again — shifting popovers toward the lower-right
and miscomputing maxHeight / width / viewport-edge clamping.

PR #1459 patched only `AgentSelector.vue` (and left its width/maxHeight
unfixed). This PR introduces a shared helper `frontend/src/utils/zoom.ts`
(`getRootZoom`, `rectToCssPx`, `cssViewportSize`) and applies it to every
affected popover:

- `AgentSelector.vue` — refactored to use the helper; also normalizes
  `width` and `maxHeight` (previously still in visual px).
- `Input-field.vue` — model dropdown, mention popup (typed @ and button-
  triggered), and agent-mode dropdown.
- `KnowledgeBaseSelector.vue` — primary anchor path and fallback path.
- `UserMenu.vue` — tenant and IM floating submenus + viewport clamp.
- `FAQTagTooltip.vue` — fixed tooltip positioning + edge clamp.
- `AgentEditorModal.vue` — placeholder popups for system_prompt,
  context_template, and rewrite/fallback prompts.
- `AgentStreamDisplay.vue` — `kb-float-popup` (absolute under body, also
  under the zoom containing block).

Verified with `npm --prefix frontend run build`.
This commit is contained in:
wizardchen
2026-05-24 22:55:44 +08:00
committed by lyingbug
parent a02d6d06b9
commit 30332cca99
8 changed files with 191 additions and 80 deletions
@@ -67,6 +67,7 @@ import { ref, computed, watch, nextTick } from 'vue'
import { useSettingsStore } from '@/stores/settings'
import { listKnowledgeBases } from '@/api/knowledge-base'
import { useI18n } from 'vue-i18n'
import { getRootZoom, rectToCssPx, cssViewportSize } from '@/utils/zoom'
interface KnowledgeBase {
id: string
@@ -174,14 +175,18 @@ const loadKnowledgeBases = async () => {
const updateDropdownPosition = () => {
const anchor = resolveAnchorEl()
// Cache root zoom for this update. Both fallback and rect-anchored paths
// need to convert visual measurements to CSS pixels (see utils/zoom.ts).
const zoom = getRootZoom()
const { width: vwFallback, height: vhFallback } = cssViewportSize(zoom)
// fallback 函数
const applyFallback = () => {
const vw = window.innerWidth;
const topFallback = Math.max(80, window.innerHeight / 2 - 160);
const topFallback = Math.max(80, vhFallback / 2 - 160);
dropdownStyle.value = {
position: 'fixed',
width: `${dropdownWidth}px`,
left: `${Math.round((vw - dropdownWidth) / 2)}px`,
left: `${Math.round((vwFallback - dropdownWidth) / 2)}px`,
top: `${Math.round(topFallback)}px`,
transform: 'none',
margin: '0',
@@ -195,33 +200,30 @@ const updateDropdownPosition = () => {
}
// 获取 anchor 的 bounding rect(相对于视口)
let rect: DOMRect | null = null
let rawRect: { top: number; left: number; right: number; bottom: number; width: number; height: number } | null = null
try {
if (typeof anchor.getBoundingClientRect === 'function') {
rect = anchor.getBoundingClientRect()
console.log('[KB Selector] Button rect:', {
top: rect.top,
bottom: rect.bottom,
left: rect.left,
right: rect.right,
width: rect.width,
height: rect.height
})
const r = anchor.getBoundingClientRect()
rawRect = { top: r.top, left: r.left, right: r.right, bottom: r.bottom, width: r.width, height: r.height }
} else if (anchor.width !== undefined && anchor.left !== undefined) {
// 已经是 DOMRect
rect = anchor as DOMRect
// Already a DOMRect-like
rawRect = anchor as DOMRect
}
} catch (e) {
console.error('[KnowledgeBaseSelector] Error getting bounding rect:', e)
}
if (!rect || rect.width === 0 || rect.height === 0) {
if (!rawRect || rawRect.width === 0 || rawRect.height === 0) {
applyFallback()
return
}
const vw = window.innerWidth
const vh = window.innerHeight
// Convert to CSS pixels so subsequent comparisons against the dropdown's
// own width/height stay in one coordinate system.
const rect = rectToCssPx(rawRect, zoom)
console.log('[KB Selector] Button rect (css px):', rect)
const vw = vwFallback
const vh = vhFallback
// 左对齐到触发元素的左边缘
// 使用 Math.floor 而不是 Math.round,避免像素对齐问题