@@ -20,7 +20,7 @@ import { DockStripActions } from "./DockStripActions";
2020import { DockTab } from "./DockTab" ;
2121import { installTabFill } from "./dockTabFill" ;
2222import { addRegisteredPanel , applySideMinimums , buildEditLayout } from "./dockLayout" ;
23- import { DOCK_PANEL_COMPONENT } from "./dockLayoutSchema" ;
23+ import { DOCK_PANEL_COMPONENT , parseDockLayout } from "./dockLayoutSchema" ;
2424import { useDockLayoutStore , type DockController , type DockSnapshot } from "./dockLayoutStore" ;
2525import {
2626 PANEL_DEFINITIONS ,
@@ -85,10 +85,20 @@ function snapshot(api: DockviewApi): DockSnapshot {
8585 } ;
8686}
8787
88- function createController ( api : DockviewApi ) : DockController {
88+ interface DockOptions {
89+ panels : readonly PanelId [ ] ;
90+ storageKey ?: string | undefined ;
91+ dockWidth : ( ) => number ;
92+ }
93+
94+ function createController ( api : DockviewApi , options : DockOptions ) : DockController {
8995 const open = ( id : PanelId ) => {
9096 if ( api . getPanel ( id ) ) return ;
91- const { zone, reopen } = PANEL_DEFINITIONS [ id ] ;
97+ const { zone, reopen : preferred } = PANEL_DEFINITIONS [ id ] ;
98+ const reopen : PanelDefinition [ "reopen" ] =
99+ zone === "center" || options . panels . includes ( preferred . near )
100+ ? preferred
101+ : { near : "preview" , direction : zone } ;
92102 // Side columns are tab groups; preview and timeline are separate groups in the centre.
93103 const sibling =
94104 zone === "center"
@@ -114,12 +124,13 @@ function createController(api: DockviewApi): DockController {
114124 } ,
115125 setTitle : ( id , title ) => api . getPanel ( id ) ?. api . setTitle ( title ) ,
116126 setGroupVisible : ( id , visible ) => api . getPanel ( id ) ?. group . api . setVisible ( visible ) ,
117- reset : ( ) => buildEditLayout ( api , window . innerWidth ) ,
127+ reset : ( ) => buildEditLayout ( api , options . dockWidth ( ) , options . panels ) ,
118128 } ;
119129}
120130
121- function restoreOrBuild ( api : DockviewApi , projectId : string | null ) {
122- const stored = readStudioUiPreferences ( undefined , projectId ) . dockLayout ;
131+ function restoreOrBuild ( api : DockviewApi , projectId : string | null , options : DockOptions ) {
132+ const { dockLayout } = readStudioUiPreferences ( undefined , projectId , options . storageKey ) ;
133+ const stored = parseDockLayout ( dockLayout , options . panels ) ;
123134 if ( stored ) {
124135 try {
125136 api . fromJSON ( stored ) ;
@@ -128,10 +139,24 @@ function restoreOrBuild(api: DockviewApi, projectId: string | null) {
128139 /* a layout the schema accepted but dockview cannot load: start over */
129140 }
130141 }
131- buildEditLayout ( api , window . innerWidth ) ;
142+ buildEditLayout ( api , options . dockWidth ( ) , options . panels ) ;
132143}
133144
134- function Root ( { projectId, children } : { projectId : string | null ; children : ReactNode } ) {
145+ const windowWidth = ( ) => window . innerWidth ;
146+
147+ function Root ( {
148+ projectId,
149+ panels = PANEL_IDS ,
150+ storageKey,
151+ dockWidth = windowWidth ,
152+ children,
153+ } : {
154+ projectId : string | null ;
155+ panels ?: readonly PanelId [ ] ;
156+ storageKey ?: string ;
157+ dockWidth ?: ( ) => number ;
158+ children : ReactNode ;
159+ } ) {
135160 const [ slots , setSlots ] = useState < Slots > ( { } ) ;
136161 const registerSlot = useCallback ( ( id : PanelId , element : HTMLElement | null ) => {
137162 setSlots ( ( prev ) => {
@@ -149,16 +174,17 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
149174 const onReady = useCallback (
150175 ( { api } : DockviewReadyEvent ) => {
151176 disposeRef . current ( ) ;
152- restoreOrBuild ( api , projectId ) ;
153- applySideMinimums ( api ) ;
177+ const options = { panels, storageKey, dockWidth } ;
178+ restoreOrBuild ( api , projectId , options ) ;
179+ applySideMinimums ( api , dockWidth ( ) ) ;
154180 const root = api . groups [ 0 ] ?. element . closest < HTMLElement > ( ".hf-dock" ) ;
155181 const disposeAccessibility = root ? installDockAccessibility ( api , root ) : ( ) => { } ;
156182 const disposeTabFill = root ? installTabFill ( api , root ) : ( ) => { } ;
157- // The dock spans the window (buildEditLayout sizes against it too); its own box lags a resize.
158- const resizeObserver = new ResizeObserver ( ( ) => applySideMinimums ( api , window . innerWidth ) ) ;
183+ // Sides size against dockWidth, the window by default; the dock's own box lags a resize.
184+ const resizeObserver = new ResizeObserver ( ( ) => applySideMinimums ( api , dockWidth ( ) ) ) ;
159185 if ( root ) resizeObserver . observe ( root ) ;
160186 const store = useDockLayoutStore . getState ( ) ;
161- store . attach ( createController ( api ) ) ;
187+ store . attach ( createController ( api , options ) , panels ) ;
162188 store . sync ( snapshot ( api ) ) ;
163189 const pending = store . takePendingActivation ( ) ;
164190 if ( pending ) api . getPanel ( pending ) ?. api . setActive ( ) ;
@@ -167,7 +193,7 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
167193 const persist = ( ) => {
168194 clearTimeout ( timer ) ;
169195 timer = setTimeout ( ( ) => {
170- writeStudioUiPreferences ( { dockLayout : api . toJSON ( ) } , undefined , projectId ) ;
196+ writeStudioUiPreferences ( { dockLayout : api . toJSON ( ) } , undefined , projectId , storageKey ) ;
171197 } , PERSIST_DEBOUNCE_MS ) ;
172198 } ;
173199 // dockview does not fire onDidLayoutChange for add/remove/activate,
@@ -178,11 +204,11 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
178204 } ;
179205 const subscriptions = [
180206 api . onDidAddPanel ( ( ) => {
181- applySideMinimums ( api ) ;
207+ applySideMinimums ( api , dockWidth ( ) ) ;
182208 onDockChange ( ) ;
183209 } ) ,
184210 api . onDidMovePanel ( ( ) => {
185- applySideMinimums ( api ) ;
211+ applySideMinimums ( api , dockWidth ( ) ) ;
186212 onDockChange ( ) ;
187213 } ) ,
188214 api . onDidRemovePanel ( onDockChange ) ,
@@ -198,7 +224,7 @@ function Root({ projectId, children }: { projectId: string | null; children: Rea
198224 useDockLayoutStore . getState ( ) . detach ( ) ;
199225 } ;
200226 } ,
201- [ projectId ] ,
227+ [ projectId , panels , storageKey , dockWidth ] ,
202228 ) ;
203229
204230 return (
@@ -235,6 +261,7 @@ function Panel({ id, title, children }: { id: PanelId; title?: string; children:
235261
236262function WindowMenu ( ) {
237263 const openPanels = useDockLayoutStore ( ( state ) => state . openPanels ) ;
264+ const panels = useDockLayoutStore ( ( state ) => state . panels ) ;
238265 const togglePanel = useDockLayoutStore ( ( state ) => state . togglePanel ) ;
239266 const resetLayout = useDockLayoutStore ( ( state ) => state . resetLayout ) ;
240267 const [ open , setOpen ] = useState ( false ) ;
@@ -265,7 +292,7 @@ function WindowMenu() {
265292 role = "menu"
266293 className = "absolute right-0 top-8 z-50 w-44 rounded-md border border-neutral-800 bg-neutral-900 py-1 shadow-lg"
267294 >
268- { PANEL_IDS . map ( ( id ) => (
295+ { panels . map ( ( id ) => (
269296 < button
270297 key = { id }
271298 type = "button"
0 commit comments