Skip to content

Commit

Permalink
Hide theme fields for serialization from public interface (#8907)
Browse files Browse the repository at this point in the history
Hide theme data properties from its public interface, since they don't provide a behavior one would naturally expect from a public property with a given name.

The only intended usage for these properties is serialization.
  • Loading branch information
NikolayXHD committed Mar 1, 2021
1 parent deaefda commit acd50e5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
11 changes: 11 additions & 0 deletions GitExtUtils/GitUI/Theming/IThemeSerializationData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Drawing;

namespace GitExtUtils.GitUI.Theming
{
public interface IThemeSerializationData
{
IReadOnlyDictionary<AppColor, Color> AppColorValues { get; }
IReadOnlyDictionary<KnownColor, Color> SysColorValues { get; }
}
}
18 changes: 11 additions & 7 deletions GitExtUtils/GitUI/Theming/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GitExtUtils.GitUI.Theming
/// <summary>
/// A set of values for .Net system colors and GitExtensions app-specific colors.
/// </summary>
public class Theme
public class Theme : IThemeSerializationData
{
private static readonly IReadOnlyDictionary<KnownColor, KnownColor> Duplicates =
new Dictionary<KnownColor, KnownColor>
Expand All @@ -19,8 +19,12 @@ public class Theme
};

private static Theme? _default;
public IReadOnlyDictionary<AppColor, Color> AppColorValues { get; }
public IReadOnlyDictionary<KnownColor, Color> SysColorValues { get; }

private readonly IReadOnlyDictionary<AppColor, Color> _appColorValues;
private readonly IReadOnlyDictionary<KnownColor, Color> _sysColorValues;

IReadOnlyDictionary<AppColor, Color> IThemeSerializationData.AppColorValues => _appColorValues;
IReadOnlyDictionary<KnownColor, Color> IThemeSerializationData.SysColorValues => _sysColorValues;

public static Theme Default => _default ??= CreateDefaultTheme();

Expand All @@ -30,8 +34,8 @@ public class Theme
ThemeId id)
{
Id = id;
AppColorValues = appColors;
SysColorValues = sysColors;
_appColorValues = appColors;
_sysColorValues = sysColors;
}

public ThemeId Id { get; }
Expand All @@ -41,15 +45,15 @@ public class Theme
/// returns <see cref="Color.Empty"/>.
/// </summary>
public Color GetColor(AppColor name) =>
AppColorValues.TryGetValue(name, out var result)
_appColorValues.TryGetValue(name, out var result)
? result
: Color.Empty;

/// <summary>
/// Get .Net system color value as defined by this instance.
/// </summary>
private Color GetSysColor(KnownColor name) =>
SysColorValues.TryGetValue(name, out var result)
_sysColorValues.TryGetValue(name, out var result)
? result
: Color.Empty;

Expand Down
2 changes: 1 addition & 1 deletion GitUI/Theming/ThemeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static ThemeSettings LoadThemeSettings()
return CreateFallbackSettings(invariantTheme);
}

IsDarkTheme = theme.SysColorValues[KnownColor.Window].GetBrightness() < 0.5;
IsDarkTheme = theme.GetNonEmptyColor(KnownColor.Window).GetBrightness() < 0.5;

return new ThemeSettings(theme, invariantTheme, AppSettings.ThemeVariations, AppSettings.UseSystemVisualStyle);
}
Expand Down
6 changes: 4 additions & 2 deletions GitUI/Theming/ThemePersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public Theme Load(string themeFileName, ThemeId themeId, IReadOnlyList<string> v

public void Save(Theme theme, string themeFileName)
{
var serializationData = (IThemeSerializationData)theme;
string serialized = string.Join(
Environment.NewLine,
theme.SysColorValues.Select(_ => string.Format(Format, _.Key, FormatColor(_.Value))).Concat(
theme.AppColorValues.Select(_ => string.Format(Format, _.Key, FormatColor(_.Value)))));
Enumerable.Concat(
serializationData.SysColorValues.Select(_ => string.Format(Format, _.Key, FormatColor(_.Value))),
serializationData.AppColorValues.Select(_ => string.Format(Format, _.Key, FormatColor(_.Value)))));

File.WriteAllText(themeFileName, serialized);
}
Expand Down

0 comments on commit acd50e5

Please sign in to comment.