modified settings services
This commit is contained in:
@@ -64,10 +64,20 @@ public sealed partial class SettingsViewModel : ObservableObject, IDisposable
|
||||
Color.FromRgb(0xF8, 0x71, 0x71),
|
||||
];
|
||||
|
||||
public SettingsViewModel(AppSettings settings, ILocalizationService localization)
|
||||
private readonly IStartupService _startup;
|
||||
|
||||
private StartupState _startupState = StartupState.Unavailable;
|
||||
|
||||
public SettingsViewModel(
|
||||
AppSettings settings,
|
||||
ILocalizationService localization,
|
||||
IStartupService startup,
|
||||
UpdateViewModel updates)
|
||||
{
|
||||
Settings = settings;
|
||||
Localization = localization;
|
||||
Updates = updates;
|
||||
_startup = startup;
|
||||
|
||||
// The interface language is a setting like any other and is stored in the same place
|
||||
Localization.CurrentLanguage = settings.Language;
|
||||
@@ -84,6 +94,9 @@ public sealed partial class SettingsViewModel : ObservableObject, IDisposable
|
||||
|
||||
public ILocalizationService Localization { get; }
|
||||
|
||||
/// <summary>The updates section: it has a state and commands of its own.</summary>
|
||||
public UpdateViewModel Updates { get; }
|
||||
|
||||
public IReadOnlyList<Color> BackgroundPalette { get; } = Palette;
|
||||
|
||||
public IReadOnlyList<Color> TextPalette { get; } = ForegroundPalette;
|
||||
@@ -96,12 +109,64 @@ public sealed partial class SettingsViewModel : ObservableObject, IDisposable
|
||||
|
||||
public IReadOnlyList<EnumOption<ScreenPosition>> ScreenPositions { get; }
|
||||
|
||||
/// <summary>Whether to show the startup setting.</summary>
|
||||
public bool IsStartupAvailable => _startupState != StartupState.Unavailable;
|
||||
|
||||
/// <summary>Startup is up to the application rather than to Windows.</summary>
|
||||
public bool CanChangeStartup => _startupState is StartupState.Enabled or StartupState.Disabled;
|
||||
|
||||
/// <summary>It has to be explained why the setting does not give in.</summary>
|
||||
public bool IsStartupLocked => IsStartupAvailable && !CanChangeStartup;
|
||||
|
||||
/// <summary>
|
||||
/// Start the application together with Windows.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The value is not stored in the application settings: Windows knows it, and it
|
||||
/// may be changed behind our back — in the "Startup apps" section. So the property
|
||||
/// answers from the last known state of the task every time.
|
||||
/// </remarks>
|
||||
public bool RunAtStartup
|
||||
{
|
||||
get => _startupState is StartupState.Enabled or StartupState.EnabledByPolicy;
|
||||
set
|
||||
{
|
||||
if (value == RunAtStartup)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_ = ApplyStartupAsync(value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asks Windows about the state of startup. Called after the window is shown: the
|
||||
/// answer has to be waited for, while the settings must open right away.
|
||||
/// </summary>
|
||||
public async Task InitializeAsync() => UpdateStartupState(await _startup.GetStateAsync());
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Settings.PropertyChanged -= OnSettingsChanged;
|
||||
Localization.PropertyChanged -= OnLocalizationChanged;
|
||||
}
|
||||
|
||||
private async Task ApplyStartupAsync(bool enabled) =>
|
||||
UpdateStartupState(await _startup.SetEnabledAsync(enabled));
|
||||
|
||||
private void UpdateStartupState(StartupState state)
|
||||
{
|
||||
_startupState = state;
|
||||
|
||||
// The change is reported even when the state is the same: the checkbox has
|
||||
// already been toggled, and only a re-read value can put it back
|
||||
OnPropertyChanged(nameof(RunAtStartup));
|
||||
OnPropertyChanged(nameof(IsStartupAvailable));
|
||||
OnPropertyChanged(nameof(CanChangeStartup));
|
||||
OnPropertyChanged(nameof(IsStartupLocked));
|
||||
}
|
||||
|
||||
private void OnSettingsChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(AppSettings.Language))
|
||||
|
||||
Reference in New Issue
Block a user