Skip to content

Commit c466080

Browse files
Fix: Fixed crash on the tags settings page (#11447)
1 parent d5f9d2b commit c466080

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/Files.App/Helpers/ColorHelpers.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,53 @@
11
using CommunityToolkit.WinUI.Helpers;
22
using System;
3+
using System.Globalization;
34
using Windows.UI;
45

56
namespace Files.App.Helpers
67
{
78
internal static class ColorHelpers
89
{
10+
private const int COLOR_LENGTH = 7;
11+
private const int COLOR_LENGTH_INCLUDING_ALPHA = 9;
12+
13+
private static readonly Color unknownTagColor = Color.FromArgb(255, 0x9E, 0xA3, 0xA1);
14+
915
/// <summary>
1016
/// Converts Hex to Windows.UI.Color.
1117
/// </summary>
12-
public static Color FromHex(string colorHex)
18+
public static Color FromHex(string? colorHex)
1319
{
20+
// If Hex string is invalid, return Unknown Tag's color
21+
if (
22+
string.IsNullOrWhiteSpace(colorHex) ||
23+
(colorHex.Length != COLOR_LENGTH && colorHex.Length != COLOR_LENGTH_INCLUDING_ALPHA)
24+
)
25+
return unknownTagColor;
26+
1427
colorHex = colorHex.Replace("#", string.Empty);
1528

1629
var alphaOffset = colorHex.Length == 8 ? 2 : 0;
1730

18-
var a = alphaOffset == 2 ? (byte)Convert.ToUInt32(colorHex.Substring(0, 2), 16) : (byte)255;
19-
var r = (byte)Convert.ToUInt32(colorHex.Substring(alphaOffset, 2), 16);
20-
var g = (byte)Convert.ToUInt32(colorHex.Substring(alphaOffset + 2, 2), 16);
21-
var b = (byte)Convert.ToUInt32(colorHex.Substring(alphaOffset + 4, 2), 16);
31+
var a = (byte)255;
32+
var alphaValid = alphaOffset == 0 || byte.TryParse(colorHex.AsSpan(0, 2), NumberStyles.HexNumber, null, out a);
33+
34+
if (
35+
alphaValid &&
36+
byte.TryParse(colorHex.AsSpan(alphaOffset, 2), NumberStyles.HexNumber, null, out byte r) &&
37+
byte.TryParse(colorHex.AsSpan(alphaOffset + 2, 2), NumberStyles.HexNumber, null, out byte g) &&
38+
byte.TryParse(colorHex.AsSpan(alphaOffset + 4, 2), NumberStyles.HexNumber, null, out byte b)
39+
)
40+
return Color.FromArgb(a, r, g, b);
2241

23-
return Color.FromArgb(a, r, g, b);
42+
return unknownTagColor;
2443
}
2544

2645
/// <summary>
2746
/// Converts Uint to Windows.UI.Color.
2847
/// </summary>
2948
public static Color FromUint(this uint value)
3049
{
31-
return Windows.UI.Color.FromArgb((byte)((value >> 24) & 0xFF),
50+
return Color.FromArgb((byte)((value >> 24) & 0xFF),
3251
(byte)((value >> 16) & 0xFF),
3352
(byte)((value >> 8) & 0xFF),
3453
(byte)(value & 0xFF));

src/Files.App/ServicesImplementation/Settings/FileTagsSettingsService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Files.App.Extensions;
22
using Files.App.Filesystem;
3+
using Files.App.Helpers;
34
using Files.App.Serialization;
45
using Files.App.Serialization.Implementation;
56
using Files.Backend.Services.Settings;
@@ -38,7 +39,15 @@ public FileTagsSettingsService()
3839

3940
public IList<TagViewModel> FileTagList
4041
{
41-
get => Get<List<TagViewModel>>(DefaultFileTags);
42+
get
43+
{
44+
var tags = Get<List<TagViewModel>>(DefaultFileTags);
45+
46+
foreach (var tag in tags!)
47+
tag.Color = ColorHelpers.FromHex(tag.Color).ToString();
48+
49+
return tags;
50+
}
4251
set
4352
{
4453
Set(value);
@@ -168,14 +177,14 @@ public override object ExportSettings()
168177

169178
private void UntagAllFiles(string uid)
170179
{
171-
var tagDoDelete = new string [] { uid };
180+
var tagDoDelete = new string[] { uid };
172181

173182
foreach (var item in FileTagsHelper.GetDbInstance().GetAll())
174183
{
175184
if (item.Tags.Contains(uid))
176-
{
185+
{
177186
FileTagsHelper.WriteFileTag(
178-
item.FilePath,
187+
item.FilePath,
179188
item.Tags.Except(tagDoDelete).ToArray());
180189
}
181190
}

0 commit comments

Comments
 (0)