Skip to content

Commit

Permalink
[FZ Editor] Layout hotkeys: add the schema element to zones-settings.…
Browse files Browse the repository at this point in the history
…json (#10132)
  • Loading branch information
SeraphimaZykova committed Mar 10, 2021
1 parent 72da703 commit de23098
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@

</Grid>
</ScrollViewer>

<Button x:Name="NewLayoutButton"
Click="NewLayoutButton_Click"
HorizontalAlignment="Right"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.Linq;

namespace FancyZonesEditor.Models
{
public class FastAccessKeysModel
{
public static SortedDictionary<int, string> SelectedKeys { get; } = new SortedDictionary<int, string>()
{
{ 0, string.Empty },
{ 1, string.Empty },
{ 2, string.Empty },
{ 3, string.Empty },
{ 4, string.Empty },
{ 5, string.Empty },
{ 6, string.Empty },
{ 7, string.Empty },
{ 8, string.Empty },
{ 9, string.Empty },
};

public FastAccessKeysModel()
{
}

public static void FreeKey(int key)
{
if (SelectedKeys.ContainsKey(key))
{
SelectedKeys[key] = string.Empty;
}
}

public static bool SelectKey(int key, string uuid)
{
if (!SelectedKeys.ContainsKey(key))
{
return false;
}

SelectedKeys[key] = uuid;
return true;
}

public static void CleanUp()
{
var keys = SelectedKeys.Keys.ToList();
foreach (var key in keys)
{
SelectedKeys[key] = string.Empty;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private enum CmdArgs
MonitorTop,
}

// parsing cmd args
private struct NativeMonitorData
{
public string MonitorId { get; set; }
Expand Down Expand Up @@ -87,6 +88,7 @@ public override string ToString()
}
}

// zones-settings: devices
private struct DeviceWrapper
{
public struct ActiveZoneSetWrapper
Expand All @@ -109,6 +111,7 @@ public struct ActiveZoneSetWrapper
public int EditorSensitivityRadius { get; set; }
}

// zones-settings: custom-zone-sets
private class CanvasInfoWrapper
{
public struct CanvasZoneWrapper
Expand All @@ -131,6 +134,7 @@ public struct CanvasZoneWrapper
public int SensitivityRadius { get; set; } = LayoutSettings.DefaultSensitivityRadius;
}

// zones-settings: custom-zone-sets
private class GridInfoWrapper
{
public int Rows { get; set; }
Expand All @@ -150,6 +154,7 @@ private class GridInfoWrapper
public int SensitivityRadius { get; set; } = LayoutSettings.DefaultSensitivityRadius;
}

// zones-settings: custom-zone-sets
private struct CustomLayoutWrapper
{
public string Uuid { get; set; }
Expand All @@ -161,6 +166,7 @@ private struct CustomLayoutWrapper
public JsonElement Info { get; set; } // CanvasInfoWrapper or GridInfoWrapper
}

// zones-settings: templates
private struct TemplateLayoutWrapper
{
public string Type { get; set; }
Expand All @@ -174,13 +180,24 @@ private struct TemplateLayoutWrapper
public int SensitivityRadius { get; set; }
}

// zones-settings: fast-access-layout-keys-wrapper
private struct FastAccessLayoutKeysWrapper
{
public int Key { get; set; }

public string Uuid { get; set; }
}

// zones-settings
private struct ZoneSettingsWrapper
{
public List<DeviceWrapper> Devices { get; set; }

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

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

public List<FastAccessLayoutKeysWrapper> FastAccessLayoutKeys { get; set; }
}

private struct EditorParams
Expand Down Expand Up @@ -528,6 +545,7 @@ public ParsingResult ParseZoneSettings()
bool devicesParsingResult = SetDevices(zoneSettings.Devices);
bool customZonesParsingResult = SetCustomLayouts(zoneSettings.CustomZoneSets);
bool templatesParsingResult = SetTemplateLayouts(zoneSettings.Templates);
bool fastAccessKeysParsingResult = SetFastAccessKeys(zoneSettings.FastAccessLayoutKeys);

if (!devicesParsingResult || !customZonesParsingResult)
{
Expand All @@ -549,6 +567,7 @@ public void SerializeZoneSettings()
zoneSettings.Devices = new List<DeviceWrapper>();
zoneSettings.CustomZoneSets = new List<CustomLayoutWrapper>();
zoneSettings.Templates = new List<TemplateLayoutWrapper>();
zoneSettings.FastAccessLayoutKeys = new List<FastAccessLayoutKeysWrapper>();

// Serialize used devices
foreach (var monitor in App.Overlay.Monitors)
Expand Down Expand Up @@ -685,6 +704,21 @@ public void SerializeZoneSettings()
zoneSettings.Templates.Add(wrapper);
}

// Serialize fast access layout keys
foreach (var pair in FastAccessKeysModel.SelectedKeys)
{
if (pair.Value != string.Empty)
{
FastAccessLayoutKeysWrapper wrapper = new FastAccessLayoutKeysWrapper
{
Key = pair.Key,
Uuid = pair.Value,
};

zoneSettings.FastAccessLayoutKeys.Add(wrapper);
}
}

try
{
string jsonString = JsonSerializer.Serialize(zoneSettings, _options);
Expand Down Expand Up @@ -846,6 +880,22 @@ private bool SetTemplateLayouts(List<TemplateLayoutWrapper> templateLayouts)
return true;
}

private bool SetFastAccessKeys(List<FastAccessLayoutKeysWrapper> fastAccessKeys)
{
if (fastAccessKeys == null)
{
return false;
}

FastAccessKeysModel.CleanUp();
foreach (var wrapper in fastAccessKeys)
{
FastAccessKeysModel.SelectKey(wrapper.Key, wrapper.Uuid);
}

return true;
}

private LayoutType JsonTagToLayoutType(string tag)
{
switch (tag)
Expand Down
8 changes: 8 additions & 0 deletions src/modules/fancyzones/lib/JsonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace NonLocalizable
const wchar_t EditorSpacingStr[] = L"editor-spacing";
const wchar_t EditorZoneCountStr[] = L"editor-zone-count";
const wchar_t EditorSensitivityRadiusStr[] = L"editor-sensitivity-radius";
const wchar_t FastAccessLayoutKeys[] = L"fast-access-layout-keys";
const wchar_t GridStr[] = L"grid";
const wchar_t HeightStr[] = L"height";
const wchar_t HistoryStr[] = L"history";
Expand Down Expand Up @@ -533,13 +534,19 @@ namespace JSONHelpers

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

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

if (before.has_value() && before->HasKey(NonLocalizable::FastAccessLayoutKeys))
{
fastAccessLayoutKeys = before->GetNamedArray(NonLocalizable::FastAccessLayoutKeys);
}
}
catch (const winrt::hresult_error&)
{
Expand All @@ -549,6 +556,7 @@ namespace JSONHelpers
root.SetNamedValue(NonLocalizable::DevicesStr, JSONHelpers::SerializeDeviceInfos(deviceInfoMap));
root.SetNamedValue(NonLocalizable::CustomZoneSetsStr, JSONHelpers::SerializeCustomZoneSets(customZoneSetsMap));
root.SetNamedValue(NonLocalizable::Templates, templates);
root.SetNamedValue(NonLocalizable::FastAccessLayoutKeys, fastAccessLayoutKeys);

if (!before.has_value() || before.value().Stringify() != root.Stringify())
{
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 @@ -1763,6 +1763,36 @@ namespace FancyZonesUnitTests
compareJsonObjects(expectedJsonObj, actualJson);
}

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

// json with fast-access-layout-keys
json::JsonObject expectedJsonObj;
json::JsonObject fastAccessKeyObj = json::JsonObject::Parse(L"{\"key\": 1, \"uuid\": \"{9AD7FC3A-0A6C-4373-B523-4958FC50C46C}\"}");
json::JsonArray fastAccessKeysArray{};
fastAccessKeysArray.Append(fastAccessKeyObj);
expectedJsonObj.SetNamedValue(L"devices", json::JsonArray{});
expectedJsonObj.SetNamedValue(L"custom-zone-sets", json::JsonArray{});
expectedJsonObj.SetNamedValue(L"fast-access-layout-keys", fastAccessKeysArray);

// write json with fast-access-layout-keys to file
json::to_file(jsonPath, expectedJsonObj);

data.SaveAppZoneHistoryAndZoneSettings();

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

// verify that fast-access-layout-keys 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

0 comments on commit de23098

Please sign in to comment.