| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ShellCultureViewModel.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.Generic; |
| | | 9 | | using System.Collections.ObjectModel; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Linq; |
| | | 12 | | using System.Reactive.Disposables; |
| | | 13 | | using System.Windows.Input; |
| | | 14 | | using MyNet.Globalization.Culture; |
| | | 15 | | using MyNet.UI.Commands; |
| | | 16 | | using MyNet.Utilities.Suspending; |
| | | 17 | | |
| | | 18 | | namespace MyNet.UI.ViewModels.Shell.Chrome; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Shell chrome for UI culture selection (status bar, title bar, etc.). |
| | | 22 | | /// </summary> |
| | | 23 | | public sealed class ShellCultureViewModel : ViewModelBase |
| | | 24 | | { |
| | | 25 | | private readonly ICultureService _cultureService; |
| | 36 | 26 | | private readonly Suspender _cultureSyncSuspender = new(); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="ShellCultureViewModel"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="cultureService">Application culture service.</param> |
| | | 32 | | /// <param name="commandFactory">Factory to create commands.</param> |
| | | 33 | | /// <param name="supportedCultures"> |
| | | 34 | | /// Cultures offered in the selector. When omitted, <see cref="SupportedCultures.French"/> and |
| | | 35 | | /// <see cref="SupportedCultures.English"/> are used. Register a custom factory in DI to define |
| | | 36 | | /// the list for your host application. |
| | | 37 | | /// </param> |
| | 36 | 38 | | public ShellCultureViewModel( |
| | 36 | 39 | | ICultureService cultureService, |
| | 36 | 40 | | ICommandFactory? commandFactory = null, |
| | 36 | 41 | | IEnumerable<CultureInfo>? supportedCultures = null) |
| | | 42 | | { |
| | 36 | 43 | | _cultureService = cultureService ?? throw new ArgumentNullException(nameof(cultureService)); |
| | 36 | 44 | | var commands = commandFactory.GetOrDefault(); |
| | | 45 | | |
| | 168 | 46 | | foreach (var culture in supportedCultures ?? [SupportedCultures.French, SupportedCultures.English]) |
| | 48 | 47 | | Cultures.Add(culture); |
| | | 48 | | |
| | 36 | 49 | | var initialCulture = ResolveSupportedCulture(_cultureService.CurrentCulture); |
| | 36 | 50 | | if (initialCulture is not null) |
| | 36 | 51 | | SelectedCulture = initialCulture; |
| | | 52 | | |
| | 36 | 53 | | _cultureService.CultureChanged += OnCultureServiceCultureChanged; |
| | | 54 | | Disposables.Add(Disposable.Create(() => _cultureService.CultureChanged -= OnCultureServiceCultureChanged)); |
| | | 55 | | |
| | | 56 | | ChangeCultureCommand = commands.Create<CultureInfo>(cultureInfo => SelectedCulture = cultureInfo); |
| | 36 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets the cultures available for selection. |
| | | 61 | | /// </summary> |
| | | 62 | | public ObservableCollection<CultureInfo> Cultures { get; } = []; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets or sets the selected UI culture. |
| | | 66 | | /// </summary> |
| | | 67 | | public CultureInfo? SelectedCulture |
| | | 68 | | { |
| | | 69 | | get; |
| | | 70 | | set |
| | | 71 | | { |
| | 42 | 72 | | if (_cultureSyncSuspender.IsSuspended) |
| | | 73 | | { |
| | 3 | 74 | | SetProperty(ref field, value); |
| | 3 | 75 | | return; |
| | | 76 | | } |
| | | 77 | | |
| | 39 | 78 | | if (!SetProperty(ref field, value)) |
| | 0 | 79 | | return; |
| | | 80 | | |
| | 39 | 81 | | var culture = value ?? CultureInfo.InstalledUICulture; |
| | 39 | 82 | | _cultureService.SetCulture(culture); |
| | 39 | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the command to change the current culture. The command parameter is the target culture. |
| | | 88 | | /// </summary> |
| | | 89 | | public ICommand ChangeCultureCommand { get; } |
| | | 90 | | |
| | | 91 | | private void OnCultureServiceCultureChanged(object? sender, CultureChangedEventArgs e) |
| | | 92 | | { |
| | 6 | 93 | | var culture = ResolveSupportedCulture(_cultureService.CurrentCulture); |
| | 6 | 94 | | if (culture is null || Equals(SelectedCulture, culture)) |
| | 3 | 95 | | return; |
| | | 96 | | |
| | 3 | 97 | | using (_cultureSyncSuspender.Suspend()) |
| | 3 | 98 | | SelectedCulture = culture; |
| | 3 | 99 | | } |
| | | 100 | | |
| | | 101 | | private CultureInfo? ResolveSupportedCulture(CultureInfo culture) |
| | | 102 | | { |
| | 42 | 103 | | var current = culture; |
| | 45 | 104 | | while (!current.Equals(CultureInfo.InvariantCulture)) |
| | | 105 | | { |
| | 45 | 106 | | var match = Cultures.FirstOrDefault(c => c.Equals(current)); |
| | 45 | 107 | | if (match is not null) |
| | 42 | 108 | | return match; |
| | | 109 | | |
| | 3 | 110 | | current = current.Parent; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | return Cultures.FirstOrDefault(); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |