Skip to content

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

:art: Improve dock panel responsiveness when resizing the window https://github.com/siyuan-note/siyuan/issues/12440

Daniel5db85ba687cc
diff --git a/app/src/boot/globalEvent/event.ts b/app/src/boot/globalEvent/event.ts
index 5737efc143..b2d2e5c890 100644
--- a/app/src/boot/globalEvent/event.ts
+++ b/app/src/boot/globalEvent/event.ts
@@ -125,10 +125,11 @@ export const initWindowEvent = (app: App) => {
// 拖拽文档页签或标题/列表项块标时,按浮窗模型控制文档树所在浮动 dock 的显隐:
// 鼠标在边缘触发区或面板内则展开,离开则收起 https://github.com/siyuan-note/siyuan/issues/18043
if (!isWindow() &&
- (!window.siyuan.layout.leftDock.pin || !window.siyuan.layout.rightDock.pin || !window.siyuan.layout.bottomDock.pin)) {
+ (window.siyuan.layout.leftDock.isFloating() || window.siyuan.layout.rightDock.isFloating() ||
+ window.siyuan.layout.bottomDock.isFloating())) {
const fileDock = getDockByType("file");
// 文档树所在 dock 为浮动且文档树图标激活时才处理
- if (fileDock && !fileDock.pin &&
+ if (fileDock && fileDock.isFloating() &&
document.querySelector('.dock__items > .dock__item--active[data-type="file"]')) {
let gutterBlockType = "";
for (const itemType of event.dataTransfer.types) {
diff --git a/app/src/boot/globalEvent/mousemove.ts b/app/src/boot/globalEvent/mousemove.ts
index 9bbebd4d2d..518f616a76 100644
--- a/app/src/boot/globalEvent/mousemove.ts
+++ b/app/src/boot/globalEvent/mousemove.ts
@@ -107,7 +107,7 @@ export const windowMouseMove = (event: MouseEvent) => {
!hasClosestByClassName(target, "protyle-toolbar") &&
!hasClosestByClassName(target, "protyle-util");
const leftDock = window.siyuan.layout.leftDock;
- const leftTrigger = canTrigger && inYRange && !leftDock.pin && leftDock.layout.element.clientWidth > 0 &&
+ const leftTrigger = canTrigger && inYRange && leftDock.isFloating() && leftDock.layout.element.clientWidth > 0 &&
event.clientX < Math.max(document.getElementById("dockLeft").clientWidth + 1, 16) &&
// 隐藏停靠栏会导致点击两侧内容触发浮动面板弹出,因此需减小鼠标范围
(leftDock.elements[0].clientWidth > 0 || event.clientX < 8);
@@ -118,7 +118,7 @@ export const windowMouseMove = (event: MouseEvent) => {
}
const rightDock = window.siyuan.layout.rightDock;
- const rightTrigger = canTrigger && inYRange && !rightDock.pin && rightDock.layout.element.clientWidth > 0 &&
+ const rightTrigger = canTrigger && inYRange && rightDock.isFloating() && rightDock.layout.element.clientWidth > 0 &&
event.clientX > window.innerWidth - Math.max(document.getElementById("dockRight").clientWidth - 2, 16) &&
(rightDock.elements[0].clientWidth > 0 || event.clientX > window.innerWidth - 8);
if (rightTrigger || rightDock.layout.element.contains(target)) {
@@ -128,7 +128,7 @@ export const windowMouseMove = (event: MouseEvent) => {
}
const bottomDock = window.siyuan.layout.bottomDock;
- const bottomTrigger = canTrigger && !bottomDock.pin && bottomDock.layout.element.clientHeight > 0 &&
+ const bottomTrigger = canTrigger && bottomDock.isFloating() && bottomDock.layout.element.clientHeight > 0 &&
event.clientY > Math.min(window.innerHeight - 10, window.innerHeight - statusHeight);
if (bottomTrigger || bottomDock.layout.element.contains(target)) {
bottomDock.showDockByHover();
diff --git a/app/src/boot/onGetConfig.ts b/app/src/boot/onGetConfig.ts
index a1f6713c8d..ccaaf5ea57 100644
--- a/app/src/boot/onGetConfig.ts
+++ b/app/src/boot/onGetConfig.ts
@@ -37,6 +37,7 @@ import {getAllEditor} from "../layout/getAll";
import {openDesktopOnboarding} from "../onboarding";
import {ensureUILayout} from "../util/ensureUILayout";
import {dispatchPluginGlobalShortcut} from "../plugin/globalShortcut";
+import {requestResponsiveDockLayout} from "../layout/dock/responsive";
export const onGetConfig = (isStart: boolean, app: App) => {
correctHotkey(app);
@@ -69,7 +70,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {
JSONToLayout(app, isStart);
setTimeout(() => {
adjustLayout();
- }); // 等待 dock 中 !this.pin 的 setTimeout
+ }); // 等待浮动 Dock 的初始化完成
/// #if !BROWSER
sendGlobalShortcut(app);
/// #endif
@@ -99,6 +100,7 @@ export const onGetConfig = (isStart: boolean, app: App) => {
let resizeTimeout = 0;
let firstResize = true;
window.addEventListener("resize", () => {
+ requestResponsiveDockLayout();
if (firstResize) {
recordBeforeResizeTop();
firstResize = false;
diff --git a/app/src/layout/dock/index.ts b/app/src/layout/dock/index.ts
index 9e2b4e3e41..66eafbfdce 100644
--- a/app/src/layout/dock/index.ts
+++ b/app/src/layout/dock/index.ts
@@ -31,6 +31,9 @@ import {getDockHotkey} from "./hotkey";
import {resolveDockPanelVisibility} from "./panelVisibility";
const TYPES = ["file", "outline", "inbox", "bookmark", "tag", "graph", "globalGraph", "backlink", "agentChat"];
+const DEFAULT_DOCK_SIZE = 232;
+const WIDE_DOCK_SIZE = 320;
+const WIDE_DOCK_TYPES = ["graph", "globalGraph", "backlink", "inbox"];
type TDockTabData = Config.IUILayoutDockTab & {entryId?: string};
export class Dock {
@@ -46,6 +49,8 @@ export class Dock {
private hideDockTimeout = 0;
private panelVisible = true;
private collapsedPanelSize = "";
+ private responsiveFloating = false;
+ private responsiveManualOverride = false;
constructor(options: {
app: App,
@@ -241,7 +246,7 @@ export class Dock {
this.showDockByHover();
});
this.layout.element.addEventListener("mouseleave", (event: MouseEvent & { toElement: HTMLElement }) => {
- if (event.buttons !== 0 || this.pin || event.toElement?.classList.contains("b3-menu") ||
+ if (event.buttons !== 0 || !this.isFloating() || event.toElement?.classList.contains("b3-menu") ||
event.toElement?.classList.contains("tooltip")) {
return;
}
@@ -262,6 +267,7 @@ export class Dock {
const direction = this.position === "Bottom" ? "tb" : "lr";
const x = event[direction === "lr" ? "clientX" : "clientY"];
const currentSize = direction === "lr" ? this.layout.element.clientWidth : this.layout.element.clientHeight;
+ let responsiveResizePrepared = false;
documentSelf.onmousemove = (moveEvent: MouseEvent) => {
moveEvent.preventDefault();
moveEvent.stopPropagation();
@@ -273,22 +279,20 @@ export class Dock {
} else {
currentNowSize = (currentSize + (x - moveEvent.clientY));
}
- let minSize = 232;
- Array.from(this.layout.element.querySelectorAll(".file-tree")).find((item) => {
- if (item.classList.contains("sy__backlink") || item.classList.contains("sy__graph")
- || item.classList.contains("sy__globalGraph") || item.classList.contains("sy__inbox")) {
- if (!item.classList.contains("fn__none") && !hasClosestByClassName(item, "fn__none")) {
- minSize = 320;
- return true;
- }
- }
- });
+ const minSize = this.getResponsiveMinimumSize();
if (currentNowSize < minSize && direction === "lr") {
return;
}
if (currentNowSize < 64 && direction === "tb") {
return;
}
+ if (currentNowSize === currentSize) {
+ return;
+ }
+ if (!responsiveResizePrepared) {
+ this.prepareForManualResize();
+ responsiveResizePrepared = true;
+ }
this.layout.element.style[direction === "lr" ? "width" : "height"] = currentNowSize + "px";
};
@@ -321,6 +325,74 @@ export class Dock {
}
}
+ public isFloating() {
+ return !this.pin || this.responsiveFloating;
+ }
+
+ public isResponsiveFloating() {
+ return this.responsiveFloating;
+ }
+
+ public hasResponsiveManualOverride() {
+ return this.responsiveManualOverride;
+ }
+
+ public clearResponsiveManualOverride() {
+ this.responsiveManualOverride = false;
+ }
+
+ public setResponsiveFloating(value: boolean) {
+ const responsiveFloating = value && this.position !== "Bottom" && this.pin;
+ if (this.responsiveFloating === responsiveFloating) {
+ return false;
+ }
+ this.restoreResponsivePreferredSize();
+ this.responsiveFloating = responsiveFloating;
+ this.applyFloatingState(false);
+ return true;
+ }
+
+ public adjustResponsiveCenterLayout() {
+ adjustLayout(window.siyuan.layout.centerLayout);
+ }
+
+ public prepareForManualResize() {
+ if (this.position === "Bottom") {
+ return;
+ }
+ this.responsiveManualOverride = true;
+ this.restoreResponsivePreferredSize();
+ }
+
+ public getResponsivePreferredSize() {
+ const isBottom = this.position === "Bottom";
+ if (!isBottom) {
+ const responsiveWidth = parseInt(this.layout.element.getAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
+ if (responsiveWidth > 0) {
+ return responsiveWidth;
+ }
+ }
+ const dimension = isBottom ? "height" : "width";
+ const styleSize = parseInt(this.layout.element.style[dimension]);
+ if (styleSize > 0) {
+ return styleSize;
+ }
+ const clientSize = isBottom ? this.layout.element.clientHeight : this.layout.element.clientWidth;
+ return clientSize || this.getMaxSize();
+ }
+
+ public getResponsiveMinimumSize() {
+ if (this.position === "Bottom") {
+ return 64;
+ }
+ const widePanel = Array.from(this.layout.element.querySelectorAll(".file-tree")).find((item) => {
+ return (item.classList.contains("sy__backlink") || item.classList.contains("sy__graph") ||
+ item.classList.contains("sy__globalGraph") || item.classList.contains("sy__inbox")) &&
+ !item.classList.contains("fn__none") && !hasClosestByClassName(item, "fn__none");
+ });
+ return widePanel ? WIDE_DOCK_SIZE : DEFAULT_DOCK_SIZE;
+ }
+
public isPanelVisible() {
return this.panelVisible && this.hasActive();
}
@@ -353,7 +425,7 @@ export class Dock {
}
const hadPanelFocus = Boolean(this.layout.element.querySelector(".layout__tab--active") ||
document.activeElement && this.layout.element.contains(document.activeElement));
- if (this.pin) {
+ if (!this.isFloating()) {
recordBeforeResizeTop();
}
if (!resolution.visible) {
@@ -363,7 +435,7 @@ export class Dock {
}
this.panelVisible = resolution.storedVisible;
this.layout.element.classList.toggle("fn__none", !resolution.visible);
- if (resolution.visible && this.pin) {
+ if (resolution.visible && !this.isFloating()) {
adjustLayout();
this.resizeElement.classList.remove("fn__none");
} else {
@@ -395,13 +467,23 @@ export class Dock {
}
public togglePin() {
+ if (this.responsiveFloating) {
+ this.responsiveFloating = false;
+ this.responsiveManualOverride = true;
+ } else {
+ this.pin = !this.pin;
+ this.responsiveManualOverride = this.pin;
+ }
+ this.restoreResponsivePreferredSize();
+ this.applyFloatingState(true);
+ }
+
+ private applyFloatingState(isSaveLayout: boolean) {
this.clearDockHoverTimeout();
- this.pin = !this.pin;
- const hasActive = this.elements[0].querySelector(".dock__item--active") ||
- this.elements[1].querySelector(".dock__item--active");
- this.resetDockPosition(hasActive ? true : false);
+ const hasActive = this.hasActive();
+ this.resetDockPosition(hasActive);
this.layout.element.style.opacity = "";
- if (!this.pin) {
+ if (this.isFloating()) {
this.resizeElement.classList.add("fn__none");
this.hideDock(true);
} else {
@@ -411,11 +493,23 @@ export class Dock {
this.resizeElement.classList.remove("fn__none");
}
}
- this.layout.element.classList.toggle("layout--float");
- resizeTabs();
+ this.layout.element.classList.toggle("layout--float", this.isFloating());
+ resizeTabs(isSaveLayout);
setTabPosition(true);
}
+ private restoreResponsivePreferredSize() {
+ if (this.position === "Bottom") {
+ return;
+ }
+ const responsiveWidth = parseInt(this.layout.element.getAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
+ this.layout.element.style.maxWidth = "";
+ this.layout.element.removeAttribute(Constants.ATTRIBUTE_DOCK_WIDTH);
+ if (responsiveWidth > 0) {
+ this.layout.element.style.width = responsiveWidth + "px";
+ }
+ }
+
private resetDockPosition(show: boolean) {
const size = this.getCurrentSize();
if (this.position === "Left") {
@@ -430,7 +524,8 @@ export class Dock {
public showDockByHover() {
window.clearTimeout(this.hideDockTimeout);
this.hideDockTimeout = 0;
- if (!this.panelVisible || this.showDockTimeout || this.pin || this.layout.element.style.opacity === "1") {
+ if (!this.panelVisible || this.showDockTimeout || !this.isFloating() ||
+ this.layout.element.style.opacity === "1") {
return;
}
this.showDockTimeout = window.setTimeout(() => {
@@ -442,7 +537,8 @@ export class Dock {
public hideDockByHover() {
window.clearTimeout(this.showDockTimeout);
this.showDockTimeout = 0;
- if (!this.panelVisible || this.hideDockTimeout || this.pin || this.layout.element.style.opacity === "0") {
+ if (!this.panelVisible || this.hideDockTimeout || !this.isFloating() ||
+ this.layout.element.style.opacity === "0") {
return;
}
this.hideDockTimeout = window.setTimeout(() => {
@@ -460,7 +556,7 @@ export class Dock {
public showDock(reset = false) {
this.clearDockHoverTimeout();
- if (!this.panelVisible || (!reset && (this.pin || this.layout.element.style.opacity === "1")) ||
+ if (!this.panelVisible || (!reset && (!this.isFloating() || this.layout.element.style.opacity === "1")) ||
(!this.elements[0].querySelector(".dock__item--active") && !this.elements[1].querySelector(".dock__item--active"))
) {
return;
@@ -502,7 +598,7 @@ export class Dock {
public hideDock(reset = false) {
this.clearDockHoverTimeout();
- if (!reset && (this.layout.element.style.opacity === "0" || this.pin)) {
+ if (!reset && (this.layout.element.style.opacity === "0" || !this.isFloating())) {
return;
}
// 关系图全屏不应该退出 & https://github.com/siyuan-note/siyuan/issues/11775
@@ -567,7 +663,7 @@ export class Dock {
return;
}
}
- if (this.pin && !restoredPanel) {
+ if (!this.isFloating() && !restoredPanel) {
recordBeforeResizeTop();
}
if (show && target.classList.contains("dock__item--active")) {
@@ -797,7 +893,7 @@ export class Dock {
if ((type === "graph" || type === "globalGraph") && this.layout.element.querySelector(".fullscreen")) {
document.getElementById("drag")?.classList.add("fn__hidden");
}
- if (this.pin) {
+ if (!this.isFloating()) {
this.layout.element.style.opacity = "";
this.hideResizeTimeout = window.setTimeout(() => {
if (this.panelVisible && this.hasActive()) {
@@ -876,7 +972,7 @@ export class Dock {
}
// 等待 dock 面板动画结束
- if (this.pin) {
+ if (!this.isFloating()) {
let rafId: number;
const updateTabPos = () => {
setTabPosition(true);
@@ -985,13 +1081,15 @@ export class Dock {
}
const activesElement = [...this.elements[0].querySelectorAll(".dock__item--active"),
...this.elements[1].querySelectorAll(".dock__item--active")];
+ const preferredWidth = this.layout.element.style.maxWidth ?
+ this.getResponsivePreferredSize() : this.layout.element.clientWidth;
activesElement.forEach((item) => {
if (this.position === "Left" || this.position === "Right") {
if (item.getAttribute("data-index") === "1" && activesElement.length > 1) {
const dockElement = (this.data[item.getAttribute("data-type") as TDock] as Model).parent.parent.element;
item.setAttribute("data-height", dockElement.style.height ? dockElement.clientHeight.toString() : "");
}
- item.setAttribute("data-width", this.layout.element.clientWidth.toString());
+ item.setAttribute("data-width", preferredWidth.toString());
} else {
if (item.getAttribute("data-index") === "1" && activesElement.length > 1) {
const dockElement = (this.data[item.getAttribute("data-type") as TDock] as Model).parent.parent.element;
@@ -1013,9 +1111,10 @@ export class Dock {
[...this.elements[0].querySelectorAll(".dock__item--active"), ...this.elements[1].querySelectorAll(".dock__item--active")].forEach((item) => {
let size;
if (this.position === "Left" || this.position === "Right") {
- size = parseInt(item.getAttribute("data-width")) || (["graph", "globalGraph", "backlink"].includes(item.getAttribute("data-type")) ? 320 : 232);
+ size = parseInt(item.getAttribute("data-width")) ||
+ (WIDE_DOCK_TYPES.includes(item.getAttribute("data-type")) ? WIDE_DOCK_SIZE : DEFAULT_DOCK_SIZE);
} else {
- size = parseInt(item.getAttribute("data-height")) || 232;
+ size = parseInt(item.getAttribute("data-height")) || DEFAULT_DOCK_SIZE;
}
if (size > max) {
max = size;
diff --git a/app/src/layout/dock/responsive.ts b/app/src/layout/dock/responsive.ts
new file mode 100644
index 0000000000..ba5838a8a0
--- /dev/null
+++ b/app/src/layout/dock/responsive.ts
@@ -0,0 +1,166 @@
+import {Constants} from "../../constants";
+import {
+ getCenterMinimumSize,
+ type ICenterMinimumLayoutNode,
+ resolveDockResponsiveLayout,
+} from "./responsiveLayout";
+
+const EDITOR_MINIMUM_WIDTH = 480;
+const RESPONSIVE_HYSTERESIS = 32;
+
+let frameId = 0;
+let applying = false;
+
+interface IRuntimeLayoutNode {
+ direction?: Config.TUILayoutDirection,
+ children?: unknown[],
+ element?: HTMLElement,
+}
+
+const getLayoutNode = (item: IRuntimeLayoutNode): ICenterMinimumLayoutNode => {
+ const isLayout = item.direction === "lr" || item.direction === "tb";
+ return {
+ inactive: item.element?.classList.contains("fn__none"),
+ direction: isLayout ? item.direction : undefined,
+ children: isLayout ? item.children?.map((child) => getLayoutNode(child as IRuntimeLayoutNode)) : undefined,
+ };
+};
+
+const hasCenterMaximumWidth = (item: IRuntimeLayoutNode): boolean => {
+ return item.children?.some((child) => {
+ const childItem = child as IRuntimeLayoutNode;
+ if (childItem.element?.style.maxWidth ||
+ childItem.element?.hasAttribute(Constants.ATTRIBUTE_DOCK_WIDTH)) {
+ return true;
+ }
+ return (childItem.direction === "lr" || childItem.direction === "tb") &&
+ hasCenterMaximumWidth(childItem);
+ }) ?? false;
+};
+
+const getLayoutGap = () => {
+ const value = parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--b3-layout-space"));
+ return Number.isFinite(value) ? Math.max(0, value) : 0;
+};
+
+const clearResponsiveWidth = (dock: typeof window.siyuan.layout.leftDock, preferredSize: number) => {
+ const element = dock.layout.element;
+ const hasResponsiveWidth = Boolean(element.style.maxWidth ||
+ element.hasAttribute(Constants.ATTRIBUTE_DOCK_WIDTH));
+ element.style.maxWidth = "";
+ element.removeAttribute(Constants.ATTRIBUTE_DOCK_WIDTH);
+ if (hasResponsiveWidth && preferredSize > 0) {
+ element.style.width = preferredSize + "px";
+ }
+};
+
+const applyResponsiveWidth = (
+ dock: typeof window.siyuan.layout.leftDock,
+ preferredSize: number,
+ fixedSize: number,
+) => {
+ if (dock.isResponsiveFloating() || fixedSize <= 0 || fixedSize >= preferredSize) {
+ return;
+ }
+ dock.layout.element.setAttribute(Constants.ATTRIBUTE_DOCK_WIDTH, preferredSize.toString());
+ dock.layout.element.style.maxWidth = fixedSize + "px";
+};
+
+export const reconcileResponsiveDockLayout = () => {
+ if (applying) {
+ return;
+ }
+ const layout = window.siyuan?.layout;
+ const leftDock = layout?.leftDock;
+ const rightDock = layout?.rightDock;
+ const centerLayout = layout?.centerLayout;
+ const outerLayout = centerLayout?.parent;
+ if (!leftDock || !rightDock || !outerLayout || outerLayout.direction !== "lr") {
+ return;
+ }
+
+ const availableSize = outerLayout.element.clientWidth;
+ if (availableSize <= 0) {
+ return;
+ }
+
+ applying = true;
+ try {
+ const centerSizeBefore = centerLayout.element.clientWidth;
+ const gapSize = getLayoutGap();
+ const centerMinimumSize = Math.max(EDITOR_MINIMUM_WIDTH, getCenterMinimumSize(
+ getLayoutNode(centerLayout),
+ EDITOR_MINIMUM_WIDTH,
+ gapSize,
+ ));
+ const leftPreferredSize = leftDock.getResponsivePreferredSize();
+ const rightPreferredSize = rightDock.getResponsivePreferredSize();
+ const leftParticipating = leftDock.pin && leftDock.isPanelVisible();
+ const rightParticipating = rightDock.pin && rightDock.isPanelVisible();
+ const preferredOccupiedSize = centerMinimumSize +
+ (leftParticipating ? leftPreferredSize + gapSize : 0) +
+ (rightParticipating ? rightPreferredSize + gapSize : 0);
+
+ if (availableSize >= preferredOccupiedSize + RESPONSIVE_HYSTERESIS) {
+ leftDock.clearResponsiveManualOverride();
+ rightDock.clearResponsiveManualOverride();
+ }
+
+ const result = resolveDockResponsiveLayout({
+ availableSize,
+ centerMinimumSize,
+ gapSize,
+ hysteresis: RESPONSIVE_HYSTERESIS,
+ left: {
+ preferredSize: leftPreferredSize,
+ minimumSize: leftDock.getResponsiveMinimumSize(),
+ participating: leftParticipating,
+ autoFloating: leftDock.isResponsiveFloating(),
+ manualOverride: leftDock.hasResponsiveManualOverride(),
+ },
+ right: {
+ preferredSize: rightPreferredSize,
+ minimumSize: rightDock.getResponsiveMinimumSize(),
+ participating: rightParticipating,
+ autoFloating: rightDock.isResponsiveFloating(),
+ manualOverride: rightDock.hasResponsiveManualOverride(),
+ },
+ });
+
+ clearResponsiveWidth(leftDock, leftPreferredSize);
+ clearResponsiveWidth(rightDock, rightPreferredSize);
+
+ // 拉宽时先恢复左侧,再恢复右侧。
+ if (!result.left.autoFloating && leftDock.isResponsiveFloating()) {
+ leftDock.setResponsiveFloating(false);
+ }
+ if (!result.right.autoFloating && rightDock.isResponsiveFloating()) {
+ rightDock.setResponsiveFloating(false);
+ }
+ // 缩窄时先浮动右侧,再浮动左侧。
+ if (result.right.autoFloating && !rightDock.isResponsiveFloating()) {
+ rightDock.setResponsiveFloating(true);
+ }
+ if (result.left.autoFloating && !leftDock.isResponsiveFloating()) {
+ leftDock.setResponsiveFloating(true);
+ }
+
+ applyResponsiveWidth(leftDock, leftPreferredSize, result.left.fixedSize);
+ applyResponsiveWidth(rightDock, rightPreferredSize, result.right.fixedSize);
+ if (centerLayout.element.clientWidth !== centerSizeBefore && hasCenterMaximumWidth(centerLayout)) {
+ leftDock.adjustResponsiveCenterLayout();
+ }
+ } finally {
+ applying = false;
+ }
+};
+
+export const requestResponsiveDockLayout = () => {
+ if (applying || frameId) {
+ return;
+ }
+ frameId = requestAnimationFrame(() => {
+ frameId = 0;
+ reconcileResponsiveDockLayout();
+ });
+};
diff --git a/app/src/layout/dock/responsiveLayout.test.ts b/app/src/layout/dock/responsiveLayout.test.ts
new file mode 100644
index 0000000000..cb5e91d142
--- /dev/null
+++ b/app/src/layout/dock/responsiveLayout.test.ts
@@ -0,0 +1,184 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ getCenterMinimumSize,
+ IDockResponsiveLayoutInput,
+ IDockResponsiveSideInput,
+ resolveDockResponsiveLayout,
+} from "./responsiveLayout";
+
+const createSide = (overrides: Partial<IDockResponsiveSideInput> = {}): IDockResponsiveSideInput => ({
+ preferredSize: 240,
+ minimumSize: 180,
+ participating: true,
+ autoFloating: false,
+ manualOverride: false,
+ ...overrides,
+});
+
+const createInput = (overrides: Partial<IDockResponsiveLayoutInput> = {}): IDockResponsiveLayoutInput => ({
+ availableSize: 1016,
+ centerMinimumSize: 500,
+ gapSize: 8,
+ hysteresis: 24,
+ left: createSide(),
+ right: createSide({preferredSize: 260}),
+ ...overrides,
+});
+
+describe("responsive dock layout", () => {
+ it("keeps preferred sizes when the center and both docks fit", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput()), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 260},
+ });
+ });
+
+ it("compresses the right dock before changing the left dock", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 950})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 194},
+ });
+ });
+
+ it("does not grow a preferred size that is below the nominal minimum", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({
+ availableSize: 1016,
+ right: createSide({preferredSize: 160, minimumSize: 180}),
+ })), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 160},
+ });
+ });
+
+ it("floats the right dock before compressing the left dock", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 750})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 700})), {
+ left: {autoFloating: false, fixedSize: 192},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ });
+
+ it("floats both docks when the window is extremely narrow", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 300})), {
+ left: {autoFloating: true, fixedSize: 0},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ });
+
+ it("restores the left dock before the right dock", () => {
+ const floatingSides = {
+ left: createSide({autoFloating: true}),
+ right: createSide({preferredSize: 260, autoFloating: true}),
+ };
+
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 900, ...floatingSides})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 960, ...floatingSides})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 204},
+ });
+ });
+
+ it("uses hysteresis when restoring a previously floating dock", () => {
+ const right = createSide({preferredSize: 260, autoFloating: true});
+
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 959, right})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 960, right})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 204},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 936})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 180},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({availableSize: 935})), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: true, fixedSize: 0},
+ });
+ });
+
+ it("clears automatic floating for docks that do not participate", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({
+ availableSize: 700,
+ left: createSide({participating: false, autoFloating: true}),
+ right: createSide({preferredSize: 260, participating: false, autoFloating: true}),
+ })), {
+ left: {autoFloating: false, fixedSize: 0},
+ right: {autoFloating: false, fixedSize: 0},
+ });
+ });
+
+ it("does not compress or float a manually overridden dock", () => {
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({
+ availableSize: 900,
+ right: createSide({preferredSize: 260, manualOverride: true}),
+ })), {
+ left: {autoFloating: true, fixedSize: 0},
+ right: {autoFloating: false, fixedSize: 260},
+ });
+ assert.deepEqual(resolveDockResponsiveLayout(createInput({
+ availableSize: 300,
+ left: createSide({manualOverride: true}),
+ right: createSide({preferredSize: 260, manualOverride: true}),
+ })), {
+ left: {autoFloating: false, fixedSize: 240},
+ right: {autoFloating: false, fixedSize: 260},
+ });
+ });
+});
+
+describe("center minimum size", () => {
+ const pane = () => ({});
+
+ it("uses the pane minimum for a leaf", () => {
+ assert.equal(getCenterMinimumSize(pane(), 480, 8), 480);
+ });
+
+ it("adds active horizontal panes and their dividers", () => {
+ assert.equal(getCenterMinimumSize({
+ direction: "lr",
+ children: [pane(), {inactive: true}, pane()],
+ }, 480, 8), 968);
+ });
+
+ it("uses the largest vertical branch", () => {
+ assert.equal(getCenterMinimumSize({
+ direction: "tb",
+ children: [
+ pane(),
+ {
+ direction: "lr",
+ children: [pane(), pane()],
+ },
+ ],
+ }, 480, 8), 968);
+ });
+
+ it("ignores inactive nested branches", () => {
+ assert.equal(getCenterMinimumSize({
+ direction: "lr",
+ children: [
+ pane(),
+ {
+ inactive: true,
+ direction: "lr",
+ children: [pane(), pane()],
+ },
+ pane(),
+ ],
+ }, 480, 8), 968);
+ assert.equal(getCenterMinimumSize({
+ direction: "tb",
+ children: [{inactive: true}, {inactive: true}],
+ }, 480, 8), 0);
+ });
+});
diff --git a/app/src/layout/dock/responsiveLayout.ts b/app/src/layout/dock/responsiveLayout.ts
new file mode 100644
index 0000000000..4fd6ca85d5
--- /dev/null
+++ b/app/src/layout/dock/responsiveLayout.ts
@@ -0,0 +1,179 @@
+export interface IDockResponsiveSideInput {
+ preferredSize: number;
+ minimumSize: number;
+ participating: boolean;
+ autoFloating: boolean;
+ manualOverride: boolean;
+}
+
+export interface IDockResponsiveLayoutInput {
+ availableSize: number;
+ centerMinimumSize: number;
+ gapSize: number;
+ hysteresis: number;
+ left: IDockResponsiveSideInput;
+ right: IDockResponsiveSideInput;
+}
+
+export interface IDockResponsiveSideResult {
+ autoFloating: boolean;
+ fixedSize: number;
+}
+
+export interface IDockResponsiveLayoutResult {
+ left: IDockResponsiveSideResult;
+ right: IDockResponsiveSideResult;
+}
+
+export interface ICenterMinimumLayoutNode {
+ inactive?: boolean;
+ direction?: "lr" | "tb";
+ children?: ICenterMinimumLayoutNode[];
+}
+
+interface INormalizedSide extends IDockResponsiveSideInput {
+ fixedSize: number;
+}
+
+type SideName = "left" | "right";
+
+const normalizeSize = (size: number) => Number.isFinite(size) ? Math.max(0, size) : 0;
+
+const normalizeSide = (side: IDockResponsiveSideInput): INormalizedSide => {
+ const preferredSize = normalizeSize(side.preferredSize);
+ const minimumSize = Math.min(normalizeSize(side.minimumSize), preferredSize);
+ return {
+ ...side,
+ preferredSize,
+ minimumSize,
+ autoFloating: side.participating && side.autoFloating,
+ fixedSize: 0,
+ };
+};
+
+const copySide = (side: INormalizedSide): INormalizedSide => ({...side});
+
+const isFixed = (side: INormalizedSide) => side.participating && !side.autoFloating;
+
+const allocateFixedSizes = (
+ leftInput: INormalizedSide,
+ rightInput: INormalizedSide,
+ availableSize: number,
+ centerMinimumSize: number,
+ gapSize: number,
+) => {
+ const left = copySide(leftInput);
+ const right = copySide(rightInput);
+ const sides = {left, right};
+ let fixedCount = 0;
+ let occupiedSize = centerMinimumSize;
+
+ for (const side of [left, right]) {
+ if (isFixed(side)) {
+ side.fixedSize = side.preferredSize;
+ fixedCount++;
+ occupiedSize += side.fixedSize;
+ } else {
+ side.fixedSize = 0;
+ }
+ }
+ occupiedSize += fixedCount * gapSize;
+
+ let deficit = Math.max(0, occupiedSize - availableSize);
+ const shrinkOrFloat = (name: SideName) => {
+ const side = sides[name];
+ if (deficit === 0 || !isFixed(side) || side.manualOverride) {
+ return;
+ }
+
+ const shrinkSize = Math.min(deficit, side.fixedSize - side.minimumSize);
+ side.fixedSize -= shrinkSize;
+ deficit -= shrinkSize;
+ if (deficit > 0) {
+ side.autoFloating = true;
+ deficit = Math.max(0, deficit - side.fixedSize - gapSize);
+ side.fixedSize = 0;
+ }
+ };
+
+ shrinkOrFloat("right");
+ shrinkOrFloat("left");
+ return sides;
+};
+
+export const resolveDockResponsiveLayout = (input: IDockResponsiveLayoutInput): IDockResponsiveLayoutResult => {
+ const availableSize = normalizeSize(input.availableSize);
+ const centerMinimumSize = normalizeSize(input.centerMinimumSize);
+ const gapSize = normalizeSize(input.gapSize);
+ const hysteresis = normalizeSize(input.hysteresis);
+ const sides = {
+ left: normalizeSide(input.left),
+ right: normalizeSide(input.right),
+ };
+
+ for (const name of ["left", "right"] as const) {
+ const side = sides[name];
+ if (!side.participating || !side.autoFloating || side.manualOverride) {
+ continue;
+ }
+
+ const previouslyFixed = (["left", "right"] as const).filter((sideName) => isFixed(sides[sideName]));
+ const candidate = {
+ left: copySide(sides.left),
+ right: copySide(sides.right),
+ };
+ candidate[name].autoFloating = false;
+ const trial = allocateFixedSizes(
+ candidate.left,
+ candidate.right,
+ Math.max(0, availableSize - hysteresis),
+ centerMinimumSize,
+ gapSize,
+ );
+ if (!trial[name].autoFloating && previouslyFixed.every((sideName) => !trial[sideName].autoFloating)) {
+ sides[name].autoFloating = false;
+ }
+ }
+
+ const result = allocateFixedSizes(sides.left, sides.right, availableSize, centerMinimumSize, gapSize);
+ return {
+ left: {
+ autoFloating: result.left.autoFloating,
+ fixedSize: result.left.fixedSize,
+ },
+ right: {
+ autoFloating: result.right.autoFloating,
+ fixedSize: result.right.fixedSize,
+ },
+ };
+};
+
+const getNodeMinimumSize = (
+ node: ICenterMinimumLayoutNode,
+ paneMinimumSize: number,
+ dividerSize: number,
+): number | undefined => {
+ if (node.inactive) {
+ return;
+ }
+ if (!node.children?.length) {
+ return paneMinimumSize;
+ }
+
+ const childSizes = node.children
+ .map((child) => getNodeMinimumSize(child, paneMinimumSize, dividerSize))
+ .filter((size): size is number => typeof size === "number");
+ if (childSizes.length === 0) {
+ return;
+ }
+ if (node.direction === "lr") {
+ return childSizes.reduce((total, size) => total + size, 0) + dividerSize * (childSizes.length - 1);
+ }
+ return Math.max(...childSizes);
+};
+
+export const getCenterMinimumSize = (
+ node: ICenterMinimumLayoutNode,
+ paneMinimumSize: number,
+ dividerSize: number,
+) => getNodeMinimumSize(node, normalizeSize(paneMinimumSize), normalizeSize(dividerSize)) ?? 0;
diff --git a/app/src/layout/dock/util.ts b/app/src/layout/dock/util.ts
index e88fd746cf..5becc0302f 100644
--- a/app/src/layout/dock/util.ts
+++ b/app/src/layout/dock/util.ts
@@ -194,13 +194,13 @@ export const openOutline = async (options: {
};
export const resetFloatDockSize = () => {
- if (!window.siyuan.layout.leftDock.pin && window.siyuan.layout.leftDock.layout.element.style.opacity === "1") {
+ if (window.siyuan.layout.leftDock.isFloating() && window.siyuan.layout.leftDock.layout.element.style.opacity === "1") {
window.siyuan.layout.leftDock.showDock(true);
}
- if (!window.siyuan.layout.rightDock.pin && window.siyuan.layout.rightDock.layout.element.style.opacity === "1") {
+ if (window.siyuan.layout.rightDock.isFloating() && window.siyuan.layout.rightDock.layout.element.style.opacity === "1") {
window.siyuan.layout.rightDock.showDock(true);
}
- if (!window.siyuan.layout.bottomDock.pin && window.siyuan.layout.bottomDock.layout.element.style.opacity === "1") {
+ if (window.siyuan.layout.bottomDock.isFloating() && window.siyuan.layout.bottomDock.layout.element.style.opacity === "1") {
window.siyuan.layout.bottomDock.showDock(true);
}
};
diff --git a/app/src/layout/tabUtil.ts b/app/src/layout/tabUtil.ts
index 5ef3fd2809..a3328b97c2 100644
--- a/app/src/layout/tabUtil.ts
+++ b/app/src/layout/tabUtil.ts
@@ -25,6 +25,7 @@ import {Constants} from "../constants";
import {fetchPost} from "../util/fetch";
import {isWindow} from "../util/functions";
import {Wnd} from "./Wnd";
+import {requestResponsiveDockLayout} from "./dock/responsive";
export const setTabPosition = (onlyPadding = false, onlyClear = false) => {
const isWindowMode = isWindow();
@@ -192,12 +193,17 @@ export const switchTabByIndex = (index: number) => {
};
let resizeTimeout: number;
+let resizeSaveLayout = false;
export const resizeTabs = (isSaveLayout = true) => {
+ resizeSaveLayout = resizeSaveLayout || isSaveLayout;
+ requestResponsiveDockLayout();
hideAllElements(["gutter"]);
clearTimeout(resizeTimeout);
// .layout .fn__flex-shrink {width .15s cubic-bezier(0, 0, .2, 1) 0ms} 时需要再次计算 padding
// PDF 避免分屏多次调用后,页码跳转到1 https://github.com/siyuan-note/siyuan/issues/5646
resizeTimeout = window.setTimeout(() => {
+ const shouldSaveLayout = resizeSaveLayout;
+ resizeSaveLayout = false;
const models = getAllModels();
models.editor.forEach((item) => {
if (item.editor && item.editor.protyle &&
@@ -230,7 +236,7 @@ export const resizeTabs = (isSaveLayout = true) => {
});
pdfResize();
hideAllElements(["gutter"]);
- if (isSaveLayout) {
+ if (shouldSaveLayout) {
saveLayout();
}
}, 200);
diff --git a/app/src/layout/util.ts b/app/src/layout/util.ts
index 6291432a48..4bc4fd481e 100644
--- a/app/src/layout/util.ts
+++ b/app/src/layout/util.ts
@@ -40,6 +40,7 @@ import {setTitle} from "../util/processTitle";
import {activateQueuedAVLocate, queueAVLocateRequest} from "../protyle/render/av/locate";
import {applyDockEntryVisibility} from "../config/entryVisibility/runtime";
import {panePercentages, resizePanePercentages} from "./resizePane";
+import {requestResponsiveDockLayout} from "./dock/responsive";
const isBuiltInCustomModel = (type: string) => {
return type === "siyuan-card" || type === "siyuan-database-row";
@@ -969,6 +970,11 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
}
const getMinSize = (element: HTMLElement) => {
+ const dock = [window.siyuan.layout?.leftDock, window.siyuan.layout?.rightDock].find((item) =>
+ item?.layout.element === element);
+ if (dock) {
+ return dock.getResponsiveMinimumSize();
+ }
let minSize = 232;
Array.from(element.querySelectorAll(".file-tree")).find((item) => {
if (item.classList.contains("sy__backlink") || item.classList.contains("sy__graph")
@@ -1020,6 +1026,10 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
const nextElement = resizeElement.nextElementSibling as HTMLElement;
const previousElement = resizeElement.previousElementSibling as HTMLElement;
const isCenterResize = !!resizeElement.parentElement.closest(".layout__center");
+ const responsiveDock = !isCenterResize && direction === "lr" ?
+ [window.siyuan.layout?.leftDock, window.siyuan.layout?.rightDock].find((dock) =>
+ dock?.layout.element === previousElement || dock?.layout.element === nextElement) : undefined;
+ let responsiveResizePrepared = false;
const paneElements = isCenterResize ? getPaneElements() : [];
const paneSizes = paneElements.map((item) => direction === "lr" ? item.clientWidth : item.clientHeight);
const previousIndex = paneElements.indexOf(previousElement);
@@ -1074,6 +1084,11 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
if (nextElement.classList.contains("layout__center") && nextNowSize <= 148) {
return;
}
+ if (responsiveDock && !responsiveResizePrepared &&
+ (previousNowSize !== previousSize || nextNowSize !== nextSize)) {
+ responsiveDock.prepareForManualResize();
+ responsiveResizePrepared = true;
+ }
if (isCenterResize) {
const percentages = resizePanePercentages(
paneSizes,
@@ -1143,6 +1158,7 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
previousElement.style.transition = "none";
if (resizeElement.classList.contains("layout__resize--lr")) {
if (previousElement.classList.contains("layout__dockl")) {
+ window.siyuan.layout.leftDock.prepareForManualResize();
document.querySelectorAll("#dockLeft .dock__item--active").forEach(item => {
if (bigType.includes(item.getAttribute("data-type"))) {
size = 320;
@@ -1151,6 +1167,7 @@ export const addResize = (obj: Layout | Wnd, after = true) => {
previousElement.style.width = size + "px";
window.siyuan.layout.leftDock.setSize();
} else if (nextElement.classList.contains("layout__dockr")) {
+ window.siyuan.layout.rightDock.prepareForManualResize();
document.querySelectorAll("#dockRight .dock__item--active").forEach(item => {
if (bigType.includes(item.getAttribute("data-type"))) {
size = 320;
@@ -1245,6 +1262,7 @@ export const adjustLayout = (layout: Layout = window.siyuan.layout.centerLayout.
adjustLayout(item);
}
});
+ requestResponsiveDockLayout();
};
export const fixWndFlex1 = (layout: Layout) => {
diff --git a/app/src/menus/workspace.ts b/app/src/menus/workspace.ts
index 7c57c355a4..f421e6d758 100644
--- a/app/src/menus/workspace.ts
+++ b/app/src/menus/workspace.ts
@@ -136,12 +136,13 @@ const editLayout = (layoutName?: string) => {
};
const togglePinDock = (id: "switchLeftDock" | "switchRightDock" | "switchBottomDock", dock: Dock, pinIcon: string, unpinIcon: string) => {
+ const isFloating = dock.isFloating();
return {
id,
- label: `${dock.pin ? window.siyuan.languages.switchToFloatingLayout : window.siyuan.languages.switchToFixedLayout}`,
- icon: `${dock.pin ? unpinIcon : pinIcon}`,
+ label: `${isFloating ? window.siyuan.languages.switchToFixedLayout : window.siyuan.languages.switchToFloatingLayout}`,
+ icon: `${isFloating ? pinIcon : unpinIcon}`,
accelerator: window.siyuan.config.keymap.general[id].custom,
- current: !dock.pin,
+ current: isFloating,
click() {
dock.togglePin();
}