move data to the common project

This commit is contained in:
seraphima 2023-11-17 17:08:31 +01:00
parent 70eb880363
commit 89e7b49de4
21 changed files with 720 additions and 641 deletions

View File

@ -572,6 +572,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UITests-FancyZones", "src\m
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UITests-FancyZonesEditor", "src\modules\fancyzones\UITests-FancyZonesEditor\UITests-FancyZonesEditor.csproj", "{3A9A791E-94A9-49F8-8401-C11CE288D5FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FancyZonesEditorCommon", "src\modules\fancyzones\FancyZonesEditorCommon\FancyZonesEditorCommon.csproj", "{C0974915-8A1D-4BF0-977B-9587D3807AB7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
@ -2522,6 +2524,18 @@ Global
{3A9A791E-94A9-49F8-8401-C11CE288D5FB}.Release|x64.Build.0 = Release|x64
{3A9A791E-94A9-49F8-8401-C11CE288D5FB}.Release|x86.ActiveCfg = Release|x64
{3A9A791E-94A9-49F8-8401-C11CE288D5FB}.Release|x86.Build.0 = Release|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|ARM64.ActiveCfg = Debug|ARM64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|ARM64.Build.0 = Debug|ARM64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|x64.ActiveCfg = Debug|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|x64.Build.0 = Debug|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|x86.ActiveCfg = Debug|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Debug|x86.Build.0 = Debug|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|ARM64.ActiveCfg = Release|ARM64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|ARM64.Build.0 = Release|ARM64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|x64.ActiveCfg = Release|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|x64.Build.0 = Release|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|x86.ActiveCfg = Release|x64
{C0974915-8A1D-4BF0-977B-9587D3807AB7}.Release|x86.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -2733,6 +2747,7 @@ Global
{9D52FD25-EF90-4F9A-A015-91EFC5DAF54F} = {AB82E5DD-C32D-4F28-9746-2C780846188E}
{FE38FC07-1C05-4B57-ADA3-2FE2F53C6A52} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD}
{3A9A791E-94A9-49F8-8401-C11CE288D5FB} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD}
{C0974915-8A1D-4BF0-977B-9587D3807AB7} = {D1D6BC88-09AE-4FB4-AD24-5DED46A791DD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C3A2F9D1-7930-4EF4-A6FC-7EE0A99821D0}

View File

@ -0,0 +1,59 @@
// 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.Collections.Generic;
namespace FancyZonesEditorCommon.Data
{
public class AppliedLayouts : EditorData<AppliedLayouts.AppliedLayoutsListWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\applied-layouts.json";
}
}
public struct AppliedLayoutWrapper
{
public struct DeviceIdWrapper
{
public string Monitor { get; set; }
public string MonitorInstance { get; set; }
public int MonitorNumber { get; set; }
public string SerialNumber { get; set; }
public string VirtualDesktop { get; set; }
}
public struct LayoutWrapper
{
public string Uuid { get; set; }
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
public DeviceIdWrapper Device { get; set; }
public LayoutWrapper AppliedLayout { get; set; }
}
public struct AppliedLayoutsListWrapper
{
public List<AppliedLayoutWrapper> AppliedLayouts { get; set; }
}
}
}

View File

@ -0,0 +1,100 @@
// 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.Collections.Generic;
using System.Text.Json;
using static FancyZonesEditorCommon.Data.CustomLayouts;
namespace FancyZonesEditorCommon.Data
{
public class CustomLayouts : EditorData<CustomLayoutListWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\custom-layouts.json";
}
}
public sealed class CanvasInfoWrapper
{
public struct CanvasZoneWrapper
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public int RefWidth { get; set; }
public int RefHeight { get; set; }
public List<CanvasZoneWrapper> Zones { get; set; }
public int SensitivityRadius { get; set; } = LayoutDefaultSettings.DefaultSensitivityRadius;
}
public sealed class GridInfoWrapper
{
public int Rows { get; set; }
public int Columns { get; set; }
public List<int> RowsPercentage { get; set; }
public List<int> ColumnsPercentage { get; set; }
public int[][] CellChildMap { get; set; }
public bool ShowSpacing { get; set; } = LayoutDefaultSettings.DefaultShowSpacing;
public int Spacing { get; set; } = LayoutDefaultSettings.DefaultSpacing;
public int SensitivityRadius { get; set; } = LayoutDefaultSettings.DefaultSensitivityRadius;
}
public struct CustomLayoutWrapper
{
public string Uuid { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public JsonElement Info { get; set; } // CanvasInfoWrapper or GridInfoWrapper
}
public struct CustomLayoutListWrapper
{
public List<CustomLayoutWrapper> CustomLayouts { get; set; }
}
public JsonElement ToJsonElement(CanvasInfoWrapper info)
{
string json = JsonSerializer.Serialize(info, this.JsonOptions);
return JsonSerializer.Deserialize<JsonElement>(json);
}
public JsonElement ToJsonElement(GridInfoWrapper info)
{
string json = JsonSerializer.Serialize(info, this.JsonOptions);
return JsonSerializer.Deserialize<JsonElement>(json);
}
public CanvasInfoWrapper CanvasFromJsonElement(string json)
{
return JsonSerializer.Deserialize<CanvasInfoWrapper>(json, this.JsonOptions);
}
public GridInfoWrapper GridFromJsonElement(string json)
{
return JsonSerializer.Deserialize<GridInfoWrapper>(json, this.JsonOptions);
}
}
}

View File

@ -0,0 +1,47 @@
// 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.Collections.Generic;
using static FancyZonesEditorCommon.Data.DefaultLayouts;
namespace FancyZonesEditorCommon.Data
{
public class DefaultLayouts : EditorData<DefaultLayoutsListWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\default-layouts.json";
}
}
public struct DefaultLayoutWrapper
{
public struct LayoutWrapper
{
public string Uuid { get; set; }
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
public string MonitorConfiguration { get; set; }
public LayoutWrapper Layout { get; set; }
}
public struct DefaultLayoutsListWrapper
{
public List<DefaultLayoutWrapper> DefaultLayouts { get; set; }
}
}
}

View File

@ -4,12 +4,16 @@
using System;
using System.Text.Json;
using FancyZonesEditorCommon.Utils;
namespace FancyZonesEditor.Data
namespace FancyZonesEditorCommon.Data
{
public class EditorData
public class EditorData<T>
{
protected string File { get; set; }
public string GetDataFolder()
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
protected JsonSerializerOptions JsonOptions
{
@ -23,9 +27,16 @@ namespace FancyZonesEditor.Data
}
}
public string GetDataFolder()
public T Read(string file)
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
IOUtils ioUtils = new IOUtils();
string data = ioUtils.ReadFile(file);
return JsonSerializer.Deserialize<T>(data, JsonOptions);
}
public string Serialize(T data)
{
return JsonSerializer.Serialize(data, JsonOptions);
}
}
}

