[FancyZones] Split zones-settings: layout hotkeys (#15514)

This commit is contained in:
Seraphima Zykova
2022-01-17 11:50:24 +03:00
committed by GitHub
parent a96187bd04
commit ba431c5bfd
28 changed files with 524 additions and 153 deletions

View File

@@ -9,7 +9,7 @@ using System.Runtime.CompilerServices;
namespace FancyZonesEditor.Models
{
public class QuickKeysModel : INotifyPropertyChanged
public class LayoutHotkeysModel : INotifyPropertyChanged
{
public SortedDictionary<string, string> SelectedKeys { get; } = new SortedDictionary<string, string>()
{
@@ -26,7 +26,7 @@ namespace FancyZonesEditor.Models
{ "9", string.Empty },
};
public QuickKeysModel()
public LayoutHotkeysModel()
{
}

View File

@@ -165,7 +165,7 @@ namespace FancyZonesEditor.Models
get
{
List<string> result = new List<string>();
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
{
if (string.IsNullOrEmpty(pair.Value) || pair.Value == Uuid)
{
@@ -195,11 +195,11 @@ namespace FancyZonesEditor.Models
if (intValue != -1)
{
MainWindowSettingsModel.QuickKeys.SelectKey(value, Uuid);
MainWindowSettingsModel.LayoutHotkeys.SelectKey(value, Uuid);
}
else
{
MainWindowSettingsModel.QuickKeys.FreeKey(prev);
MainWindowSettingsModel.LayoutHotkeys.FreeKey(prev);
}
FirePropertyChanged(nameof(QuickKey));
@@ -253,7 +253,7 @@ namespace FancyZonesEditor.Models
{
if (_quickKey != -1)
{
MainWindowSettingsModel.QuickKeys.FreeKey(QuickKey);
MainWindowSettingsModel.LayoutHotkeys.FreeKey(QuickKey);
}
var customModels = MainWindowSettingsModel.CustomModels;
@@ -298,9 +298,9 @@ namespace FancyZonesEditor.Models
PersistData();
}
public void QuickSwitchKeys_PropertyChanged(object sender, PropertyChangedEventArgs e)
public void LayoutHotkeys_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
{
if (pair.Value == Uuid)
{

View File

@@ -143,7 +143,7 @@ namespace FancyZonesEditor
{
foreach (LayoutModel model in _customModels)
{
QuickKeys.PropertyChanged -= model.QuickSwitchKeys_PropertyChanged;
LayoutHotkeys.PropertyChanged -= model.LayoutHotkeys_PropertyChanged;
}
_customModels.Clear();
@@ -151,7 +151,7 @@ namespace FancyZonesEditor
foreach (LayoutModel model in _customModels)
{
QuickKeys.PropertyChanged += model.QuickSwitchKeys_PropertyChanged;
LayoutHotkeys.PropertyChanged += model.LayoutHotkeys_PropertyChanged;
}
}
}
@@ -166,7 +166,7 @@ namespace FancyZonesEditor
}
}
public static QuickKeysModel QuickKeys { get; } = new QuickKeysModel();
public static LayoutHotkeysModel LayoutHotkeys { get; } = new LayoutHotkeysModel();
public LayoutModel SelectedModel
{

View File

@@ -384,6 +384,15 @@ namespace FancyZonesEditor.Properties {
}
}
/// <summary>
/// Looks up a localized string similar to An error occurred while parsing layout hotkeys..
/// </summary>
public static string Error_Parsing_Layout_Hotkeys_Message {
get {
return ResourceManager.GetString("Error_Parsing_Layout_Hotkeys_Message", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to A layout that contained invalid data has been removed..
/// </summary>
@@ -394,7 +403,7 @@ namespace FancyZonesEditor.Properties {
}
/// <summary>
/// Looks up a localized string similar to Editor settings parsing error..
/// Looks up a localized string similar to Editor data parsing error..
/// </summary>
public static string Error_Parsing_Zones_Settings_Title {
get {

View File

@@ -305,7 +305,7 @@
<comment>A tooltip on a button that allows the user to delete a zone</comment>
</data>
<data name="Error_Parsing_Zones_Settings_Title" xml:space="preserve">
<value>Editor settings parsing error.</value>
<value>Editor data parsing error.</value>
</data>
<data name="Error_Parsing_Zones_Settings_Message" xml:space="preserve">
<value>A layout that contained invalid data has been removed.</value>
@@ -383,4 +383,7 @@
<data name="OpenSettings" xml:space="preserve">
<value>Open settings</value>
</data>
<data name="Error_Parsing_Layout_Hotkeys_Message" xml:space="preserve">
<value>An error occurred while parsing layout hotkeys.</value>
</data>
</root>

View File

@@ -29,6 +29,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 ParamsFile = "\\Microsoft\\PowerToys\\FancyZones\\editor-parameters.json";
// Non-localizable string: Multi-monitor id
@@ -49,6 +50,8 @@ namespace FancyZonesEditor.Utils
public string FancyZonesSettingsFile { get; private set; }
public string FancyZonesLayoutHotkeysFile { get; private set; }
public string FancyZonesEditorParamsFile { get; private set; }
private enum CmdArgs
@@ -193,12 +196,18 @@ namespace FancyZonesEditor.Utils
public int SensitivityRadius { get; set; }
}
// zones-settings: quick-layout-keys-wrapper
private struct QuickLayoutKeysWrapper
// layout-hotkeys: layout-hotkeys-wrapper
private struct LayoutHotkeyWrapper
{
public int Key { get; set; }
public string Uuid { get; set; }
public string LayoutId { get; set; }
}
// layout-hotkeys: layout-hotkeys-wrapper
private struct LayoutHotkeysWrapper
{
public List<LayoutHotkeyWrapper> LayoutHotkeys { get; set; }
}
// zones-settings
@@ -209,8 +218,6 @@ namespace FancyZonesEditor.Utils
public List<CustomLayoutWrapper> CustomZoneSets { get; set; }
public List<TemplateLayoutWrapper> Templates { get; set; }
public List<QuickLayoutKeysWrapper> QuickLayoutKeys { get; set; }
}
private struct EditorParams
@@ -242,6 +249,7 @@ namespace FancyZonesEditor.Utils
{
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
FancyZonesSettingsFile = localAppDataDir + ZonesSettingsFile;
FancyZonesLayoutHotkeysFile = localAppDataDir + LayoutHotkeysFile;
FancyZonesEditorParamsFile = localAppDataDir + ParamsFile;
}
@@ -516,7 +524,6 @@ namespace FancyZonesEditor.Utils
bool devicesParsingResult = SetDevices(zoneSettings.Devices);
bool customZonesParsingResult = SetCustomLayouts(zoneSettings.CustomZoneSets);
bool templatesParsingResult = SetTemplateLayouts(zoneSettings.Templates);
bool quickLayoutSwitchKeysParsingResult = SetQuickLayoutSwitchKeys(zoneSettings.QuickLayoutKeys);
if (!devicesParsingResult || !customZonesParsingResult)
{
@@ -530,6 +537,51 @@ namespace FancyZonesEditor.Utils
}
}
var parsingHotkeysResult = ParseLayoutHotkeys();
if (!parsingHotkeysResult.Result)
{
return parsingHotkeysResult;
}
return new ParsingResult(true);
}
public ParsingResult ParseLayoutHotkeys()
{
Logger.LogTrace();
if (_fileSystem.File.Exists(FancyZonesLayoutHotkeysFile))
{
LayoutHotkeysWrapper layoutHotkeys;
string dataString = string.Empty;
try
{
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);
}
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);
}
@@ -541,7 +593,6 @@ namespace FancyZonesEditor.Utils
zoneSettings.Devices = new List<DeviceWrapper>();
zoneSettings.CustomZoneSets = new List<CustomLayoutWrapper>();
zoneSettings.Templates = new List<TemplateLayoutWrapper>();
zoneSettings.QuickLayoutKeys = new List<QuickLayoutKeysWrapper>();
// Serialize used devices
foreach (var monitor in App.Overlay.Monitors)
@@ -678,20 +729,38 @@ namespace FancyZonesEditor.Utils
zoneSettings.Templates.Add(wrapper);
}
// Serialize quick layout switch keys
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
try
{
string jsonString = JsonSerializer.Serialize(zoneSettings, _options);
_fileSystem.File.WriteAllText(FancyZonesSettingsFile, jsonString);
}
catch (Exception ex)
{
Logger.LogError("Serialize zone settings error", ex);
App.ShowExceptionMessageBox(Properties.Resources.Error_Applying_Layout, ex);
}
SerializeLayoutHotkeys();
}
private void SerializeLayoutHotkeys()
{
LayoutHotkeysWrapper hotkeys = new LayoutHotkeysWrapper { };
hotkeys.LayoutHotkeys = new List<LayoutHotkeyWrapper>();
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
{
if (!string.IsNullOrEmpty(pair.Value))
{
try
{
QuickLayoutKeysWrapper wrapper = new QuickLayoutKeysWrapper
LayoutHotkeyWrapper wrapper = new LayoutHotkeyWrapper
{
Key = int.Parse(pair.Key),
Uuid = pair.Value,
LayoutId = pair.Value,
};
zoneSettings.QuickLayoutKeys.Add(wrapper);
hotkeys.LayoutHotkeys.Add(wrapper);
}
catch (Exception ex)
{
@@ -702,12 +771,12 @@ namespace FancyZonesEditor.Utils
try
{
string jsonString = JsonSerializer.Serialize(zoneSettings, _options);
_fileSystem.File.WriteAllText(FancyZonesSettingsFile, jsonString);
string jsonString = JsonSerializer.Serialize(hotkeys, _options);
_fileSystem.File.WriteAllText(FancyZonesLayoutHotkeysFile, jsonString);
}
catch (Exception ex)
{
Logger.LogError("Serialize zone settings error", ex);
Logger.LogError("Serialize layout hotkeys error", ex);
App.ShowExceptionMessageBox(Properties.Resources.Error_Applying_Layout, ex);
}
}
@@ -862,19 +931,14 @@ namespace FancyZonesEditor.Utils
return true;
}
private bool SetQuickLayoutSwitchKeys(List<QuickLayoutKeysWrapper> quickSwitchKeys)
private bool SetLayoutHotkeys(LayoutHotkeysWrapper layoutHotkeys)
{
Logger.LogTrace();
if (quickSwitchKeys == null)
MainWindowSettingsModel.LayoutHotkeys.CleanUp();
foreach (var wrapper in layoutHotkeys.LayoutHotkeys)
{
return false;
}
MainWindowSettingsModel.QuickKeys.CleanUp();
foreach (var wrapper in quickSwitchKeys)
{
MainWindowSettingsModel.QuickKeys.SelectKey(wrapper.Key.ToString(), wrapper.Uuid);
MainWindowSettingsModel.LayoutHotkeys.SelectKey(wrapper.Key.ToString(), wrapper.LayoutId);
}
return true;