返回 DeepSeek-Reasonix
workspaceSplit.ts
根目录 / desktop / frontend / src / lib / workspaceSplit.ts
1 export function clampWorkspaceSplitTreeWidth({
2 width,
3 panelWidth,
4 railWidth = 0,
5 treeMinWidth,
6 previewMinWidth,
7 }: {
8 width: number;
9 panelWidth?: number;
10 railWidth?: number;
11 treeMinWidth: number;
12 previewMinWidth: number;
13 }): number {
14 const min = Math.round(treeMinWidth);
15 const rounded = Math.round(width);
16 if (typeof panelWidth !== "number" || !Number.isFinite(panelWidth)) {
17 return Math.max(min, rounded);
18 }
19 const splitWidth = Math.max(0, Math.round(panelWidth) - Math.round(railWidth));
20 const max = Math.max(min, splitWidth - Math.round(previewMinWidth));
21 return Math.min(max, Math.max(min, rounded));
22 }
23
24 export function initialWorkspaceSplitTreeWidth({
25 panelWidth,
26 railWidth = 0,
27 savedTreeWidth,
28 treeMinWidth,
29 previewMinWidth,
30 }: {
31 panelWidth?: number;
32 railWidth?: number;
33 savedTreeWidth: number | null;
34 treeMinWidth: number;
35 previewMinWidth: number;
36 }): number {
37 const target =
38 savedTreeWidth !== null && Number.isFinite(savedTreeWidth)
39 ? savedTreeWidth
40 : typeof panelWidth === "number" && Number.isFinite(panelWidth)
41 ? Math.max(0, panelWidth - railWidth) / 2
42 : treeMinWidth;
43 return clampWorkspaceSplitTreeWidth({ width: target, panelWidth, railWidth, treeMinWidth, previewMinWidth });
44 }
45
46 export type WorkspaceSplitTreeWidthMode = "even" | "manual";
47
48 export function shouldInitializeWorkspaceSplitOnFileSelect({
49 previewVisible,
50 treeVisible,
51 }: {
52 previewVisible: boolean;
53 treeVisible: boolean;
54 }): boolean {
55 return !previewVisible || !treeVisible;
56 }
57
58 export function resolveWorkspaceSplitTreeWidth({
59 mode,
60 currentTreeWidth,
61 panelWidth,
62 railWidth = 0,
63 treeMinWidth,
64 previewMinWidth,
65 }: {
66 mode: WorkspaceSplitTreeWidthMode;
67 currentTreeWidth: number;
68 panelWidth?: number;
69 railWidth?: number;
70 treeMinWidth: number;
71 previewMinWidth: number;
72 }): number {
73 if (mode === "even") {
74 return initialWorkspaceSplitTreeWidth({
75 panelWidth,
76 railWidth,
77 savedTreeWidth: null,
78 treeMinWidth,
79 previewMinWidth,
80 });
81 }
82 return clampWorkspaceSplitTreeWidth({
83 width: currentTreeWidth,
84 panelWidth,
85 railWidth,
86 treeMinWidth,
87 previewMinWidth,
88 });
89 }
90
91 export function workspaceSplitCanFit({
92 panelWidth,
93 railWidth = 0,
94 treeMinWidth,
95 previewMinWidth,
96 }: {
97 panelWidth?: number;
98 railWidth?: number;
99 treeMinWidth: number;
100 previewMinWidth: number;
101 }): boolean {
102 if (typeof panelWidth !== "number" || !Number.isFinite(panelWidth)) {
103 return true;
104 }
105 return Math.round(panelWidth) >= Math.round(railWidth) + Math.round(treeMinWidth) + Math.round(previewMinWidth);
106 }
107
108 export function workspaceSplitTreeWidthFromPointer({
109 clientX,
110 panelLeft,
111 panelWidth,
112 railWidth = 0,
113 treeMinWidth,
114 previewMinWidth,
115 treeOnRight = false,
116 }: {
117 clientX: number;
118 panelLeft: number;
119 panelWidth?: number;
120 railWidth?: number;
121 treeMinWidth: number;
122 previewMinWidth: number;
123 treeOnRight?: boolean;
124 }): number {
125 if (treeOnRight && typeof panelWidth === "number") {
126 // Tree width is the distance from the pointer to the panel's right edge.
127 // The rail still occupies its own column on the opposite side.
128 return clampWorkspaceSplitTreeWidth({
129 width: panelLeft + panelWidth - clientX,
130 panelWidth,
131 railWidth,
132 treeMinWidth,
133 previewMinWidth,
134 });
135 }
136 return clampWorkspaceSplitTreeWidth({
137 width: clientX - panelLeft - railWidth,
138 panelWidth,
139 railWidth,
140 treeMinWidth,
141 previewMinWidth,
142 });
143 }
144 export const WORKSPACE_TREE_MIN_WIDTH = 140;
145 export const WORKSPACE_TREE_DEFAULT_WIDTH = 300;
146 export const WORKSPACE_PREVIEW_MIN_WIDTH = 140;
147 export const WORKSPACE_PREVIEW_TARGET_WIDTH = 360;
148 export const WORKSPACE_DUAL_PANEL_TARGET_WIDTH = WORKSPACE_TREE_DEFAULT_WIDTH + WORKSPACE_PREVIEW_TARGET_WIDTH;
149
150 export function clampWorkspaceTreeWidth(width: number, panelWidth?: number): number {
151 return clampWorkspaceSplitTreeWidth({ width, panelWidth, treeMinWidth: WORKSPACE_TREE_MIN_WIDTH, previewMinWidth: WORKSPACE_PREVIEW_MIN_WIDTH });
152 }
153
153 lines TYPESCRIPT