From acd50e5144e17a211c170242b61f4490eba8c3da Mon Sep 17 00:00:00 2001 From: NikolayHD Date: Mon, 1 Mar 2021 03:31:57 +0300 Subject: [PATCH] Hide theme fields for serialization from public interface (#8907) 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. --- .../GitUI/Theming/IThemeSerializationData.cs | 11 +++++++++++ GitExtUtils/GitUI/Theming/Theme.cs | 18 +++++++++++------- GitUI/Theming/ThemeModule.cs | 2 +- GitUI/Theming/ThemePersistence.cs | 6 ++++-- 4 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 GitExtUtils/GitUI/Theming/IThemeSerializationData.cs diff --git a/GitExtUtils/GitUI/Theming/IThemeSerializationData.cs b/GitExtUtils/GitUI/Theming/IThemeSerializationData.cs new file mode 100644 index 00000000000..264e730795b --- /dev/null +++ b/GitExtUtils/GitUI/Theming/IThemeSerializationData.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; +using System.Drawing; + +namespace GitExtUtils.GitUI.Theming +{ + public interface IThemeSerializationData + { + IReadOnlyDictionary AppColorValues { get; } + IReadOnlyDictionary SysColorValues { get; } + } +} diff --git a/GitExtUtils/GitUI/Theming/Theme.cs b/GitExtUtils/GitUI/Theming/Theme.cs index e1b8240448d..d881d4df4ee 100644 --- a/GitExtUtils/GitUI/Theming/Theme.cs +++ b/GitExtUtils/GitUI/Theming/Theme.cs @@ -8,7 +8,7 @@ namespace GitExtUtils.GitUI.Theming /// /// A set of values for .Net system colors and GitExtensions app-specific colors. /// - public class Theme + public class Theme : IThemeSerializationData { private static readonly IReadOnlyDictionary Duplicates = new Dictionary @@ -19,8 +19,12 @@ public class Theme }; private static Theme? _default; - public IReadOnlyDictionary AppColorValues { get; } - public IReadOnlyDictionary SysColorValues { get; } + + private readonly IReadOnlyDictionary _appColorValues; + private readonly IReadOnlyDictionary _sysColorValues; + + IReadOnlyDictionary IThemeSerializationData.AppColorValues => _appColorValues; + IReadOnlyDictionary IThemeSerializationData.SysColorValues => _sysColorValues; public static Theme Default => _default ??= CreateDefaultTheme(); @@ -30,8 +34,8 @@ public class Theme ThemeId id) { Id = id; - AppColorValues = appColors; - SysColorValues = sysColors; + _appColorValues = appColors; + _sysColorValues = sysColors; } public ThemeId Id { get; } @@ -41,7 +45,7 @@ public class Theme /// returns . /// public Color GetColor(AppColor name) => - AppColorValues.TryGetValue(name, out var result) + _appColorValues.TryGetValue(name, out var result) ? result : Color.Empty; @@ -49,7 +53,7 @@ public class Theme /// Get .Net system color value as defined by this instance. /// private Color GetSysColor(KnownColor name) => - SysColorValues.TryGetValue(name, out var result) + _sysColorValues.TryGetValue(name, out var result) ? result : Color.Empty; diff --git a/GitUI/Theming/ThemeModule.cs b/GitUI/Theming/ThemeModule.cs index 32e5f7d295b..4a41bc11d7c 100644 --- a/GitUI/Theming/ThemeModule.cs +++ b/GitUI/Theming/ThemeModule.cs @@ -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); } diff --git a/GitUI/Theming/ThemePersistence.cs b/GitUI/Theming/ThemePersistence.cs index b4cf85e0bb0..f9f9f01abe9 100644 --- a/GitUI/Theming/ThemePersistence.cs +++ b/GitUI/Theming/ThemePersistence.cs @@ -30,10 +30,12 @@ public Theme Load(string themeFileName, ThemeId themeId, IReadOnlyList 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); }