From e2c5ae4bfdde9ae74d20214eca313e39cf026f6c Mon Sep 17 00:00:00 2001 From: John Lewin Date: Tue, 23 Oct 2018 12:37:50 -0700 Subject: [PATCH] Add SchemaVersion to reset themes to default on change - Extract embedded ThemeSet type to new file - Eliminate redundant SerializedTheme type - Issue MatterHackers/MCCentral#4353 Toggle buttons missing dots --- .../ApplicationView/ApplicationController.cs | 30 +++------ .../ApplicationView/Themes/DirectoryTheme.cs | 42 ++++++------- .../ApplicationView/Themes/IColorTheme.cs | 4 +- .../ApplicationView/Themes/ThemeSet.cs | 63 +++++++++++++++++++ .../ApplicationSettings/ThemeColorPanel.cs | 4 +- StaticData/Themes/System/Affinity/theme.json | 5 -- StaticData/Themes/System/Classic/theme.json | 5 -- StaticData/Themes/System/Solarized/theme.json | 5 -- 8 files changed, 93 insertions(+), 65 deletions(-) create mode 100644 MatterControlLib/ApplicationView/Themes/ThemeSet.cs delete mode 100644 StaticData/Themes/System/Affinity/theme.json delete mode 100644 StaticData/Themes/System/Classic/theme.json delete mode 100644 StaticData/Themes/System/Solarized/theme.json diff --git a/MatterControlLib/ApplicationView/ApplicationController.cs b/MatterControlLib/ApplicationView/ApplicationController.cs index 0c7341030..073a8c96c 100644 --- a/MatterControlLib/ApplicationView/ApplicationController.cs +++ b/MatterControlLib/ApplicationView/ApplicationController.cs @@ -152,6 +152,12 @@ namespace MatterHackers.MatterControl if (File.Exists(ProfileManager.Instance.ProfileThemeSetPath)) { themeset = JsonConvert.DeserializeObject(File.ReadAllText(ProfileManager.Instance.ProfileThemeSetPath)); + + // If the serialized format is older than the current format, null and fall back to latest default below + if (themeset.SchemeVersion != ThemeSet.LatestSchemeVersion) + { + themeset = null; + } } } catch { } @@ -159,7 +165,7 @@ namespace MatterHackers.MatterControl if (themeset == null) { var themeProvider = ThemeProviders.Values.First(); - themeset = themeProvider.GetTheme(themeProvider.ThemeNames.First(), themeProvider.DefaultColor); + themeset = themeProvider.GetTheme(themeProvider.ThemeNames.First()); } DefaultThumbView.ThumbColor = new Color(themeset.Theme.Colors.PrimaryTextColor, 30); @@ -199,28 +205,6 @@ namespace MatterHackers.MatterControl } } - public class ThemeSet - { - public string ThemeID { get; set; } - - public string Name { get; set; } - - public ThemeConfig Theme { get; set; } - - public ThemeConfig MenuTheme { get; set; } - - public List AccentColors { get; set; } = new List(); - - public void SetAccentColor(Color accentColor) - { - this.Theme.PrimaryAccentColor = accentColor; - this.Theme.AccentMimimalOverlay = accentColor.WithAlpha(90); - - this.MenuTheme.PrimaryAccentColor = accentColor; - this.MenuTheme.AccentMimimalOverlay = accentColor.WithAlpha(90); - } - } - public class ApplicationController { public HelpArticle HelpArticles { get; set; } diff --git a/MatterControlLib/ApplicationView/Themes/DirectoryTheme.cs b/MatterControlLib/ApplicationView/Themes/DirectoryTheme.cs index e628a8f8b..de8cb5789 100644 --- a/MatterControlLib/ApplicationView/Themes/DirectoryTheme.cs +++ b/MatterControlLib/ApplicationView/Themes/DirectoryTheme.cs @@ -37,50 +37,45 @@ namespace MatterHackers.MatterControl using MatterHackers.Agg.Platform; using Newtonsoft.Json; - public class SerializedTheme : IColorTheme - { - public string Name { get; set; } - - public Color DefaultColor { get; set; } - - public IEnumerable ThemeNames { get; set; } - - public ThemeSet GetTheme(string mode, Color accentColor) - { - throw new System.NotImplementedException(); - } - } - public class DirectoryTheme : IColorTheme { private string path; + public DirectoryTheme() { } public DirectoryTheme(string directory) { - var themeSetData = JsonConvert.DeserializeObject( - AggContext.StaticData.ReadAllText(Path.Combine(directory, "theme.json"))); - path = directory; this.Name = Path.GetFileName(directory); this.ThemeNames = AggContext.StaticData.GetFiles(directory).Where(p => Path.GetFileName(p) != "theme.json").Select(p => Path.GetFileNameWithoutExtension(p)).ToArray(); - - this.DefaultColor = themeSetData.DefaultColor; } public string Name { get; } - public Color DefaultColor { get; } - public IEnumerable ThemeNames { get; } public ThemeSet GetTheme(string themeName, Color accentColor) { - ThemeSet themeset = null; + var themeset = this.LoadTheme(themeName); + themeset.SetAccentColor(accentColor); + return themeset; + } + + public ThemeSet GetTheme(string themeName) + { + var themeset = this.LoadTheme(themeName); + themeset.SetAccentColor(themeset.AccentColors.First()); + + return themeset; + } + + private ThemeSet LoadTheme(string themeName) + { + ThemeSet themeset; try { themeset = JsonConvert.DeserializeObject( @@ -92,8 +87,9 @@ namespace MatterHackers.MatterControl AggContext.StaticData.ReadAllText(Path.Combine(path, this.ThemeNames.First() + ".json"))); } + // Set SchemaVersion at construction time + themeset.SchemeVersion = ThemeSet.LatestSchemeVersion; themeset.ThemeID = themeName; - themeset.SetAccentColor(accentColor); return themeset; } diff --git a/MatterControlLib/ApplicationView/Themes/IColorTheme.cs b/MatterControlLib/ApplicationView/Themes/IColorTheme.cs index eb39c46aa..6ccd54719 100644 --- a/MatterControlLib/ApplicationView/Themes/IColorTheme.cs +++ b/MatterControlLib/ApplicationView/Themes/IColorTheme.cs @@ -36,9 +36,11 @@ namespace MatterHackers.MatterControl public interface IColorTheme { string Name { get; } - Color DefaultColor { get; } + + ThemeSet GetTheme(string mode); ThemeSet GetTheme(string mode, Color accentColor); + IEnumerable ThemeNames { get; } } } \ No newline at end of file diff --git a/MatterControlLib/ApplicationView/Themes/ThemeSet.cs b/MatterControlLib/ApplicationView/Themes/ThemeSet.cs new file mode 100644 index 000000000..3df28705b --- /dev/null +++ b/MatterControlLib/ApplicationView/Themes/ThemeSet.cs @@ -0,0 +1,63 @@ +/* +Copyright (c) 2018, John Lewin +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation are those +of the authors and should not be interpreted as representing official policies, +either expressed or implied, of the FreeBSD Project. +*/ + +using System.Collections.Generic; +using MatterHackers.Agg; + +namespace MatterHackers.MatterControl +{ + public class ThemeSet + { + public static int LatestSchemeVersion { get; } = 20181023; + + public string ThemeID { get; set; } + + public string Name { get; set; } + + public ThemeConfig Theme { get; set; } + + public ThemeConfig MenuTheme { get; set; } + + public List AccentColors { get; set; } = new List(); + + public void SetAccentColor(Color accentColor) + { + this.Theme.PrimaryAccentColor = accentColor; + this.Theme.AccentMimimalOverlay = accentColor.WithAlpha(90); + + this.MenuTheme.PrimaryAccentColor = accentColor; + this.MenuTheme.AccentMimimalOverlay = accentColor.WithAlpha(90); + } + + /// + /// The latest version of the theme file format. When changed, the clients state becomes invalid and will require a reset to the new theme format + /// + public int SchemeVersion { get; set; } + } +} \ No newline at end of file diff --git a/MatterControlLib/ConfigurationPage/ApplicationSettings/ThemeColorPanel.cs b/MatterControlLib/ConfigurationPage/ApplicationSettings/ThemeColorPanel.cs index b59537824..93d3b11d6 100644 --- a/MatterControlLib/ConfigurationPage/ApplicationSettings/ThemeColorPanel.cs +++ b/MatterControlLib/ConfigurationPage/ApplicationSettings/ThemeColorPanel.cs @@ -95,8 +95,6 @@ namespace MatterHackers.MatterControl.ConfigurationPage colorSelector.RebuildColorButtons(); this.CreateThemeModeButtons(); - - this.PreviewTheme(_themeProvider.DefaultColor); } } private void CreateThemeModeButtons() @@ -138,7 +136,7 @@ namespace MatterHackers.MatterControl.ConfigurationPage } else { - themeset = provider.GetTheme(themeName, provider.DefaultColor); + themeset = provider.GetTheme(themeName); } previewContainer.AddChild(new ThemePreviewButton(themeset, this) diff --git a/StaticData/Themes/System/Affinity/theme.json b/StaticData/Themes/System/Affinity/theme.json deleted file mode 100644 index 2fd50cc7a..000000000 --- a/StaticData/Themes/System/Affinity/theme.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "DefaultMode": "Dark", - "DefaultColor": "#cb4b16", - "Colors": ["#b58900", "#cb4b16", "#dc322f", "#d33682", "#6c71c4", "#268bd2", "#2aa198", "#859900"] -} \ No newline at end of file diff --git a/StaticData/Themes/System/Classic/theme.json b/StaticData/Themes/System/Classic/theme.json deleted file mode 100644 index ab9fb9901..000000000 --- a/StaticData/Themes/System/Classic/theme.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "DefaultMode": "Dark", - "DefaultColor": "#ac193dff", - "Colors": ["#ac193dff", "#dc4fadff", "#ff8119ff", "#008a17ff", "#004b8bff", "#008299ff", "#5db2ffff", "#4617b4ff", "#8c0095ff", "#585858ff"] -} \ No newline at end of file diff --git a/StaticData/Themes/System/Solarized/theme.json b/StaticData/Themes/System/Solarized/theme.json deleted file mode 100644 index ae45c2800..000000000 --- a/StaticData/Themes/System/Solarized/theme.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "DefaultMode": "Dark", - "DefaultColor": "#b58900", - "Colors": ["#b58900", "#cb4b16", "#dc322f", "#d33682", "#6c71c4", "#268bd2", "#2aa198", "#859900"] -} \ No newline at end of file