| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DisplayPreferencesViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.ObjectModel; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Reactive.Disposables; |
| | | 11 | | using MyNet.Primitives; |
| | | 12 | | using MyNet.UI.Resources; |
| | | 13 | | using MyNet.UI.Theming; |
| | | 14 | | using MyNet.UI.ViewModels.Workspace; |
| | | 15 | | |
| | | 16 | | namespace MyNet.UI.ViewModels.Preferences; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Preferences page for theme and display settings. |
| | | 20 | | /// </summary> |
| | | 21 | | public sealed class DisplayPreferencesViewModel : WorkspaceViewModel |
| | | 22 | | { |
| | | 23 | | private readonly IThemeService _themeService; |
| | | 24 | | private bool _syncingFromTheme; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="DisplayPreferencesViewModel"/> class. |
| | | 28 | | /// </summary> |
| | 3 | 29 | | public DisplayPreferencesViewModel(IThemeService themeService, IThemeBaseRegistry themeBaseRegistry) |
| | | 30 | | { |
| | 3 | 31 | | ArgumentNullException.ThrowIfNull(themeService); |
| | 3 | 32 | | ArgumentNullException.ThrowIfNull(themeBaseRegistry); |
| | | 33 | | |
| | 3 | 34 | | _themeService = themeService; |
| | | 35 | | |
| | 18 | 36 | | foreach (var themeBase in themeBaseRegistry.AvailableBases) |
| | 6 | 37 | | AvailableBases.Add(themeBase); |
| | | 38 | | |
| | 3 | 39 | | UpdateFromTheme(_themeService.CurrentTheme); |
| | 3 | 40 | | _themeService.ThemeChanged += OnThemeServiceThemeChanged; |
| | | 41 | | |
| | | 42 | | Disposables.Add(Disposable.Create(() => _themeService.ThemeChanged -= OnThemeServiceThemeChanged)); |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the available theme bases. |
| | | 47 | | /// </summary> |
| | | 48 | | public ObservableCollection<IThemeBase> AvailableBases { get; } = []; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets the selected theme base. |
| | | 52 | | /// </summary> |
| | | 53 | | public IThemeBase? ThemeBase |
| | | 54 | | { |
| | | 55 | | get; |
| | | 56 | | set |
| | | 57 | | { |
| | 6 | 58 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 3 | 59 | | return; |
| | | 60 | | |
| | 3 | 61 | | (value is not null && value != _themeService.CurrentTheme.Base) |
| | 3 | 62 | | .IfTrue(() => _themeService.ApplyBaseTheme(value!)); |
| | 3 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets or sets the primary color. |
| | | 68 | | /// </summary> |
| | | 69 | | public string? PrimaryColor |
| | | 70 | | { |
| | | 71 | | get; |
| | | 72 | | set |
| | | 73 | | { |
| | 3 | 74 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 3 | 75 | | return; |
| | | 76 | | |
| | 0 | 77 | | (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.PrimaryColor) |
| | 0 | 78 | | .IfTrue(() => _themeService.ApplyPrimary(value!, AutoPrimaryForegroundColor ? null : PrimaryForegroundCo |
| | 0 | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets or sets the accent color. |
| | | 84 | | /// </summary> |
| | | 85 | | public string? AccentColor |
| | | 86 | | { |
| | | 87 | | get; |
| | | 88 | | set |
| | | 89 | | { |
| | 3 | 90 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 3 | 91 | | return; |
| | | 92 | | |
| | 0 | 93 | | (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.AccentColor) |
| | 0 | 94 | | .IfTrue(() => _themeService.ApplyAccent(value!, AutoAccentForegroundColor ? null : AccentForegroundColor |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets or sets the primary foreground color. |
| | | 100 | | /// </summary> |
| | | 101 | | public string? PrimaryForegroundColor |
| | | 102 | | { |
| | | 103 | | get; |
| | | 104 | | set |
| | | 105 | | { |
| | 0 | 106 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 0 | 107 | | return; |
| | | 108 | | |
| | 0 | 109 | | (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.PrimaryForegroundColor) |
| | 0 | 110 | | .IfTrue(() => _themeService.ApplyPrimary(PrimaryColor!, AutoPrimaryForegroundColor ? null : value)); |
| | 0 | 111 | | } |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Gets or sets the accent foreground color. |
| | | 116 | | /// </summary> |
| | | 117 | | public string? AccentForegroundColor |
| | | 118 | | { |
| | | 119 | | get; |
| | | 120 | | set |
| | | 121 | | { |
| | 0 | 122 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 0 | 123 | | return; |
| | | 124 | | |
| | 0 | 125 | | (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.AccentForegroundColor) |
| | 0 | 126 | | .IfTrue(() => _themeService.ApplyAccent(AccentColor!, AutoAccentForegroundColor ? null : value)); |
| | 0 | 127 | | } |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Gets or sets a value indicating whether the primary foreground is computed automatically. |
| | | 132 | | /// </summary> |
| | | 133 | | public bool AutoPrimaryForegroundColor |
| | | 134 | | { |
| | | 135 | | get; |
| | | 136 | | set |
| | | 137 | | { |
| | 0 | 138 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 0 | 139 | | return; |
| | | 140 | | |
| | 0 | 141 | | _themeService.ApplyPrimary(PrimaryColor!, value ? null : PrimaryForegroundColor); |
| | 0 | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets or sets a value indicating whether the accent foreground is computed automatically. |
| | | 147 | | /// </summary> |
| | | 148 | | public bool AutoAccentForegroundColor |
| | | 149 | | { |
| | | 150 | | get; |
| | | 151 | | set |
| | | 152 | | { |
| | 0 | 153 | | if (!SetProperty(ref field, value) || _syncingFromTheme) |
| | 0 | 154 | | return; |
| | | 155 | | |
| | 0 | 156 | | _themeService.ApplyAccent(AccentColor!, value ? null : AccentForegroundColor); |
| | 0 | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Gets or sets the contrast ratio (UI binding). |
| | | 162 | | /// </summary> |
| | 0 | 163 | | public float ContrastRatio { get; set => SetProperty(ref field, value); } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Gets or sets a value indicating whether contrast enforcement is enabled. |
| | | 167 | | /// </summary> |
| | 0 | 168 | | public bool EnableContrast { get; set => SetProperty(ref field, value); } |
| | | 169 | | |
| | | 170 | | /// <inheritdoc /> |
| | 3 | 171 | | protected override string? CreateTitle(CultureInfo culture) => UiResources.Display; |
| | | 172 | | |
| | 0 | 173 | | private void OnThemeServiceThemeChanged(object? sender, ThemeChangedEventArgs e) => UpdateFromTheme(e.CurrentTheme); |
| | | 174 | | |
| | | 175 | | private void UpdateFromTheme(Theme theme) |
| | | 176 | | { |
| | 3 | 177 | | _syncingFromTheme = true; |
| | | 178 | | try |
| | | 179 | | { |
| | 3 | 180 | | if (ThemeBase != theme.Base) |
| | 3 | 181 | | ThemeBase = theme.Base; |
| | | 182 | | |
| | 3 | 183 | | if (PrimaryColor != theme.PrimaryColor) |
| | 3 | 184 | | PrimaryColor = theme.PrimaryColor; |
| | | 185 | | |
| | 3 | 186 | | if (AccentColor != theme.AccentColor) |
| | 3 | 187 | | AccentColor = theme.AccentColor; |
| | | 188 | | |
| | 3 | 189 | | if (PrimaryForegroundColor != theme.PrimaryForegroundColor) |
| | 0 | 190 | | PrimaryForegroundColor = theme.PrimaryForegroundColor; |
| | | 191 | | |
| | 3 | 192 | | if (AccentForegroundColor != theme.AccentForegroundColor) |
| | 0 | 193 | | AccentForegroundColor = theme.AccentForegroundColor; |
| | 3 | 194 | | } |
| | | 195 | | finally |
| | | 196 | | { |
| | 3 | 197 | | _syncingFromTheme = false; |
| | 3 | 198 | | } |
| | 3 | 199 | | } |
| | | 200 | | } |
| | | 201 | | |