@@ -296,48 +296,80 @@ bool LoadSettings() {
296296 return true ;
297297}
298298
299+ static TabState* CloneTabState (const TabState* src) {
300+ TabState* dst = (TabState*)AllocStruct<TabState>();
301+ dst->filePath = str::Dup (src->filePath );
302+ dst->displayMode = str::Dup (src->displayMode );
303+ dst->pageNo = src->pageNo ;
304+ dst->zoom = str::Dup (src->zoom );
305+ dst->rotation = src->rotation ;
306+ dst->scrollPos = src->scrollPos ;
307+ dst->showToc = src->showToc ;
308+ dst->tocState = new Vec<int >(*src->tocState );
309+ return dst;
310+ }
311+
312+ Vec<SessionData*>* gInitialSessionData = nullptr ;
313+
299314static void RememberSessionState () {
300- Vec<SessionData*>* sessionData = gGlobalPrefs ->sessionData ;
301- ResetSessionState (sessionData );
315+ Vec<SessionData*>* sessionState = gGlobalPrefs ->sessionData ;
316+ FreeSessionState (sessionState );
302317
303318 if (!gGlobalPrefs ->rememberOpenedFiles ) {
304319 return ;
305320 }
306321
307- if (gWindows .size () == 0 ) {
308- return ;
309- }
310-
311322 for (auto * win : gWindows ) {
312- SessionData* data = NewSessionData ();
323+ SessionData* windowState = NewSessionData ();
313324 for (WindowTab* tab : win->Tabs ()) {
314- if (tab->IsAboutTab ()) {
325+ if (!tab->filePath ) {
326+ // home page tab
315327 continue ;
316328 }
317329 const char * fp = tab->filePath ;
318- FileState* fs = NewDisplayState (fp);
319- if (tab->ctrl ) {
320- tab->ctrl ->GetDisplayState (fs);
330+ if (!tab->ctrl ) {
331+ // lazy loading, file not loaded into a tab
332+ // use the saved state from previous session
333+ // note: might stil have issues if multiple tabs with same file
334+ bool didFind = false ;
335+ int nWindows = gInitialSessionData ->Size ();
336+ for (int i = 0 ; i < nWindows; i++) {
337+ SessionData* psd = gInitialSessionData ->At (i);
338+ int nTabs = psd->tabStates ->Size ();
339+ for (int j = 0 ; j < nTabs; j++) {
340+ TabState* pts = psd->tabStates ->At (j);
341+ if (str::Eq (pts->filePath , fp)) {
342+ TabState* ts = CloneTabState (pts);
343+ windowState->tabStates ->Append (ts);
344+ didFind = true ;
345+ break ;
346+ }
347+ }
348+ }
349+ ReportIf (!didFind);
350+ continue ;
321351 }
352+ FileState* fs = NewDisplayState (fp);
353+ tab->ctrl ->GetDisplayState (fs);
322354 fs->showToc = tab->showToc ;
323355 *fs->tocState = tab->tocState ;
324356 TabState* ts = NewTabState (fs);
325- data ->tabStates ->Append (ts);
357+ windowState ->tabStates ->Append (ts);
326358 DeleteDisplayState (fs);
327359 }
328- if (data ->tabStates ->Size () == 0 ) {
360+ if (windowState ->tabStates ->Size () == 0 ) {
329361 continue ;
330362 }
331- data ->tabIndex = win->GetTabIdx (win->CurrentTab ()) + 1 ;
332- if (data ->tabIndex < 0 ) {
333- data ->tabIndex = 0 ;
363+ windowState ->tabIndex = win->GetTabIdx (win->CurrentTab ()) + 1 ;
364+ if (windowState ->tabIndex < 0 ) {
365+ windowState ->tabIndex = 0 ;
334366 }
335367 // TODO: allow recording this state without changing gGlobalPrefs
336368 RememberDefaultWindowPosition (win);
337- data ->windowState = gGlobalPrefs ->windowState ;
338- data ->windowPos = gGlobalPrefs ->windowPos ;
339- data ->sidebarDx = gGlobalPrefs ->sidebarDx ;
340- sessionData ->Append (data );
369+ windowState ->windowState = gGlobalPrefs ->windowState ;
370+ windowState ->windowPos = gGlobalPrefs ->windowPos ;
371+ windowState ->sidebarDx = gGlobalPrefs ->sidebarDx ;
372+ sessionState ->Append (windowState );
341373 }
342374}
343375
0 commit comments