Browse Source

Consistently calling RestoreState always within AnnounceState

Source commit: 5929e2a413e4f8daf6c9fffe5db6bdf1bbcc993b
Martin Prikryl 1 year ago
parent
commit
f4b984ee0f

+ 64 - 52
source/forms/ScpCommander.cpp

@@ -615,42 +615,46 @@ void __fastcall TScpCommanderForm::StartingWithoutSession()
   AddStartupSequence(L"K");
 }
 //---------------------------------------------------------------------------
-void TScpCommanderForm::RestoreSessionLocalDirView(
-  TDirView * ALocalDirView, const UnicodeString & LocalDirectory, TObject * State)
+void TScpCommanderForm::RestoreSessionLocalDirView(TDirView * ALocalDirView, const UnicodeString & LocalDirectory)
 {
-  ALocalDirView->AnnounceState(State);
-  try
-  {
-    // we will load completely different directory, so particularly
-    // do not attempt to select previously selected directory
-    ALocalDirView->ContinueSession(false);
+  // we will load completely different directory, so particularly
+  // do not attempt to select previously selected directory
+  ALocalDirView->ContinueSession(false);
 
-    // reset home directory
-    ALocalDirView->HomeDirectory = L"";
+  // reset home directory
+  ALocalDirView->HomeDirectory = L"";
 
-    if (!LocalDirectory.IsEmpty() &&
-        (FFirstTerminal || !WinConfiguration->ScpCommander.PreserveLocalDirectory || ManagedSession->LocalBrowser))
+  if (!LocalDirectory.IsEmpty() &&
+      (FFirstTerminal || !WinConfiguration->ScpCommander.PreserveLocalDirectory || ManagedSession->LocalBrowser))
+  {
+    try
     {
-      try
+      ALocalDirView->Path = LocalDirectory;
+    }
+    catch(Exception & E)
+    {
+      if (!ManagedSession->SessionData->UpdateDirectories)
       {
-        ALocalDirView->Path = LocalDirectory;
+        ManagedSession->ShowExtendedException(&E);
       }
-      catch(Exception & E)
+      else
       {
-        if (!ManagedSession->SessionData->UpdateDirectories)
-        {
-          ManagedSession->ShowExtendedException(&E);
-        }
-        else
-        {
-          ALocalDirView->OpenFallbackPath(LocalDirectory);
-        }
+        ALocalDirView->OpenFallbackPath(LocalDirectory);
       }
     }
   }
-  __finally
+}
+//---------------------------------------------------------------------------
+void TScpCommanderForm::AnnounceLocalStates(
+  bool RestoreState, bool LocalBrowser, TObject * State, TObject * OtherState)
+{
+  if (RestoreState)
   {
-    ALocalDirView->AnnounceState(NULL);
+    LocalDirView->AnnounceState(State);
+    if (LocalBrowser)
+    {
+      OtherLocalDirView->AnnounceState(OtherState);
+    }
   }
 }
 //---------------------------------------------------------------------------
@@ -665,42 +669,50 @@ void __fastcall TScpCommanderForm::SessionChanged(bool Replaced)
     bool RestoreState =
       WinConfiguration->PreservePanelState &&
         (!WinConfiguration->ScpCommander.PreserveLocalDirectory || ManagedSession->LocalBrowser);
+    AnnounceLocalStates(
+      RestoreState, ManagedSession->LocalBrowser,
+      ManagedSession->LocalExplorerState, ManagedSession->OtherLocalExplorerState);
 
-    RestoreSessionLocalDirView(
-      LocalDirView, ManagedSession->StateData->LocalDirectory, ManagedSession->LocalExplorerState);
-
-    if (ManagedSession->LocalBrowser)
+    try
     {
-      RestoreSessionLocalDirView(
-        OtherLocalDirView, ManagedSession->StateData->OtherLocalDirectory, ManagedSession->OtherLocalExplorerState);
-    }
+      RestoreSessionLocalDirView(LocalDirView, ManagedSession->StateData->LocalDirectory);
 
-    FFirstTerminal = false;
+      if (ManagedSession->LocalBrowser)
+      {
+        RestoreSessionLocalDirView(OtherLocalDirView, ManagedSession->StateData->OtherLocalDirectory);
+      }
 
-    // Happens when opening a connection from a command-line (StartingWithoutSession was not called),
-    // which does not have a local directory set yet.
-    // Or when starting with vanila local browser.
-    if (LocalDirView->Path.IsEmpty())
-    {
-      LocalDefaultDirectory();
-    }
+      FFirstTerminal = false;
 
-    if (WinConfiguration->DefaultDirIsHome &&
-        !ManagedSession->SessionData->UpdateDirectories &&
-        DebugAlwaysTrue(!ManagedSession->LocalBrowser))
-    {
-      LocalDirView->HomeDirectory = ManagedSession->SessionData->LocalDirectoryExpanded;
-    }
+      // Happens when opening a connection from a command-line (StartingWithoutSession was not called),
+      // which does not have a local directory set yet.
+      // Or when starting with vanila local browser.
+      if (LocalDirView->Path.IsEmpty())
+      {
+        LocalDefaultDirectory();
+      }
 
-    if (RestoreState)
-    {
-      LocalDirView->RestoreState(ManagedSession->LocalExplorerState);
+      if (WinConfiguration->DefaultDirIsHome &&
+          !ManagedSession->SessionData->UpdateDirectories &&
+          DebugAlwaysTrue(!ManagedSession->LocalBrowser))
+      {
+        LocalDirView->HomeDirectory = ManagedSession->SessionData->LocalDirectoryExpanded;
+      }
 
-      if (ManagedSession->LocalBrowser)
+      if (RestoreState)
       {
-        OtherLocalDirView->RestoreState(ManagedSession->OtherLocalExplorerState);
+        LocalDirView->RestoreState(ManagedSession->LocalExplorerState);
+
+        if (ManagedSession->LocalBrowser)
+        {
+          OtherLocalDirView->RestoreState(ManagedSession->OtherLocalExplorerState);
+        }
+        NonVisualDataModule->SynchronizeBrowsingAction2->Checked = ManagedSession->StateData->SynchronizeBrowsing;
       }
-      NonVisualDataModule->SynchronizeBrowsingAction2->Checked = ManagedSession->StateData->SynchronizeBrowsing;
+    }
+    __finally
+    {
+      AnnounceLocalStates(RestoreState, ManagedSession->LocalBrowser, NULL, NULL);
     }
 
     if (ManagedSession->LocalBrowser)

+ 2 - 1
source/forms/ScpCommander.h

@@ -672,7 +672,8 @@ protected:
   virtual void __fastcall UpdateRemotePathComboBox(bool TextOnly);
   void __fastcall SetToolbar2ItemAction(TTBXItem * Item, TBasicAction * Action);
   virtual void __fastcall NeedSession(bool Startup);
-  void RestoreSessionLocalDirView(TDirView * ALocalDirView, const UnicodeString & LocalDirectory, TObject * State);
+  void RestoreSessionLocalDirView(TDirView * ALocalDirView, const UnicodeString & LocalDirectory);
+  void AnnounceLocalStates(bool RestoreState, bool LocalBrowser, TObject * State, TObject * OtherState);
   virtual UnicodeString GetTabHintDetails(TManagedTerminal * ASession);
   virtual UnicodeString GetNewTabHintDetails();
 

+ 2 - 0
source/packages/filemng/CustomDirView.pas

@@ -3542,6 +3542,8 @@ begin
     State := AState as TDirViewState;
     Assert(Assigned(State));
 
+    Assert((not Assigned(FAnnouncedState)) or (FAnnouncedState = AState));
+
     FHistoryPaths.Assign(State.HistoryPaths);
     FBackCount := State.BackCount;
     DoHistoryChange;