@@ -16,6 +16,8 @@ type DesktopPanelKey = keyof typeof minimumDesktopPanelWidths
1616
1717type DesktopPanelWidths = Record < DesktopPanelKey , number >
1818
19+ const desktopPanelKeys = [ "sources" , "chunks" , "chat" ] as const
20+
1921type DesktopPanelResizeInput = {
2022 readonly leftPanel : DesktopPanelKey
2123 readonly rightPanel : DesktopPanelKey
@@ -29,6 +31,9 @@ type WorkspaceShellStateModule = {
2931 readonly minimumDesktopPanelWidths : typeof minimumDesktopPanelWidths
3032 readonly defaultDesktopPanelWidths : typeof defaultDesktopPanelWidths
3133 readonly getMinimumDesktopPanelWidth : ( ) => number
34+ readonly fitDesktopPanelWidthsToContainer : (
35+ containerWidth : number ,
36+ ) => DesktopPanelWidths
3237 readonly resizeDesktopPanelWidths : (
3338 currentWidths : Readonly < DesktopPanelWidths > ,
3439 resize : DesktopPanelResizeInput ,
@@ -44,6 +49,65 @@ function getMinimumDesktopPanelWidth(): number {
4449 )
4550}
4651
52+ function getDefaultDesktopPanelContentWidth ( ) : number {
53+ return (
54+ defaultDesktopPanelWidths . sources +
55+ defaultDesktopPanelWidths . chunks +
56+ defaultDesktopPanelWidths . chat
57+ )
58+ }
59+
60+ function getMinimumDesktopPanelContentWidth ( ) : number {
61+ return (
62+ minimumDesktopPanelWidths . sources +
63+ minimumDesktopPanelWidths . chunks +
64+ minimumDesktopPanelWidths . chat
65+ )
66+ }
67+
68+ function fitDesktopPanelWidthsToContainer (
69+ containerWidth : number ,
70+ ) : DesktopPanelWidths {
71+ if ( ! Number . isFinite ( containerWidth ) || containerWidth <= 0 ) {
72+ return { ...defaultDesktopPanelWidths }
73+ }
74+
75+ const availableContentWidth = containerWidth - desktopPanelGutterWidth * 2
76+ const defaultContentWidth = getDefaultDesktopPanelContentWidth ( )
77+ if ( availableContentWidth >= defaultContentWidth ) {
78+ return { ...defaultDesktopPanelWidths }
79+ }
80+
81+ const minimumContentWidth = getMinimumDesktopPanelContentWidth ( )
82+ if ( availableContentWidth <= minimumContentWidth ) {
83+ return { ...minimumDesktopPanelWidths }
84+ }
85+
86+ const defaultExtraWidth = defaultContentWidth - minimumContentWidth
87+ const availableExtraWidth = availableContentWidth - minimumContentWidth
88+ const fittedWidths = { } as DesktopPanelWidths
89+ let assignedWidth = 0
90+
91+ for ( const [ index , panel ] of desktopPanelKeys . entries ( ) ) {
92+ const isLastPanel = index === desktopPanelKeys . length - 1
93+ if ( isLastPanel ) {
94+ fittedWidths [ panel ] = availableContentWidth - assignedWidth
95+ break
96+ }
97+
98+ const panelExtraWidth =
99+ defaultDesktopPanelWidths [ panel ] - minimumDesktopPanelWidths [ panel ]
100+ const fittedWidth = Math . round (
101+ minimumDesktopPanelWidths [ panel ] +
102+ ( panelExtraWidth / defaultExtraWidth ) * availableExtraWidth ,
103+ )
104+ fittedWidths [ panel ] = fittedWidth
105+ assignedWidth += fittedWidth
106+ }
107+
108+ return fittedWidths
109+ }
110+
47111function resizeDesktopPanelWidths (
48112 currentWidths : Readonly < DesktopPanelWidths > ,
49113 resize : DesktopPanelResizeInput ,
@@ -73,5 +137,6 @@ export const workspaceShellState: WorkspaceShellStateModule = {
73137 minimumDesktopPanelWidths,
74138 defaultDesktopPanelWidths,
75139 getMinimumDesktopPanelWidth,
140+ fitDesktopPanelWidthsToContainer,
76141 resizeDesktopPanelWidths,
77142}
0 commit comments