Settings backup and restore (#20551)

* Merge and conflict resolution

* Messages good, backup/restore algo better.

* Start of "GetExportVerion"

* fixed spelling

* New backup/restore mode working.

* Rename a project

* Removed test project

* Switch to text.json

* Renamed BackupAndSync to BackupAndRestore

* Added IgnoredPTRunSettings and full merge

* Restored "fixed" settings that change for no reason

* Various UI updates.

* speling

* Some cleanup and zip support.

* Merge and clean

* code clean up

* code clean up

* code clean up

* Smarter settings compare and merge.

* config based file include/exclude

* Removed some "words"

* Code clean up

* cleanup

* cleanup

* cleanup

* cleanup

* fixed spelling.

* Fixed clean up 1

* more clean up

* Trying to add ptb as an OK word

* Some UI updates.

* UI tweaks and PR review items.

* UI tweaks

* Merge conflicts resolved.

* Added CurrentSettingMatchText

* PR review updates.

* Removed weird file.

* Review updates and fixes

* More UI tweaks.

* UI tweaks

* Set default backup location to "%USERPROFILE%\\Documents\\PowerToys\\Backup"

* settings ui tweaks

* Added ExpanderContentSettingStyle

* fix missing config file

* fix missing config file, part 2

* update ui, cleanup cope

* update ui, cleanup code - Part2

* update method comments

* code cleanup and adjust Backup message time

* fix changing backup location on empty Regsitry

* fix select location - part 2

* location input box min-width

* remove lastRestoreDate from ViewModel

* Code or backup timing, and error handling.

* Should fix file/folder name crash.

* Progress to instance class for backup/restore

* Persist backup status state, added refresh button.

* Better auto check for settings status

* Some UI/text updates.

* Clean up

* Added prefix for "General_Settings" to resources

* Code review updates.

* Code review changes.

* Changed to FolderPicker per review

* Fixed issue with early delete of cleanup.

* Testing issues with FolderPicker

* Removed WinForm req and fixed win10 issue.

* Review update.

* Review changes.

Co-authored-by: htcfreek <61519853+htcfreek@users.noreply.github.com>
This commit is contained in:
Jeff Lord
2022-10-13 03:41:21 -04:00
committed by GitHub
parent 79e037eef4
commit ee904ae1b1
17 changed files with 1981 additions and 53 deletions

View File

@@ -1619,7 +1619,7 @@ pstr
pstream
pstrm
psz
PTB
ptb
ptc
ptd
PTOKEN

View File

@@ -49,7 +49,7 @@
<?define PowerPreviewFiles=PowerToys.powerpreview.dll;PowerToys.PreviewHandlerCommon.dll;PowerToys.PreviewHandlerCommon.deps.json;PowerToys.ManagedCommon.dll;System.Management.dll;PowerToys.ManagedTelemetry.dll;PowerToys.SvgPreviewHandler.dll;PowerToys.SvgPreviewHandler.comhost.dll;PowerToys.SvgPreviewHandler.runtimeconfig.json;PowerToys.SvgPreviewHandler.deps.json;PowerToys.SvgThumbnailProvider.dll;PowerToys.SvgThumbnailProvider.comhost.dll;PowerToys.SvgThumbnailProvider.runtimeconfig.json;PowerToys.SvgThumbnailProvider.deps.json;PowerToys.MarkdownPreviewHandler.dll;PowerToys.MarkdownPreviewHandler.comhost.dll;PowerToys.MarkdownPreviewHandler.runtimeconfig.json;PowerToys.MarkdownPreviewHandler.deps.json;Markdig.Signed.dll;HtmlAgilityPack.dll;System.IO.Abstractions.dll;monaco_languages.json;monacoSpecialLanguages.js;PowerToys.Common.UI.dll;PowerToys.Settings.UI.Lib.dll;PowerToys.MonacoPreviewHandler.dll;PowerToys.MonacoPreviewHandler.comhost.dll;PowerToys.MonacoPreviewHandler.runtimeconfig.json;PowerToys.MonacoPreviewHandler.deps.json;ControlzEx.dll;Microsoft.Web.WebView2.Core.dll;Microsoft.Web.WebView2.WinForms.dll;Microsoft.Web.WebView2.Wpf.dll;WebView2Loader.dll;System.Runtime.WindowsRuntime.dll;index.html;PowerToys.PdfPreviewHandler.dll;PowerToys.PdfPreviewHandler.comhost.dll;PowerToys.PdfPreviewHandler.runtimeconfig.json;PowerToys.PdfPreviewHandler.deps.json;Microsoft.Windows.SDK.NET.dll;WinRT.Runtime.dll;PowerToys.PdfThumbnailProvider.dll;PowerToys.PdfThumbnailProvider.comhost.dll;PowerToys.PdfThumbnailProvider.runtimeconfig.json;PowerToys.PdfThumbnailProvider.deps.json;PowerToys.GcodePreviewHandler.dll;PowerToys.GcodePreviewHandler.comhost.dll;PowerToys.GcodePreviewHandler.runtimeconfig.json;PowerToys.GcodePreviewHandler.deps.json;PowerToys.GcodeThumbnailProvider.dll;PowerToys.GcodeThumbnailProvider.comhost.dll;PowerToys.GcodeThumbnailProvider.runtimeconfig.json;PowerToys.GcodeThumbnailProvider.deps.json;PowerToys.StlThumbnailProvider.dll;PowerToys.StlThumbnailProvider.comhost.dll;PowerToys.StlThumbnailProvider.runtimeconfig.json;PowerToys.StlThumbnailProvider.deps.json;HelixToolkit.dll;HelixToolkit.Core.Wpf.dll;Ijwhost.dll;Microsoft.Xaml.Behaviors.dll;System.Text.Json.dll?>
<?define SettingsV2Files=Ijwhost.dll;ColorCode.Core.dll;ColorCode.WinUI.dll;CommunityToolkit.Common.dll;CommunityToolkit.WinUI.dll;CommunityToolkit.WinUI.UI.Controls.Core.dll;CommunityToolkit.WinUI.UI.Controls.DataGrid.dll;CommunityToolkit.WinUI.UI.Controls.Input.dll;CommunityToolkit.WinUI.UI.Controls.Layout.dll;CommunityToolkit.WinUI.UI.Controls.Markdown.dll;CommunityToolkit.WinUI.UI.Controls.Media.dll;CommunityToolkit.WinUI.UI.Controls.Primitives.dll;CommunityToolkit.WinUI.UI.dll;icon.ico;Microsoft.Graphics.Canvas.Interop.dll;Microsoft.InteractiveExperiences.Projection.dll;Microsoft.Windows.ApplicationModel.DynamicDependency.Projection.dll;Microsoft.Windows.ApplicationModel.Resources.Projection.dll;Microsoft.Windows.ApplicationModel.WindowsAppRuntime.Projection.dll;Microsoft.Windows.AppLifecycle.Projection.dll;Microsoft.Windows.SDK.NET.dll;Microsoft.Windows.System.Power.Projection.dll;Microsoft.WindowsAppRuntime.Bootstrap.Net.dll;Microsoft.WinUI.dll;Microsoft.Xaml.Interactions.dll;Microsoft.Xaml.Interactivity.dll;PowerToys.ManagedCommon.dll;PowerToys.ManagedTelemetry.dll;PowerToys.Settings.deps.json;PowerToys.Settings.dll;PowerToys.Settings.exe;PowerToys.Settings.runtimeconfig.json;PowerToys.Settings.UI.Lib.dll;resources.pri;System.CodeDom.dll;System.IO.Abstractions.dll;System.Text.Json.dll;WinRT.Runtime.dll;Microsoft.Graphics.Canvas.dll;System.Management.dll?>
<?define SettingsV2Files=backup_restore_settings.json;Ijwhost.dll;ColorCode.Core.dll;ColorCode.WinUI.dll;CommunityToolkit.Common.dll;CommunityToolkit.WinUI.dll;CommunityToolkit.WinUI.UI.Controls.Core.dll;CommunityToolkit.WinUI.UI.Controls.DataGrid.dll;CommunityToolkit.WinUI.UI.Controls.Input.dll;CommunityToolkit.WinUI.UI.Controls.Layout.dll;CommunityToolkit.WinUI.UI.Controls.Markdown.dll;CommunityToolkit.WinUI.UI.Controls.Media.dll;CommunityToolkit.WinUI.UI.Controls.Primitives.dll;CommunityToolkit.WinUI.UI.dll;icon.ico;Microsoft.Graphics.Canvas.Interop.dll;Microsoft.InteractiveExperiences.Projection.dll;Microsoft.Windows.ApplicationModel.DynamicDependency.Projection.dll;Microsoft.Windows.ApplicationModel.Resources.Projection.dll;Microsoft.Windows.ApplicationModel.WindowsAppRuntime.Projection.dll;Microsoft.Windows.AppLifecycle.Projection.dll;Microsoft.Windows.SDK.NET.dll;Microsoft.Windows.System.Power.Projection.dll;Microsoft.WindowsAppRuntime.Bootstrap.Net.dll;Microsoft.WinUI.dll;Microsoft.Xaml.Interactions.dll;Microsoft.Xaml.Interactivity.dll;PowerToys.ManagedCommon.dll;PowerToys.ManagedTelemetry.dll;PowerToys.Settings.deps.json;PowerToys.Settings.dll;PowerToys.Settings.exe;PowerToys.Settings.runtimeconfig.json;PowerToys.Settings.UI.Lib.dll;resources.pri;System.CodeDom.dll;System.IO.Abstractions.dll;System.Text.Json.dll;WinRT.Runtime.dll;Microsoft.Graphics.Canvas.dll;System.Management.dll?>
<?define SettingsV2AssetsModulesFiles=ColorPicker.png;FancyZones.png;AlwaysOnTop.png;Awake.png;ImageResizer.png;KBM.png;MouseUtils.png;PowerAccent.png;PowerOCR.png;PowerLauncher.png;PowerPreview.png;PowerRename.png;PT.png;ScreenRuler.png;ShortcutGuide.png;VideoConference.png?>

View File

@@ -149,7 +149,7 @@ int runner(bool isProcessElevated, bool openSettings, std::string settingsWindow
L"modules/ShortcutGuide/ShortcutGuideModuleInterface/PowerToys.ShortcutGuideModuleInterface.dll",
L"modules/ColorPicker/PowerToys.ColorPicker.dll",
L"modules/Awake/PowerToys.AwakeModuleInterface.dll",
L"modules/MouseUtils/PowerToys.FindMyMouse.dll" ,
L"modules/MouseUtils/PowerToys.FindMyMouse.dll",
L"modules/MouseUtils/PowerToys.MouseHighlighter.dll",
L"modules/AlwaysOnTop/PowerToys.AlwaysOnTopModuleInterface.dll",
L"modules/MouseUtils/PowerToys.MousePointerCrosshairs.dll",
@@ -324,7 +324,7 @@ void cleanup_updates()
}
}
}
// Log files
auto rootPath{ PTSettingsHelper::get_root_save_folder_location() };
auto currentVersion = left_trim<wchar_t>(get_product_version(), L"v");
@@ -474,9 +474,12 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
{
result = runner(elevated, open_settings, settings_window, openOobe, openScoobe);
// Save settings on closing
auto general_settings = get_general_settings();
PTSettingsHelper::save_general_settings(general_settings.to_json());
if (result == 0)
{
// Save settings on closing, if closed 'normal'
auto general_settings = get_general_settings();
PTSettingsHelper::save_general_settings(general_settings.to_json());
}
}
else
{

View File

@@ -8,7 +8,8 @@ enum State
None,
RestartAsElevated,
RestartAsElevatedOpenSettings,
RestartAsNonElevated
RestartAsNonElevated,
RestartAsNonElevatedOpenSettings
};
static State state = None;
@@ -17,12 +18,16 @@ void schedule_restart_as_elevated(bool openSettings)
state = openSettings ? RestartAsElevatedOpenSettings : RestartAsElevated;
}
void schedule_restart_as_non_elevated()
{
state = RestartAsNonElevated;
}
void schedule_restart_as_non_elevated(bool openSettings)
{
state = openSettings ? RestartAsNonElevatedOpenSettings : RestartAsNonElevated;
}
bool is_restart_scheduled()
{
return state != None;
@@ -40,6 +45,8 @@ bool restart_if_scheduled()
return run_elevated(exe_path.get(), {});
case RestartAsElevatedOpenSettings:
return run_elevated(exe_path.get(), L"--open-settings");
case RestartAsNonElevatedOpenSettings:
return run_non_elevated(exe_path.get(), L"--open-settings", NULL);
case RestartAsNonElevated:
return run_non_elevated(exe_path.get(), L"", NULL);
default:

View File

@@ -1,6 +1,7 @@
#pragma once
void schedule_restart_as_elevated(bool openSettings);
void schedule_restart_as_non_elevated();
void schedule_restart_as_non_elevated(bool openSettings);
bool is_restart_scheduled();
bool restart_if_scheduled();
bool restart_same_elevation();

View File

@@ -83,6 +83,24 @@ std::optional<std::wstring> dispatch_json_action_to_module(const json::JsonObjec
PostQuitMessage(0);
}
}
else if (action == L"restart_maintain_elevation")
{
// this was added to restart and maintain elevation, which is needed after settings are change from outside the normal process.
// since a normal PostQuitMessage(0) would usually cause this process to save it's in memory settings to disk, we need to
// send a PostQuitMessage(1) and check for that on exit, and skip the settings-flush.
auto loaded = PTSettingsHelper::load_general_settings();
if (is_process_elevated())
{
schedule_restart_as_elevated(true);
PostQuitMessage(1);
}
else
{
schedule_restart_as_non_elevated(true);
PostQuitMessage(1);
}
}
else if (action == L"check_for_updates")
{
CheckForUpdatesCallback();

View File

@@ -1,31 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\Version.props" />
<Import Project="..\..\Version.props" />
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Version>$(Version).0</Version>
<Authors>Microsoft Corporation</Authors>
<Product>PowerToys</Product>
<Description>PowerToys Settings UI Library</Description>
<AssemblyName>PowerToys.Settings.UI.Lib</AssemblyName>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Version>$(Version).0</Version>
<Authors>Microsoft Corporation</Authors>
<Product>PowerToys</Product>
<Description>PowerToys Settings UI Library</Description>
<AssemblyName>PowerToys.Settings.UI.Lib</AssemblyName>
<UseWindowsForms>false</UseWindowsForms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<None Include="backup_restore_settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.Text.Json" Version="6.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -139,5 +139,30 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
return _settingsPath.GetSettingsPath(powertoy, fileName);
}
/// <summary>
/// Method <c>BackupSettings</c> Mostly a wrapper for SettingsBackupAndRestoreUtils.BackupSettings
/// </summary>
public static (bool success, string message, string severity, bool lastBackupExists) BackupSettings()
{
var settingsBackupAndRestoreUtilsX = SettingsBackupAndRestoreUtils.Instance;
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils._settingsPath.GetSettingsPath(string.Empty, string.Empty));
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtilsX.GetSettingsBackupAndRestoreDir();
return settingsBackupAndRestoreUtilsX.BackupSettings(appBasePath, settingsBackupAndRestoreDir, false);
}
/// <summary>
/// Method <c>RestoreSettings</c> Mostly a wrapper for SettingsBackupAndRestoreUtils.RestoreSettings
/// </summary>
public static (bool success, string message, string severity) RestoreSettings()
{
var settingsBackupAndRestoreUtilsX = SettingsBackupAndRestoreUtils.Instance;
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils._settingsPath.GetSettingsPath(string.Empty, string.Empty));
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtilsX.GetSettingsBackupAndRestoreDir();
return settingsBackupAndRestoreUtilsX.RestoreSettings(appBasePath, settingsBackupAndRestoreDir);
}
}
}

