Skip to content

Commit d2e0af5

Browse files
committed
Code Quality: Store sidebar group expansion state
1 parent cea51f6 commit d2e0af5

3 files changed

Lines changed: 130 additions & 9 deletions

File tree

src/Files.App/Data/Contracts/IGeneralSettingsService.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,41 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
170170
/// </summary>
171171
bool ShowFileTagsSection { get; set; }
172172

173+
/// <summary>
174+
/// Gets or sets a value indicating if the pinned section should be expanded.
175+
/// </summary>
176+
bool IsPinnedSectionExpanded { get; set; }
177+
178+
/// <summary>
179+
/// Gets or sets a value indicating if the library section should be expanded.
180+
/// </summary>
181+
bool IsLibrarySectionExpanded { get; set; }
182+
183+
/// <summary>
184+
/// Gets or sets a value indicating if the drive section should be expanded.
185+
/// </summary>
186+
bool IsDriveSectionExpanded { get; set; }
187+
188+
/// <summary>
189+
/// Gets or sets a value indicating if the cloud drive section should be expanded.
190+
/// </summary>
191+
bool IsCloudDriveSectionExpanded { get; set; }
192+
193+
/// <summary>
194+
/// Gets or sets a value indicating if the network section should be expanded.
195+
/// </summary>
196+
bool IsNetworkSectionExpanded { get; set; }
197+
198+
/// <summary>
199+
/// Gets or sets a value indicating if the wsl section should be expanded.
200+
/// </summary>
201+
bool IsWslSectionExpanded { get; set; }
202+
203+
/// <summary>
204+
/// Gets or sets a value indicating if the file tags section should be expanded.
205+
/// </summary>
206+
bool IsFileTagsSectionExpanded { get; set; }
207+
173208
/// <summary>
174209
/// Gets or sets a value indicating whether or not to move shell extensions into a sub menu.
175210
/// </summary>

src/Files.App/Services/Settings/GeneralSettingsService.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,48 @@ public bool ShowFileTagsSection
209209
set => Set(value);
210210
}
211211

