using System.IO; using System.Text.Json; using System.Text.Json.Serialization; namespace DeskCore; public enum DesignMode { System, Light, Dark } public enum ViewMode { Small, Medium, Large, List } public class CategoryMeta { public string? Glyph { get; set; } public string? Color { get; set; } public bool Fence { get; set; } public double? FenceX { get; set; } public double? FenceY { get; set; } public double FenceW { get; set; } = 340; public double FenceH { get; set; } = 260; public bool FenceCollapsed { get; set; } } public class SortRule { public string Contains { get; set; } = ""; public string Category { get; set; } = ""; } /// Alle Einstellungen, gespeichert in %APPDATA%\DeskCore\settings.json. public class AppSettings { public DesignMode Design { get; set; } = DesignMode.System; public ViewMode View { get; set; } = ViewMode.Medium; public bool TrayEnabled { get; set; } = true; public bool CloseToTray { get; set; } = true; public bool TrayHintShown { get; set; } public bool HotkeyEnabled { get; set; } = true; public int HotkeyModifiers { get; set; } = Hotkey.ModWin | Hotkey.ModShift; public int HotkeyKey { get; set; } = 0x44; // D public bool AutoSort { get; set; } public bool AutoSortRemoveFromDesktop { get; set; } public List Rules { get; set; } = []; public bool FencesEnabled { get; set; } = true; // Fenster merken (WPF-Einheiten) public double? WindowLeft { get; set; } public double? WindowTop { get; set; } public double? WindowWidth { get; set; } public double? WindowHeight { get; set; } public bool WindowMaximized { get; set; } // Updates public bool AutoUpdateCheck { get; set; } = true; public DateTime? LastUpdateCheck { get; set; } public string? SkippedVersion { get; set; } public List CategoryOrder { get; set; } = []; public Dictionary Categories { get; set; } = new(StringComparer.OrdinalIgnoreCase); // ---------- Laden/Speichern ---------- public static readonly string Folder = Migration.Pick(Environment.SpecialFolder.ApplicationData); private static readonly string FilePath = Path.Combine(Folder, "settings.json"); public static readonly JsonSerializerOptions Json = new() { WriteIndented = true, Converters = { new JsonStringEnumConverter() }, }; public static AppSettings Current { get; private set; } = Load(); public static event Action? Saved; private static AppSettings Load() { AppSettings s; try { s = File.Exists(FilePath) ? JsonSerializer.Deserialize(File.ReadAllText(FilePath), Json) ?? new() : new(); } catch { s = new(); } s.Categories = new(s.Categories ?? [], StringComparer.OrdinalIgnoreCase); s.Rules ??= []; s.CategoryOrder ??= []; // Übernahme der alten Einstellung aus Version 2 var old = Path.Combine(Folder, "design.txt"); if (File.Exists(old)) { try { var v = File.ReadAllText(old).Trim(); s.Design = v == "Light" ? DesignMode.Light : v == "Dark" ? DesignMode.Dark : DesignMode.System; File.Delete(old); } catch { } } return s; } public void Save() { try { Directory.CreateDirectory(Folder); File.WriteAllText(FilePath, JsonSerializer.Serialize(this, Json)); } catch { } Saved?.Invoke(); } public CategoryMeta Meta(string category) { if (!Categories.TryGetValue(category, out var m)) Categories[category] = m = new CategoryMeta(); return m; } public void RenameCategory(string oldName, string newName) { if (Categories.Remove(oldName, out var m)) Categories[newName] = m; for (int i = 0; i < CategoryOrder.Count; i++) if (string.Equals(CategoryOrder[i], oldName, StringComparison.OrdinalIgnoreCase)) CategoryOrder[i] = newName; foreach (var r in Rules) if (string.Equals(r.Category, oldName, StringComparison.OrdinalIgnoreCase)) r.Category = newName; } public void RemoveCategory(string name) { Categories.Remove(name); CategoryOrder.RemoveAll(n => string.Equals(n, name, StringComparison.OrdinalIgnoreCase)); Rules.RemoveAll(r => string.Equals(r.Category, name, StringComparison.OrdinalIgnoreCase)); } }