Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ public bool IsCustomLayoutActive

public MainWindowSettingsModel()
{
// Initialize the five default layout models: Blank, Focus, Columns, Rows, Grid, and PriorityGrid
DefaultModels = new List<LayoutModel>(6);

// Initialize default layout models: Blank, Focus, Columns, Rows, Grid, and PriorityGrid
_blankModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Blank, LayoutType.Blank);
_blankModel.TemplateZoneCount = 0;
_blankModel.SensitivityRadius = 0;
DefaultModels.Add(_blankModel);

_focusModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Focus, LayoutType.Focus);
Expand Down Expand Up @@ -137,7 +136,7 @@ public bool IsCtrlKeyPressed

private bool _isCtrlKeyPressed;

public IList<LayoutModel> DefaultModels { get; }
public static IList<LayoutModel> DefaultModels { get; } = new List<LayoutModel>(6);

public static ObservableCollection<LayoutModel> CustomModels
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,26 @@ private struct CustomLayoutWrapper
public JsonElement Info { get; set; } // CanvasInfoWrapper or GridInfoWrapper
}

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

private struct ZoneSettingsWrapper
{
public List<DeviceWrapper> Devices { get; set; }

public List<CustomLayoutWrapper> CustomZoneSets { get; set; }

public List<TemplateLayoutWrapper> Templates { get; set; }
}

private struct EditorParams
Expand Down Expand Up @@ -512,6 +527,7 @@ public ParsingResult ParseZoneSettings()
{
bool devicesParsingResult = SetDevices(zoneSettings.Devices);
bool customZonesParsingResult = SetCustomLayouts(zoneSettings.CustomZoneSets);
bool templatesParsingResult = SetTemplateLayouts(zoneSettings.Templates);

if (!devicesParsingResult || !customZonesParsingResult)
{
Expand All @@ -532,6 +548,7 @@ public void SerializeZoneSettings()
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)
Expand Down Expand Up @@ -649,6 +666,25 @@ public void SerializeZoneSettings()
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);
Expand All @@ -671,6 +707,11 @@ private string ReadFile(string fileName)

private bool SetDevices(List<DeviceWrapper> devices)
{
if (devices == null)
{
return false;
}

bool result = true;
var monitors = App.Overlay.Monitors;
foreach (var device in devices)
Expand Down Expand Up @@ -713,6 +754,11 @@ private bool SetDevices(List<DeviceWrapper> devices)

private bool SetCustomLayouts(List<CustomLayoutWrapper> customLayouts)
{
if (customLayouts == null)
{
return false;
}

MainWindowSettingsModel.CustomModels.Clear();
bool result = true;

Expand Down Expand Up @@ -768,6 +814,38 @@ private bool SetCustomLayouts(List<CustomLayoutWrapper> customLayouts)
return result;
}

private bool SetTemplateLayouts(List<TemplateLayoutWrapper> templateLayouts)
{
if (templateLayouts == null)
{
return false;
}

foreach (var wrapper in templateLayouts)
{
var type = JsonTagToLayoutType(wrapper.Type);

foreach (var layout in MainWindowSettingsModel.DefaultModels)
{
if (layout.Type == type)
{
layout.SensitivityRadius = wrapper.SensitivityRadius;
layout.TemplateZoneCount = wrapper.ZoneCount;

if (layout is GridLayoutModel grid)
{
grid.ShowSpacing = wrapper.ShowSpacing;
grid.Spacing = wrapper.Spacing;
}

layout.InitTemplateZones();
}
}
}

return true;
}

private LayoutType JsonTagToLayoutType(string tag)
{
switch (tag)
Expand Down
20 changes: 18 additions & 2 deletions src/modules/fancyzones/lib/JsonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace NonLocalizable
const wchar_t SensitivityRadius[] = L"sensitivity-radius";
const wchar_t ShowSpacing[] = L"show-spacing";
const wchar_t Spacing[] = L"spacing";
const wchar_t Templates[] = L"templates";
const wchar_t TypeStr[] = L"type";
const wchar_t UuidStr[] = L"uuid";
const wchar_t WidthStr[] = L"width";
Expand Down Expand Up @@ -533,14 +534,29 @@ namespace JSONHelpers
const TAppZoneHistoryMap& appZoneHistoryMap)

{
auto before = json::from_file(zonesSettingsFileName);

json::JsonObject root{};
json::JsonObject appZoneHistoryRoot{};
json::JsonArray templates{};

try
{
if (before.has_value() && before->HasKey(NonLocalizable::Templates))
{
templates = before->GetNamedArray(NonLocalizable::Templates);
}
}
catch (const winrt::hresult_error&)
{

}

appZoneHistoryRoot.SetNamedValue(NonLocalizable::AppZoneHistoryStr, JSONHelpers::SerializeAppZoneHistory(appZoneHistoryMap));
root.SetNamedValue(NonLocalizable::DevicesStr, JSONHelpers::SerializeDeviceInfos(deviceInfoMap));
root.SetNamedValue(NonLocalizable::CustomZoneSetsStr, JSONHelpers::SerializeCustomZoneSets(customZoneSetsMap));

auto before = json::from_file(zonesSettingsFileName);
root.SetNamedValue(NonLocalizable::Templates, templates);

if (!before.has_value() || before.value().Stringify() != root.Stringify())
{
Trace::FancyZones::DataChanged();
Expand Down
30 changes: 30 additions & 0 deletions src/modules/fancyzones/tests/UnitTests/JsonHelpers.Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,36 @@ namespace FancyZonesUnitTests
Assert::IsTrue(actual);
}

TEST_METHOD (SaveFancyZonesDataWithTemplates)
{
FancyZonesData data;
data.SetSettingsModulePath(m_moduleName);
const auto& jsonPath = data.zonesSettingsFileName;

// json with templates
json::JsonObject expectedJsonObj;
json::JsonObject templateObj = json::JsonObject::Parse(L"{\"type\": \"focus\", \"show-spacing\": false, \"spacing\": 15, \"zone-count\": 7, \"sensitivity-radius\": 25}");
json::JsonArray templatesArray{};
templatesArray.Append(templateObj);
expectedJsonObj.SetNamedValue(L"devices", json::JsonArray{});
expectedJsonObj.SetNamedValue(L"custom-zone-sets", json::JsonArray{});
expectedJsonObj.SetNamedValue(L"templates", templatesArray);

// write json with templates to file
json::to_file(jsonPath, expectedJsonObj);

data.SaveFancyZonesData();

// verify that file was written successfully
Assert::IsTrue(std::filesystem::exists(jsonPath));

// verify that templates were not changed after calling SaveFancyZonesData()
std::wstring str;
std::wifstream { jsonPath, std::ios::binary } >> str;
json::JsonObject actualJson = json::JsonObject::Parse(str);
compareJsonObjects(expectedJsonObj, actualJson);
}

TEST_METHOD (AppLastZoneIndex)
{
const std::wstring deviceId = L"device-id";
Expand Down