[Registry Preview] * Two settings folders reduces to one. (#26842)

* [Registry Preview] * Two settings folders reduces to one.
* Two settings files reduced to one.
* Folder creation if not exist added.

* Add size/position properties to fix saving from Settings app

* Separate settings.json and app-placement.json

---------

Co-authored-by: Stefan Markovic <stefan@janeasystems.com>
This commit is contained in:
gokcekantarci 2023-06-26 13:45:52 +03:00 committed by GitHub
parent 8dcdcbaa37
commit 1b9094ae2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 25 deletions

View File

@ -88,7 +88,7 @@ END
STRINGTABLE STRINGTABLE
BEGIN BEGIN
IDS_REGISTRYPREVIEW_NAME "Registry Preview" IDS_REGISTRYPREVIEW_NAME "RegistryPreview"
END END
#endif // English (United States) resources #endif // English (United States) resources

View File

@ -29,10 +29,10 @@ namespace RegistryPreview
/// </summary> /// </summary>
private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args) private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
{ {
jsonSettings.SetNamedValue("appWindow.Position.X", JsonValue.CreateNumberValue(appWindow.Position.X)); jsonWindowPlacement.SetNamedValue("appWindow.Position.X", JsonValue.CreateNumberValue(appWindow.Position.X));
jsonSettings.SetNamedValue("appWindow.Position.Y", JsonValue.CreateNumberValue(appWindow.Position.Y)); jsonWindowPlacement.SetNamedValue("appWindow.Position.Y", JsonValue.CreateNumberValue(appWindow.Position.Y));
jsonSettings.SetNamedValue("appWindow.Size.Width", JsonValue.CreateNumberValue(appWindow.Size.Width)); jsonWindowPlacement.SetNamedValue("appWindow.Size.Width", JsonValue.CreateNumberValue(appWindow.Size.Width));
jsonSettings.SetNamedValue("appWindow.Size.Height", JsonValue.CreateNumberValue(appWindow.Size.Height)); jsonWindowPlacement.SetNamedValue("appWindow.Size.Height", JsonValue.CreateNumberValue(appWindow.Size.Height));
} }
/// <summary> /// <summary>
@ -55,8 +55,8 @@ namespace RegistryPreview
resourceLoader.GetString("YesNoCancelDialogCloseButtonText")); resourceLoader.GetString("YesNoCancelDialogCloseButtonText"));
} }
// Save app settings // Save window placement
SaveSettingsFile(settingsFolder, settingsFile); SaveWindowPlacementFile(settingsFolder, windowPlacementFile);
} }
/// <summary> /// <summary>

View File

