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

@@ -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;
}
}
}