2020-04-08 01:19:14 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-08-14 06:02:05 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.Helpers;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.Interface;
|
2020-05-14 17:36:27 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
|
2020-08-14 06:02:05 +08:00
|
|
|
|
using Microsoft.PowerToys.Settings.UI.Lib.ViewModels.Commands;
|
2020-03-12 14:25:24 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Lib.ViewModels
|
2020-03-12 14:25:24 +08:00
|
|
|
|
{
|
|
|
|
|
public class GeneralViewModel : Observable
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
private GeneralSettings GeneralSettingsConfig { get; set; }
|
2020-09-22 01:14:44 +08:00
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
|
private readonly ISettingsUtils _settingsUtils;
|
2020-04-18 06:25:08 +08:00
|
|
|
|
|
|
|
|
|
public ButtonClickCommand CheckFoUpdatesEventHandler { get; set; }
|
|
|
|
|
|
|
|
|
|
public ButtonClickCommand RestartElevatedButtonEventHandler { get; set; }
|
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
public Func<string, int> UpdateUIThemeCallBack { get; }
|
2020-08-07 02:16:25 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
public Func<string, int> SendConfigMSG { get; }
|
2020-08-07 02:16:25 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
public Func<string, int> SendRestartAsAdminConfigMSG { get; }
|
|
|
|
|
|
|
|
|
|
public Func<string, int> SendCheckForUpdatesConfigMSG { get; }
|
|
|
|
|
|
2020-08-20 06:59:10 +08:00
|
|
|
|
public string RunningAsUserDefaultText { get; set; }
|
2020-08-14 06:02:05 +08:00
|
|
|
|
|
2020-08-20 06:59:10 +08:00
|
|
|
|
public string RunningAsAdminDefaultText { get; set; }
|
2020-05-06 01:02:31 +08:00
|
|
|
|
|
2020-08-20 06:59:10 +08:00
|
|
|
|
private string _settingsConfigFileFolder = string.Empty;
|
2020-08-14 06:02:05 +08:00
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
|
public GeneralViewModel(ISettingsUtils settingsUtils, ISettingsRepository<GeneralSettings> settingsRepository, string runAsAdminText, string runAsUserText, bool isElevated, bool isAdmin, Func<string, int> updateTheme, Func<string, int> ipcMSGCallBackFunc, Func<string, int> ipcMSGRestartAsAdminMSGCallBackFunc, Func<string, int> ipcMSGCheckForUpdatesCallBackFunc, string configFileSubfolder = "")
|
2020-03-12 14:25:24 +08:00
|
|
|
|
{
|
2020-08-20 06:59:10 +08:00
|
|
|
|
CheckFoUpdatesEventHandler = new ButtonClickCommand(CheckForUpdates_Click);
|
|
|
|
|
RestartElevatedButtonEventHandler = new ButtonClickCommand(Restart_Elevated);
|
2020-09-22 01:14:44 +08:00
|
|
|
|
_settingsUtils = settingsUtils ?? throw new ArgumentNullException(nameof(settingsUtils));
|
2020-04-18 06:25:08 +08:00
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
|
// To obtain the general settings configuration of PowerToys if it exists, else to create a new file and return the default configurations.
|
|
|
|
|
GeneralSettingsConfig = settingsRepository.SettingsConfig;
|
2020-04-18 06:25:08 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
// set the callback functions value to hangle outgoing IPC message.
|
|
|
|
|
SendConfigMSG = ipcMSGCallBackFunc;
|
|
|
|
|
SendCheckForUpdatesConfigMSG = ipcMSGCheckForUpdatesCallBackFunc;
|
|
|
|
|
SendRestartAsAdminConfigMSG = ipcMSGRestartAsAdminMSGCallBackFunc;
|
|
|
|
|
|
|
|
|
|
// set the callback function value to update the UI theme.
|
|
|
|
|
UpdateUIThemeCallBack = updateTheme;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme.ToLower());
|
2020-08-14 06:02:05 +08:00
|
|
|
|
|
|
|
|
|
// Update Settings file folder:
|
2020-08-20 06:59:10 +08:00
|
|
|
|
_settingsConfigFileFolder = configFileSubfolder;
|
2020-08-14 06:02:05 +08:00
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
|
switch (GeneralSettingsConfig.Theme.ToLower())
|
2020-04-18 06:25:08 +08:00
|
|
|
|
{
|
|
|
|
|
case "light":
|
|
|
|
|
_isLightThemeRadioButtonChecked = true;
|
|
|
|
|
break;
|
|
|
|
|
case "dark":
|
|
|
|
|
_isDarkThemeRadioButtonChecked = true;
|
|
|
|
|
break;
|
|
|
|
|
case "system":
|
|
|
|
|
_isSystemThemeRadioButtonChecked = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 04:20:32 +08:00
|
|
|
|
_startup = GeneralSettingsConfig.Startup;
|
|
|
|
|
_autoDownloadUpdates = GeneralSettingsConfig.AutoDownloadUpdates;
|
2020-08-14 06:02:05 +08:00
|
|
|
|
_isElevated = isElevated;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
_runElevated = GeneralSettingsConfig.RunElevated;
|
2020-05-16 00:38:47 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
RunningAsUserDefaultText = runAsUserText;
|
|
|
|
|
RunningAsAdminDefaultText = runAsAdminText;
|
2020-05-16 00:38:47 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
_isAdmin = isAdmin;
|
2020-04-18 06:25:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
private bool _packaged = false;
|
|
|
|
|
private bool _startup = false;
|
|
|
|
|
private bool _isElevated = false;
|
|
|
|
|
private bool _runElevated = false;
|
|
|
|
|
private bool _isAdmin = false;
|
|
|
|
|
private bool _isDarkThemeRadioButtonChecked = false;
|
|
|
|
|
private bool _isLightThemeRadioButtonChecked = false;
|
|
|
|
|
private bool _isSystemThemeRadioButtonChecked = false;
|
|
|
|
|
private bool _autoDownloadUpdates = false;
|
|
|
|
|
|
2020-09-04 16:56:52 +08:00
|
|
|
|
private string _latestAvailableVersion = string.Empty;
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
// Gets or sets a value indicating whether packaged.
|
|
|
|
|
public bool Packaged
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _packaged;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_packaged != value)
|
|
|
|
|
{
|
|
|
|
|
_packaged = value;
|
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gets or sets a value indicating whether run powertoys on start-up.
|
|
|
|
|
public bool Startup
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _startup;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_startup != value)
|
|
|
|
|
{
|
|
|
|
|
_startup = value;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.Startup = value;
|
2020-04-18 06:25:08 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-16 00:38:47 +08:00
|
|
|
|
public string RunningAsText
|
2020-05-06 01:02:31 +08:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (!IsElevated)
|
|
|
|
|
{
|
2020-05-16 00:38:47 +08:00
|
|
|
|
return RunningAsUserDefaultText;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-05-16 00:38:47 +08:00
|
|
|
|
return RunningAsAdminDefaultText;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged("RunningAsAdminText");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
// Gets or sets a value indicating whether the powertoy elevated.
|
|
|
|
|
public bool IsElevated
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isElevated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_isElevated != value)
|
|
|
|
|
{
|
|
|
|
|
_isElevated = value;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
OnPropertyChanged("IsElevated");
|
|
|
|
|
OnPropertyChanged("IsAdminButtonEnabled");
|
|
|
|
|
OnPropertyChanged("RunningAsAdminText");
|
2020-04-18 06:25:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 01:02:31 +08:00
|
|
|
|
public bool IsAdminButtonEnabled
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return !IsElevated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
OnPropertyChanged("IsAdminButtonEnabled");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
// Gets or sets a value indicating whether powertoys should run elevated.
|
|
|
|
|
public bool RunElevated
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _runElevated;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_runElevated != value)
|
|
|
|
|
{
|
|
|
|
|
_runElevated = value;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.RunElevated = value;
|
2020-04-18 06:25:08 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-14 17:36:27 +08:00
|
|
|
|
// Gets a value indicating whether the user is part of administrators group.
|
|
|
|
|
public bool IsAdmin
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isAdmin;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 18:17:06 +08:00
|
|
|
|
public bool AutoDownloadUpdates
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _autoDownloadUpdates;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_autoDownloadUpdates != value)
|
|
|
|
|
{
|
|
|
|
|
_autoDownloadUpdates = value;
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.AutoDownloadUpdates = value;
|
2020-05-03 18:17:06 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
public bool IsDarkThemeRadioButtonChecked
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isDarkThemeRadioButtonChecked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == true)
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.Theme = "dark";
|
2020-04-18 06:25:08 +08:00
|
|
|
|
_isDarkThemeRadioButtonChecked = value;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);
|
2020-05-06 01:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsLightThemeRadioButtonChecked
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isLightThemeRadioButtonChecked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == true)
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.Theme = "light";
|
2020-04-18 06:25:08 +08:00
|
|
|
|
_isLightThemeRadioButtonChecked = value;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);
|
2020-05-06 01:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSystemThemeRadioButtonChecked
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _isSystemThemeRadioButtonChecked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == true)
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
GeneralSettingsConfig.Theme = "system";
|
2020-04-18 06:25:08 +08:00
|
|
|
|
_isSystemThemeRadioButtonChecked = value;
|
2020-05-06 01:02:31 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-09-24 04:20:32 +08:00
|
|
|
|
UpdateUIThemeCallBack(GeneralSettingsConfig.Theme);
|
2020-05-06 01:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 01:02:31 +08:00
|
|
|
|
public string PowerToysVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return Helper.GetProductVersion();
|
|
|
|
|
}
|
2020-05-05 05:40:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 16:56:52 +08:00
|
|
|
|
// Temp string. Appears when a user clicks "Check for updates" button and shows latest version available on the Github.
|
|
|
|
|
public string LatestAvailableVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _latestAvailableVersion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_latestAvailableVersion != value)
|
|
|
|
|
{
|
|
|
|
|
_latestAvailableVersion = value;
|
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
|
{
|
|
|
|
|
// Notify UI of property change
|
|
|
|
|
OnPropertyChanged(propertyName);
|
2020-09-24 04:20:32 +08:00
|
|
|
|
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig);
|
2020-04-18 06:25:08 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
SendConfigMSG(outsettings.ToString());
|
2020-04-18 06:25:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// callback function to launch the URL to check for updates.
|
2020-08-20 06:59:10 +08:00
|
|
|
|
private void CheckForUpdates_Click()
|
2020-04-18 06:25:08 +08:00
|
|
|
|
{
|
2020-09-22 01:14:44 +08:00
|
|
|
|
GeneralSettings settings = _settingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
2020-06-23 20:53:02 +08:00
|
|
|
|
settings.CustomActionName = "check_for_updates";
|
|
|
|
|
|
|
|
|
|
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
|
|
|
|
|
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
|
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
SendCheckForUpdatesConfigMSG(customaction.ToString());
|
2020-04-18 06:25:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-03 18:17:06 +08:00
|
|
|
|
public void Restart_Elevated()
|
2020-04-18 06:25:08 +08:00
|
|
|
|
{
|
2020-09-22 01:14:44 +08:00
|
|
|
|
GeneralSettings settings = _settingsUtils.GetSettings<GeneralSettings>(_settingsConfigFileFolder);
|
2020-05-03 18:17:06 +08:00
|
|
|
|
settings.CustomActionName = "restart_elevation";
|
|
|
|
|
|
2020-04-18 06:25:08 +08:00
|
|
|
|
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
|
2020-05-03 18:17:06 +08:00
|
|
|
|
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
|
2020-04-18 06:25:08 +08:00
|
|
|
|
|
2020-08-14 06:02:05 +08:00
|
|
|
|
SendRestartAsAdminConfigMSG(customaction.ToString());
|
2020-03-12 14:25:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|