diff --git a/PowerToys.sln b/PowerToys.sln index ab6bba5df5..9f8aac046c 100644 --- a/PowerToys.sln +++ b/PowerToys.sln @@ -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} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs new file mode 100644 index 0000000000..188e834b37 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/AppliedLayouts.cs @@ -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 + { + 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 AppliedLayouts { get; set; } + } + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs new file mode 100644 index 0000000000..92f31892aa --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/CustomLayouts.cs @@ -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 + { + 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 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 RowsPercentage { get; set; } + + public List 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 CustomLayouts { get; set; } + } + + public JsonElement ToJsonElement(CanvasInfoWrapper info) + { + string json = JsonSerializer.Serialize(info, this.JsonOptions); + return JsonSerializer.Deserialize(json); + } + + public JsonElement ToJsonElement(GridInfoWrapper info) + { + string json = JsonSerializer.Serialize(info, this.JsonOptions); + return JsonSerializer.Deserialize(json); + } + + public CanvasInfoWrapper CanvasFromJsonElement(string json) + { + return JsonSerializer.Deserialize(json, this.JsonOptions); + } + + public GridInfoWrapper GridFromJsonElement(string json) + { + return JsonSerializer.Deserialize(json, this.JsonOptions); + } + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs new file mode 100644 index 0000000000..307d33adc1 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/DefaultLayouts.cs @@ -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 + { + 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 DefaultLayouts { get; set; } + } + } +} diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorData.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs similarity index 61% rename from src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorData.cs rename to src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs index 1f8749e6fe..913881a72e 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorData.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorData`1.cs @@ -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 { - 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(data, JsonOptions); + } + + public string Serialize(T data) + { + return JsonSerializer.Serialize(data, JsonOptions); } } } diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs new file mode 100644 index 0000000000..e973c6070f --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/EditorParameters.cs @@ -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 + { + 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 Monitors { get; set; } + } + + public EditorParameters() + : base() + { + } + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutDefaultSettings.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutDefaultSettings.cs new file mode 100644 index 0000000000..8045113566 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutDefaultSettings.cs @@ -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; + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs new file mode 100644 index 0000000000..4b0ad7672e --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutHotkeys.cs @@ -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 + { + 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 LayoutHotkeys { get; set; } + } + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs new file mode 100644 index 0000000000..2b7cfdd551 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Data/LayoutTemplates.cs @@ -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 + { + 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 LayoutTemplates { get; set; } + } + } +} diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/FancyZonesEditorCommon.csproj b/src/modules/fancyzones/FancyZonesEditorCommon/FancyZonesEditorCommon.csproj new file mode 100644 index 0000000000..954c17d444 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/FancyZonesEditorCommon.csproj @@ -0,0 +1,21 @@ + + + + + net7.0-windows + win-x64;win-arm64 + $(Version).0 + Microsoft Corporation + PowerToys + PowerToys FancyZonesEditorCommon + PowerToys.FancyZonesEditorCommon + + + + + + + + ..\..\..\..\$(Platform)\$(Configuration)\FancyZonesEditorCommon\ + + diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/DashCaseNamingPolicy.cs similarity index 89% rename from src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs rename to src/modules/fancyzones/FancyZonesEditorCommon/Utils/DashCaseNamingPolicy.cs index b88f25d7e4..529ff11d7c 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/DashCaseNamingPolicy.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/DashCaseNamingPolicy.cs @@ -4,9 +4,7 @@ using System.Text.Json; -using FancyZonesEditor.Utils; - -namespace FancyZonesEditor +namespace FancyZonesEditorCommon.Utils { public class DashCaseNamingPolicy : JsonNamingPolicy { diff --git a/src/modules/fancyzones/FancyZonesEditorCommon/Utils/IOUtils.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/IOUtils.cs new file mode 100644 index 0000000000..f48a341695 --- /dev/null +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/IOUtils.cs @@ -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; + } + } +} diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/StringUtils.cs similarity index 94% rename from src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs rename to src/modules/fancyzones/FancyZonesEditorCommon/Utils/StringUtils.cs index 027003477b..6584559faf 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/StringUtils.cs +++ b/src/modules/fancyzones/FancyZonesEditorCommon/Utils/StringUtils.cs @@ -4,7 +4,7 @@ using System.Linq; -namespace FancyZonesEditor.Utils +namespace FancyZonesEditorCommon.Utils { public static class StringUtils { diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorParameters.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorParameters.cs deleted file mode 100644 index baa1a5cb13..0000000000 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Data/EditorParameters.cs +++ /dev/null @@ -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 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(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); - } - } -} diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj index dae02ff5ba..98b9c0874c 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj +++ b/src/modules/fancyzones/editor/FancyZonesEditor/FancyZonesEditor.csproj @@ -69,6 +69,7 @@ + diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs index 537dd741d1..9808c1b2e1 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs @@ -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() diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs index ef2b2a4617..028bcd26b1 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs @@ -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; } } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutSettings.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutSettings.cs index dd0f7dc53c..dfc3258745 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutSettings.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutSettings.cs @@ -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; } } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs index deee65388c..21dd44a195 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.Designer.cs @@ -487,6 +487,15 @@ namespace FancyZonesEditor.Properties { } } + /// + /// Looks up a localized string similar to An error occurred while parsing editor parameters.. + /// + public static string Error_Parsing_Editor_Parameters_Message { + get { + return ResourceManager.GetString("Error_Parsing_Editor_Parameters_Message", resourceCulture); + } + } + /// /// Looks up a localized string similar to An error occurred while parsing layout hotkeys.. /// diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx index f98e90b9ba..265866eba0 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Properties/Resources.resx @@ -435,4 +435,7 @@ Set layout as a default for vertical monitor orientation + + An error occurred while parsing editor parameters. + \ No newline at end of file diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs index 75988fc0ab..af19d5fce4 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Utils/FancyZonesEditorIO.cs @@ -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 _unusedLayouts = new List(); - - 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 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 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 RowsPercentage { get; set; } - - public List 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 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 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 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 DefaultLayouts { get; set; } - } + private List _unusedLayouts = new List(); 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(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(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(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(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(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(); + AppliedLayouts.AppliedLayoutsListWrapper layouts = new AppliedLayouts.AppliedLayoutsListWrapper { }; + layouts.AppliedLayouts = new List(); // 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(); + LayoutHotkeys.LayoutHotkeysWrapper hotkeys = new LayoutHotkeys.LayoutHotkeysWrapper { }; + hotkeys.LayoutHotkeys = new List(); 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(); + LayoutTemplates.TemplateLayoutsListWrapper templates = new LayoutTemplates.TemplateLayoutsListWrapper { }; + templates.LayoutTemplates = new List(); 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(); + CustomLayouts serializer = new CustomLayouts(); + CustomLayouts.CustomLayoutListWrapper layouts = new CustomLayouts.CustomLayoutListWrapper { }; + layouts.CustomLayouts = new List(); 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(), + Zones = new List(), 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(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(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(); + DefaultLayouts.DefaultLayoutsListWrapper layouts = new DefaultLayouts.DefaultLayoutsListWrapper { }; + layouts.DefaultLayouts = new List(); 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 layouts) + private bool SetAppliedLayouts(List layouts) { Logger.LogTrace(); @@ -907,7 +673,7 @@ namespace FancyZonesEditor.Utils return result; } - private bool SetCustomLayouts(List customLayouts) + private bool SetCustomLayouts(List customLayouts) { Logger.LogTrace(); @@ -960,7 +726,7 @@ namespace FancyZonesEditor.Utils return result; } - private bool SetTemplateLayouts(List templateLayouts) + private bool SetTemplateLayouts(List 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 layouts) + private bool SetDefaultLayouts(List 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(wrapper.Info.GetRawText(), _options); + CustomLayouts deserializer = new CustomLayouts(); + var info = deserializer.CanvasFromJsonElement(wrapper.Info.GetRawText()); var zones = new List(); 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(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)