fix: re-place floating host controls promptly on screen changes

Return to the default corner once it and the current spot are free, and
skip the move confirmation/minimum interval for ~1.5 s after the home <->
voice page switch or a skin switch, so a spot picked for the previous
screen no longer covers controls on the next one (e.g. Export identity).
This commit is contained in:
OddTurtleHead committed 2026-11-15 23:04:31 +08:00
1 parent 2b019d82ff
commit 99d7b86d09
6 files changed
+87 -12

No files matched your search

+1 -1
View File
@@ -492,7 +492,7 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动
**这不是编译期限制。** `skin.css` 校验器不会因为是否声明 `slots` 而改变,也不会生成额外的隐藏或定位规则;停靠完全由宿主运行时完成。胶囊位于顶层,会挡住它下面的一切,因此宿主还会检查胶囊的占位(按观察到的最大尺寸计算,紧凑模式展开后的宽度也算在内)是否压到了可见的受保护控件(`PROTECTED_PART_IDS`,例如 `voice.audio-dock.microphone`、`voice.audio-dock.output`、`home.footer.clear`、`voice.connection-status.reconnect`);锚点的祖先容器(如包住专用锚点的 `voice.audio-dock`)不算,锚点自身只有在它是或包含可交互控件时才算。压到时同样回退。示例 `host-chrome-demo` 因此把胶囊靠 `start` 对齐在音频栏左侧的说明文字上,避开右侧的麦克风/输出按钮。锚点一旦不可用,胶囊立即回到默认左下角(仍在闭合 Shadow DOM 和顶层,皮肤无法遮挡或隐藏)。为避免闪烁,回退后需要连续 2 次检查都通过才会重新停靠;停靠期间约每 400 ms 复查一次,浮动态保持原来的 1 s 节奏。窗口缩放、滚动和锚点/胶囊尺寸变化都会触发复查。
**浮动位置的运行时避让。** 没有停靠(未声明 `slots`,或锚点回退)时,恢复胶囊默认在左下角;宿主约每 400 ms(以及每次点击后约 150 ms)检查一次它是否盖住了皮肤里可见、处于最上层的可交互控件(按钮、链接、输入框、`[tabindex]` 自定义按钮、iframe 等;禁用的“发送”按钮也算,透明度为 0 的不算;受保护部件权重最高,其次是打开的对话框/底部弹层里的控件;只因位于皮肤根 `app` 之内的普通按钮不算受保护)。盖住时,胶囊会换到左下、右下、左上、右上、左中、右中、底部居中、顶部居中里遮挡最少的位置,但不会换到鼠标最后所在的位置或当前聚焦的控件上;只有收起能至少让出一个控件时,才收成只显示 ◈ 的紧凑形态(悬停、聚焦或点按展开)。鼠标悬停(触屏上不算残留的悬停)或焦点在胶囊内时不会移动。为避免闪动:遮挡要持续约 250 ms 才移动,两次移动至少相隔 1 s,10 s 内移动超过 6 次就停在默认角落 10 s 不动;当前位置不遮挡时不会为了“回到默认角落”而移动。位置计算和 CSS 一样计入 `env(safe-area-inset-*)`(刘海屏、底部手势条)。宿主自带的应用库悬浮按钮(皮肤没有提供 `voice.plugin-launcher` 挂载点时出现在右下角)使用同一套避让,两者也互相避开。这同样只是运行时行为,不新增任何 `skin.css` 编译期限制;皮肤作者无需为它做任何事,但如果希望胶囊固定在设计好的位置,仍建议用 `slots` 停靠。
**浮动位置的运行时避让。** 没有停靠(未声明 `slots`,或锚点回退)时,恢复胶囊默认在左下角;宿主约每 400 ms(以及每次点击后约 150 ms)检查一次它是否盖住了皮肤里可见、处于最上层的可交互控件(按钮、链接、输入框、`[tabindex]` 自定义按钮、iframe 等;禁用的“发送”按钮也算,透明度为 0 的不算;受保护部件权重最高,其次是打开的对话框/底部弹层里的控件;只因位于皮肤根 `app` 之内的普通按钮不算受保护)。盖住时,胶囊会换到左下、右下、左上、右上、左中、右中、底部居中、顶部居中里遮挡最少的位置,但不会换到鼠标最后所在的位置或当前聚焦的控件上;只有收起能至少让出一个控件时,才收成只显示 ◈ 的紧凑形态(悬停、聚焦或点按展开)。鼠标悬停(触屏上不算残留的悬停)或焦点在胶囊内时不会移动。为避免闪动:遮挡要持续约 250 ms 才移动,两次移动至少相隔 1 s,10 s 内移动超过 6 次就停在默认角落 10 s 不动;默认角落重新空出来(且当前位置也不遮挡、鼠标不在那里)时,会按同样的节奏回到默认角落,免得为上一屏选的位置盖住下一屏新出现的控件;进入或离开语音页、切换皮肤时,宿主会在约 1.5 s 内跳过确认等待和最短间隔、连续复查几次,让胶囊尽快换到新页面合适的位置(通常就是重新空出来的默认角落)。位置计算和 CSS 一样计入 `env(safe-area-inset-*)`(刘海屏、底部手势条)。宿主自带的应用库悬浮按钮(皮肤没有提供 `voice.plugin-launcher` 挂载点时出现在右下角)使用同一套避让,两者也互相避开。这同样只是运行时行为,不新增任何 `skin.css` 编译期限制;皮肤作者无需为它做任何事,但如果希望胶囊固定在设计好的位置,仍建议用 `slots` 停靠。
**样式令牌**:皮肤可在皮肤根(`:root`)、锚点或锚点的祖先上设置下列普通 `--skin-*` 变量;宿主从锚点(存在时)或活动皮肤根的计算样式读取、校验,再写入闭合 Shadow DOM 内部的宿主变量。没有声明 `slots` 的皮肤也能用这些令牌给浮动态胶囊换肤;锚点仍在 DOM 中时,停靠态和浮动态都会继续使用这些令牌;锚点被移除或隐藏时,宿主读取皮肤根上可继承的值,否则使用默认值。每个 `--skin-recovery-*` 都有全局回退 `--skin-host-<同名>`,`--skin-recovery-*` 优先;只有颜色对比度不达标时整组颜色才回退到宿主默认值。声明了插槽但没写颜色令牌时,胶囊使用不透明的宿主默认色(白底),而不是无插槽时的半透明底。
+6
View File
@@ -393,6 +393,12 @@ watch([slotConfig, hostChromeActiveSkinId], () => {
syncSlotBinding();
});
}, { flush: "post" });
// Home <-> voice page and skin switches replace the controls the floating
// capsule was avoiding; re-place it promptly (and back to its default corner
// when that is free) instead of leaving it where the previous screen needed it.
watch([() => props.connected, hostChromeActiveSkinId], () => {
void nextTick(() => floatingAvoidance?.screenChanged());
}, { flush: "post" });
watch(thirdPartyAllowed, allowed => { if (!allowed) shortcutOpen.value = false; });
onMounted(() => {
@@ -47,6 +47,19 @@ test("the default corner is kept while it is free", () => {
assert.deepEqual(choice, { position: "bottom-left", score: 0 });
});
test("a free capsule away from home goes back once the default corner is free, but not while home is busy or under the pointer", () => {
// Left the voice page: the spot picked there is free, and so is home.
const back = chooseFloatingPosition({ current: "middle-right", size: capsule, viewport, margins, obstacles: [], home: "bottom-left" });
assert.deepEqual(back, { position: "bottom-left", score: 0 });
// Home still covers the microphone: stay put (no flapping).
const busy = chooseFloatingPosition({ current: "middle-right", size: capsule, viewport, margins, obstacles: [{ rect: rect(150, 730, 34, 34), weight: FLOATING_WEIGHT_PROTECTED }], home: "bottom-left" });
assert.equal(busy.position, "middle-right");
// The pointer rests on home: do not jump under it.
const pointer: FloatingObstacle = { rect: rect(84, 727, 32, 32), weight: FLOATING_WEIGHT_POINTER };
const underPointer = chooseFloatingPosition({ current: "middle-right", size: capsule, viewport, margins, obstacles: [], moveObstacles: [pointer], home: "bottom-left" });
assert.equal(underPointer.position, "middle-right");
});
test("a capsule over the microphone moves to the first free corner", () => {
// Persona-style dock: microphone at x≈226 under the default capsule.
const obstacles: FloatingObstacle[] = [
+44 -11
View File
@@ -88,6 +88,14 @@ export const HOST_FLOATING_MOVE_MIN_INTERVAL_MS = 1000;
export const HOST_FLOATING_FLAP_WINDOW_MS = 10_000;
export const HOST_FLOATING_FLAP_MAX_MOVES = 6;
export const HOST_FLOATING_FLAP_FREEZE_MS = 10_000;
/**
* After the screen changes (home <-> voice page, skin switch) the old spot was
* chosen for controls that no longer exist, so for this long the control moves
* as soon as a sample disagrees, without the confirm delay or minimum interval.
*/
export const HOST_FLOATING_SCREEN_CHANGE_MS = 1500;
/** Re-samples after a screen change (the new screen renders over a few frames). */
export const HOST_FLOATING_SCREEN_CHANGE_SAMPLES_MS = [0, 120, 400, 900] as const;
/** Parts that wrap ordinary controls; a control inside them is not "protected" by that alone. */
const CONTAINER_PARTS: ReadonlySet<string> = new Set(["app", "home.footer"]);
/** Radius around the last pointer position that a move must not land on. */
@@ -144,12 +152,14 @@ export interface FloatingChoice {
}
/**
* Keep `current` while it is collision-free (a free control never moves, even
* when the default corner becomes free again: stability beats tidiness);
* otherwise take the position with the lowest score. Ties keep `current`, then
* follow `FLOATING_POSITIONS` order. `moveObstacles` (the user's pointer and
* focused control) only count against positions the control would move to, so
* it never jumps under the cursor or onto the field being typed in.
* Keep `current` while it is collision-free; otherwise take the position with
* the lowest score. Ties keep `current`, then follow `FLOATING_POSITIONS`
* order. When `home` (the default corner) is given and both it and `current`
* are free, go back home: a spot picked for one screen must not linger over
* controls that appear on the next one (e.g. after leaving the voice page).
* `moveObstacles` (the user's pointer and focused control) only count against
* positions the control would move to, so it never jumps under the cursor or
* onto the field being typed in.
*/
export function chooseFloatingPosition(input: {
current: FloatingPosition;
@@ -159,6 +169,7 @@ export function chooseFloatingPosition(input: {
obstacles: readonly FloatingObstacle[];
moveObstacles?: readonly FloatingObstacle[];
positions?: readonly FloatingPosition[];
home?: FloatingPosition;
}): FloatingChoice {
const positions = input.positions ?? FLOATING_POSITIONS;
const moveObstacles = input.moveObstacles ?? [];
@@ -168,7 +179,10 @@ export function chooseFloatingPosition(input: {
return position === input.current || !moveObstacles.length ? score : score + floatingScore(footprint, moveObstacles);
};
const currentScore = scoreAt(input.current);
if (currentScore === 0) return { position: input.current, score: 0 };
if (currentScore === 0) {
if (input.home && input.home !== input.current && scoreAt(input.home) === 0) return { position: input.home, score: 0 };
return { position: input.current, score: 0 };
}
let best: FloatingChoice = { position: input.current, score: currentScore };
for (const position of positions) {
if (position === input.current) continue;
@@ -306,6 +320,12 @@ export interface FloatingAvoidanceOptions {
export interface FloatingAvoidance {
revalidate(): void;
/**
* The page behind the control changed (route/view or skin switch): drop any
* pending confirmation and re-place promptly instead of keeping a spot that
* was picked for the previous screen.
*/
screenChanged(): void;
readonly position: FloatingPosition;
dispose(): void;
}
@@ -328,6 +348,8 @@ export function bindFloatingAvoidance(options: FloatingAvoidanceOptions): Floati
let frozenUntil = 0;
let moveTimes: number[] = [];
let confirmTimer: ReturnType<typeof setTimeout> | undefined;
let urgentUntil = 0;
let screenTimers: ReturnType<typeof setTimeout>[] = [];
function scheduleConfirm(delay: number) {
if (confirmTimer) clearTimeout(confirmTimer);
confirmTimer = setTimeout(() => { confirmTimer = undefined; safeEvaluate(); }, Math.max(0, delay) + 20);
@@ -389,7 +411,7 @@ export function bindFloatingAvoidance(options: FloatingAvoidanceOptions): Floati
const rect = focused.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) moveObstacles.push({ rect: toRect(rect), weight: FLOATING_WEIGHT_POINTER });
}
const full = chooseFloatingPosition({ current: position, size, viewport, margins, obstacles, moveObstacles });
const full = chooseFloatingPosition({ current: position, size, viewport, margins, obstacles, moveObstacles, home: defaultPosition });
let next = full;
let nextCompact = false;
if (full.score > 0 && small && small.width > 0 && small.height > 0) {
@@ -405,16 +427,17 @@ export function bindFloatingAvoidance(options: FloatingAvoidanceOptions): Floati
}
const now = Date.now();
if (now < frozenUntil) return;
if (!pendingSince) {
const urgent = now < urgentUntil;
if (!urgent && !pendingSince) {
pendingSince = now;
scheduleConfirm(HOST_FLOATING_MOVE_CONFIRM_MS);
return;
}
if (now - pendingSince < HOST_FLOATING_MOVE_CONFIRM_MS) {
if (!urgent && now - pendingSince < HOST_FLOATING_MOVE_CONFIRM_MS) {
scheduleConfirm(HOST_FLOATING_MOVE_CONFIRM_MS - (now - pendingSince));
return;
}
if (now - lastMoveAt < HOST_FLOATING_MOVE_MIN_INTERVAL_MS) {
if (!urgent && now - lastMoveAt < HOST_FLOATING_MOVE_MIN_INTERVAL_MS) {
scheduleConfirm(HOST_FLOATING_MOVE_MIN_INTERVAL_MS - (now - lastMoveAt));
return;
}
@@ -472,11 +495,21 @@ export function bindFloatingAvoidance(options: FloatingAvoidanceOptions): Floati
}
safeEvaluate();
function screenChanged() {
if (disposed) return;
for (const pending of screenTimers) clearTimeout(pending);
pendingSince = 0;
urgentUntil = Date.now() + HOST_FLOATING_SCREEN_CHANGE_MS;
screenTimers = HOST_FLOATING_SCREEN_CHANGE_SAMPLES_MS.map(delay => setTimeout(safeEvaluate, delay));
}
return {
revalidate: safeEvaluate,
screenChanged,
get position() { return position; },
dispose() {
disposed = true;
for (const pending of screenTimers) clearTimeout(pending);
registry.delete(entry);
if (timer) clearInterval(timer);
if (clickTimer) clearTimeout(clickTimer);
+17
View File
@@ -135,6 +135,23 @@ try {
await settle();
results.push(snap("wall-removed"));
// Screen change (voice page -> home): the dock goes away and a new control
// appears exactly where the capsule was parked. screenChanged() re-places it
// at once (back to the now-free default corner), without the confirm delay.
const away = binding.position;
const capRect = control.getBoundingClientRect();
document.getElementById("mic").style.display = "none";
document.body.insertAdjacentHTML("beforeend", `<button type="button" id="export" style="position:fixed;left:${capRect.left}px;top:${capRect.top}px;width:${capRect.width}px;height:${capRect.height}px">Export identity</button>`);
binding.screenChanged();
await sleep(150);
results.push({ ...snap("screen-change"), away, exportCovered: (() => {
const r = document.getElementById("export").getBoundingClientRect();
return document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2) === host;
})() });
document.getElementById("export").remove();
document.getElementById("mic").style.display = "";
await settle();
// Weights: an unmarked skin button inside the protected "app" root is an
// ordinary control; the microphone is protected; invisible controls do not
// count; a custom div[tabindex] button does.
+6
View File
@@ -355,6 +355,12 @@ test("floating host controls move off skin controls at runtime", { skip: !CHROME
assert.equal(byName.wall.capsuleHit, true);
assert.equal(byName["wall-removed"].compact, false);
// A screen change re-places the capsule within ~150 ms: back to the free
// default corner, off the control that appeared under its old spot.
const screen = byName["screen-change"];
assert.notEqual(screen.away, "bottom-left", JSON.stringify(screen));
assert.equal(screen.position, "bottom-left", JSON.stringify(screen));
assert.equal(screen.exportCovered, false, JSON.stringify(screen));
// Weighting: plain skin buttons are ordinary even inside the protected app root.
assert.deepEqual(
{ plain: byName.weights.plain, mic: byName.weights.mic, ghost: byName.weights.ghost, custom: byName.weights.custom },