Skip to content

Commit bd108cb

Browse files
committed
a
1 parent 9bdc914 commit bd108cb

4 files changed

Lines changed: 153 additions & 116 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
namespace Files.App.Data.Items
5+
{
6+
public static class ToolbarDefaultsTemplate
7+
{
8+
public const string AlwaysVisibleContextId = "AlwaysVisible";
9+
public const string FolderPagesContextId = "FolderPages";
10+
public const string ArchiveFilesContextId = "ArchiveFiles";
11+
public const string ScriptFilesContextId = "ScriptFiles";
12+
public const string ImageFilesContextId = "ImageFiles";
13+
public const string MediaFilesContextId = "MediaFiles";
14+
public const string FontFilesContextId = "FontFiles";
15+
public const string DriverFilesContextId = "DriverFiles";
16+
public const string CertificateFilesContextId = "CertificateFiles";
17+
public const string RecycleBinContextId = "RecycleBin";
18+
public const string OtherContextsContextId = "OtherItems";
19+
20+
private const string GroupPrefix = "Group:";
21+
private const string IconAndLabelSuffix = "|label";
22+
23+
private static readonly string[] ContextIdsWithoutDefaults =
24+
[
25+
FolderPagesContextId,
26+
OtherContextsContextId,
27+
];
28+
29+
public static readonly IReadOnlyDictionary<string, string[]> DefaultIdentifiersByContext =
30+
new Dictionary<string, string[]>(StringComparer.Ordinal)
31+
{
32+
[AlwaysVisibleContextId] =
33+
[
34+
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.NewItem))),
35+
ToolbarItemDescriptor.SeparatorIdentifier,
36+
nameof(CommandCodes.CutItem),
37+
nameof(CommandCodes.CopyItem),
38+
nameof(CommandCodes.PasteItem),
39+
nameof(CommandCodes.Rename),
40+
nameof(CommandCodes.ShareItem),
41+
nameof(CommandCodes.DeleteItem),
42+
nameof(CommandCodes.OpenProperties),
43+
],
44+
[ArchiveFilesContextId] =
45+
[
46+
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.Extract))),
47+
],
48+
[ScriptFilesContextId] =
49+
[
50+
WithLabel(nameof(CommandCodes.RunWithPowershell)),
51+
WithLabel(nameof(CommandCodes.EditInNotepad)),
52+
],
53+
[ImageFilesContextId] =
54+
[
55+
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.SetAs))),
56+
WithLabel(nameof(CommandCodes.SetAsSlideshowBackground)),
57+
WithLabel(nameof(CommandCodes.RotateLeft)),
58+
WithLabel(nameof(CommandCodes.RotateRight)),
59+
],
60+
[MediaFilesContextId] =
61+
[
62+
WithLabel(nameof(CommandCodes.PlayAll)),
63+
],
64+
[FontFilesContextId] =
65+
[
66+
WithLabel(nameof(CommandCodes.InstallFont)),
67+
],
68+
[DriverFilesContextId] =
69+
[
70+
WithLabel(nameof(CommandCodes.InstallInfDriver)),
71+
],
72+
[CertificateFilesContextId] =
73+
[
74+
WithLabel(nameof(CommandCodes.InstallCertificate)),
75+
],
76+
[RecycleBinContextId] =
77+
[
78+
WithLabel(nameof(CommandCodes.EmptyRecycleBin)),
79+
WithLabel(nameof(CommandCodes.RestoreAllRecycleBin)),
80+
WithLabel(nameof(CommandCodes.RestoreRecycleBin)),
81+
],
82+
};
83+
84+
public static readonly string[] ContextOrder = BuildContextOrder();
85+
86+
public static Dictionary<string, List<string>> CreateDefaultIdentifiersByContext()
87+
=> ContextOrder
88+
.ToDictionary(
89+
static contextId => contextId,
90+
static contextId => DefaultIdentifiersByContext.TryGetValue(contextId, out var identifiers)
91+
? identifiers.ToList()
92+
: [],
93+
StringComparer.Ordinal);
94+
95+
private static string WithLabel(string identifier)
96+
=> identifier + IconAndLabelSuffix;
97+
98+
private static string CreateGroupIdentifier(string groupName)
99+
=> GroupPrefix + groupName;
100+
101+
private static string[] BuildContextOrder()
102+
{
103+
var contextOrder = new List<string>(DefaultIdentifiersByContext.Count + ContextIdsWithoutDefaults.Length)
104+
{
105+
AlwaysVisibleContextId,
106+
};
107+
108+
var seenContextIds = new HashSet<string>(StringComparer.Ordinal)
109+
{
110+
AlwaysVisibleContextId,
111+
};
112+
113+
foreach (var contextId in DefaultIdentifiersByContext.Keys)
114+
{
115+
if (seenContextIds.Add(contextId))
116+
contextOrder.Add(contextId);
117+
}
118+
119+
foreach (var contextId in ContextIdsWithoutDefaults)
120+
{
121+
if (seenContextIds.Add(contextId))
122+
contextOrder.Add(contextId);
123+
}
124+
125+
return [.. contextOrder];
126+
}
127+
}
128+
}

