Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Align floating dock backgrounds and animations https://github.com/siyuan-note/siyuan/issues/20833

Danielb062a478f7af
diff --git a/app/src/assets/scss/business/_layout.scss b/app/src/assets/scss/business/_layout.scss
index aedb14cac3..eafb4ac5ce 100644
--- a/app/src/assets/scss/business/_layout.scss
+++ b/app/src/assets/scss/business/_layout.scss
@@ -439,7 +439,6 @@
position: fixed;
left: 0;
height: auto;
- background-color: var(--b3-body-background);
&#dockRight {
left: auto;
@@ -447,6 +446,42 @@
}
}
+ &--closing {
+ pointer-events: none;
+ }
+
+ // 背景独立于可滚动的图标栏,内圆角可以伸入编辑区而不被滚动容器裁剪。
+ &__backdrop {
+ position: fixed;
+ left: 0;
+ // 隐藏顶栏时窗口按钮仍位于背景之上,浮动面板和全屏内容也可覆盖背景。
+ z-index: 6;
+ pointer-events: none;
+ width: calc(var(--b3-ui-dock-width, 42px) + var(--b3-border-radius-b));
+ background-color: var(--b3-body-background);
+ background-image: var(--b3-window-background-image);
+ background-size: 100vw 100vh;
+ background-position: left var(--b3-dock-background-y);
+ background-repeat: no-repeat;
+ mask-image: linear-gradient(#000, #000),
+ radial-gradient(circle at 100% 100%, transparent calc(var(--b3-border-radius-b) - .5px), #000 var(--b3-border-radius-b)),
+ radial-gradient(circle at 100% 0, transparent calc(var(--b3-border-radius-b) - .5px), #000 var(--b3-border-radius-b));
+ mask-size: calc(100% - var(--b3-border-radius-b)) 100%,
+ var(--b3-border-radius-b) var(--b3-border-radius-b), var(--b3-border-radius-b) var(--b3-border-radius-b);
+ mask-position: left top, right top, right bottom;
+ mask-repeat: no-repeat;
+
+ &--right {
+ left: auto;
+ right: 0;
+ background-position-x: right;
+ mask-image: linear-gradient(#000, #000),
+ radial-gradient(circle at 0 100%, transparent calc(var(--b3-border-radius-b) - .5px), #000 var(--b3-border-radius-b)),
+ radial-gradient(circle at 0 0, transparent calc(var(--b3-border-radius-b) - .5px), #000 var(--b3-border-radius-b));
+ mask-position: right top, left top, left bottom;
+ }
+ }
+
&::-webkit-scrollbar {
display: none;
}
diff --git a/app/src/assets/scss/main/_main.scss b/app/src/assets/scss/main/_main.scss
index 9e5b826301..4efc505ca8 100644
--- a/app/src/assets/scss/main/_main.scss
+++ b/app/src/assets/scss/main/_main.scss
@@ -32,11 +32,12 @@ html {
}
body {
- background-image: var(--b3-body-background-gradient, radial-gradient(
+ --b3-window-background-image: var(--b3-body-background-gradient, radial-gradient(
ellipse 45% 20% at 0% 0%,
hsla(var(--b3-body-background-hl), calc(var(--b3-body-background-hl-opacity) * 0.5)) 0%,
transparent 100%
));
+ background-image: var(--b3-window-background-image);
}
.fullscreen {
diff --git a/app/src/layout/dock/barVisibility.ts b/app/src/layout/dock/barVisibility.ts
index 4a8602b57f..cc2538a9be 100644
--- a/app/src/layout/dock/barVisibility.ts
+++ b/app/src/layout/dock/barVisibility.ts
@@ -6,6 +6,81 @@ export const shouldShowDockSplit = (hasVisibleEntryBefore: boolean, hasVisibleEn
const hoveredDockBars = new WeakSet<HTMLElement>();
let floatingBarResizeObserver: ResizeObserver;
+const floatingBarEffects = new WeakMap<HTMLElement, {
+ backdrop: HTMLElement;
+ animations: Animation[];
+ closing: boolean;
+ frame: number;
+}>();
+
+const clearFloatingBarEffects = (element: HTMLElement) => {
+ const effects = floatingBarEffects.get(element);
+ effects?.animations.forEach(animation => animation.cancel());
+ if (effects) {
+ cancelAnimationFrame(effects.frame);
+ }
+ effects?.backdrop.remove();
+ floatingBarEffects.delete(element);
+ element.classList.remove("dock--closing");
+ element.inert = false;
+};
+
+const animateFloatingBar = (element: HTMLElement, from: number, to: number, closing: boolean, panel?: HTMLElement) => {
+ const effects = floatingBarEffects.get(element);
+ const opacity = effects.animations.length ? getComputedStyle(element).opacity : closing ? "1" : "0";
+ effects.animations.forEach(animation => animation.cancel());
+ cancelAnimationFrame(effects.frame);
+ effects.closing = closing;
+ element.classList.toggle("dock--closing", closing);
+ element.inert = closing;
+ effects.animations = [element, effects.backdrop].flatMap(target => [
+ target.animate([{transform: `translateX(${from}px)`}, {transform: `translateX(${to}px)`}], {
+ duration: 200, easing: "cubic-bezier(0, 0, .2, 1)", fill: "forwards",
+ }),
+ target.animate([{opacity}, {opacity: closing ? "0" : "1"}], {
+ duration: 300, easing: "cubic-bezier(0, 0, .2, 1)", fill: "forwards",
+ }),
+ ]);
+ const finish = () => {
+ if (closing) {
+ clearFloatingBarEffects(element);
+ syncDockBarVisibility();
+ } else {
+ effects.animations.forEach(animation => animation.cancel());
+ effects.animations = [];
+ }
+ };
+ effects.animations[effects.animations.length - 1].onfinish = finish;
+ if (panel) {
+ // 原生过渡在反向播放时会缩短时长,复制其关键帧和时间轴以保持边缘贴合。
+ effects.frame = requestAnimationFrame(() => {
+ const offset = (element.id === "dockLeft" ? -1 : 1) * element.clientWidth;
+ for (const source of panel.getAnimations()) {
+ if (!(source instanceof CSSTransition) || !["transform", "opacity"].includes(source.transitionProperty)) {
+ continue;
+ }
+ const transform = source.transitionProperty === "transform";
+ const frames = (source.effect as KeyframeEffect).getKeyframes().map(frame => ({
+ offset: frame.offset, easing: frame.easing,
+ ...(transform ? {transform: `translateX(${new DOMMatrixReadOnly(frame.transform as string).m41 + offset}px)`} :
+ {opacity: frame.opacity}),
+ }));
+ [element, effects.backdrop].forEach((target, index) => {
+ const slot = index * 2 + (transform ? 0 : 1);
+ effects.animations[slot].cancel();
+ const animation = target.animate(frames, {...source.effect.getTiming(), fill: "forwards"});
+ if (typeof source.startTime === "number") {
+ animation.startTime = source.startTime;
+ }
+ effects.animations[slot] = animation;
+ });
+ }
+ effects.animations[effects.animations.length - 1].onfinish = finish;
+ });
+ }
+};
+
+const getTranslateX = (element: HTMLElement) => new DOMMatrixReadOnly(getComputedStyle(element).transform).m41;
export const adjustDockPadding = () => {
const layoutElement = window.siyuan.layout?.layout?.children[0]?.element;
@@ -35,6 +110,7 @@ export const adjustDockPadding = () => {
export const syncDockBarVisibility = () => {
let changed = false;
let hasFloatingBar = false;
+ const cornerElements: HTMLElement[] = [];
// 右侧停靠栏占用布局空间时记录宽度,供通知等固定定位元素避让
let rightDockWidth = "0px";
document.querySelectorAll<HTMLElement>(".dock").forEach((item) => {
@@ -56,24 +132,62 @@ export const syncDockBarVisibility = () => {
}
const floating = Boolean(canFloat && (floatingPanelOpen || hoveredDockBars.has(item)));
const wasFloating = item.classList.contains("dock--float");
- item.classList.toggle("dock--float", floating);
+ const effects = floatingBarEffects.get(item);
+ const direction = item.id === "dockLeft" ? -1 : 1;
+ if (!floating && effects) {
+ if (canFloat && dock.isPanelVisible() && !dock.layout.element.classList.contains("fn__none")) {
+ if (!effects.closing) {
+ animateFloatingBar(item, getTranslateX(item),
+ direction * (dock.layout.element.clientWidth + item.clientWidth + 8), true, dock.layout.element);
+ }
+ } else {
+ clearFloatingBarEffects(item);
+ }
+ }
+ const displayingFloat = floating || floatingBarEffects.has(item);
+ item.classList.toggle("dock--float", displayingFloat);
const hidden = !shouldShowDockBar(
window.siyuan.config.uiLayout.hideDock,
sectionVisibility.some(Boolean),
- floating,
+ displayingFloat,
);
- changed = item.classList.contains("fn__none") !== hidden || wasFloating !== floating || changed;
+ changed = item.classList.contains("fn__none") !== hidden || wasFloating !== displayingFloat || changed;
item.classList.toggle("fn__none", hidden);
- if (item.id === "dockRight" && !hidden && !floating) {
+ if (item.id === "dockRight" && !hidden && !displayingFloat) {
rightDockWidth = "var(--b3-ui-dock-width, 42px)";
}
- if (floating) {
+ if (displayingFloat) {
hasFloatingBar = true;
// 保留停靠栏原有的顶栏避让和高度范围,临时显示时脱离编辑区布局。
const rowRect = item.parentElement.getBoundingClientRect();
item.style.top = rowRect.top + "px";
item.style.bottom = (window.innerHeight - rowRect.bottom) + "px";
item.style.zIndex = (Math.max(10, Number(dock.layout.element.style.zIndex)) - 1).toString();
+ if (floating && !floatingBarEffects.has(item)) {
+ const backdrop = document.createElement("div");
+ backdrop.className = item.id === "dockLeft" ? "dock__backdrop" : "dock__backdrop dock__backdrop--right";
+ backdrop.setAttribute("aria-hidden", "true");
+ item.before(backdrop);
+ floatingBarEffects.set(item, {backdrop, animations: [], closing: false, frame: 0});
+ }
+ const currentEffects = floatingBarEffects.get(item);
+ const cornerElement = document.body.classList.contains("body--toolbar-hide") ?
+ window.siyuan.layout?.centerLayout?.element.querySelector<HTMLElement>(item.id === "dockLeft" ?
+ ".layout__wnd--left .layout-tab-container" : ".layout__wnd--right .layout-tab-container") : undefined;
+ // 融合顶栏的页签保持透明,内圆角从页签下方的编辑内容上沿开始。
+ const backgroundTop = cornerElement?.clientHeight ? cornerElement.getBoundingClientRect().top : rowRect.top;
+ if (cornerElement) {
+ cornerElements.push(cornerElement);
+ }
+ currentEffects.backdrop.style.top = backgroundTop + "px";
+ currentEffects.backdrop.style.bottom = item.style.bottom;
+ currentEffects.backdrop.style.setProperty("--b3-dock-background-y", -backgroundTop + "px");
+ if (floating && (!effects || effects.closing)) {
+ // 面板先进入窗口,图标栏贴着面板外沿使用相同的位移曲线滑入。
+ const from = effects ? getTranslateX(item) : floatingPanelOpen ?
+ getTranslateX(dock.layout.element) + direction * item.clientWidth : direction * item.clientWidth;
+ animateFloatingBar(item, from, 0, false, floatingPanelOpen ? dock.layout.element : undefined);
+ }
} else if (wasFloating) {
item.style.top = "";
item.style.bottom = "";
@@ -102,6 +216,7 @@ export const syncDockBarVisibility = () => {
floatingBarResizeObserver.disconnect();
floatingBarResizeObserver = undefined;
}
+ cornerElements.forEach(element => floatingBarResizeObserver?.observe(element));
return changed;
};
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 0f38be13aa..0372ee117b 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -561,18 +561,19 @@ export class Dock {
window.clearTimeout(this.hideDockTimeout);
this.hideDockTimeout = 0;
const canRevealBar = (this.position === "Left" || this.position === "Right") && window.siyuan.config.uiLayout.hideDock;
- const needsBar = canRevealBar && !this.elements[0].parentElement.classList.contains("dock--float");
+ const needsBar = canRevealBar && (!this.elements[0].parentElement.classList.contains("dock--float") ||
+ this.elements[0].parentElement.classList.contains("dock--closing"));
if (isTouchHoverInput() || this.showDockTimeout || !this.isFloating() ||
!needsBar && (!this.panelVisible || this.layout.element.style.opacity === "1" || canRevealBar && !this.hasActive())) {
return;
}
this.showDockTimeout = window.setTimeout(() => {
this.showDockTimeout = 0;
+ this.showDock();
if (canRevealBar && window.siyuan.config.uiLayout.hideDock && this.isFloating()) {
this.layout.element.style.zIndex = (++window.siyuan.zIndex).toString();
setDockBarHover(this.elements[0].parentElement, true);
}
- this.showDock();
}, Constants.TIMEOUT_DOCK_TOGGLE);
}
diff --git a/app/tests/dockBarVisibility.test.js b/app/tests/dockBarVisibility.test.js
index 6bf694384a..06417eb0f3 100644
--- a/app/tests/dockBarVisibility.test.js
+++ b/app/tests/dockBarVisibility.test.js
@@ -15,13 +15,23 @@ const browserCases = async (sources, css, template) => {
};
document.head.innerHTML = "<style></style>";
document.querySelector("style").textContent = css;
- document.body.innerHTML = '<div id="toolbar" class="toolbar"></div>' + template +
+ if (window.innerWidth === 700) {
+ document.body.style.setProperty("--b3-body-background-gradient", document.body.classList.contains("body--toolbar-hide") ?
+ "none" : "radial-gradient(ellipse 60% 40% at 0% 0%, rgba(220, 80, 40, .6), transparent)");
+ }
+ document.body.innerHTML = '<div id="toolbar" class="toolbar">' +
+ '<span style="display:block;width:20px;height:20px;margin:6px;background:rgb(180,40,80)"></span></div>' + template +
'<div id="status" class="status"></div><div id="commonMenu" class="fn__none"></div>';
const layouts = document.getElementById("layouts");
layouts.classList.add("fn__flex-column");
layouts.innerHTML = '<div id="outer" class="fn__flex fn__flex-1">' +
'<div id="left" class="layout fn__flex-column"></div><div class="layout__resize"></div>' +
- '<div id="center" class="layout__center fn__flex-1 fn__flex"><div class="layout-tab-container fn__flex-1">Editor</div></div>' +
+ '<div id="center" class="layout__center fn__flex-1 fn__flex"><div class="fn__flex-1 fn__flex">' +
+ '<div data-type="wnd" class="fn__flex-column fn__flex-1">' +
+ '<div class="fn__flex"><ul class="layout-tab-bar fn__flex"><li class="item item--focus">' +
+ '<span class="item__text">Document</span></li></ul><ul class="layout-tab-bar layout-tab-bar--readonly fn__flex-1">' +
+ '<li class="item item--readonly"><span class="fn__flex-1"></span></li></ul></div>' +
+ '<div class="layout-tab-container fn__flex-1">Editor</div></div></div></div>' +
'<div class="layout__resize"></div>' +
'<div id="right" class="layout fn__flex-column"></div></div><div class="layout__resize"></div>' +
'<div id="bottom" class="layout fn__flex"></div>';
@@ -35,6 +45,10 @@ const browserCases = async (sources, css, template) => {
const right = makeLayout("right");
const bottom = makeLayout("bottom");
const center = {element: document.getElementById("center")};
+ if (document.body.classList.contains("body--toolbar-hide")) {
+ center.element.firstElementChild.classList.add("layout__wnd--left", "layout__wnd--right", "layout__wnd--center");
+ center.element.querySelector('[data-type="wnd"]').firstElementChild.style.paddingLeft = "112px";
+ }
const outer = {element: document.getElementById("outer"), children: [left, center, right]};
window.siyuan = {zIndex: 10, config: {uiLayout: {hideDock: true}, appearance: {hideStatusBar: false}, system: {os: "linux"}},
layout: {layout: {children: [outer, bottom]}, centerLayout: center}};
@@ -133,21 +147,57 @@ const browserCases = async (sources, css, template) => {
position === "Left" ? barRect.right : barRect.left, "panel stays beside the bar");
const temporary = {bar: rect(element), panel: rect(dock.layout.element),
barStyle: appearance(element), panelStyle: appearance(dock.layout.element)};
- const effectiveBackground = getComputedStyle(element).backgroundColor;
- check.equal(effectiveBackground, getComputedStyle(document.body).backgroundColor, "bar uses the window background color");
+ const backdrop = element.previousElementSibling;
+ check.ok(backdrop.classList.contains("dock__backdrop"));
+ check.equal(getComputedStyle(backdrop).backgroundColor, getComputedStyle(document.body).backgroundColor);
+ check.equal(getComputedStyle(backdrop).backgroundImage, getComputedStyle(document.body).backgroundImage,
+ "the overlay shares the window gradient, including theme overrides");
+ const rowTop = document.body.classList.contains("body--toolbar-hide") ?
+ center.element.querySelector(".layout-tab-container").getBoundingClientRect().top :
+ element.parentElement.getBoundingClientRect().top;
+ const radius = parseFloat(getComputedStyle(center.element).borderTopLeftRadius);
+ const edge = position === "Left" ? barRect.right : barRect.left;
+ const sign = position === "Left" ? 1 : -1;
+ const toolbarButton = document.getElementById("toolbar").firstElementChild.getBoundingClientRect();
+ const samples = [
+ {x: edge - sign * 2, y: rowTop + 2},
+ {x: edge + sign * 2, y: rowTop + 1},
+ {x: edge + sign * (radius + 2), y: rowTop + 2},
+ {x: edge + sign * 2, y: barRect.bottom - 2},
+ {x: toolbarButton.x + toolbarButton.width / 2, y: toolbarButton.y + toolbarButton.height / 2},
+ ];
+ const pixels = () => require("electron").ipcRenderer.invoke("dock-bar-pixels", samples);
+ const overlayPixels = await pixels();
window.siyuan.config.uiLayout.hideDock = false;
bar.syncDockBarVisibility();
dock.showDock(true);
check.deepEqual({bar: rect(element), panel: rect(dock.layout.element),
barStyle: appearance(element), panelStyle: appearance(dock.layout.element)}, temporary,
"floating bars preserve the normal dock geometry and panel corners");
+ const normalPixels = await pixels();
+ overlayPixels.forEach((pixel, index) => pixel.forEach((channel, component) =>
+ check.ok(Math.abs(channel - normalPixels[index][component]) <= 3,
+ `background and inner corner sample ${index} matches the normal dock: ${pixel} / ${normalPixels[index]}`)));
window.siyuan.config.uiLayout.hideDock = true;
bar.syncDockBarVisibility();
dock.showDock(true);
+ await settle();
const expandedEditor = rect(center.element);
if (process.env.SIYUAN_DOCK_TEST_SCREENSHOTS) {
await require("electron").ipcRenderer.invoke("dock-bar-capture", position);
}
+ if (document.body.classList.contains("body--toolbar-hide")) {
+ const header = center.element.querySelector('[data-type="wnd"]').firstElementChild;
+ header.classList.add("fn__none");
+ await settle();
+ check.equal(parseFloat(element.previousElementSibling.style.top),
+ center.element.querySelector(".layout-tab-container").getBoundingClientRect().top,
+ "merged layout without tabs restores the top corner to the editor edge");
+ header.classList.remove("fn__none");
+ await settle();
+ check.equal(parseFloat(element.previousElementSibling.style.top), rowTop,
+ "opening tabs places the inner corner below the merged tab bar");
+ }
dock.hideDockByHover();
// 覆盖停靠栏最外侧像素,不能因旧的边缘范围少两个像素而收起。
@@ -175,6 +225,32 @@ const browserCases = async (sources, css, template) => {
await settle();
check.ok(hidden(element), "position refreshes do not cancel a pending hover close");
dock.showDock();
+ await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ const during = [];
+ for (let frame = 0; frame < 4; frame++) {
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ const movingPanel = dock.layout.element.getBoundingClientRect();
+ const movingBar = element.getBoundingClientRect();
+ const gap = position === "Left" ? movingPanel.left - movingBar.right : movingBar.left - movingPanel.right;
+ check.ok(Math.abs(gap) < 2, `bar follows the panel during entry, gap=${gap}`);
+ during.push(position === "Left" ? movingBar.left : window.innerWidth - movingBar.right);
+ check.deepEqual(rect(center.element), initialEditor, "animation does not move the editor");
+ }
+ check.ok(during[0] < 0 && during.at(-1) > during[0], "the bar slides into the window instead of appearing instantly");
+ dock.hideDock();
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ dock.showDock();
+ for (let frame = 0; frame < 4; frame++) {
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ const movingPanel = dock.layout.element.getBoundingClientRect();
+ const movingBar = element.getBoundingClientRect();
+ const gap = position === "Left" ? movingPanel.left - movingBar.right : movingBar.left - movingPanel.right;
+ check.ok(Math.abs(gap) < 2, `interrupted animations keep the bar attached, gap=${gap}`);
+ }
+ await settle();
+ check.equal(element.inert, false, "reentering during close restores interaction");
+ check.equal(element.parentElement.querySelectorAll(".dock__backdrop").length, 1,
+ "interrupted animation reuses its background");
const input = dock.layout.element.querySelector(`[data-id="${position}two"] input`);
input.focus();
@@ -229,11 +305,13 @@ const browserCases = async (sources, css, template) => {
check.ok(!hidden(leftBar) && !hidden(rightBar));
checkRightDockSpace("a temporary right bar overlays the editor instead of reserving space");
docks.Left.hideDock();
+ await settle();
check.ok(hidden(leftBar) && !hidden(rightBar), "each side closes independently");
check.equal(bottom.element.style.left, "0px");
check.equal(bottom.element.style.right, "0px", "temporary bars do not narrow the bottom panel");
check.deepEqual(rect(center.element), initialEditor);
docks.Right.hideDock();
+ await settle();
check.ok(hidden(leftBar) && hidden(rightBar), "the bottom panel alone reveals no side bars");
check.equal(bottom.element.style.right, "0px", "closing the side bar restores bottom panel width");
docks.Bottom.hideDock();
@@ -299,6 +377,17 @@ if (process.versions.electron && process.type === "browser") {
nodeIntegration: true, contextIsolation: false, offscreen: true, backgroundThrottling: false,
}});
let captureName;
+ ipcMain.handle("dock-bar-pixels", async (_event, points) => {
+ const capture = await win.webContents.capturePage();
+ const bitmap = capture.toBitmap();
+ const size = capture.getSize();
+ const [width] = win.getContentSize();
+ const scale = size.width / width;
+ return points.map(({x, y}) => {
+ const offset = (Math.floor(y * scale) * size.width + Math.floor(x * scale)) * 4;
+ return Array.from(bitmap.subarray(offset, offset + 4));
+ });
+ });
if (process.env.SIYUAN_DOCK_TEST_SCREENSHOTS) {
ipcMain.handle("dock-bar-capture", async (_event, position) => {
assert.ok(["Left", "Right"].includes(position));