| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Windows.Media;
- using Newtonsoft.Json;
- using WpfWindowPlacement;
- namespace DesktopClock.Properties;
- public sealed class Settings : INotifyPropertyChanged
- {
- private DateTime _fileLastUsed = DateTime.UtcNow;
- public static readonly string Path = "DesktopClock.settings";
- private static readonly Lazy<Settings> _default = new(() => TryLoad() ?? new Settings());
- private static readonly JsonSerializerSettings _jsonSerializerSettings = new()
- {
- Formatting = Formatting.Indented
- };
- private Settings()
- {
- var random = new Random();
- // Random default theme.
- Theme = App.Themes[random.Next(0, App.Themes.Count)];
- }
- #pragma warning disable CS0067 // The event 'Settings.PropertyChanged' is never used
- public event PropertyChangedEventHandler PropertyChanged;
- #pragma warning restore CS0067 // The event 'Settings.PropertyChanged' is never used
- public static Settings Default => _default.Value;
- #region "Properties"
- public WindowPlacement Placement { get; set; }
- public bool Topmost { get; set; } = true;
- public bool ShowInTaskbar { get; set; } = true;
- public double Height { get; set; } = 48;
- public string TimeZone { get; set; } = string.Empty;
- public string Format { get; set; } = "dddd, MMM dd, HH:mm:ss";
- public bool BackgroundEnabled { get; set; } = true;
- public double BackgroundOpacity { get; set; } = 0.90;
- public Color OuterColor { get; set; }
- public Color TextColor { get; set; }
- public string FontFamily { get; set; } = "Consolas";
- public DateTimeOffset CountdownTo { get; set; } = DateTimeOffset.MinValue;
- [JsonIgnore]
- public Theme Theme
- {
- get => new("Custom", TextColor.ToString(), OuterColor.ToString());
- set
- {
- TextColor = (Color)ColorConverter.ConvertFromString(value.PrimaryColor);
- OuterColor = (Color)ColorConverter.ConvertFromString(value.SecondaryColor);
- }
- }
- #endregion "Properties"
- /// <summary>
- /// Determines if the settings file has been modified externally since the last time it was used.
- /// </summary>
- public bool CheckIfModifiedExternally() =>
- File.GetLastWriteTimeUtc(Path) > _fileLastUsed;
- /// <summary>
- /// Saves to the default path.
- /// </summary>
- public void Save()
- {
- using (var fileStream = new FileStream(Path, FileMode.Create))
- using (var streamWriter = new StreamWriter(fileStream))
- using (var jsonWriter = new JsonTextWriter(streamWriter))
- JsonSerializer.Create(_jsonSerializerSettings).Serialize(jsonWriter, this);
- _fileLastUsed = DateTime.UtcNow;
- }
- /// <summary>
- /// Saves to the default path unless a save has already happened from an external source.
- /// </summary>
- public void SaveIfNotModifiedExternally()
- {
- if (!CheckIfModifiedExternally())
- Save();
- }
- /// <summary>
- /// Loads from the default path.
- /// </summary>
- private static Settings Load()
- {
- using (var fileStream = new FileStream(Path, FileMode.Open))
- using (var streamReader = new StreamReader(fileStream))
- using (var jsonReader = new JsonTextReader(streamReader))
- return JsonSerializer.Create(_jsonSerializerSettings).Deserialize<Settings>(jsonReader);
- }
- /// <summary>
- /// Returns loaded settings from the default path or null if it fails.
- /// </summary>
- private static Settings TryLoad()
- {
- try
- {
- return Load();
- }
- catch
- {
- return null;
- }
- }
- }
|