src/Files.App/Data/Items/ToolbarItemDescriptor.cs

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.UI.Xaml;
55
using System.Text.RegularExpressions;
66
using Files.App.Actions;
7+
using static Files.App.Data.Items.ToolbarDefaultsTemplate;
78

89
namespace Files.App.Data.Items
910
{
@@ -12,18 +13,6 @@ namespace Files.App.Data.Items
1213
/// </summary>
1314
public sealed partial class ToolbarItemDescriptor : ObservableObject
1415
{
15-
public const string AlwaysVisibleContextId = "AlwaysVisible";
16-
public const string FolderPagesContextId = "FolderPages";
17-
public const string ArchiveFilesContextId = "ArchiveFiles";
18-
public const string ScriptFilesContextId = "ScriptFiles";
19-
public const string ImageFilesContextId = "ImageFiles";
20-
public const string MediaFilesContextId = "MediaFiles";
21-
public const string FontFilesContextId = "FontFiles";
22-
public const string DriverFilesContextId = "DriverFiles";
23-
public const string CertificateFilesContextId = "CertificateFiles";
24-
public const string RecycleBinContextId = "RecycleBin";
25-
public const string OtherContextsContextId = "OtherItems";
26-
2716
private const string GroupPrefix = "Group:";
2817
private const string IconAndLabelSuffix = "|label";
2918
private const string LabelOnlySuffix = "|labelonly";
@@ -139,7 +128,7 @@ public static IEnumerable<ToolbarItemDescriptor> GetAvailableItemsForContext(str
139128
}
140129

141130
public static bool IsKnownContextId(string contextId)
142-
=> ContextOrder.Contains(contextId, StringComparer.Ordinal);
131+
=> ToolbarDefaultsTemplate.ContextOrder.Contains(contextId, StringComparer.Ordinal);
143132

144133
public static bool IsOtherContextsId(string contextId)
145134
=> string.Equals(contextId, OtherContextsContextId, StringComparison.Ordinal);
@@ -180,15 +169,6 @@ public static string ResolveContextId(string identifier, ICommandManager command
180169
return OtherContextsContextId;
181170
}
182171

183-
public static Dictionary<string, List<string>> CreateDefaultIdentifiersByContext()
184-
=> ContextOrder
185-
.ToDictionary(
186-
static contextId => contextId,
187-
static contextId => DefaultIdentifiersByContext.TryGetValue(contextId, out var identifiers)
188-
? identifiers.ToList()
189-
: [],
190-
StringComparer.Ordinal);
191-
192172
public static Dictionary<string, List<string>> MigrateLegacyIdentifiersByContext(IReadOnlyList<string> identifiers, ICommandManager commandManager)
193173
{
194174
var migrated = ContextOrder
@@ -265,21 +245,6 @@ public static (string Identifier, ToolbarItemDisplayMode DisplayMode) ParseSetti
265245
return null;
266246
}
267247

268-
public static readonly string[] ContextOrder =
269-
[
270-
AlwaysVisibleContextId,
271-
FolderPagesContextId,
272-
ArchiveFilesContextId,
273-
ScriptFilesContextId,
274-
ImageFilesContextId,
275-
MediaFilesContextId,
276-
FontFilesContextId,
277-
DriverFilesContextId,
278-
CertificateFilesContextId,
279-
RecycleBinContextId,
280-
OtherContextsContextId,
281-
];
282-
283248
private static string GetContextId(CommandGroup group, ICommandManager commandManager)
284249
=> group.Name switch
285250
{
@@ -402,62 +367,6 @@ or CommandCodes.OpenStorageSenseFromSidebar
402367
_ => OtherContextsContextId,
403368
};
404369

405-
// Default toolbar item identifiers grouped by context, matching the original hardcoded XAML layout.
406-
public static readonly IReadOnlyDictionary<string, string[]> DefaultIdentifiersByContext =
407-
new Dictionary<string, string[]>(StringComparer.Ordinal)
408-
{
409-
[AlwaysVisibleContextId] =
410-
[
411-
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.NewItem))),
412-
SeparatorIdentifier,
413-
nameof(CommandCodes.CutItem),
414-
nameof(CommandCodes.CopyItem),
415-
nameof(CommandCodes.PasteItem),
416-
nameof(CommandCodes.Rename),
417-
nameof(CommandCodes.ShareItem),
418-
nameof(CommandCodes.DeleteItem),
419-
nameof(CommandCodes.OpenProperties),
420-
],
421-
[ArchiveFilesContextId] =
422-
[
423-
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.Extract))),
424-
],
425-
[ScriptFilesContextId] =
426-
[
427-
WithLabel(nameof(CommandCodes.RunWithPowershell)),
428-
WithLabel(nameof(CommandCodes.EditInNotepad)),
429-
],
430-
[ImageFilesContextId] =
431-
[
432-
WithLabel(CreateGroupIdentifier(nameof(CommandGroups.SetAs))),
433-
WithLabel(nameof(CommandCodes.SetAsSlideshowBackground)),
434-
WithLabel(nameof(CommandCodes.RotateLeft)),
435-
WithLabel(nameof(CommandCodes.RotateRight)),
436-
],
437-
[MediaFilesContextId] =
438-
[
439-
WithLabel(nameof(CommandCodes.PlayAll)),
440-
],
441-
[FontFilesContextId] =
442-
[
443-
WithLabel(nameof(CommandCodes.InstallFont)),
444-
],
445-
[DriverFilesContextId] =
446-
[
447-
WithLabel(nameof(CommandCodes.InstallInfDriver)),
448-
],
449-
[CertificateFilesContextId] =
450-
[
451-
WithLabel(nameof(CommandCodes.InstallCertificate)),
452-
],
453-
[RecycleBinContextId] =
454-
[
455-
WithLabel(nameof(CommandCodes.EmptyRecycleBin)),
456-
WithLabel(nameof(CommandCodes.RestoreAllRecycleBin)),
457-
WithLabel(nameof(CommandCodes.RestoreRecycleBin)),
458-
],
459-
};
460-
461370
}
462371