View File

@ -0,0 +1,89 @@
// 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.Collections.Generic;
using System.Globalization;
using System.Text;
namespace FancyZonesEditorCommon.Data
{
public class EditorParameters : EditorData<EditorParameters.ParamsWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json";
}
}
public struct NativeMonitorDataWrapper
{
public string Monitor { get; set; }
public string MonitorInstanceId { get; set; }
public string MonitorSerialNumber { get; set; }
public int MonitorNumber { get; set; }
public string VirtualDesktop { get; set; }
public int Dpi { get; set; }
public int LeftCoordinate { get; set; }
public int TopCoordinate { get; set; }
public int WorkAreaWidth { get; set; }
public int WorkAreaHeight { get; set; }
public int MonitorWidth { get; set; }
public int MonitorHeight { get; set; }
public bool IsSelected { get; set; }
public override string ToString()
{
var sb = new StringBuilder();
// using CultureInfo.InvariantCulture since this is internal data
sb.Append("Monitor: ");
sb.AppendLine(Monitor);
sb.Append("Virtual desktop: ");
sb.AppendLine(VirtualDesktop);
sb.Append("DPI: ");
sb.AppendLine(Dpi.ToString(CultureInfo.InvariantCulture));
sb.Append("X: ");
sb.AppendLine(LeftCoordinate.ToString(CultureInfo.InvariantCulture));
sb.Append("Y: ");
sb.AppendLine(TopCoordinate.ToString(CultureInfo.InvariantCulture));
sb.Append("Width: ");
sb.AppendLine(MonitorWidth.ToString(CultureInfo.InvariantCulture));
sb.Append("Height: ");
sb.AppendLine(MonitorHeight.ToString(CultureInfo.InvariantCulture));
return sb.ToString();
}
}
public struct ParamsWrapper
{
public int ProcessId { get; set; }
public bool SpanZonesAcrossMonitors { get; set; }
public List<NativeMonitorDataWrapper> Monitors { get; set; }
}
public EditorParameters()
: base()
{
}
}
}

View File

@ -0,0 +1,20 @@
// 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.
namespace FancyZonesEditorCommon.Data
{
public class LayoutDefaultSettings
{
// TODO: share the constants b/w C# Editor and FancyZoneLib
public const bool DefaultShowSpacing = true;
public const int DefaultSpacing = 16;
public const int DefaultZoneCount = 3;
public const int DefaultSensitivityRadius = 20;
public const int MaxZones = 128;
}
}

View File

