< Summary

Information
Class: MyNet.UI.ViewModels.Preferences.DisplayPreferencesViewModel
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Preferences/DisplayPreferencesViewModel.cs
Tag: 323_28699572109
Line coverage
52%
Covered lines: 32
Uncovered lines: 29
Coverable lines: 61
Total lines: 201
Line coverage: 52.4%
Branch coverage
33%
Covered branches: 18
Total branches: 54
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Preferences/DisplayPreferencesViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DisplayPreferencesViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.ObjectModel;
 9using System.Globalization;
 10using System.Reactive.Disposables;
 11using MyNet.Primitives;
 12using MyNet.UI.Resources;
 13using MyNet.UI.Theming;
 14using MyNet.UI.ViewModels.Workspace;
 15
 16namespace MyNet.UI.ViewModels.Preferences;
 17
 18/// <summary>
 19/// Preferences page for theme and display settings.
 20/// </summary>
 21public 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>
 329    public DisplayPreferencesViewModel(IThemeService themeService, IThemeBaseRegistry themeBaseRegistry)
 30    {
 331        ArgumentNullException.ThrowIfNull(themeService);
 332        ArgumentNullException.ThrowIfNull(themeBaseRegistry);
 33
 334        _themeService = themeService;
 35
 1836        foreach (var themeBase in themeBaseRegistry.AvailableBases)
 637            AvailableBases.Add(themeBase);
 38
 339        UpdateFromTheme(_themeService.CurrentTheme);
 340        _themeService.ThemeChanged += OnThemeServiceThemeChanged;
 41
 42        Disposables.Add(Disposable.Create(() => _themeService.ThemeChanged -= OnThemeServiceThemeChanged));
 343    }
 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        {
 658            if (!SetProperty(ref field, value) || _syncingFromTheme)
 359                return;
 60
 361            (value is not null && value != _themeService.CurrentTheme.Base)
 362                .IfTrue(() => _themeService.ApplyBaseTheme(value!));
 363        }
 64    }
 65
 66    /// <summary>
 67    /// Gets or sets the primary color.
 68    /// </summary>
 69    public string? PrimaryColor
 70    {
 71        get;
 72        set
 73        {
 374            if (!SetProperty(ref field, value) || _syncingFromTheme)
 375                return;
 76
 077            (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.PrimaryColor)
 078                .IfTrue(() => _themeService.ApplyPrimary(value!, AutoPrimaryForegroundColor ? null : PrimaryForegroundCo
 079        }
 80    }
 81
 82    /// <summary>
 83    /// Gets or sets the accent color.
 84    /// </summary>
 85    public string? AccentColor
 86    {
 87        get;
 88        set
 89        {
 390            if (!SetProperty(ref field, value) || _syncingFromTheme)
 391                return;
 92
 093            (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.AccentColor)
 094                .IfTrue(() => _themeService.ApplyAccent(value!, AutoAccentForegroundColor ? null : AccentForegroundColor
 095        }
 96    }
 97
 98    /// <summary>
 99    /// Gets or sets the primary foreground color.
 100    /// </summary>
 101    public string? PrimaryForegroundColor
 102    {
 103        get;
 104        set
 105        {
 0106            if (!SetProperty(ref field, value) || _syncingFromTheme)
 0107                return;
 108
 0109            (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.PrimaryForegroundColor)
 0110                .IfTrue(() => _themeService.ApplyPrimary(PrimaryColor!, AutoPrimaryForegroundColor ? null : value));
 0111        }
 112    }
 113
 114    /// <summary>
 115    /// Gets or sets the accent foreground color.
 116    /// </summary>
 117    public string? AccentForegroundColor
 118    {
 119        get;
 120        set
 121        {
 0122            if (!SetProperty(ref field, value) || _syncingFromTheme)
 0123                return;
 124
 0125            (!string.IsNullOrEmpty(value) && value != _themeService.CurrentTheme.AccentForegroundColor)
 0126                .IfTrue(() => _themeService.ApplyAccent(AccentColor!, AutoAccentForegroundColor ? null : value));
 0127        }
 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        {
 0138            if (!SetProperty(ref field, value) || _syncingFromTheme)
 0139                return;
 140
 0141            _themeService.ApplyPrimary(PrimaryColor!, value ? null : PrimaryForegroundColor);
 0142        }
 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        {
 0153            if (!SetProperty(ref field, value) || _syncingFromTheme)
 0154                return;
 155
 0156            _themeService.ApplyAccent(AccentColor!, value ? null : AccentForegroundColor);
 0157        }
 158    }
 159
 160    /// <summary>
 161    /// Gets or sets the contrast ratio (UI binding).
 162    /// </summary>
 0163    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>
 0168    public bool EnableContrast { get; set => SetProperty(ref field, value); }
 169
 170    /// <inheritdoc />
 3171    protected override string? CreateTitle(CultureInfo culture) => UiResources.Display;
 172
 0173    private void OnThemeServiceThemeChanged(object? sender, ThemeChangedEventArgs e) => UpdateFromTheme(e.CurrentTheme);
 174
 175    private void UpdateFromTheme(Theme theme)
 176    {
 3177        _syncingFromTheme = true;
 178        try
 179        {
 3180            if (ThemeBase != theme.Base)
 3181                ThemeBase = theme.Base;
 182
 3183            if (PrimaryColor != theme.PrimaryColor)
 3184                PrimaryColor = theme.PrimaryColor;
 185
 3186            if (AccentColor != theme.AccentColor)
 3187                AccentColor = theme.AccentColor;
 188
 3189            if (PrimaryForegroundColor != theme.PrimaryForegroundColor)
 0190                PrimaryForegroundColor = theme.PrimaryForegroundColor;
 191
 3192            if (AccentForegroundColor != theme.AccentForegroundColor)
 0193                AccentForegroundColor = theme.AccentForegroundColor;
 3194        }
 195        finally
 196        {
 3197            _syncingFromTheme = false;
 3198        }
 3199    }
 200}
 201