463372
}

src/Files.App/UserControls/Toolbar.xaml.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ private HashSet<string> GetActiveToolbarContexts()
152152
{
153153
var activeContexts = new HashSet<string>(StringComparer.Ordinal)
154154
{
155-
ToolbarItemDescriptor.AlwaysVisibleContextId,
155+
ToolbarDefaultsTemplate.AlwaysVisibleContextId,
156156
};
157157

158158
foreach (var command in Commands)
@@ -164,7 +164,7 @@ private HashSet<string> GetActiveToolbarContexts()
164164

165165
var contextId = ToolbarItemDescriptor.ResolveContextId(command.Code.ToString(), Commands);
166166

167-
if (string.Equals(contextId, ToolbarItemDescriptor.RecycleBinContextId, StringComparison.Ordinal)
167+
if (string.Equals(contextId, ToolbarDefaultsTemplate.RecycleBinContextId, StringComparison.Ordinal)
168168
&& ViewModel?.InstanceViewModel?.IsPageTypeRecycleBin != true)
169169
{
170170
continue;
@@ -178,7 +178,7 @@ private HashSet<string> GetActiveToolbarContexts()
178178

179179
private static bool ShouldShowContext(string contextId, ISet<string> activeContexts)
180180
{
181-
if (string.Equals(contextId, ToolbarItemDescriptor.AlwaysVisibleContextId, StringComparison.Ordinal))
181+
if (string.Equals(contextId, ToolbarDefaultsTemplate.AlwaysVisibleContextId, StringComparison.Ordinal))
182182
return true;
183183

184184
if (ToolbarItemDescriptor.IsOtherContextsId(contextId))
@@ -191,13 +191,13 @@ private static bool ShouldShowContext(string contextId, ISet<string> activeConte
191191
{
192192
var identifiersByContext = GetMutableToolbarItemsByContext();
193193

194-
foreach (var contextId in ToolbarItemDescriptor.ContextOrder)
194+
foreach (var contextId in ToolbarDefaultsTemplate.ContextOrder)
195195
{
196196
if (!identifiersByContext.TryGetValue(contextId, out var contextIdentifiers) || contextIdentifiers.Count == 0)
197197
continue;
198198

199199
// Auto-inject a separator before non-AlwaysVisible contexts
200-
if (!string.Equals(contextId, ToolbarItemDescriptor.AlwaysVisibleContextId, StringComparison.Ordinal))
200+
if (!string.Equals(contextId, ToolbarDefaultsTemplate.AlwaysVisibleContextId, StringComparison.Ordinal))
201201
yield return (contextId, ToolbarItemDescriptor.SeparatorIdentifier, null);
202202

203203
for (int index = 0; index < contextIdentifiers.Count; index++)
@@ -272,7 +272,7 @@ private Dictionary<string, List<string>> GetMutableToolbarItemsByContext()
272272

273273
var migrated = legacyIdentifiers is { Count: > 0 }
274274
? ToolbarItemDescriptor.MigrateLegacyIdentifiersByContext(legacyIdentifiers, Commands)
275-
: ToolbarItemDescriptor.CreateDefaultIdentifiersByContext();
275+
: ToolbarDefaultsTemplate.CreateDefaultIdentifiersByContext();
276276

277277
ApplyNewDefaultToolbarItemsIfNeeded(migrated, hasExistingToolbarConfig);
278278

@@ -282,7 +282,7 @@ private Dictionary<string, List<string>> GetMutableToolbarItemsByContext()
282282
private void ApplyNewDefaultToolbarItemsIfNeeded(Dictionary<string, List<string>> itemsByContext, bool hasExistingToolbarConfig)
283283
{
284284
var appearanceSettings = UserSettingsService.AppearanceSettingsService;
285-
var currentDefaultTemplate = ToolbarItemDescriptor.CreateDefaultIdentifiersByContext();
285+
var currentDefaultTemplate = ToolbarDefaultsTemplate.CreateDefaultIdentifiersByContext();
286286

287287
if (!hasExistingToolbarConfig)
288288
{
@@ -301,7 +301,7 @@ private void ApplyNewDefaultToolbarItemsIfNeeded(Dictionary<string, List<string>
301301

302302
var hasChanges = false;
303303

304-
foreach (var contextId in ToolbarItemDescriptor.ContextOrder)
304+
foreach (var contextId in ToolbarDefaultsTemplate.ContextOrder)
305305
{
306306
var defaultItems = currentDefaultTemplate.TryGetValue(contextId, out var defaultsForContext)
307307
? defaultsForContext
@@ -373,7 +373,7 @@ private static bool AreTemplatesEqual(
373373
private ICommandBarElement? CreateToolbarElement(string contextId, string settingsString)
374374
{
375375
var (identifier, displayMode) = ToolbarItemDescriptor.ParseSettingsString(settingsString);
376-
var keepVisibleWhenDisabled = string.Equals(contextId, ToolbarItemDescriptor.AlwaysVisibleContextId, StringComparison.Ordinal);
376+
var keepVisibleWhenDisabled = string.Equals(contextId, ToolbarDefaultsTemplate.AlwaysVisibleContextId, StringComparison.Ordinal);
377377

378378
if (ToolbarItemDescriptor.IsSeparatorIdentifier(identifier))
379379
return new AppBarSeparator();

0 commit comments

Comments
 (0)