@@ -381,6 +381,8 @@ public static void RenameProfileFile(ProfileFileInfo profileFileInfo, string new
381381 File . Copy ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
382382 ProfileFiles . Add ( newProfileFileInfo ) ;
383383
384+ MigrateGroupExpandState ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
385+
384386 // Switch profile, if it was previously loaded
385387 if ( switchProfile )
386388 {
@@ -407,6 +409,8 @@ public static void DeleteProfileFile(ProfileFileInfo profileFileInfo)
407409 if ( LoadedProfileFile != null && LoadedProfileFile . Equals ( profileFileInfo ) )
408410 LoadedProfileFileChanged ( ProfileFiles . FirstOrDefault ( x => ! x . Equals ( profileFileInfo ) ) ) ;
409411
412+ RemoveGroupExpandState ( profileFileInfo . Path ) ;
413+
410414 File . Delete ( profileFileInfo . Path ) ;
411415 ProfileFiles . Remove ( profileFileInfo ) ;
412416 }
@@ -478,6 +482,8 @@ public static void EnableEncryption(ProfileFileInfo profileFileInfo, SecureStrin
478482 // Add the new profile
479483 ProfileFiles . Add ( newProfileFileInfo ) ;
480484
485+ MigrateGroupExpandState ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
486+
481487 // Switch profile, if it was previously loaded
482488 if ( switchProfile )
483489 {
@@ -563,6 +569,8 @@ public static void ChangeMasterPassword(ProfileFileInfo profileFileInfo, SecureS
563569 // Add the new profile
564570 ProfileFiles . Add ( newProfileFileInfo ) ;
565571
572+ MigrateGroupExpandState ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
573+
566574 // Switch profile, if it was previously loaded
567575 if ( switchProfile )
568576 {
@@ -632,6 +640,8 @@ public static void DisableEncryption(ProfileFileInfo profileFileInfo, SecureStri
632640 // Add the new profile
633641 ProfileFiles . Add ( newProfileFileInfo ) ;
634642
643+ MigrateGroupExpandState ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
644+
635645 // Switch profile, if it was previously loaded
636646 if ( switchProfile )
637647 {
@@ -739,6 +749,8 @@ private static void Load(ProfileFileInfo profileFileInfo)
739749 // Add the new profile
740750 ProfileFiles . Add ( newProfileFileInfo ) ;
741751
752+ MigrateGroupExpandState ( profileFileInfo . Path , newProfileFileInfo . Path ) ;
753+
742754 // Switch profile
743755 Log . Info ( $ "Switching to migrated profile file: { newProfileFileInfo . Path } .") ;
744756 Switch ( newProfileFileInfo , false ) ;
@@ -849,6 +861,70 @@ public static void Switch(ProfileFileInfo info, bool saveLoadedProfiles = true)
849861
850862 #endregion
851863
864+ #region Group expand state persistence
865+
866+ /// <summary>
867+ /// Moves the persisted group-expand state (see <see cref="SettingsInfo.Profiles_GroupExpandState" />)
868+ /// from the old to the new profile file path. No-op if the paths are equal or there is no state for
869+ /// the old path.
870+ /// </summary>
871+ private static void MigrateGroupExpandState ( string oldPath , string newPath )
872+ {
873+ if ( oldPath == newPath )
874+ return ;
875+
876+ if ( SettingsManager . Current . Profiles_GroupExpandState . Remove ( oldPath , out var state ) )
877+ {
878+ SettingsManager . Current . Profiles_GroupExpandState [ newPath ] = state ;
879+ SettingsManager . Current . SettingsChanged = true ;
880+ }
881+ }
882+
883+ /// <summary>
884+ /// Removes the persisted group-expand state for a profile file that no longer exists.
885+ /// </summary>
886+ private static void RemoveGroupExpandState ( string path )
887+ {
888+ if ( SettingsManager . Current . Profiles_GroupExpandState . Remove ( path ) )
889+ SettingsManager . Current . SettingsChanged = true ;
890+ }
891+
892+ /// <summary>
893+ /// Renames the group key inside the persisted group-expand state for the currently loaded profile file.
894+ /// </summary>
895+ private static void MigrateGroupExpandStateKey ( string oldGroupName , string newGroupName )
896+ {
897+ if ( oldGroupName == newGroupName )
898+ return ;
899+
900+ if ( LoadedProfileFile == null )
901+ return ;
902+
903+ if ( ! SettingsManager . Current . Profiles_GroupExpandState . TryGetValue ( LoadedProfileFile . Path , out var state ) )
904+ return ;
905+
906+ if ( ! state . Remove ( oldGroupName , out var isExpanded ) )
907+ return ;
908+
909+ state [ newGroupName ] = isExpanded ;
910+ SettingsManager . Current . SettingsChanged = true ;
911+ }
912+
913+ /// <summary>
914+ /// Removes the group key from the persisted group-expand state for the currently loaded profile file.
915+ /// </summary>
916+ private static void RemoveGroupExpandStateKey ( string groupName )
917+ {
918+ if ( LoadedProfileFile == null )
919+ return ;
920+
921+ if ( SettingsManager . Current . Profiles_GroupExpandState . TryGetValue ( LoadedProfileFile . Path , out var state )
922+ && state . Remove ( groupName ) )
923+ SettingsManager . Current . SettingsChanged = true ;
924+ }
925+
926+ #endregion
927+
852928 #region Serialize and deserialize
853929
854930 /// <summary>
@@ -1085,6 +1161,8 @@ public static void ReplaceGroup(GroupInfo oldGroup, GroupInfo newGroup)
10851161 LoadedProfileFileData . Groups . Remove ( oldGroup ) ;
10861162 LoadedProfileFileData . Groups . Add ( newGroup ) ;
10871163
1164+ MigrateGroupExpandStateKey ( oldGroup . Name , newGroup . Name ) ;
1165+
10881166 ProfilesUpdated ( ) ;
10891167 }
10901168
@@ -1099,6 +1177,8 @@ public static void RemoveGroup(GroupInfo group)
10991177
11001178 LoadedProfileFileData . Groups . Remove ( group ) ;
11011179
1180+ RemoveGroupExpandStateKey ( group . Name ) ;
1181+
11021182 ProfilesUpdated ( ) ;
11031183 }
11041184
0 commit comments