| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellThemeViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reactive.Disposables; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using MyNet.UI.Commands; |
| | | 11 | | using MyNet.UI.Theming; |
| | | 12 | | using MyNet.Utilities.Suspending; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.Shell.Chrome; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Shell chrome for quick light/dark theme base switching. |
| | | 18 | | /// Detailed theme colors remain in preferences display settings. |
| | | 19 | | /// </summary> |
| | | 20 | | public sealed class ShellThemeViewModel : ViewModelBase |
| | | 21 | | { |
| | | 22 | | private readonly IThemeService _themeService; |
| | | 23 | | private readonly IThemeBaseRegistry _themeBaseRegistry; |
| | 27 | 24 | | private readonly Suspender _themeSyncSuspender = new(); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="ShellThemeViewModel"/> class. |
| | | 28 | | /// </summary> |
| | 27 | 29 | | public ShellThemeViewModel( |
| | 27 | 30 | | IThemeService themeService, |
| | 27 | 31 | | IThemeBaseRegistry themeBaseRegistry, |
| | 27 | 32 | | ICommandFactory? commandFactory = null) |
| | | 33 | | { |
| | 27 | 34 | | _themeService = themeService ?? throw new ArgumentNullException(nameof(themeService)); |
| | 27 | 35 | | _themeBaseRegistry = themeBaseRegistry ?? throw new ArgumentNullException(nameof(themeBaseRegistry)); |
| | | 36 | | |
| | 27 | 37 | | var commands = commandFactory.GetOrDefault(); |
| | | 38 | | IsDarkCommand = commands.Create(() => IsDark = true); |
| | | 39 | | IsLightCommand = commands.Create(() => IsDark = false); |
| | | 40 | | ChangeThemeCommand = commands.CreateRequired<IThemeBase>(themeBase => _themeService.ApplyBaseTheme(themeBase)); |
| | | 41 | | |
| | 27 | 42 | | IsDark = _themeService.CurrentTheme.Base.IsDark; |
| | | 43 | | |
| | 27 | 44 | | _themeService.ThemeChanged += OnThemeServiceThemeChanged; |
| | | 45 | | Disposables.Add(Disposable.Create(() => _themeService.ThemeChanged -= OnThemeServiceThemeChanged)); |
| | 27 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | public IThemeBase CurrentTheme => _themeService.CurrentTheme.Base; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets a value indicating whether the dark theme base is active. |
| | | 52 | | /// </summary> |
| | | 53 | | public bool IsDark |
| | | 54 | | { |
| | | 55 | | get; |
| | | 56 | | set |
| | | 57 | | { |
| | 30 | 58 | | if (_themeSyncSuspender.IsSuspended) |
| | | 59 | | { |
| | 0 | 60 | | SetProperty(ref field, value); |
| | 0 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 30 | 64 | | if (!SetProperty(ref field, value)) |
| | 27 | 65 | | return; |
| | | 66 | | |
| | 3 | 67 | | _themeService.ApplyBaseTheme(value ? _themeBaseRegistry.Dark : _themeBaseRegistry.Light); |
| | 3 | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the command that switches to the dark theme base. |
| | | 73 | | /// </summary> |
| | | 74 | | public ICommand IsDarkCommand { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the command that switches to the light theme base. |
| | | 78 | | /// </summary> |
| | | 79 | | public ICommand IsLightCommand { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the command that toggles between light and dark theme bases. |
| | | 83 | | /// </summary> |
| | | 84 | | public ICommand ChangeThemeCommand { get; } |
| | | 85 | | |
| | | 86 | | private void OnThemeServiceThemeChanged(object? sender, ThemeChangedEventArgs e) |
| | | 87 | | { |
| | 0 | 88 | | var isDark = e.CurrentTheme.Base.IsDark; |
| | 0 | 89 | | if (IsDark == isDark) |
| | 0 | 90 | | return; |
| | | 91 | | |
| | 0 | 92 | | using (_themeSyncSuspender.Suspend()) |
| | 0 | 93 | | IsDark = isDark; |
| | | 94 | | |
| | 0 | 95 | | NotifyPropertyChanged(nameof(CurrentTheme)); |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | |