mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 02:36:19 +02:00
[FancyZones] Split zones-settings: layout templates (#15588)
This commit is contained in:
@@ -31,7 +31,7 @@ namespace FancyZonesEditor
|
||||
App.Overlay.SetLayoutSettings(App.Overlay.Monitors[App.Overlay.CurrentDesktop], model);
|
||||
}
|
||||
|
||||
App.FancyZonesEditorIO.SerializeZoneSettings();
|
||||
App.FancyZonesEditorIO.SerializeLayoutTemplates();
|
||||
|
||||
Close();
|
||||
}
|
||||
|
||||
@@ -281,6 +281,8 @@ namespace FancyZonesEditor
|
||||
CancelLayoutChanges();
|
||||
|
||||
App.FancyZonesEditorIO.SerializeZoneSettings();
|
||||
App.FancyZonesEditorIO.SerializeLayoutHotkeys();
|
||||
App.FancyZonesEditorIO.SerializeLayoutTemplates();
|
||||
App.Overlay.CloseLayoutWindow();
|
||||
App.Current.Shutdown();
|
||||
}
|
||||
@@ -424,6 +426,8 @@ namespace FancyZonesEditor
|
||||
}
|
||||
|
||||
App.FancyZonesEditorIO.SerializeZoneSettings();
|
||||
App.FancyZonesEditorIO.SerializeLayoutTemplates();
|
||||
App.FancyZonesEditorIO.SerializeLayoutHotkeys();
|
||||
|
||||
// reset selected model
|
||||
Select(_settings.AppliedModel);
|
||||
|
||||
@@ -393,6 +393,15 @@ namespace FancyZonesEditor.Properties {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to An error occurred while parsing template layouts..
|
||||
/// </summary>
|
||||
public static string Error_Parsing_Layout_Templates_Message {
|
||||
get {
|
||||
return ResourceManager.GetString("Error_Parsing_Layout_Templates_Message", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to A layout that contained invalid data has been removed..
|
||||
/// </summary>
|
||||
|
||||
@@ -386,4 +386,7 @@
|
||||
<data name="Error_Parsing_Layout_Hotkeys_Message" xml:space="preserve">
|
||||
<value>An error occurred while parsing layout hotkeys.</value>
|
||||
</data>
|
||||
<data name="Error_Parsing_Layout_Templates_Message" xml:space="preserve">
|
||||
<value>An error occurred while parsing template layouts.</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -30,6 +30,7 @@ namespace FancyZonesEditor.Utils
|
||||
// Non-localizable strings: Files
|
||||
private const string ZonesSettingsFile = "\\Microsoft\\PowerToys\\FancyZones\\zones-settings.json";
|
||||
private const string LayoutHotkeysFile = "\\Microsoft\\PowerToys\\FancyZones\\layout-hotkeys.json";
|
||||
private const string LayoutTemplatesFile = "\\Microsoft\\PowerToys\\FancyZones\\layout-templates.json";
|
||||
private const string ParamsFile = "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json";
|
||||
|
||||
// Non-localizable string: Multi-monitor id
|
||||
@@ -52,6 +53,8 @@ namespace FancyZonesEditor.Utils
|
||||
|
||||
public string FancyZonesLayoutHotkeysFile { get; private set; }
|
||||
|
||||
public string FancyZonesLayoutTemplatesFile { get; private set; }
|
||||
|
||||
public string FancyZonesEditorParamsFile { get; private set; }
|
||||
|
||||
private enum CmdArgs
|
||||
@@ -182,7 +185,7 @@ namespace FancyZonesEditor.Utils
|
||||
public JsonElement Info { get; set; } // CanvasInfoWrapper or GridInfoWrapper
|
||||
}
|
||||
|
||||
// zones-settings: templates
|
||||
// layout-templates: templates
|
||||
private struct TemplateLayoutWrapper
|
||||
{
|
||||
public string Type { get; set; }
|
||||
@@ -196,6 +199,12 @@ namespace FancyZonesEditor.Utils
|
||||
public int SensitivityRadius { get; set; }
|
||||
}
|
||||
|
||||
// layout-templates: layout-templates-wrapper
|
||||
private struct TemplateLayoutsListWrapper
|
||||
{
|
||||
public List<TemplateLayoutWrapper> TemplatesList { get; set; }
|
||||
}
|
||||
|
||||
// layout-hotkeys: layout-hotkeys-wrapper
|
||||
private struct LayoutHotkeyWrapper
|
||||
{
|
||||
@@ -216,8 +225,6 @@ namespace FancyZonesEditor.Utils
|
||||
public List<DeviceWrapper> Devices { get; set; }
|
||||
|
||||
public List<CustomLayoutWrapper> CustomZoneSets { get; set; }
|
||||
|
||||
public List<TemplateLayoutWrapper> Templates { get; set; }
|
||||
}
|
||||
|
||||
private struct EditorParams
|
||||
@@ -250,6 +257,7 @@ namespace FancyZonesEditor.Utils
|
||||
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
|
||||
FancyZonesSettingsFile = localAppDataDir + ZonesSettingsFile;
|
||||
FancyZonesLayoutHotkeysFile = localAppDataDir + LayoutHotkeysFile;
|
||||
FancyZonesLayoutTemplatesFile = localAppDataDir + LayoutTemplatesFile;
|
||||
FancyZonesEditorParamsFile = localAppDataDir + ParamsFile;
|
||||
}
|
||||
|
||||
@@ -523,8 +531,6 @@ namespace FancyZonesEditor.Utils
|
||||
{
|
||||
bool devicesParsingResult = SetDevices(zoneSettings.Devices);
|
||||
bool customZonesParsingResult = SetCustomLayouts(zoneSettings.CustomZoneSets);
|
||||
bool templatesParsingResult = SetTemplateLayouts(zoneSettings.Templates);
|
||||
|
||||
if (!devicesParsingResult || !customZonesParsingResult)
|
||||
{
|
||||
return new ParsingResult(false, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Message, settingsString);
|
||||
@@ -543,6 +549,12 @@ namespace FancyZonesEditor.Utils
|
||||
return parsingHotkeysResult;
|
||||
}
|
||||
|
||||
var parsingTemplatesResult = ParseLayoutTemplates();
|
||||
if (!parsingTemplatesResult.Result)
|
||||
{
|
||||
return parsingTemplatesResult;
|
||||
}
|
||||
|
||||
return new ParsingResult(true);
|
||||
}
|
||||
|
||||
@@ -585,6 +597,46 @@ namespace FancyZonesEditor.Utils
|
||||
return new ParsingResult(true);
|
||||
}
|
||||
|
||||
public ParsingResult ParseLayoutTemplates()
|
||||
{
|
||||
Logger.LogTrace();
|
||||
|
||||
if (_fileSystem.File.Exists(FancyZonesLayoutTemplatesFile))
|
||||
{
|
||||
TemplateLayoutsListWrapper templates;
|
||||
string dataString = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
bool parsingResult = SetTemplateLayouts(templates.TemplatesList);
|
||||
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);
|
||||
}
|
||||
|
||||
public void SerializeZoneSettings()
|
||||
{
|
||||
Logger.LogTrace();
|
||||
@@ -592,7 +644,6 @@ namespace FancyZonesEditor.Utils
|
||||
ZoneSettingsWrapper zoneSettings = new ZoneSettingsWrapper { };
|
||||
zoneSettings.Devices = new List<DeviceWrapper>();
|
||||
zoneSettings.CustomZoneSets = new List<CustomLayoutWrapper>();
|
||||
zoneSettings.Templates = new List<TemplateLayoutWrapper>();
|
||||
|
||||
// Serialize used devices
|
||||
foreach (var monitor in App.Overlay.Monitors)
|
||||
@@ -710,25 +761,6 @@ namespace FancyZonesEditor.Utils
|
||||
zoneSettings.CustomZoneSets.Add(customLayout);
|
||||
}
|
||||
|
||||
// Serialize template layouts
|
||||
foreach (LayoutModel layout in MainWindowSettingsModel.DefaultModels)
|
||||
{
|
||||
TemplateLayoutWrapper wrapper = new TemplateLayoutWrapper
|
||||
{
|
||||
Type = LayoutTypeToJsonTag(layout.Type),
|
||||
SensitivityRadius = layout.SensitivityRadius,
|
||||
ZoneCount = layout.TemplateZoneCount,
|
||||
};
|
||||
|
||||
if (layout is GridLayoutModel grid)
|
||||
{
|
||||
wrapper.ShowSpacing = grid.ShowSpacing;
|
||||
wrapper.Spacing = grid.Spacing;
|
||||
}
|
||||
|
||||
zoneSettings.Templates.Add(wrapper);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(zoneSettings, _options);
|
||||
@@ -739,11 +771,9 @@ namespace FancyZonesEditor.Utils
|
||||
Logger.LogError("Serialize zone settings error", ex);
|
||||
App.ShowExceptionMessageBox(Properties.Resources.Error_Applying_Layout, ex);
|
||||
}
|
||||
|
||||
SerializeLayoutHotkeys();
|
||||
}
|
||||
|
||||
private void SerializeLayoutHotkeys()
|
||||
public void SerializeLayoutHotkeys()
|
||||
{
|
||||
LayoutHotkeysWrapper hotkeys = new LayoutHotkeysWrapper { };
|
||||
hotkeys.LayoutHotkeys = new List<LayoutHotkeyWrapper>();
|
||||
@@ -781,6 +811,41 @@ namespace FancyZonesEditor.Utils
|
||||
}
|
||||
}
|
||||
|
||||
public void SerializeLayoutTemplates()
|
||||
{
|
||||
TemplateLayoutsListWrapper templates = new TemplateLayoutsListWrapper { };
|
||||
templates.TemplatesList = new List<TemplateLayoutWrapper>();
|
||||
|
||||
foreach (LayoutModel layout in MainWindowSettingsModel.DefaultModels)
|
||||
{
|
||||
TemplateLayoutWrapper wrapper = new TemplateLayoutWrapper
|
||||
{
|
||||
Type = LayoutTypeToJsonTag(layout.Type),
|
||||
SensitivityRadius = layout.SensitivityRadius,
|
||||
ZoneCount = layout.TemplateZoneCount,
|
||||
};
|
||||
|
||||
if (layout is GridLayoutModel grid)
|
||||
{
|
||||
wrapper.ShowSpacing = grid.ShowSpacing;
|
||||
wrapper.Spacing = grid.Spacing;
|
||||
}
|
||||
|
||||
templates.TemplatesList.Add(wrapper);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string jsonString = JsonSerializer.Serialize(templates, _options);
|
||||
_fileSystem.File.WriteAllText(FancyZonesLayoutTemplatesFile, jsonString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Serialize layout templates error", ex);
|
||||
App.ShowExceptionMessageBox(Properties.Resources.Error_Applying_Layout, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private string ReadFile(string fileName)
|
||||
{
|
||||
Logger.LogTrace();
|
||||
|
||||
Reference in New Issue
Block a user