212+
public bool IsPinnedSectionExpanded
213+
{
214+
get => Get(true);
215+
set => Set(value);
216+
}
217+
218+
public bool IsLibrarySectionExpanded
219+
{
220+
get => Get(false);
221+
set => Set(value);
222+
}
223+
224+
public bool IsDriveSectionExpanded
225+
{
226+
get => Get(false);
227+
set => Set(value);
228+
}
229+
230+
public bool IsCloudDriveSectionExpanded
231+
{
232+
get => Get(false);
233+
set => Set(value);
234+
}
235+
236+
public bool IsNetworkSectionExpanded
237+
{
238+
get => Get(false);
239+
set => Set(value);
240+
}
241+
242+
public bool IsWslSectionExpanded
243+
{
244+
get => Get(false);
245+
set => Set(value);
246+
}
247+
248+
public bool IsFileTagsSectionExpanded
249+
{
250+
get => Get(false);
251+
set => Set(value);
252+
}
253+
212254
public bool MoveShellExtensionsToSubMenu
213255
{
214256
get => Get(true);

src/Files.App/ViewModels/UserControls/SidebarViewModel.TabExpansion.cs

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,8 @@ namespace Files.App.ViewModels.UserControls
88
{
99
public sealed partial class SidebarViewModel
1010
{
11-
// Each tab is the sole source of truth for its own expansion (sections and folders alike); Pinned/Drives/CloudDrives are seeded as expanded so a fresh tab matches the historical defaults.
11+
// Each tab is the sole source of truth for its own expansion (sections and folders alike); section expansion is additionally mirrored into the per-section IGeneralSettingsService.Is*SectionExpanded bools so it survives across sessions and seeds freshly opened tabs.
1212
private static readonly string[] SectionKeyByEnum = BuildSectionKeyByEnum();
13-
private static readonly string[] DefaultExpandedSectionKeys =
14-
{
15-
SectionKeyByEnum[(int)SectionType.Pinned],
16-
SectionKeyByEnum[(int)SectionType.Drives],
17-
SectionKeyByEnum[(int)SectionType.CloudDrives],
18-
};
1913

2014
private static string[] BuildSectionKeyByEnum()
2115
{
@@ -26,6 +20,54 @@ private static string[] BuildSectionKeyByEnum()
2620
return arr;
2721
}
2822

23+
private IEnumerable<string> GetPersistedExpandedSectionKeys()
24+
{
25+
var general = UserSettingsService.GeneralSettingsService;
26+
if (general.IsPinnedSectionExpanded)
27+
yield return SectionKeyByEnum[(int)SectionType.Pinned];
28+
if (general.IsLibrarySectionExpanded)
29+
yield return SectionKeyByEnum[(int)SectionType.Library];
30+
if (general.IsDriveSectionExpanded)
31+
yield return SectionKeyByEnum[(int)SectionType.Drives];
32+
if (general.IsCloudDriveSectionExpanded)
33+
yield return SectionKeyByEnum[(int)SectionType.CloudDrives];
34+
if (general.IsNetworkSectionExpanded)
35+
yield return SectionKeyByEnum[(int)SectionType.Network];
36+
if (general.IsWslSectionExpanded)
37+
yield return SectionKeyByEnum[(int)SectionType.WSL];
38+
if (general.IsFileTagsSectionExpanded)
39+
yield return SectionKeyByEnum[(int)SectionType.FileTag];
40+
}
41+
42+
private void PersistSectionExpansion(SectionType section, bool isExpanded)
43+
{
44+
var general = UserSettingsService.GeneralSettingsService;
45+
switch (section)
46+
{
47+
case SectionType.Pinned:
48+
general.IsPinnedSectionExpanded = isExpanded;
49+
break;
50+
case SectionType.Library:
51+
general.IsLibrarySectionExpanded = isExpanded;
52+
break;
53+
case SectionType.Drives:
54+
general.IsDriveSectionExpanded = isExpanded;
55+
break;
56+
case SectionType.CloudDrives:
57+
general.IsCloudDriveSectionExpanded = isExpanded;
58+
break;
59+
case SectionType.Network:
60+
general.IsNetworkSectionExpanded = isExpanded;
61+
break;
62+
case SectionType.WSL:
63+
general.IsWslSectionExpanded = isExpanded;
64+
break;
65+
case SectionType.FileTag:
66+
general.IsFileTagsSectionExpanded = isExpanded;
67+
break;
68+
}
69+
}
70+
2971
private readonly Dictionary<TabBarItem, HashSet<string>> tabExpansionState = new();
3072
private readonly HashSet<INotifyPropertyChanged> trackedItems = new();
3173
private readonly HashSet<INotifyCollectionChanged> trackedChildCollections = new();
@@ -113,8 +155,8 @@ private HashSet<string> GetOrInitTabState(TabBarItem tab)
113155
{
114156
if (!tabExpansionState.TryGetValue(tab, out var state))
115157
{
116-
state = new HashSet<string>(DefaultExpandedSectionKeys, StringComparer.OrdinalIgnoreCase);
117-
// Inherit whatever section state the user currently sees so a freshly opened tab matches expectation rather than snapping back to defaults.
158+
state = new HashSet<string>(GetPersistedExpandedSectionKeys(), StringComparer.OrdinalIgnoreCase);
159+
// Inherit whatever section state the user currently sees so a freshly opened tab matches expectation rather than snapping back to the persisted baseline.
118160
foreach (var sectionObj in sidebarItems)
119161
{
120162
if (sectionObj is LocationItem section && section.IsExpanded)
@@ -231,6 +273,8 @@ private void CaptureExpansionToCurrentTab(INavigationControlItem item)
231273
state.Add(key);
232274
else
233275
state.Remove(key);
276+
if (item is LocationItem { IsHeader: true } section)
277+
PersistSectionExpansion(section.Section, item.IsExpanded);
234278
}
235279

236280
// Restores expansion for the initial pass over sidebarItems and for items realized later (async section sync, lazy-loaded subfolders).

0 commit comments

Comments
 (0)