@ -9,6 +9,7 @@ using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using Microsoft.UI.Input; using Microsoft.UI.Input;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
@ -842,7 +843,7 @@ namespace RegistryPreview
ChangeCursor(gridPreview, false); ChangeCursor(gridPreview, false);
} }
private void OpenSettingsFile(string path, string filename) private void OpenWindowPlacementFile(string path, string filename)
{ {
string fileContents = string.Empty; string fileContents = string.Empty;
string storageFile = Path.Combine(path, filename); string storageFile = Path.Combine(path, filename);
@ -860,23 +861,27 @@ namespace RegistryPreview
fileContents = "{ }"; fileContents = "{ }";
} }
} }
else
{
Task.Run(() => SaveWindowPlacementFile(path, filename)).GetAwaiter().GetResult();
}
try try
{ {
jsonSettings = Windows.Data.Json.JsonObject.Parse(fileContents); jsonWindowPlacement = Windows.Data.Json.JsonObject.Parse(fileContents);
} }
catch catch
{ {
// set up default JSON blob // set up default JSON blob
fileContents = "{ }"; fileContents = "{ }";
jsonSettings = Windows.Data.Json.JsonObject.Parse(fileContents); jsonWindowPlacement = Windows.Data.Json.JsonObject.Parse(fileContents);
} }
} }
/// <summary> /// <summary>
/// Save the settings JSON blob out to a local file /// Save the window placement JSON blob out to a local file
/// </summary> /// </summary>
private async void SaveSettingsFile(string path, string filename) private async void SaveWindowPlacementFile(string path, string filename)
{ {
StorageFolder storageFolder = null; StorageFolder storageFolder = null;
StorageFile storageFile = null; StorageFile storageFile = null;
@ -905,7 +910,7 @@ namespace RegistryPreview
try try
{ {
fileContents = jsonSettings.Stringify(); fileContents = jsonWindowPlacement.Stringify();
await Windows.Storage.FileIO.WriteTextAsync(storageFile, fileContents); await Windows.Storage.FileIO.WriteTextAsync(storageFile, fileContents);
} }
catch (Exception ex) catch (Exception ex)

View File

@ -19,7 +19,7 @@ namespace RegistryPreview
// Const values // Const values
private const string REGISTRYHEADER4 = "regedit4"; private const string REGISTRYHEADER4 = "regedit4";
private const string REGISTRYHEADER5 = "windows registry editor version 5.00"; private const string REGISTRYHEADER5 = "windows registry editor version 5.00";
private const string APPNAME = "Registry Preview"; private const string APPNAME = "RegistryPreview";
private const string KEYIMAGE = "ms-appx:///Assets/folder32.png"; private const string KEYIMAGE = "ms-appx:///Assets/folder32.png";
private const string DELETEDKEYIMAGE = "ms-appx:///Assets/deleted-folder32.png"; private const string DELETEDKEYIMAGE = "ms-appx:///Assets/deleted-folder32.png";
private const string ERRORIMAGE = "ms-appx:///Assets/error32.png"; private const string ERRORIMAGE = "ms-appx:///Assets/error32.png";
@ -30,9 +30,9 @@ namespace RegistryPreview
private bool visualTreeReady; private bool visualTreeReady;
private Dictionary<string, TreeViewNode> mapRegistryKeys; private Dictionary<string, TreeViewNode> mapRegistryKeys;
private List<RegistryValue> listRegistryValues; private List<RegistryValue> listRegistryValues;
private JsonObject jsonSettings; private JsonObject jsonWindowPlacement;
private string settingsFolder = string.Empty; private string settingsFolder = string.Empty;
private string settingsFile = string.Empty; private string windowPlacementFile = "app-placement.json";
internal MainWindow() internal MainWindow()
{ {
@ -43,8 +43,7 @@ namespace RegistryPreview
// Open settings file; this moved to after the window tweak because it gives the window time to start up // Open settings file; this moved to after the window tweak because it gives the window time to start up
settingsFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerToys\" + APPNAME; settingsFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\PowerToys\" + APPNAME;
settingsFile = APPNAME + "_settings.json"; OpenWindowPlacementFile(settingsFolder, windowPlacementFile);
OpenSettingsFile(settingsFolder, settingsFile);
// Update the Win32 looking window with the correct icon (and grab the appWindow handle for later) // Update the Win32 looking window with the correct icon (and grab the appWindow handle for later)
IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this); IntPtr windowHandle = WinRT.Interop.WindowNative.GetWindowHandle(this);
@ -58,14 +57,14 @@ namespace RegistryPreview
SetTitleBar(titleBar); SetTitleBar(titleBar);
// if have settings, update the location of the window // if have settings, update the location of the window
if (jsonSettings != null) if (jsonWindowPlacement != null)
{ {
// resize the window // resize the window
if (jsonSettings.ContainsKey("appWindow.Size.Width") && jsonSettings.ContainsKey("appWindow.Size.Height")) if (jsonWindowPlacement.ContainsKey("appWindow.Size.Width") && jsonWindowPlacement.ContainsKey("appWindow.Size.Height"))
{ {
SizeInt32 size; SizeInt32 size;
size.Width = (int)jsonSettings.GetNamedNumber("appWindow.Size.Width"); size.Width = (int)jsonWindowPlacement.GetNamedNumber("appWindow.Size.Width");
size.Height = (int)jsonSettings.GetNamedNumber("appWindow.Size.Height"); size.Height = (int)jsonWindowPlacement.GetNamedNumber("appWindow.Size.Height");
// check to make sure the size values are reasonable before attempting to restore the last saved size // check to make sure the size values are reasonable before attempting to restore the last saved size
if (size.Width >= 320 && size.Height >= 240) if (size.Width >= 320 && size.Height >= 240)
@ -75,11 +74,11 @@ namespace RegistryPreview
} }
// reposition the window // reposition the window
if (jsonSettings.ContainsKey("appWindow.Position.X") && jsonSettings.ContainsKey("appWindow.Position.Y")) if (jsonWindowPlacement.ContainsKey("appWindow.Position.X") && jsonWindowPlacement.ContainsKey("appWindow.Position.Y"))
{ {
PointInt32 point; PointInt32 point;
point.X = (int)jsonSettings.GetNamedNumber("appWindow.Position.X"); point.X = (int)jsonWindowPlacement.GetNamedNumber("appWindow.Position.X");
point.Y = (int)jsonSettings.GetNamedNumber("appWindow.Position.Y"); point.Y = (int)jsonWindowPlacement.GetNamedNumber("appWindow.Position.Y");
// check to make sure the move values are reasonable before attempting to restore the last saved location // check to make sure the move values are reasonable before attempting to restore the last saved location
if (point.X >= 0 && point.Y >= 0) if (point.X >= 0 && point.Y >= 0)