Browse Source

Select a random theme by default

Daniel Chalmers 4 years ago
parent
commit
232af71d6a
3 changed files with 19 additions and 11 deletions
  1. 1 1
      DesktopClock/App.xaml.cs
  2. 1 8
      DesktopClock/MainViewModel.cs
  3. 17 2
      DesktopClock/Properties/Settings.cs

+ 1 - 1
DesktopClock/App.xaml.cs

@@ -14,7 +14,7 @@ namespace DesktopClock
         public static string Title { get; } = Assembly.GetCustomAttribute<AssemblyTitleAttribute>().Title;
 
         // https://www.materialui.co/colors
-        public static IReadOnlyCollection<Theme> Themes { get; } = new Theme[]
+        public static IReadOnlyList<Theme> Themes { get; } = new Theme[]
         {
             new Theme("White", "#FFFFFF", "#000000"),
             new Theme("Black", "#000000", "#9E9E9E"),

+ 1 - 8
DesktopClock/MainViewModel.cs

@@ -1,7 +1,6 @@
 using System;
 using System.ComponentModel;
 using System.Windows.Input;
-using System.Windows.Media;
 using DesktopClock.Properties;
 using GalaSoft.MvvmLight;
 using GalaSoft.MvvmLight.CommandWpf;
@@ -36,7 +35,7 @@ namespace DesktopClock
         /// <summary>
         /// Sets app theme to parameter's value.
         /// </summary>
-        public ICommand SetThemeCommand { get; } = new RelayCommand<Theme>(SetTheme);
+        public ICommand SetThemeCommand { get; } = new RelayCommand<Theme>((t) => Settings.Default.Theme = t);
 
         /// <summary>
         /// Sets format string in settings to parameter's string.
@@ -79,11 +78,5 @@ namespace DesktopClock
         }
 
         private void UpdateTimeString() => RaisePropertyChanged(nameof(CurrentTimeOrCountdownString));
-
-        private static void SetTheme(Theme theme)
-        {
-            Settings.Default.TextColor = (Color)ColorConverter.ConvertFromString(theme.PrimaryColor);
-            Settings.Default.OuterColor = (Color)ColorConverter.ConvertFromString(theme.SecondaryColor);
-        }
     }
 }

+ 17 - 2
DesktopClock/Properties/Settings.cs

@@ -21,6 +21,10 @@ namespace DesktopClock.Properties
 
         private Settings()
         {
+            var random = new Random();
+
+            // Random default theme.
+            Theme = App.Themes[random.Next(0, App.Themes.Count)];
         }
 
         public event PropertyChangedEventHandler PropertyChanged;
@@ -37,11 +41,22 @@ namespace DesktopClock.Properties
         public string Format { get; set; } = "dddd, MMM dd, HH:mm:ss";
         public bool BackgroundEnabled { get; set; } = false;
         public double BackgroundOpacity { get; set; } = 0.90;
-        public Color OuterColor { get; set; } = Colors.DarkGray;
-        public Color TextColor { get; set; } = Colors.Black;
+        public Color OuterColor { get; set; }
+        public Color TextColor { get; set; }
         public string FontFamily { get; set; } = "Arial";
         public DateTimeOffset CountdownTo { get; set; } = DateTimeOffset.MinValue;
 
+        [JsonIgnore]
+        public Theme Theme
+        {
+            get => new Theme("Custom", TextColor.ToString(), OuterColor.ToString());
+            set
+            {
+                TextColor = (Color)ColorConverter.ConvertFromString(value.PrimaryColor);
+                OuterColor = (Color)ColorConverter.ConvertFromString(value.SecondaryColor);
+            }
+        }
+
         #endregion "Properties"
 
         /// <summary>