@ -0,0 +1,32 @@
// 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.Collections.Generic;
using static FancyZonesEditorCommon.Data.LayoutHotkeys;
namespace FancyZonesEditorCommon.Data
{
public class LayoutHotkeys : EditorData<LayoutHotkeysWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-hotkeys.json";
}
}
public struct LayoutHotkeyWrapper
{
public int Key { get; set; }
public string LayoutId { get; set; }
}
public struct LayoutHotkeysWrapper
{
public List<LayoutHotkeyWrapper> LayoutHotkeys { get; set; }
}
}
}

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.Collections.Generic;
using static FancyZonesEditorCommon.Data.LayoutTemplates;
namespace FancyZonesEditorCommon.Data
{
public class LayoutTemplates : EditorData<TemplateLayoutsListWrapper>
{
public string File
{
get
{
return GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\layout-templates.json";
}
}
public struct TemplateLayoutWrapper
{
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
public struct TemplateLayoutsListWrapper
{
public List<TemplateLayoutWrapper> LayoutTemplates { get; set; }
}
}
}

View File

@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\..\Version.props" />
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<RuntimeIdentifiers>win-x64;win-arm64</RuntimeIdentifiers>
<Version>$(Version).0</Version>
<Authors>Microsoft Corporation</Authors>
<Product>PowerToys</Product>
<Description>PowerToys FancyZonesEditorCommon</Description>
<AssemblyName>PowerToys.FancyZonesEditorCommon</AssemblyName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.IO.Abstractions" />
</ItemGroup>
<PropertyGroup>
<OutputPath>..\..\..\..\$(Platform)\$(Configuration)\FancyZonesEditorCommon\</OutputPath>
</PropertyGroup>
</Project>

View File

@ -4,9 +4,7 @@
using System.Text.Json;
using FancyZonesEditor.Utils;
namespace FancyZonesEditor
namespace FancyZonesEditorCommon.Utils
{
public class DashCaseNamingPolicy : JsonNamingPolicy
{

View File

@ -0,0 +1,54 @@
// 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 System.IO;
using System.IO.Abstractions;
using System.Threading.Tasks;
namespace FancyZonesEditorCommon.Utils
{
public class IOUtils
{
private readonly IFileSystem _fileSystem = new FileSystem();
public IOUtils()
{
}
public void WriteFile(string fileName, string data)
{
_fileSystem.File.WriteAllText(fileName, data);
}
public string ReadFile(string fileName)
{
if (_fileSystem.File.Exists(fileName))
{
var attempts = 0;
while (attempts < 10)
{
try
{
using (Stream inputStream = _fileSystem.File.Open(fileName, FileMode.Open))
using (StreamReader reader = new StreamReader(inputStream))
{
string data = reader.ReadToEnd();
inputStream.Close();
return data;
}
}
catch (Exception)
{
Task.Delay(10).Wait();
}
attempts++;
}
}
return string.Empty;
}
}
}

View File

@ -4,7 +4,7 @@
using System.Linq;
namespace FancyZonesEditor.Utils
namespace FancyZonesEditorCommon.Utils
{
public static class StringUtils
{

View File

@ -1,178 +0,0 @@
// 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 System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Windows;
using FancyZonesEditor.Models;
using FancyZonesEditor.Utils;
using Microsoft.FancyZonesEditor.UITests.Utils;
namespace FancyZonesEditor.Data
{
public class EditorParameters : EditorData
{
protected struct NativeMonitorData
{
public string Monitor { get; set; }
public string MonitorInstanceId { get; set; }
public string MonitorSerialNumber { get; set; }
public int MonitorNumber { get; set; }
public string VirtualDesktop { get; set; }
public int Dpi { get; set; }
public int LeftCoordinate { get; set; }
public int TopCoordinate { get; set; }
public int WorkAreaWidth { get; set; }
public int WorkAreaHeight { get; set; }
public int MonitorWidth { get; set; }
public int MonitorHeight { get; set; }
public bool IsSelected { get; set; }
public override string ToString()
{
var sb = new StringBuilder();
// using CultureInfo.InvariantCulture since this is internal data
sb.Append("Monitor: ");
sb.AppendLine(Monitor);
sb.Append("Virtual desktop: ");
sb.AppendLine(VirtualDesktop);
sb.Append("DPI: ");
sb.AppendLine(Dpi.ToString(CultureInfo.InvariantCulture));
sb.Append("X: ");
sb.AppendLine(LeftCoordinate.ToString(CultureInfo.InvariantCulture));
sb.Append("Y: ");
sb.AppendLine(TopCoordinate.ToString(CultureInfo.InvariantCulture));
sb.Append("Width: ");
sb.AppendLine(MonitorWidth.ToString(CultureInfo.InvariantCulture));
sb.Append("Height: ");
sb.AppendLine(MonitorHeight.ToString(CultureInfo.InvariantCulture));
return sb.ToString();
}
}
protected struct EditorParams
{
public int ProcessId { get; set; }
public bool SpanZonesAcrossMonitors { get; set; }
public List<NativeMonitorData> Monitors { get; set; }
}
public EditorParameters()
: base()
{
File = GetDataFolder() + "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json";
}
public ParsingResult Parse()
{
IOHelper ioHelper = new IOHelper();
string data = ioHelper.ReadFile(File);
try
{
EditorParams editorParams = JsonSerializer.Deserialize<EditorParams>(data, JsonOptions);
// Process ID
App.PowerToysPID = editorParams.ProcessId;
// Span zones across monitors
App.Overlay.SpanZonesAcrossMonitors = editorParams.SpanZonesAcrossMonitors;
if (!App.Overlay.SpanZonesAcrossMonitors)
{
string targetMonitorId = string.Empty;
string targetMonitorSerialNumber = string.Empty;
string targetVirtualDesktop = string.Empty;
int targetMonitorNumber = 0;
foreach (NativeMonitorData nativeData in editorParams.Monitors)
{
Rect workArea = new Rect(nativeData.LeftCoordinate, nativeData.TopCoordinate, nativeData.WorkAreaWidth, nativeData.WorkAreaHeight);
if (nativeData.IsSelected)
{
targetMonitorId = nativeData.Monitor;
targetMonitorSerialNumber = nativeData.MonitorSerialNumber;
targetMonitorNumber = nativeData.MonitorNumber;
targetVirtualDesktop = nativeData.VirtualDesktop;
}
Size monitorSize = new Size(nativeData.MonitorWidth, nativeData.MonitorHeight);
var monitor = new Monitor(workArea, monitorSize);
monitor.Device.MonitorName = nativeData.Monitor;
monitor.Device.MonitorInstanceId = nativeData.MonitorInstanceId;
monitor.Device.MonitorSerialNumber = nativeData.MonitorSerialNumber;
monitor.Device.MonitorNumber = nativeData.MonitorNumber;
monitor.Device.VirtualDesktopId = nativeData.VirtualDesktop;
monitor.Device.Dpi = nativeData.Dpi;
App.Overlay.AddMonitor(monitor);
}
// Set active desktop
var monitors = App.Overlay.Monitors;
for (int i = 0; i < monitors.Count; i++)
{
var monitor = monitors[i];
if (monitor.Device.MonitorName == targetMonitorId &&
monitor.Device.MonitorSerialNumber == targetMonitorSerialNumber &&
monitor.Device.MonitorNumber == targetMonitorNumber &&
monitor.Device.VirtualDesktopId == targetVirtualDesktop)
{
App.Overlay.CurrentDesktop = i;
break;
}
}
}
else
{
if (editorParams.Monitors.Count != 1)
{
return new ParsingResult(false);
}
var nativeData = editorParams.Monitors[0];
Rect workArea = new Rect(nativeData.LeftCoordinate, nativeData.TopCoordinate, nativeData.WorkAreaWidth, nativeData.WorkAreaHeight);
Size monitorSize = new Size(nativeData.MonitorWidth, nativeData.MonitorHeight);
var monitor = new Monitor(workArea, monitorSize);
monitor.Device.MonitorName = nativeData.Monitor;
monitor.Device.MonitorInstanceId = nativeData.MonitorInstanceId;
monitor.Device.MonitorSerialNumber = nativeData.MonitorSerialNumber;
monitor.Device.MonitorNumber = nativeData.MonitorNumber;
monitor.Device.VirtualDesktopId = nativeData.VirtualDesktop;
App.Overlay.AddMonitor(monitor);
}
}
catch (Exception ex)
{
return new ParsingResult(false, ex.Message, data);
}
return new ParsingResult(true);
}
}
}

View File

@ -69,6 +69,7 @@
<ProjectReference Include="..\..\..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\..\..\common\Common.UI\Common.UI.csproj" />
<ProjectReference Include="..\..\FancyZonesEditorCommon\FancyZonesEditorCommon.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">

View File

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using FancyZonesEditorCommon.Data;
namespace FancyZonesEditor.Models
{
@ -99,7 +100,7 @@ namespace FancyZonesEditor.Models
}
}
private bool _showSpacing = LayoutSettings.DefaultShowSpacing;
private bool _showSpacing = LayoutDefaultSettings.DefaultShowSpacing;
// Spacing - free space between cells
public int Spacing
@ -129,7 +130,7 @@ namespace FancyZonesEditor.Models
get { return 1000; }
}
private int _spacing = LayoutSettings.DefaultSpacing;
private int _spacing = LayoutDefaultSettings.DefaultSpacing;
public GridLayoutModel()
: base()

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using FancyZonesEditorCommon.Data;
namespace FancyZonesEditor.Models
{
@ -195,7 +196,7 @@ namespace FancyZonesEditor.Models
}
}
private int _sensitivityRadius = LayoutSettings.DefaultSensitivityRadius;
private int _sensitivityRadius = LayoutDefaultSettings.DefaultSensitivityRadius;
public int SensitivityRadiusMinimum
{
@ -304,13 +305,13 @@ namespace FancyZonesEditor.Models
}
}
private int _zoneCount = LayoutSettings.DefaultZoneCount;
private int _zoneCount = LayoutDefaultSettings.DefaultZoneCount;
public bool IsZoneAddingAllowed
{
get
{
return TemplateZoneCount < LayoutSettings.MaxZones;
return TemplateZoneCount < LayoutDefaultSettings.MaxZones;
}
}

View File

@ -3,32 +3,22 @@
// See the LICENSE file in the project root for more information.
using FancyZonesEditor.Models;
using FancyZonesEditorCommon.Data;
namespace FancyZonesEditor
{
public class LayoutSettings
{
// TODO: share the constants b/w C# Editor and FancyZoneLib
public const bool DefaultShowSpacing = true;
public const int DefaultSpacing = 16;
public const int DefaultZoneCount = 3;
public const int DefaultSensitivityRadius = 20;
public const int MaxZones = 128;
public string ZonesetUuid { get; set; } = string.Empty;
public LayoutType Type { get; set; } = LayoutType.PriorityGrid;
public bool ShowSpacing { get; set; } = DefaultShowSpacing;
public bool ShowSpacing { get; set; } = LayoutDefaultSettings.DefaultShowSpacing;
public int Spacing { get; set; } = DefaultSpacing;
public int Spacing { get; set; } = LayoutDefaultSettings.DefaultSpacing;
public int ZoneCount { get; set; } = DefaultZoneCount;
public int ZoneCount { get; set; } = LayoutDefaultSettings.DefaultZoneCount;
public int SensitivityRadius { get; set; } = DefaultSensitivityRadius;
public int SensitivityRadius { get; set; } = LayoutDefaultSettings.DefaultSensitivityRadius;
}
}

View File

@ -487,6 +487,15 @@ namespace FancyZonesEditor.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to An error occurred while parsing editor parameters..
/// </summary>
public static string Error_Parsing_Editor_Parameters_Message {
get {
return ResourceManager.GetString("Error_Parsing_Editor_Parameters_Message", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to An error occurred while parsing layout hotkeys..
/// </summary>

View File

@ -435,4 +435,7 @@
<data name="Set_Layout_As_Vertical_Default" xml:space="preserve">
<value>Set layout as a default for vertical monitor orientation</value>
</data>
<data name="Error_Parsing_Editor_Parameters_Message" xml:space="preserve">
<value>An error occurred while parsing editor parameters.</value>
</data>
</root>

View File

@ -6,13 +6,11 @@ using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using FancyZonesEditor.Data;
using FancyZonesEditor.Models;
using FancyZonesEditorCommon.Data;
using FancyZonesEditorCommon.Utils;
using ManagedCommon;
namespace FancyZonesEditor.Utils
@ -30,233 +28,103 @@ namespace FancyZonesEditor.Utils
private const string HorizontalJsonTag = "horizontal";
private const string VerticalJsonTag = "vertical";
// Non-localizable strings: Files
private const string AppliedLayoutsFile = "\\Microsoft\\PowerToys\\FancyZones\\applied-layouts.json";
private const string LayoutHotkeysFile = "\\Microsoft\\PowerToys\\FancyZones\\layout-hotkeys.json";
private const string LayoutTemplatesFile = "\\Microsoft\\PowerToys\\FancyZones\\layout-templates.json";
private const string CustomLayoutsFile = "\\Microsoft\\PowerToys\\FancyZones\\custom-layouts.json";
private const string DefaultLayoutsFile = "\\Microsoft\\PowerToys\\FancyZones\\default-layouts.json";
// Non-localizable string: default virtual desktop id
private const string DefaultVirtualDesktopGuid = "{00000000-0000-0000-0000-000000000000}";
private readonly IFileSystem _fileSystem = new FileSystem();
private readonly JsonSerializerOptions _options = new JsonSerializerOptions
{
PropertyNamingPolicy = new DashCaseNamingPolicy(),
WriteIndented = true,
};
private List<AppliedLayoutWrapper> _unusedLayouts = new List<AppliedLayoutWrapper>();
public string FancyZonesAppliedLayoutsFile { get; private set; }
public string FancyZonesLayoutHotkeysFile { get; private set; }
public string FancyZonesLayoutTemplatesFile { get; private set; }
public string FancyZonesCustomLayoutsFile { get; private set; }
public string FancyZonesDefaultLayoutsFile { get; private set; }
public string FancyZonesEditorParamsFile { get; private set; }
private enum CmdArgs
{
PowerToysPID = 0,
SpanZones,
TargetMonitorId,
MonitorsCount,
MonitorId,
DPI,
MonitorLeft,
MonitorTop,
MonitorWidth,
MonitorHeight,
}
// applied-layouts.json
private struct AppliedLayoutWrapper
{
public struct DeviceIdWrapper
{
public string Monitor { get; set; }
public string MonitorInstance { get; set; }
public int MonitorNumber { get; set; }
public string SerialNumber { get; set; }
public string VirtualDesktop { get; set; }
}
public struct LayoutWrapper
{
public string Uuid { get; set; }
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
public DeviceIdWrapper Device { get; set; }
public LayoutWrapper AppliedLayout { get; set; }
}
// applied-layouts.json
private struct AppliedLayoutsListWrapper
{
public List<AppliedLayoutWrapper> AppliedLayouts { get; set; }
}
// custom-layouts.json
private sealed class CanvasInfoWrapper
{
public struct CanvasZoneWrapper
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public int RefWidth { get; set; }
public int RefHeight { get; set; }
public List<CanvasZoneWrapper> Zones { get; set; }
public int SensitivityRadius { get; set; } = LayoutSettings.DefaultSensitivityRadius;
}
// custom-layouts.json
private sealed class GridInfoWrapper
{
public int Rows { get; set; }
public int Columns { get; set; }
public List<int> RowsPercentage { get; set; }
public List<int> ColumnsPercentage { get; set; }
public int[][] CellChildMap { get; set; }
public bool ShowSpacing { get; set; } = LayoutSettings.DefaultShowSpacing;
public int Spacing { get; set; } = LayoutSettings.DefaultSpacing;
public int SensitivityRadius { get; set; } = LayoutSettings.DefaultSensitivityRadius;
}
// custom-layouts.json
private struct CustomLayoutWrapper
{
public string Uuid { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public JsonElement Info { get; set; } // CanvasInfoWrapper or GridInfoWrapper
}
// custom-layouts.json
private struct CustomLayoutListWrapper
{
public List<CustomLayoutWrapper> CustomLayouts { get; set; }
}
// layout-templates.json
private struct TemplateLayoutWrapper
{
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
// layout-templates.json
private struct TemplateLayoutsListWrapper
{
public List<TemplateLayoutWrapper> LayoutTemplates { get; set; }
}
// layout-hotkeys.json
private struct LayoutHotkeyWrapper
{
public int Key { get; set; }
public string LayoutId { get; set; }
}
// layout-hotkeys.json
private struct LayoutHotkeysWrapper
{
public List<LayoutHotkeyWrapper> LayoutHotkeys { get; set; }
}
// default-layouts.json
private struct DefaultLayoutWrapper
{
public struct LayoutWrapper
{
public string Uuid { get; set; }
public string Type { get; set; }
public bool ShowSpacing { get; set; }
public int Spacing { get; set; }
public int ZoneCount { get; set; }
public int SensitivityRadius { get; set; }
}
public string MonitorConfiguration { get; set; }
public LayoutWrapper Layout { get; set; }
}
// default-layouts.json
private struct DefaultLayoutsListWrapper
{
public List<DefaultLayoutWrapper> DefaultLayouts { get; set; }
}
private List<AppliedLayouts.AppliedLayoutWrapper> _unusedLayouts = new List<AppliedLayouts.AppliedLayoutWrapper>();
public FancyZonesEditorIO()
{
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
FancyZonesAppliedLayoutsFile = localAppDataDir + AppliedLayoutsFile;
FancyZonesLayoutHotkeysFile = localAppDataDir + LayoutHotkeysFile;
FancyZonesLayoutTemplatesFile = localAppDataDir + LayoutTemplatesFile;
FancyZonesCustomLayoutsFile = localAppDataDir + CustomLayoutsFile;
FancyZonesDefaultLayoutsFile = localAppDataDir + DefaultLayoutsFile;
}
public ParsingResult ParseParams()
{
Logger.LogTrace();
EditorParameters parser = new EditorParameters();
return parser.Parse();
try
{
EditorParameters parser = new EditorParameters();
var editorParams = parser.Read(parser.File);
// Process ID
App.PowerToysPID = editorParams.ProcessId;
// Span zones across monitors
App.Overlay.SpanZonesAcrossMonitors = editorParams.SpanZonesAcrossMonitors;
if (!App.Overlay.SpanZonesAcrossMonitors)
{
string targetMonitorId = string.Empty;
string targetMonitorSerialNumber = string.Empty;
string targetVirtualDesktop = string.Empty;
int targetMonitorNumber = 0;
foreach (EditorParameters.NativeMonitorDataWrapper nativeData in editorParams.Monitors)
{
Rect workArea = new Rect(nativeData.LeftCoordinate, nativeData.TopCoordinate, nativeData.WorkAreaWidth, nativeData.WorkAreaHeight);
if (nativeData.IsSelected)
{
targetMonitorId = nativeData.Monitor;
targetMonitorSerialNumber = nativeData.MonitorSerialNumber;
targetMonitorNumber = nativeData.MonitorNumber;
targetVirtualDesktop = nativeData.VirtualDesktop;
}
Size monitorSize = new Size(nativeData.MonitorWidth, nativeData.MonitorHeight);
var monitor = new Monitor(workArea, monitorSize);
monitor.Device.MonitorName = nativeData.Monitor;
monitor.Device.MonitorInstanceId = nativeData.MonitorInstanceId;
monitor.Device.MonitorSerialNumber = nativeData.MonitorSerialNumber;
monitor.Device.MonitorNumber = nativeData.MonitorNumber;
monitor.Device.VirtualDesktopId = nativeData.VirtualDesktop;
monitor.Device.Dpi = nativeData.Dpi;
App.Overlay.AddMonitor(monitor);
}
// Set active desktop
var monitors = App.Overlay.Monitors;
for (int i = 0; i < monitors.Count; i++)
{
var monitor = monitors[i];
if (monitor.Device.MonitorName == targetMonitorId &&
monitor.Device.MonitorSerialNumber == targetMonitorSerialNumber &&
monitor.Device.MonitorNumber == targetMonitorNumber &&
monitor.Device.VirtualDesktopId == targetVirtualDesktop)
{
App.Overlay.CurrentDesktop = i;
break;
}
}
}
else
{
if (editorParams.Monitors.Count != 1)
{
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Editor_Parameters_Message);
}
var nativeData = editorParams.Monitors[0];
Rect workArea = new Rect(nativeData.LeftCoordinate, nativeData.TopCoordinate, nativeData.WorkAreaWidth, nativeData.WorkAreaHeight);
Size monitorSize = new Size(nativeData.MonitorWidth, nativeData.MonitorHeight);
var monitor = new Monitor(workArea, monitorSize);
monitor.Device.MonitorName = nativeData.Monitor;
monitor.Device.MonitorInstanceId = nativeData.MonitorInstanceId;
monitor.Device.MonitorSerialNumber = nativeData.MonitorSerialNumber;
monitor.Device.MonitorNumber = nativeData.MonitorNumber;
monitor.Device.VirtualDesktopId = nativeData.VirtualDesktop;
App.Overlay.AddMonitor(monitor);
}
return new ParsingResult(true);
}
catch (Exception e)
{
return new ParsingResult(false, e.Message);
}
}
public ParsingResult ParseAppliedLayouts()
@ -265,36 +133,22 @@ namespace FancyZonesEditor.Utils
_unusedLayouts.Clear();
if (_fileSystem.File.Exists(FancyZonesAppliedLayoutsFile))
try
{
AppliedLayoutsListWrapper appliedLayouts;
string settingsString = string.Empty;
AppliedLayouts parser = new AppliedLayouts();
var appliedLayouts = parser.Read(parser.File);
try
bool parsingResult = SetAppliedLayouts(appliedLayouts.AppliedLayouts);
if (!parsingResult)
{
settingsString = ReadFile(FancyZonesAppliedLayoutsFile);
appliedLayouts = JsonSerializer.Deserialize<AppliedLayoutsListWrapper>(settingsString, _options);
}
catch (Exception ex)
{
Logger.LogError("Applied layouts parsing error", ex);
return new ParsingResult(false, ex.Message, settingsString);
}
try
{
bool parsingResult = SetAppliedLayouts(appliedLayouts.AppliedLayouts);
if (!parsingResult)
{
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Applied_Layouts_Message, settingsString);
}
}
catch (Exception ex)
{
Logger.LogError("Applied layouts parsing error", ex);
return new ParsingResult(false, ex.Message, settingsString);
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Applied_Layouts_Message);
}
}
catch (Exception ex)
{
Logger.LogError("Applied layouts parsing error", ex);
return new ParsingResult(false, ex.Message);
}
return new ParsingResult(true);
}
@ -303,167 +157,105 @@ namespace FancyZonesEditor.Utils
{
Logger.LogTrace();
if (_fileSystem.File.Exists(FancyZonesLayoutHotkeysFile))
try
{
LayoutHotkeysWrapper layoutHotkeys;
string dataString = string.Empty;
LayoutHotkeys parser = new LayoutHotkeys();
var layoutHotkeys = parser.Read(parser.File);
try
bool layoutHotkeysParsingResult = SetLayoutHotkeys(layoutHotkeys);
if (!layoutHotkeysParsingResult)
{
dataString = ReadFile(FancyZonesLayoutHotkeysFile);
layoutHotkeys = JsonSerializer.Deserialize<LayoutHotkeysWrapper>(dataString, _options);
}
catch (Exception ex)
{
Logger.LogError("Layout hotkeys parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Layout_Hotkeys_Message);
}
try
{
bool layoutHotkeysParsingResult = SetLayoutHotkeys(layoutHotkeys);
if (!layoutHotkeysParsingResult)
{
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Layout_Hotkeys_Message, dataString);
}
}
catch (Exception ex)
{
Logger.LogError("Layout hotkeys parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
}
return new ParsingResult(true);
}
catch (Exception ex)
{
Logger.LogError("Layout hotkeys parsing error", ex);
return new ParsingResult(false, ex.Message);
}
return new ParsingResult(true);
}
public ParsingResult ParseLayoutTemplates()
{
Logger.LogTrace();
if (_fileSystem.File.Exists(FancyZonesLayoutTemplatesFile))
try
{
TemplateLayoutsListWrapper templates;
string dataString = string.Empty;
LayoutTemplates parser = new LayoutTemplates();
var templates = parser.Read(parser.File);
try
bool parsingResult = SetTemplateLayouts(templates.LayoutTemplates);
if (parsingResult)
{
dataString = ReadFile(FancyZonesLayoutTemplatesFile);
templates = JsonSerializer.Deserialize<TemplateLayoutsListWrapper>(dataString, _options);
}
catch (Exception ex)
{
Logger.LogError("Layout templates parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
return new ParsingResult(true);
}
try
{
bool parsingResult = SetTemplateLayouts(templates.LayoutTemplates);
if (parsingResult)
{
return new ParsingResult(true);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Layout_Templates_Message, dataString);
}
catch (Exception ex)
{
Logger.LogError("Layout templates parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Layout_Templates_Message);
}
catch (Exception ex)
{
Logger.LogError("Layout templates parsing error", ex);
return new ParsingResult(false, ex.Message);
}
return new ParsingResult(true);
}
public ParsingResult ParseCustomLayouts()
{
Logger.LogTrace();
if (_fileSystem.File.Exists(FancyZonesCustomLayoutsFile))
try
{
CustomLayoutListWrapper wrapper;
string dataString = string.Empty;
CustomLayouts parser = new CustomLayouts();
var wrapper = parser.Read(parser.File);
try
bool parsingResult = SetCustomLayouts(wrapper.CustomLayouts);
if (parsingResult)
{
dataString = ReadFile(FancyZonesCustomLayoutsFile);
wrapper = JsonSerializer.Deserialize<CustomLayoutListWrapper>(dataString, _options);
}
catch (Exception ex)
{
Logger.LogError("Custom layouts parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
return new ParsingResult(true);
}
try
{
bool parsingResult = SetCustomLayouts(wrapper.CustomLayouts);
if (parsingResult)
{
return new ParsingResult(true);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Custom_Layouts_Message, dataString);
}
catch (Exception ex)
{
Logger.LogError("Custom layouts parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Custom_Layouts_Message);
}
catch (Exception ex)
{
Logger.LogError("Custom layouts parsing error", ex);
return new ParsingResult(false, ex.Message);
}
return new ParsingResult(true);
}
public ParsingResult ParseDefaultLayouts()
{
Logger.LogTrace();
if (_fileSystem.File.Exists(FancyZonesDefaultLayoutsFile))
try
{
DefaultLayoutsListWrapper wrapper;
string dataString = string.Empty;
DefaultLayouts parser = new DefaultLayouts();
var wrapper = parser.Read(parser.File);
try
bool parsingResult = SetDefaultLayouts(wrapper.DefaultLayouts);
if (parsingResult)
{
dataString = ReadFile(FancyZonesDefaultLayoutsFile);
wrapper = JsonSerializer.Deserialize<DefaultLayoutsListWrapper>(dataString, _options);
}
catch (Exception ex)
{
Logger.LogError("Default layouts parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
return new ParsingResult(true);
}
try
{
bool parsingResult = SetDefaultLayouts(wrapper.DefaultLayouts);
if (parsingResult)
{
return new ParsingResult(true);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Default_Layouts_Message, dataString);
}
catch (Exception ex)
{
Logger.LogError("Default layouts parsing error", ex);
return new ParsingResult(false, ex.Message, dataString);
}
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Default_Layouts_Message);
}
catch (Exception ex)
{
Logger.LogError("Default layouts parsing error", ex);
return new ParsingResult(false, ex.Message);
}
return new ParsingResult(true);
}
public void SerializeAppliedLayouts()
{
Logger.LogTrace();
AppliedLayoutsListWrapper layouts = new AppliedLayoutsListWrapper { };
layouts.AppliedLayouts = new List<AppliedLayoutWrapper>();
AppliedLayouts.AppliedLayoutsListWrapper layouts = new AppliedLayouts.AppliedLayoutsListWrapper { };
layouts.AppliedLayouts = new List<AppliedLayouts.AppliedLayoutWrapper>();
// Serialize used layouts
foreach (var monitor in App.Overlay.Monitors)
@ -474,9 +266,9 @@ namespace FancyZonesEditor.Utils
continue;
}
layouts.AppliedLayouts.Add(new AppliedLayoutWrapper
layouts.AppliedLayouts.Add(new AppliedLayouts.AppliedLayoutWrapper
{
Device = new AppliedLayoutWrapper.DeviceIdWrapper
Device = new AppliedLayouts.AppliedLayoutWrapper.DeviceIdWrapper
{
Monitor = monitor.Device.MonitorName,
MonitorInstance = monitor.Device.MonitorInstanceId,
@ -485,7 +277,7 @@ namespace FancyZonesEditor.Utils
VirtualDesktop = monitor.Device.VirtualDesktopId,
},
AppliedLayout = new AppliedLayoutWrapper.LayoutWrapper
AppliedLayout = new AppliedLayouts.AppliedLayoutWrapper.LayoutWrapper
{
Uuid = zoneset.ZonesetUuid,
Type = LayoutTypeToJsonTag(zoneset.Type),
@ -505,8 +297,9 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(layouts, _options);
_fileSystem.File.WriteAllText(FancyZonesAppliedLayoutsFile, jsonString);
AppliedLayouts serializer = new AppliedLayouts();
IOUtils iOUtils = new IOUtils();
iOUtils.WriteFile(serializer.File, serializer.Serialize(layouts));
}
catch (Exception ex)
{
@ -517,8 +310,8 @@ namespace FancyZonesEditor.Utils
public void SerializeLayoutHotkeys()
{
LayoutHotkeysWrapper hotkeys = new LayoutHotkeysWrapper { };
hotkeys.LayoutHotkeys = new List<LayoutHotkeyWrapper>();
LayoutHotkeys.LayoutHotkeysWrapper hotkeys = new LayoutHotkeys.LayoutHotkeysWrapper { };
hotkeys.LayoutHotkeys = new List<LayoutHotkeys.LayoutHotkeyWrapper>();
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
{
@ -526,7 +319,7 @@ namespace FancyZonesEditor.Utils
{
try
{
LayoutHotkeyWrapper wrapper = new LayoutHotkeyWrapper
LayoutHotkeys.LayoutHotkeyWrapper wrapper = new LayoutHotkeys.LayoutHotkeyWrapper
{
Key = int.Parse(pair.Key, CultureInfo.CurrentCulture),
LayoutId = pair.Value,
@ -543,8 +336,9 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(hotkeys, _options);
_fileSystem.File.WriteAllText(FancyZonesLayoutHotkeysFile, jsonString);
LayoutHotkeys serializer = new LayoutHotkeys();
IOUtils iOUtils = new IOUtils();
iOUtils.WriteFile(serializer.File, serializer.Serialize(hotkeys));
}
catch (Exception ex)
{
@ -555,12 +349,12 @@ namespace FancyZonesEditor.Utils
public void SerializeLayoutTemplates()
{
TemplateLayoutsListWrapper templates = new TemplateLayoutsListWrapper { };
templates.LayoutTemplates = new List<TemplateLayoutWrapper>();
LayoutTemplates.TemplateLayoutsListWrapper templates = new LayoutTemplates.TemplateLayoutsListWrapper { };
templates.LayoutTemplates = new List<LayoutTemplates.TemplateLayoutWrapper>();
foreach (LayoutModel layout in MainWindowSettingsModel.TemplateModels)
{
TemplateLayoutWrapper wrapper = new TemplateLayoutWrapper
LayoutTemplates.TemplateLayoutWrapper wrapper = new LayoutTemplates.TemplateLayoutWrapper
{
Type = LayoutTypeToJsonTag(layout.Type),
SensitivityRadius = layout.SensitivityRadius,
@ -578,8 +372,9 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(templates, _options);
_fileSystem.File.WriteAllText(FancyZonesLayoutTemplatesFile, jsonString);
LayoutTemplates serializer = new LayoutTemplates();
IOUtils iOUtils = new IOUtils();
iOUtils.WriteFile(serializer.File, serializer.Serialize(templates));
}
catch (Exception ex)
{
@ -590,8 +385,9 @@ namespace FancyZonesEditor.Utils
public void SerializeCustomLayouts()
{
CustomLayoutListWrapper layouts = new CustomLayoutListWrapper { };
layouts.CustomLayouts = new List<CustomLayoutWrapper>();
CustomLayouts serializer = new CustomLayouts();
CustomLayouts.CustomLayoutListWrapper layouts = new CustomLayouts.CustomLayoutListWrapper { };
layouts.CustomLayouts = new List<CustomLayouts.CustomLayoutWrapper>();
foreach (LayoutModel layout in MainWindowSettingsModel.CustomModels)
{
@ -609,17 +405,17 @@ namespace FancyZonesEditor.Utils
canvasRect = App.Overlay.WorkArea;
}
var wrapper = new CanvasInfoWrapper
var wrapper = new CustomLayouts.CanvasInfoWrapper
{
RefWidth = (int)canvasRect.Width,
RefHeight = (int)canvasRect.Height,
Zones = new List<CanvasInfoWrapper.CanvasZoneWrapper>(),
Zones = new List<CustomLayouts.CanvasInfoWrapper.CanvasZoneWrapper>(),
SensitivityRadius = canvasLayout.SensitivityRadius,
};
foreach (var zone in canvasLayout.Zones)
{
wrapper.Zones.Add(new CanvasInfoWrapper.CanvasZoneWrapper
wrapper.Zones.Add(new CustomLayouts.CanvasInfoWrapper.CanvasZoneWrapper
{
X = zone.X,
Y = zone.Y,
@ -628,8 +424,7 @@ namespace FancyZonesEditor.Utils
});
}
string json = JsonSerializer.Serialize(wrapper, _options);
info = JsonSerializer.Deserialize<JsonElement>(json);
info = serializer.ToJsonElement(wrapper);
}
else if (layout is GridLayoutModel)
{
@ -646,7 +441,7 @@ namespace FancyZonesEditor.Utils
}
}
var wrapper = new GridInfoWrapper
var wrapper = new CustomLayouts.GridInfoWrapper
{
Rows = gridLayout.Rows,
Columns = gridLayout.Columns,
@ -658,8 +453,7 @@ namespace FancyZonesEditor.Utils
SensitivityRadius = gridLayout.SensitivityRadius,
};
string json = JsonSerializer.Serialize(wrapper, _options);
info = JsonSerializer.Deserialize<JsonElement>(json);
info = serializer.ToJsonElement(wrapper);
}
else
{
@ -667,7 +461,7 @@ namespace FancyZonesEditor.Utils
continue;
}
CustomLayoutWrapper customLayout = new CustomLayoutWrapper
CustomLayouts.CustomLayoutWrapper customLayout = new CustomLayouts.CustomLayoutWrapper
{
Uuid = layout.Uuid,
Name = layout.Name,
@ -680,8 +474,8 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(layouts, _options);
_fileSystem.File.WriteAllText(FancyZonesCustomLayoutsFile, jsonString);
IOUtils iOUtils = new IOUtils();
iOUtils.WriteFile(serializer.File, serializer.Serialize(layouts));
}
catch (Exception ex)
{
@ -692,14 +486,14 @@ namespace FancyZonesEditor.Utils
public void SerializeDefaultLayouts()
{
DefaultLayoutsListWrapper layouts = new DefaultLayoutsListWrapper { };
layouts.DefaultLayouts = new List<DefaultLayoutWrapper>();
DefaultLayouts.DefaultLayoutsListWrapper layouts = new DefaultLayouts.DefaultLayoutsListWrapper { };
layouts.DefaultLayouts = new List<DefaultLayouts.DefaultLayoutWrapper>();
foreach (LayoutModel layout in MainWindowSettingsModel.TemplateModels)
{
if (layout.IsHorizontalDefault || layout.IsVerticalDefault)
{
DefaultLayoutWrapper.LayoutWrapper layoutWrapper = new DefaultLayoutWrapper.LayoutWrapper
DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper layoutWrapper = new DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper
{
Uuid = string.Empty,
Type = LayoutTypeToJsonTag(layout.Type),
@ -716,7 +510,7 @@ namespace FancyZonesEditor.Utils
// can be both horizontal and vertical, so check separately
if (layout.IsHorizontalDefault)
{
DefaultLayoutWrapper wrapper = new DefaultLayoutWrapper
DefaultLayouts.DefaultLayoutWrapper wrapper = new DefaultLayouts.DefaultLayoutWrapper
{
MonitorConfiguration = MonitorConfigurationTypeToJsonTag(MonitorConfigurationType.Horizontal),
Layout = layoutWrapper,
@ -727,7 +521,7 @@ namespace FancyZonesEditor.Utils
if (layout.IsVerticalDefault)
{
DefaultLayoutWrapper wrapper = new DefaultLayoutWrapper
DefaultLayouts.DefaultLayoutWrapper wrapper = new DefaultLayouts.DefaultLayoutWrapper
{
MonitorConfiguration = MonitorConfigurationTypeToJsonTag(MonitorConfigurationType.Vertical),
Layout = layoutWrapper,
@ -742,7 +536,7 @@ namespace FancyZonesEditor.Utils
{
if (layout.IsHorizontalDefault || layout.IsVerticalDefault)
{
DefaultLayoutWrapper.LayoutWrapper layoutWrapper = new DefaultLayoutWrapper.LayoutWrapper
DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper layoutWrapper = new DefaultLayouts.DefaultLayoutWrapper.LayoutWrapper
{
Uuid = layout.Uuid,
Type = LayoutTypeToJsonTag(LayoutType.Custom),
@ -757,7 +551,7 @@ namespace FancyZonesEditor.Utils
// can be both horizontal and vertical, so check separately
if (layout.IsHorizontalDefault)
{
DefaultLayoutWrapper wrapper = new DefaultLayoutWrapper
DefaultLayouts.DefaultLayoutWrapper wrapper = new DefaultLayouts.DefaultLayoutWrapper
{
MonitorConfiguration = MonitorConfigurationTypeToJsonTag(MonitorConfigurationType.Horizontal),
Layout = layoutWrapper,
@ -768,7 +562,7 @@ namespace FancyZonesEditor.Utils
if (layout.IsVerticalDefault)
{
DefaultLayoutWrapper wrapper = new DefaultLayoutWrapper
DefaultLayouts.DefaultLayoutWrapper wrapper = new DefaultLayouts.DefaultLayoutWrapper
{
MonitorConfiguration = MonitorConfigurationTypeToJsonTag(MonitorConfigurationType.Vertical),
Layout = layoutWrapper,
@ -781,8 +575,9 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(layouts, _options);
_fileSystem.File.WriteAllText(FancyZonesDefaultLayoutsFile, jsonString);
DefaultLayouts serializer = new DefaultLayouts();
IOUtils iOUtils = new IOUtils();
iOUtils.WriteFile(serializer.File, serializer.Serialize(layouts));
}
catch (Exception ex)
{
@ -791,36 +586,7 @@ namespace FancyZonesEditor.Utils
}
}
private string ReadFile(string fileName)
{
Logger.LogTrace();
var attempts = 0;
while (attempts < 10)
{
try
{
using (Stream inputStream = _fileSystem.File.Open(fileName, FileMode.Open))
using (StreamReader reader = new StreamReader(inputStream))
{
string data = reader.ReadToEnd();
inputStream.Close();
return data;
}
}
catch (Exception)
{
Logger.LogError("File reading error, retry");
Task.Delay(10).Wait();
}
attempts++;
}
return string.Empty;
}
private bool SetAppliedLayouts(List<AppliedLayoutWrapper> layouts)
private bool SetAppliedLayouts(List<AppliedLayouts.AppliedLayoutWrapper> layouts)
{
Logger.LogTrace();
@ -907,7 +673,7 @@ namespace FancyZonesEditor.Utils
return result;
}
private bool SetCustomLayouts(List<CustomLayoutWrapper> customLayouts)
private bool SetCustomLayouts(List<CustomLayouts.CustomLayoutWrapper> customLayouts)
{
Logger.LogTrace();
@ -960,7 +726,7 @@ namespace FancyZonesEditor.Utils
return result;
}
private bool SetTemplateLayouts(List<TemplateLayoutWrapper> templateLayouts)
private bool SetTemplateLayouts(List<LayoutTemplates.TemplateLayoutWrapper> templateLayouts)
{
Logger.LogTrace();
@ -989,7 +755,7 @@ namespace FancyZonesEditor.Utils
return true;
}
private bool SetLayoutHotkeys(LayoutHotkeysWrapper layoutHotkeys)
private bool SetLayoutHotkeys(LayoutHotkeys.LayoutHotkeysWrapper layoutHotkeys)
{
Logger.LogTrace();
@ -1002,7 +768,7 @@ namespace FancyZonesEditor.Utils
return true;
}
private bool SetDefaultLayouts(List<DefaultLayoutWrapper> layouts)
private bool SetDefaultLayouts(List<DefaultLayouts.DefaultLayoutWrapper> layouts)
{
Logger.LogTrace();
@ -1059,9 +825,10 @@ namespace FancyZonesEditor.Utils
return true;
}
private CanvasLayoutModel ParseCanvasInfo(CustomLayoutWrapper wrapper)
private CanvasLayoutModel ParseCanvasInfo(CustomLayouts.CustomLayoutWrapper wrapper)
{
var info = JsonSerializer.Deserialize<CanvasInfoWrapper>(wrapper.Info.GetRawText(), _options);
CustomLayouts deserializer = new CustomLayouts();
var info = deserializer.CanvasFromJsonElement(wrapper.Info.GetRawText());
var zones = new List<Int32Rect>();
foreach (var zone in info.Zones)
@ -1081,9 +848,10 @@ namespace FancyZonesEditor.Utils
return layout;
}
private GridLayoutModel ParseGridInfo(CustomLayoutWrapper wrapper)
private GridLayoutModel ParseGridInfo(CustomLayouts.CustomLayoutWrapper wrapper)
{
var info = JsonSerializer.Deserialize<GridInfoWrapper>(wrapper.Info.GetRawText(), _options);
CustomLayouts deserializer = new CustomLayouts();
var info = deserializer.GridFromJsonElement(wrapper.Info.GetRawText());
// Check if rows and columns are valid
if (info.Rows <= 0 || info.Columns <= 0)