View File

@@ -69,6 +69,11 @@ namespace Microsoft.PowerToys.Settings.UI.Library
{
try
{
if (LastCheckedDate == null)
{
return string.Empty;
}
long seconds = long.Parse(LastCheckedDate, CultureInfo.CurrentCulture);
var date = DateTimeOffset.FromUnixTimeSeconds(seconds).UtcDateTime;
return date.ToLocalTime().ToString(CultureInfo.CurrentCulture);

View File

@@ -4,8 +4,13 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library.Helpers;
using Microsoft.PowerToys.Settings.UI.Library.Interfaces;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
@@ -21,6 +26,20 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
public ButtonClickCommand CheckForUpdatesEventHandler { get; set; }
public object ResourceLoader { get; set; }
private Action HideBackupAndRestoreMessageAreaAction { get; set; }
private Action<int> DoBackupAndRestoreDryRun { get; set; }
public ButtonClickCommand BackupConfigsEventHandler { get; set; }
public ButtonClickCommand RestoreConfigsEventHandler { get; set; }
public ButtonClickCommand RefreshBackupStatusEventHandler { get; set; }
public ButtonClickCommand SelectSettingBackupDirEventHandler { get; set; }
public ButtonClickCommand RestartElevatedButtonEventHandler { get; set; }
public ButtonClickCommand UpdateNowButtonEventHandler { get; set; }
@@ -41,11 +60,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private IFileSystemWatcher _fileWatcher;
public GeneralViewModel(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 = "", Action dispatcherAction = null)
private Func<Task<string>> PickSingleFolderDialog { get; }
private SettingsBackupAndRestoreUtils settingsBackupAndRestoreUtils = SettingsBackupAndRestoreUtils.Instance;
public GeneralViewModel(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 = "", Action dispatcherAction = null, Action hideBackupAndRestoreMessageAreaAction = null, Action<int> doBackupAndRestoreDryRun = null, Func<Task<string>> pickSingleFolderDialog = null, object resourceLoader = null)
{
CheckForUpdatesEventHandler = new ButtonClickCommand(CheckForUpdatesClick);
RestartElevatedButtonEventHandler = new ButtonClickCommand(RestartElevated);
UpdateNowButtonEventHandler = new ButtonClickCommand(UpdateNowClick);
BackupConfigsEventHandler = new ButtonClickCommand(BackupConfigsClick);
SelectSettingBackupDirEventHandler = new ButtonClickCommand(SelectSettingBackupDir);
RestoreConfigsEventHandler = new ButtonClickCommand(RestoreConfigsClick);
RefreshBackupStatusEventHandler = new ButtonClickCommand(RefreshBackupStatusEventHandlerClick);
HideBackupAndRestoreMessageAreaAction = hideBackupAndRestoreMessageAreaAction;
DoBackupAndRestoreDryRun = doBackupAndRestoreDryRun;
PickSingleFolderDialog = pickSingleFolderDialog;
ResourceLoader = resourceLoader;
// To obtain the general settings configuration of PowerToys if it exists, else to create a new file and return the default configurations.
if (settingsRepository == null)
@@ -92,6 +123,7 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
_startup = GeneralSettingsConfig.Startup;
_autoDownloadUpdates = GeneralSettingsConfig.AutoDownloadUpdates;
_isElevated = isElevated;
_runElevated = GeneralSettingsConfig.RunElevated;
@@ -127,6 +159,10 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
private bool _isNewVersionDownloading;
private bool _isNewVersionChecked;
private bool _settingsBackupRestoreMessageVisible;
private string _settingsBackupMessage;
private string _backupRestoreMessageSeverity;
// Gets or sets a value indicating whether run powertoys on start-up.
public bool Startup
{
@@ -253,6 +289,23 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public string SettingsBackupAndRestoreDir
{
get
{
return settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
}
set
{
if (settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir() != value)
{
SettingsBackupAndRestoreUtils.SetRegSettingsBackupAndRestoreItem("SettingsBackupAndRestoreDir", value);
NotifyPropertyChanged();
}
}
}
public int ThemeIndex
{
get
@@ -313,6 +366,155 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public string LastSettingsBackupDate
{
get
{
try
{
var manifest = settingsBackupAndRestoreUtils.GetLatestSettingsBackupManifest();
if (manifest != null)
{
if (manifest["CreateDateTime"] != null)
{
if (DateTime.TryParse(manifest["CreateDateTime"].ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out var theDateTime))
{
return theDateTime.ToString("G", CultureInfo.CurrentCulture);
}
else
{
Logger.LogError("Failed to parse time from backup");
return GetResourceString("General_SettingsBackupAndRestore_FailedToParseTime");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupTime");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupDate", e);
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupTime");
}
}
}
public string CurrentSettingMatchText
{
get
{
try
{
var results = settingsBackupAndRestoreUtils.GetLastBackupSettingsResults();
var resultText = string.Empty;
if (!results.lastRan.HasValue)
{
// not ran since started.
return GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsNoChecked"); // "Current Settings Unknown";
}
else
{
if (results.success)
{
if (results.lastBackupExists)
{
// if true, it means a backup would have been made
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsDiffer"); // "Current Settings Differ";
}
else
{
// would have done the backup, but there also was not an existing one there.
resultText = GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
else
{
if (results.hadError)
{
// if false and error we don't really know
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsUnknown"); // "Current Settings Unknown";
}
else
{
// if false, it means a backup would not have been needed/made
resultText = GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsMatch"); // "Current Settings Match";
}
}
return $"{resultText} {GetResourceString("General_SettingsBackupAndRestore_CurrentSettingsStatusAt")} {results.lastRan.Value.ToLocalTime().ToString("G", CultureInfo.CurrentCulture)}";
}
}
catch (Exception e)
{
Logger.LogError("Error getting CurrentSettingMatchText", e);
return string.Empty;
}
}
}
public string LastSettingsBackupSource
{
get
{
try
{
var manifest = settingsBackupAndRestoreUtils.GetLatestSettingsBackupManifest();
if (manifest != null)
{
if (manifest["BackupSource"] != null)
{
if (manifest["BackupSource"].ToString().Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase))
{
return GetResourceString("General_SettingsBackupAndRestore_ThisMachine");
}
else
{
return manifest["BackupSource"].ToString();
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupSource");
}
}
else
{
return GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupSource", e);
return GetResourceString("General_SettingsBackupAndRestore_UnknownBackupSource");
}
}
}
public string LastSettingsBackupFileName
{
get
{
try
{
var fileName = settingsBackupAndRestoreUtils.GetLatestBackupFileName();
return !string.IsNullOrEmpty(fileName) ? fileName : GetResourceString("General_SettingsBackupAndRestore_NoBackupFound");
}
catch (Exception e)
{
Logger.LogError("Error getting LastSettingsBackupFileName", e);
return string.Empty;
}
}
}
public UpdatingSettings.UpdatingState PowerToysUpdatingState
{
get
@@ -389,6 +591,30 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public bool SettingsBackupRestoreMessageVisible
{
get
{
return _settingsBackupRestoreMessageVisible;
}
}
public string BackupRestoreMessageSeverity
{
get
{
return _backupRestoreMessageSeverity;
}
}
public string SettingsBackupMessage
{
get
{
return _settingsBackupMessage;
}
}
public bool IsDownloadAllowed
{
get
@@ -397,13 +623,114 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
}
}
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null, bool reDoBackupDryRun = true)
{
// Notify UI of property change
OnPropertyChanged(propertyName);
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig);
SendConfigMSG(outsettings.ToString());
if (reDoBackupDryRun && DoBackupAndRestoreDryRun != null)
{
DoBackupAndRestoreDryRun(500);
}
}
/// <summary>
/// Method <c>SelectSettingBackupDir</c> opens folder browser to select a backup and retore location.
/// </summary>
private async void SelectSettingBackupDir()
{
var currentDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
var newPath = await PickSingleFolderDialog();
if (!string.IsNullOrEmpty(newPath))
{
SettingsBackupAndRestoreDir = newPath;
NotifyAllBackupAndRestoreProperties();
}
}
private void RefreshBackupStatusEventHandlerClick()
{
DoBackupAndRestoreDryRun(0);
}
/// <summary>
/// Method <c>RestoreConfigsClick</c> starts the restore.
/// </summary>
private void RestoreConfigsClick()
{
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
SelectSettingBackupDir();
}
var results = SettingsUtils.RestoreSettings();
_backupRestoreMessageSeverity = results.severity;
if (!results.success)
{
_settingsBackupRestoreMessageVisible = true;
_settingsBackupMessage = GetResourceString(results.message);
NotifyAllBackupAndRestoreProperties();
HideBackupAndRestoreMessageAreaAction();
}
else
{
// make sure not to do NotifyPropertyChanged here, else it will persist the configs from memory and
// undo the settings restore.
SettingsBackupAndRestoreUtils.SetRegSettingsBackupAndRestoreItem("LastSettingsRestoreDate", DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture));
Restart();
}
}
/// <summary>
/// Method <c>BackupConfigsClick</c> starts the backup.
/// </summary>
private void BackupConfigsClick()
{
string settingsBackupAndRestoreDir = settingsBackupAndRestoreUtils.GetSettingsBackupAndRestoreDir();
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
SelectSettingBackupDir();
}
var results = SettingsUtils.BackupSettings();
_settingsBackupRestoreMessageVisible = true;
_backupRestoreMessageSeverity = results.severity;
_settingsBackupMessage = GetResourceString(results.message);
// now we do a dry run to get the results for "setting match"
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils.GetSettingsFilePath());
settingsBackupAndRestoreUtils.BackupSettings(appBasePath, settingsBackupAndRestoreDir, true);
NotifyAllBackupAndRestoreProperties();
HideBackupAndRestoreMessageAreaAction();
}
public void NotifyAllBackupAndRestoreProperties()
{
NotifyPropertyChanged(nameof(LastSettingsBackupDate), false);
NotifyPropertyChanged(nameof(LastSettingsBackupSource), false);
NotifyPropertyChanged(nameof(LastSettingsBackupFileName), false);
NotifyPropertyChanged(nameof(CurrentSettingMatchText), false);
NotifyPropertyChanged(nameof(SettingsBackupMessage), false);
NotifyPropertyChanged(nameof(BackupRestoreMessageSeverity), false);
NotifyPropertyChanged(nameof(SettingsBackupRestoreMessageVisible), false);
}
// callback function to launch the URL to check for updates.
@@ -435,6 +762,36 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
Process.Start(new ProcessStartInfo(Helper.GetPowerToysInstallationFolder() + "\\PowerToys.exe") { Arguments = "powertoys://update_now/" });
}
/// <summary>
/// Class <c>GetResourceString</c> gets a localized text.
/// </summary>
/// <remarks>
/// To do: see if there is a betting way to do this, there should be. It does allow us to return missing localization in a way that makes it obvious they were missed.
/// </remarks>
public string GetResourceString(string resource)
{
if (ResourceLoader != null)
{
var type = ResourceLoader.GetType();
MethodInfo methodInfo = type.GetMethod("GetString");
object classInstance = Activator.CreateInstance(type, null);
object[] parametersArray = new object[] { resource };
var result = (string)methodInfo.Invoke(ResourceLoader, parametersArray);
if (string.IsNullOrEmpty(result))
{
return resource.ToUpperInvariant() + "!!!";
}
else
{
return result;
}
}
else
{
return resource;
}
}
public void RequestUpdateCheckedDate()
{
GeneralSettingsConfig.CustomActionName = "request_update_state_date";
@@ -455,6 +812,36 @@ namespace Microsoft.PowerToys.Settings.UI.Library.ViewModels
SendRestartAsAdminConfigMSG(customaction.ToString());
}
/// <summary>
/// Class <c>Restart</c> begin a restart and signal we want to maintain elevation
/// </summary>
/// <remarks>
/// Other restarts either raised or lowered elevation
/// </remarks>
public void Restart()
{
GeneralSettingsConfig.CustomActionName = "restart_maintain_elevation";
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(GeneralSettingsConfig);
GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
var dataToSend = customaction.ToString();
dataToSend = JsonSerializer.Serialize(new { action = new { general = new { action_name = "restart_maintain_elevation" } } });
SendRestartAsAdminConfigMSG(dataToSend);
}
/// <summary>
/// Class <c>HideBackupAndRestoreMessageArea</c> hides the backup/restore message area
/// </summary>
/// <remarks>
/// We want to have it go away after a short period.
/// </remarks>
public void HideBackupAndRestoreMessageArea()
{
_settingsBackupRestoreMessageVisible = false;
NotifyAllBackupAndRestoreProperties();
}
public void RefreshUpdatingState()
{
object oLock = new object();

View File

@@ -0,0 +1,46 @@
{
"RestartAfterRestore": true,
"IncludeFiles": [
"*Keyboard Manager\\default.json",
"*settings.json",
"*FancyZones\\layout-hotkeys.json",
"*FancyZones\\layout-templates.json"
],
"IgnoreFiles": [
"*PowerToys\\log_settings.json",
"*PowerToys\\oobe_settings.json"
],
"IgnoredPTRunSettings": [
{
"Id": "525995402BEF4A8CA860D92F6D108092",
"Names": [
"IconPathDark",
"IconPathLight"
]
},
{
"Id": "5D69806A5A474115821C3E4C56B9C793",
"Names": [
"Description"
]
}
],
"IgnoredSettings": {
"backup-restore_settings.json": [
"RestartAfterRestore"
],
"settings.json": [
"powertoys_version"
],
"PowerToys Run\\Settings\\PowerToysRunSettings.json": [
"WindowLeft",
"WindowTop",
"ActivateTimes"
],
"PowerToys Run\\Settings\\Plugins\\Microsoft.Plugin.Program\\ProgramPluginSettings.json": [
"LastIndexTime",
"WindowTop",
"ActivateTimes"
]
}
}

View File

@@ -0,0 +1,38 @@
// 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.
using System;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
namespace Microsoft.PowerToys.Settings.UI.Converters
{
public sealed class StringToInfoBarSeverityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
try
{
if (value == null)
{
return Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational;
}
else
{
return (Microsoft.UI.Xaml.Controls.InfoBarSeverity)Enum.Parse(typeof(Microsoft.UI.Xaml.Controls.InfoBarSeverity), (string)value, true);
}
}
catch
{
return Microsoft.UI.Xaml.Controls.InfoBarSeverity.Informational;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}

View File

@@ -648,6 +648,24 @@
<data name="GeneralPage_CheckForUpdates.Content" xml:space="preserve">
<value>Check for updates</value>
</data>
<data name="General_SettingsBackupAndRestoreLocationText.Header" xml:space="preserve">
<value>Location</value>
</data>
<data name="General_SettingsBackupAndRestore_ButtonBackup.Content" xml:space="preserve">
<value>Backup</value>
</data>
<data name="General_SettingsBackupInfo_FileNameHeader.Text" xml:space="preserve">
<value>File name:</value>
</data>
<data name="General_SettingsBackupAndRestore_LinkRefresh.Text" xml:space="preserve">
<value>Refresh</value>
</data>
<data name="General_SettingsBackupAndRestore_ButtonRestore.Content" xml:space="preserve">
<value>Restore</value>
</data>
<data name="General_SettingsBackupAndRestore_ButtonSelectLocation.Content" xml:space="preserve">
<value>Browse</value>
</data>
<data name="GeneralPage_UpdateNow.Content" xml:space="preserve">
<value>Update now</value>
</data>
@@ -812,6 +830,12 @@
<data name="ShortcutGuide_Appearance_Behavior.Header" xml:space="preserve">
<value>Appearance &amp; behavior</value>
</data>
<data name="General_SettingsBackupAndRestoreTitle.Header" xml:space="preserve">
<value>Backup &amp; restore</value>
</data>
<data name="General_SettingsBackupAndRestore.Header" xml:space="preserve">
<value>Backup and restore your settings at any time</value>
</data>
<data name="ShortcutGuide_Enable.Header" xml:space="preserve">
<value>Enable Shortcut Guide</value>
<comment>do not loc the Product name. Do you want this feature on / off</comment>
@@ -1045,6 +1069,21 @@
<data name="General_VersionLastChecked.Text" xml:space="preserve">
<value>Last checked: </value>
</data>
<data name="General_SettingsBackupInfo_DateHeader.Text" xml:space="preserve">
<value>Created at:</value>
</data>
<data name="General_SettingsBackupAndRestoreDescription.Text" xml:space="preserve">
<value>PowerToys will restart automatically if needed.</value>
</data>
<data name="General_SettingsBackupAndRestoreStatusInfo.Header" xml:space="preserve">
<value>Backup information</value>
</data>
<data name="General_SettingsBackupInfo_SourceHeader.Text" xml:space="preserve">
<value>Source machine:</value>
</data>
<data name="General_SettingsBackupInfo_StatusHeader.Text" xml:space="preserve">
<value>Status:</value>
</data>
<data name="General_Version.[using:Microsoft.UI.Xaml.Automation]AutomationProperties.Name" xml:space="preserve">
<value>Version</value>
</data>
@@ -2354,6 +2393,64 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="LearnMore_TextExtractor.Text" xml:space="preserve">
<value>Learn more about Text Extractor</value>
</data>
<data name="General_SettingsBackupAndRestore_NothingToBackup" xml:space="preserve">
<value>A new backup was not created because no settings have been changed since last backup.</value>
</data>
<data name="General_SettingsBackupAndRestore_NoBackupFound" xml:space="preserve">
<value>No backup found</value>
</data>
<data name="General_SettingsBackupAndRestore_FailedToParseTime" xml:space="preserve">
<value>Failed to parse time</value>
</data>
<data name="General_SettingsBackupAndRestore_UnknownBackupTime" xml:space="preserve">
<value>Unknown</value>
</data>
<data name="General_SettingsBackupAndRestore_UnknownBackupSource" xml:space="preserve">
<value>Unknown</value>
</data>
<data name="General_SettingsBackupAndRestore_ThisMachine" xml:space="preserve">
<value>This computer</value>
</data>
<data name="General_SettingsBackupAndRestore_CurrentSettingsMatch" xml:space="preserve">
<value>Current settings match</value>
</data>
<data name="General_SettingsBackupAndRestore_CurrentSettingsStatusAt" xml:space="preserve">
<value>at</value>
<comment>E.g., Food was served 'at' noon.</comment>
</data>
<data name="General_SettingsBackupAndRestore_CurrentSettingsDiffer" xml:space="preserve">
<value>Current settings differ</value>
</data>
<data name="General_SettingsBackupAndRestore_CurrentSettingsNoChecked" xml:space="preserve">
<value>Checking...</value>
</data>
<data name="General_SettingsBackupAndRestore_CurrentSettingsUnknown" xml:space="preserve">
<value>Unknown</value>
</data>
<data name="General_SettingsBackupAndRestore_NeverRestored" xml:space="preserve">
<value>Never restored</value>
</data>
<data name="General_SettingsBackupAndRestore_NothingToRestore" xml:space="preserve">
<value>Nothing to restore.</value>
</data>
<data name="General_SettingsBackupAndRestore_NoSettingsFilesFound" xml:space="preserve">
<value>No settings files found.</value>
</data>
<data name="General_SettingsBackupAndRestore_BackupError" xml:space="preserve">
<value>There was an error. Try another backup location.</value>
</data>
<data name="General_SettingsBackupAndRestore_BackupComplete" xml:space="preserve">
<value>Backup completed.</value>
</data>
<data name="General_SettingsBackupAndRestore_NoBackupSyncPath" xml:space="preserve">
<value>No backup location selected.</value>
</data>
<data name="General_SettingsBackupAndRestore_NoBackupsFound" xml:space="preserve">
<value>No backups found to restore.</value>
</data>
<data name="General_SettingsBackupAndRestore_InvalidBackupLocation" xml:space="preserve">
<value>Invalid backup location.</value>
</data>
<data name="TextExtractor.ModuleDescription" xml:space="preserve">
<value>Text Extractor is a convenient way to copy text from anywhere on screen</value>
</data>

View File

@@ -20,4 +20,11 @@
<Setter Property="FontSize" Value="{StaticResource SecondaryTextFontSize}"/>
<Setter Property="Foreground" Value="{ThemeResource TextFillColorSecondaryBrush}"/>
</Style>
<x:Double x:Key="HeaderTextFontSize">12</x:Double>
<Style x:Key="HeaderTextStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="{StaticResource HeaderTextFontSize}"/>
<Setter Property="Foreground" Value="{ThemeResource TextFillColorSecondaryBrush}"/>
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>

View File

@@ -14,6 +14,7 @@
<converters:BoolToVisibilityConverter x:Key="VisibleIfTrueConverter"/>
<converters:BoolNegationConverter x:Key="NegationConverter"/>
<localConverters:UpdateStateToBoolConverter x:Key="UpdateStateToBoolConverter" />
<localConverters:StringToInfoBarSeverityConverter x:Key="StringToInfoBarSeverityConverter"/>
</Page.Resources>
<controls:SettingsPageControl x:Uid="General"
@@ -24,11 +25,11 @@
<controls:Setting Header="{Binding PowerToysVersion}" Icon="&#xE895;">
<controls:Setting.Description>
<StackPanel Orientation="Vertical">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run x:Uid="General_VersionLastChecked" />
<Run Text="{Binding UpdateCheckedDate, Mode=OneWay}"/>
</TextBlock>
</TextBlock>
<HyperlinkButton x:Uid="ReleaseNotes"
NavigateUri="https://github.com/microsoft/PowerToys/releases/"
Margin="0,2,0,0"/>
@@ -76,7 +77,7 @@
Command="{Binding UpdateNowButtonEventHandler}"
IsEnabled="{Binding IsDownloadAllowed, Mode=OneWay}"
Visibility="{Binding Mode=OneWay, Path=IsNewVersionDownloading, Converter={StaticResource NegationConverter}}"/>
<!-- In progress panel -->
<StackPanel Visibility="{Binding Mode=OneWay, Path=IsNewVersionDownloading, Converter={StaticResource VisibleIfTrueConverter}}"
Orientation="Horizontal"
@@ -137,7 +138,7 @@
Command="{Binding UpdateNowButtonEventHandler}"
IsEnabled="{Binding IsDownloadAllowed, Mode=OneWay}"
Visibility="{Binding Mode=OneWay, Path=IsNewVersionDownloading, Converter={StaticResource NegationConverter}}"/>
<!-- In progress panel -->
<StackPanel Visibility="{Binding Mode=OneWay, Path=IsNewVersionDownloading, Converter={StaticResource VisibleIfTrueConverter}}"
Orientation="Horizontal"
@@ -167,7 +168,7 @@
<ToggleSwitch IsOn="{Binding Mode=TwoWay, Path=AutoDownloadUpdates}" x:Uid="ToggleSwitch"/>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:SettingsGroup>
</controls:SettingsGroup>
@@ -207,26 +208,179 @@
<controls:SettingsGroup IsEnabled="True" x:Uid="ShortcutGuide_Appearance_Behavior">
<controls:Setting x:Uid="ColorModeHeader" Icon="&#xE790;">
<controls:Setting.Description>
<HyperlinkButton Click="OpenColorsSettings_Click"
<controls:Setting.Description>
<HyperlinkButton Click="OpenColorsSettings_Click"
x:Uid="Windows_Color_Settings"/>
</controls:Setting.Description>
<controls:Setting.ActionContent>
<ComboBox SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.ThemeIndex}" MinWidth="{StaticResource SettingActionControlMinWidth}">
<ComboBoxItem x:Uid="Radio_Theme_Dark"/>
<ComboBoxItem x:Uid="Radio_Theme_Light"/>
<ComboBoxItem x:Uid="Radio_Theme_Default"/>
</ComboBox>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:Setting.Description>
<controls:Setting.ActionContent>
<ComboBox SelectedIndex="{x:Bind Mode=TwoWay, Path=ViewModel.ThemeIndex}" MinWidth="{StaticResource SettingActionControlMinWidth}">
<ComboBoxItem x:Uid="Radio_Theme_Dark"/>
<ComboBoxItem x:Uid="Radio_Theme_Light"/>
<ComboBoxItem x:Uid="Radio_Theme_Default"/>
</ComboBox>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:Setting x:Uid="GeneralPage_RunAtStartUp">
<controls:Setting.ActionContent>
<ToggleSwitch IsOn="{Binding Mode=TwoWay, Path=Startup}" x:Uid="ToggleSwitch"/>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:SettingsGroup>
<!-- Start General_SettingsBackupAndRestore -->
<controls:SettingsGroup x:Uid="General_SettingsBackupAndRestoreTitle" Visibility="Visible">
<controls:SettingExpander IsExpanded="True">
<controls:SettingExpander.Header>
<controls:Setting x:Uid="General_SettingsBackupAndRestore" Icon="&#xE777;" Style="{StaticResource ExpanderHeaderSettingStyle}">
<controls:Setting.Description>
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run x:Uid="General_SettingsBackupAndRestoreDescription" />
</TextBlock>
</controls:Setting.Description>
<controls:Setting.ActionContent>
<Grid Visibility="Visible">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" x:Uid="General_SettingsBackupAndRestore_ButtonBackup"
Command="{Binding BackupConfigsEventHandler}"
IsEnabled="True"
Visibility="Visible"
HorizontalAlignment="Right"/>
<Button Grid.Column="2" x:Uid="General_SettingsBackupAndRestore_ButtonRestore"
Command="{Binding RestoreConfigsEventHandler}"
IsEnabled="True"
Visibility="Visible"
HorizontalAlignment="Right"/>
</Grid>
</controls:Setting.ActionContent>
</controls:Setting>
</controls:SettingExpander.Header>
<controls:SettingExpander.Content>
<StackPanel Orientation="Vertical">
<controls:Setting x:Uid="General_SettingsBackupAndRestoreLocationText" Visibility="Visible" Style="{StaticResource ExpanderContentSettingStyle}">
<controls:Setting.ActionContent>
<Grid Padding="10,5,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock MaxWidth="350" Grid.Row="0" Grid.Column="1" Text="{x:Bind Mode=TwoWay, Path=ViewModel.SettingsBackupAndRestoreDir}" IsTextSelectionEnabled="True" MinWidth="350" />
<Button Grid.Row="0" Grid.Column="2" x:Uid="General_SettingsBackupAndRestore_ButtonSelectLocation"
Command="{Binding SelectSettingBackupDirEventHandler}"
IsEnabled="True"
Visibility="Visible"
HorizontalAlignment="Left"/>
</Grid>
</controls:Setting.ActionContent>
</controls:Setting>
<controls:Setting x:Uid="General_SettingsBackupAndRestoreStatusInfo" Visibility="Visible" Style="{StaticResource ExpanderContentSettingStyle}"/>
<StackPanel>
<Grid Padding="55,0,10,10">
<Grid.Resources >
<Style TargetType="Border" >
<Setter Property="Padding" Value="2,0,5,0" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Border Grid.Row="0" Grid.Column="0">
<TextBlock Style="{StaticResource HeaderTextStyle}">
<Run x:Uid="General_SettingsBackupInfo_DateHeader" />
</TextBlock>
</Border>
<Border Grid.Row="0" Grid.Column="1">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run Text="{Binding LastSettingsBackupDate, Mode=OneWay}"/>
</TextBlock>
</Border>
<Border Grid.Row="1" Grid.Column="0">
<TextBlock Style="{StaticResource HeaderTextStyle}">
<Run x:Uid="General_SettingsBackupInfo_StatusHeader" />
</TextBlock>
</Border>
<Border Grid.Row="1" Grid.Column="1">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run Text="{Binding CurrentSettingMatchText, Mode=OneWay}"/>
</TextBlock>
<TextBlock Padding="5,0,0,0" Style="{StaticResource SecondaryTextStyle}"><Hyperlink Click="UpdateBackupAndRestoreStatusText" TextDecorations="Underline">
<Run x:Uid="General_SettingsBackupAndRestore_LinkRefresh" />
</Hyperlink></TextBlock>
</StackPanel>
</Border>
<Border Grid.Row="2" Grid.Column="0">
<TextBlock Style="{StaticResource HeaderTextStyle}">
<Run x:Uid="General_SettingsBackupInfo_SourceHeader" />
</TextBlock>
</Border>
<Border Grid.Row="2" Grid.Column="1">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run Text="{Binding LastSettingsBackupSource, Mode=OneWay}"/>
</TextBlock>
</Border>
<Border Grid.Row="3" Grid.Column="0">
<TextBlock Style="{StaticResource HeaderTextStyle}">
<Run x:Uid="General_SettingsBackupInfo_FileNameHeader" />
</TextBlock>
</Border>
<Border Grid.Row="3" Grid.Column="1">
<TextBlock Style="{StaticResource SecondaryTextStyle}">
<Run Text="{Binding LastSettingsBackupFileName, Mode=OneWay}"/>
</TextBlock>
</Border>
</Grid>
</StackPanel>
</StackPanel>
</controls:SettingExpander.Content>
</controls:SettingExpander>
</controls:SettingsGroup>
<InfoBar x:Uid="General_SettingsBackupMessageResults"
IsClosable="False"
Severity="{Binding BackupRestoreMessageSeverity, Converter={StaticResource StringToInfoBarSeverityConverter}}"
IsTabStop="{Binding SettingsBackupRestoreMessageVisible, Mode=OneWay}"
IsOpen="{Binding SettingsBackupRestoreMessageVisible, Mode=OneWay}"
Title="{Binding SettingsBackupMessage}"/>
<!-- End General_SettingsBackupAndRestore -->
</StackPanel>
</controls:SettingsPageControl.ModuleContent>
<controls:SettingsPageControl.PrimaryLinks>
<controls:PageLink x:Uid="GeneralPage_Documentation" Link="https://aka.ms/PowerToysOverview"/>

View File

@@ -3,12 +3,17 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
using Microsoft.PowerToys.Settings.UI.Library.ViewModels;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.ApplicationModel.Resources;
using Windows.Storage;
using Windows.Storage.Pickers;
namespace Microsoft.PowerToys.Settings.UI.Views
{
@@ -17,6 +22,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
/// </summary>
public sealed partial class GeneralPage : Page
{
private static DateTime OkToHideBackupAndRestoreMessageTime { get; set; }
/// <summary>
/// Gets or sets view model.
/// </summary>
@@ -42,6 +49,25 @@ namespace Microsoft.PowerToys.Settings.UI.Views
});
};
Action hideBackupAndRestoreMessageArea = () =>
{
this.DispatcherQueue.TryEnqueue(async () =>
{
const int messageShowTimeIs = 10000;
// in order to keep the message for about 5 seconds after the last call
// and not need any lock/thread-synch, use an OK-To-Hide time, and wait just a little longer than that.
OkToHideBackupAndRestoreMessageTime = DateTime.UtcNow.AddMilliseconds(messageShowTimeIs - 16);
await System.Threading.Tasks.Task.Delay(messageShowTimeIs);
if (DateTime.UtcNow > OkToHideBackupAndRestoreMessageTime)
{
ViewModel.HideBackupAndRestoreMessageArea();
}
});
};
var doRefreshBackupRestoreStatus = new Action<int>(RefreshBackupRestoreStatus);
ViewModel = new GeneralViewModel(
SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
loader.GetString("GeneralSettings_RunningAsAdminText"),
@@ -53,9 +79,15 @@ namespace Microsoft.PowerToys.Settings.UI.Views
ShellPage.SendRestartAdminIPCMessage,
ShellPage.SendCheckForUpdatesIPCMessage,
string.Empty,
stateUpdatingAction);
stateUpdatingAction,
hideBackupAndRestoreMessageArea,
doRefreshBackupRestoreStatus,
PickSingleFolderDialog,
loader);
DataContext = ViewModel;
doRefreshBackupRestoreStatus(100);
}
public static int UpdateUIThemeMethod(string themeName)
@@ -84,5 +116,38 @@ namespace Microsoft.PowerToys.Settings.UI.Views
{
Helpers.StartProcessHelper.Start(Helpers.StartProcessHelper.ColorsSettings);
}
private void RefreshBackupRestoreStatus(int delayMs = 0)
{
Task.Run(() =>
{
if (delayMs > 0)
{
Thread.Sleep(delayMs);
}
var settingsBackupAndRestoreUtils = SettingsBackupAndRestoreUtils.Instance;
var results = settingsBackupAndRestoreUtils.DryRunBackup();
this.DispatcherQueue.TryEnqueue(() =>
{
ViewModel.NotifyAllBackupAndRestoreProperties();
});
});
}
private void UpdateBackupAndRestoreStatusText(Microsoft.UI.Xaml.Documents.Hyperlink sender, Microsoft.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
RefreshBackupRestoreStatus();
}
private async Task<string> PickSingleFolderDialog()
{
var openPicker = new FolderPicker();
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.GetSettingsWindow());
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hwnd);
openPicker.FileTypeFilter.Add("*");
var folder = await openPicker.PickSingleFolderAsync();
return folder?.Path;
}
}
}