Skip to content

Commit 1f5f7db

Browse files
[FZ Editor] Serialize/deserialize settings (#8615)
1 parent be675b6 commit 1f5f7db

19 files changed

Lines changed: 411 additions & 1219 deletions

src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.IO;
1010
using System.Linq;
1111
using System.Text;
12+
using System.Threading;
13+
using System.Threading.Tasks;
1214
using System.Windows;
1315
using FancyZonesEditor.Utils;
1416
using ManagedCommon;
@@ -41,6 +43,9 @@ public partial class App : Application
4143
private const string CrashReportDynamicAssemblyTag = "dynamic assembly doesn't have location";
4244
private const string CrashReportLocationNullTag = "location is null or empty";
4345

46+
private const string ParsingErrorReportTag = "Settings parsing error";
47+
private const string ParsingErrorDataTag = "Data: ";
48+
4449
public MainWindowSettingsModel MainWindowSettings { get; }
4550

4651
public static FancyZonesEditorIO FancyZonesEditorIO { get; private set; }
@@ -87,7 +92,55 @@ private void OnStartup(object sender, StartupEventArgs e)
8792
_themeManager = new ThemeManager(this);
8893

8994
FancyZonesEditorIO.ParseCommandLineArguments();
90-
FancyZonesEditorIO.ParseDeviceInfoData();
95+
96+
var parseResult = FancyZonesEditorIO.ParseZoneSettings();
97+
98+
// 10ms retry loop with 1 second timeout
99+
if (!parseResult.Result)
100+
{
101+
CancellationTokenSource ts = new CancellationTokenSource();
102+
Task t = Task.Run(() =>
103+
{
104+
while (!parseResult.Result && !ts.IsCancellationRequested)
105+
{
106+
Task.Delay(10).Wait();
107+
parseResult = FancyZonesEditorIO.ParseZoneSettings();
108+
}
109+
});
110+
111+
try
112+
{
113+
bool result = t.Wait(1000, ts.Token);
114+
ts.Cancel();
115+
}
116+
catch (OperationCanceledException)
117+
{
118+
ts.Dispose();
119+
}
120+
}
121+
122+
// Error message if parsing failed
123+
if (!parseResult.Result)
124+
{
125+
var sb = new StringBuilder();
126+
sb.AppendLine();
127+
sb.AppendLine("## " + ParsingErrorReportTag);
128+
sb.AppendLine();
129+
sb.AppendLine(parseResult.Message);
130+
sb.AppendLine();
131+
sb.AppendLine(ParsingErrorDataTag);
132+
sb.AppendLine(parseResult.MalformedData);
133+
134+
string message = parseResult.Message + Environment.NewLine + Environment.NewLine + FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_User_Choice;
135+
if (MessageBox.Show(message, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Title, MessageBoxButton.YesNo) == MessageBoxResult.No)
136+
{
137+
// TODO: log error
138+
ShowExceptionReportMessageBox(sb.ToString());
139+
Environment.Exit(0);
140+
}
141+
142+
ShowExceptionReportMessageBox(sb.ToString());
143+
}
91144

92145
MainWindowSettingsModel settings = ((App)Current).MainWindowSettings;
93146
settings.UpdateSelectedLayoutModel();

src/modules/fancyzones/editor/FancyZonesEditor/EditorWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected void OnSaveApplyTemplate(object sender, RoutedEventArgs e)
2525
model.Persist();
2626
}
2727

28-
LayoutModel.SerializeDeletedCustomZoneSets();
28+
App.FancyZonesEditorIO.SerializeZoneSettings();
2929

3030
_backToLayoutPicker = false;
3131
Close();

src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void Apply()
196196

197197
private void OnClosing(object sender, EventArgs e)
198198
{
199-
LayoutModel.SerializeDeletedCustomZoneSets();
199+
App.FancyZonesEditorIO.SerializeZoneSettings();
200200
App.Overlay.CloseLayoutWindow();
201201
App.Current.Shutdown();
202202
}
@@ -250,19 +250,18 @@ private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e
250250
private void Reset_Click(object sender, RoutedEventArgs e)
251251
{
252252
var overlay = App.Overlay;
253-
MainWindowSettingsModel settings = ((App)Application.Current).MainWindowSettings;
254253

255254
if (overlay.CurrentDataContext is LayoutModel model)
256255
{
257256
model.IsSelected = false;
258257
model.IsApplied = false;
259258
}
260259

261-
overlay.CurrentLayoutSettings.ZonesetUuid = settings.BlankModel.Uuid;
260+
overlay.CurrentLayoutSettings.ZonesetUuid = MainWindowSettingsModel.BlankModel.Uuid;
262261
overlay.CurrentLayoutSettings.Type = LayoutType.Blank;
263-
overlay.CurrentDataContext = settings.BlankModel;
262+
overlay.CurrentDataContext = MainWindowSettingsModel.BlankModel;
264263

265-
App.FancyZonesEditorIO.SerializeAppliedLayouts();
264+
App.FancyZonesEditorIO.SerializeZoneSettings();
266265

267266
if (!overlay.MultiMonitorMode)
268267
{

src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Text.Json;
87
using System.Windows;
98

109
namespace FancyZonesEditor.Models
@@ -14,7 +13,7 @@ namespace FancyZonesEditor.Models
1413
public class CanvasLayoutModel : LayoutModel
1514
{
1615
// Non-localizable strings
17-
private const string ModelTypeID = "canvas";
16+
public const string ModelTypeID = "canvas";
1817

1918
public Rect CanvasRect { get; private set; }
2019

@@ -83,84 +82,11 @@ public void RestoreTo(CanvasLayoutModel other)
8382
}
8483
}
8584

86-
private struct Zone
87-
{
88-
public int X { get; set; }
89-
90-
public int Y { get; set; }
91-
92-
public int Width { get; set; }
93-
94-
public int Height { get; set; }
95-
}
96-
97-
private struct CanvasLayoutInfo
98-
{
99-
public int RefWidth { get; set; }
100-
101-
public int RefHeight { get; set; }
102-
103-
public Zone[] Zones { get; set; }
104-
}
105-
106-
private struct CanvasLayoutJson
107-
{
108-
public string Uuid { get; set; }
109-
110-
public string Name { get; set; }
111-
112-
public string Type { get; set; }
113-
114-
public CanvasLayoutInfo Info { get; set; }
115-
}
116-
11785
// PersistData
11886
// Implements the LayoutModel.PersistData abstract method
11987
protected override void PersistData()
12088
{
12189
AddCustomLayout(this);
122-
123-
var canvasRect = CanvasRect;
124-
if (canvasRect.Width == 0 || canvasRect.Height == 0)
125-
{
126-
canvasRect = App.Overlay.WorkArea;
127-
}
128-
129-
CanvasLayoutInfo layoutInfo = new CanvasLayoutInfo
130-
{
131-
RefWidth = (int)canvasRect.Width,
132-
RefHeight = (int)canvasRect.Height,
133-
Zones = new Zone[Zones.Count],
134-
};
135-
136-
for (int i = 0; i < Zones.Count; ++i)
137-
{
138-
Zone zone = new Zone
139-
{
140-
X = Zones[i].X,
141-
Y = Zones[i].Y,
142-
Width = Zones[i].Width,
143-
Height = Zones[i].Height,
144-
};
145-
146-
layoutInfo.Zones[i] = zone;
147-
}
148-
149-
CanvasLayoutJson jsonObj = new CanvasLayoutJson
150-
{
151-
Uuid = Uuid,
152-
Name = Name,
153-
Type = ModelTypeID,
154-
Info = layoutInfo,
155-
};
156-
JsonSerializerOptions options = new JsonSerializerOptions
157-
{
158-
PropertyNamingPolicy = new DashCaseNamingPolicy(),
159-
};
160-
161-
string jsonString = JsonSerializer.Serialize(jsonObj, options);
162-
AddCustomLayoutJson(JsonSerializer.Deserialize<JsonElement>(jsonString));
163-
SerializeCreatedCustomZonesets();
16490
}
16591
}
16692
}

src/modules/fancyzones/editor/FancyZonesEditor/Models/GridLayoutModel.cs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
using System;
66
using System.Collections.Generic;
7-
using System.Text.Json;
8-
using System.Windows;
97

108
namespace FancyZonesEditor.Models
119
{
@@ -14,7 +12,7 @@ namespace FancyZonesEditor.Models
1412
public class GridLayoutModel : LayoutModel
1513
{
1614
// Non-localizable strings
17-
private const string ModelTypeID = "grid";
15+
public const string ModelTypeID = "grid";
1816

1917
// Rows - number of rows in the Grid
2018
public int Rows
@@ -173,69 +171,11 @@ public void RestoreTo(GridLayoutModel layout)
173171
layout.ColumnPercents = colPercents;
174172
}
175173

176-
private struct GridLayoutInfo
177-
{
178-
public int Rows { get; set; }
179-
180-
public int Columns { get; set; }
181-
182-
public List<int> RowsPercentage { get; set; }
183-
184-
public List<int> ColumnsPercentage { get; set; }
185-
186-
public int[][] CellChildMap { get; set; }
187-
}
188-
189-
private struct GridLayoutJson
190-
{
191-
public string Uuid { get; set; }
192-
193-
public string Name { get; set; }
194-
195-
public string Type { get; set; }
196-
197-
public GridLayoutInfo Info { get; set; }
198-
}
199-
200174
// PersistData
201175
// Implements the LayoutModel.PersistData abstract method
202176
protected override void PersistData()
203177
{
204178
AddCustomLayout(this);
205-
206-
GridLayoutInfo layoutInfo = new GridLayoutInfo
207-
{
208-
Rows = Rows,
209-
Columns = Columns,
210-
RowsPercentage = RowPercents,
211-
ColumnsPercentage = ColumnPercents,
212-
CellChildMap = new int[Rows][],
213-
};
214-
215-
for (int row = 0; row < Rows; row++)
216-
{
217-
layoutInfo.CellChildMap[row] = new int[Columns];
218-
for (int col = 0; col < Columns; col++)
219-
{
220-
layoutInfo.CellChildMap[row][col] = CellChildMap[row, col];
221-
}
222-
}
223-
224-
GridLayoutJson jsonObj = new GridLayoutJson
225-
{
226-
Uuid = Uuid,
227-
Name = Name,
228-
Type = ModelTypeID,
229-
Info = layoutInfo,
230-
};
231-
JsonSerializerOptions options = new JsonSerializerOptions
232-
{
233-
PropertyNamingPolicy = new DashCaseNamingPolicy(),
234-
};
235-
236-
string jsonString = JsonSerializer.Serialize(jsonObj, options);
237-
AddCustomLayoutJson(JsonSerializer.Deserialize<JsonElement>(jsonString));
238-
SerializeCreatedCustomZonesets();
239179
}
240180
}
241181
}

src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
7-
using System.Collections.ObjectModel;
86
using System.ComponentModel;
97
using System.Runtime.CompilerServices;
10-
using System.Text.Json;
118

129
namespace FancyZonesEditor.Models
1310
{
@@ -134,61 +131,36 @@ protected virtual void FirePropertyChanged([CallerMemberName] string propertyNam
134131
// Removes this Layout from the registry and the loaded CustomModels list
135132
public void Delete()
136133
{
137-
int i = _customModels.IndexOf(this);
134+
var customModels = MainWindowSettingsModel.CustomModels;
135+
int i = customModels.IndexOf(this);
138136
if (i != -1)
139137
{
140-
_customModels.RemoveAt(i);
141-
_deletedCustomModels.Add(Guid.ToString().ToUpper());
138+
customModels.RemoveAt(i);
142139
}
143140
}
144141

145142
// Adds new custom Layout
146143
public void AddCustomLayout(LayoutModel model)
147144
{
148145
bool updated = false;
149-
for (int i = 0; i < _customModels.Count && !updated; i++)
146+
var customModels = MainWindowSettingsModel.CustomModels;
147+
for (int i = 0; i < customModels.Count && !updated; i++)
150148
{
151-
if (_customModels[i].Uuid == model.Uuid)
149+
if (customModels[i].Uuid == model.Uuid)
152150
{
153-
_customModels[i] = model;
151+
customModels[i] = model;
154152
updated = true;
155153
}
156154
}
157155

158156
if (!updated)
159157
{
160-
_customModels.Add(model);
158+
customModels.Add(model);
161159
}
162-
}
163-
164-
// Add custom layouts json data that would be serialized to a temp file
165-
public void AddCustomLayoutJson(JsonElement json)
166-
{
167-
_createdCustomLayouts.Add(json);
168-
}
169-
170-
public static void SerializeDeletedCustomZoneSets()
171-
{
172-
App.FancyZonesEditorIO.SerializeDeletedCustomZoneSets(_deletedCustomModels);
173-
}
174-
175-
public static void SerializeCreatedCustomZonesets()
176-
{
177-
App.FancyZonesEditorIO.SerializeCreatedCustomZonesets(_createdCustomLayouts);
178-
}
179160

180-
// Loads all the custom Layouts from tmp file passed by FancyZonesLib
181-
public static ObservableCollection<LayoutModel> LoadCustomModels()
182-
{
183-
_customModels = new ObservableCollection<LayoutModel>();
184-
App.FancyZonesEditorIO.ParseLayouts(ref _customModels, ref _deletedCustomModels);
185-
return _customModels;
161+
App.FancyZonesEditorIO.SerializeZoneSettings();
186162
}
187163

188-
private static ObservableCollection<LayoutModel> _customModels;
189-
private static List<string> _deletedCustomModels = new List<string>();
190-
private static List<JsonElement> _createdCustomLayouts = new List<JsonElement>();
191-
192164
// Callbacks that the base LayoutModel makes to derived types
193165
protected abstract void PersistData();
194166

@@ -211,7 +183,7 @@ public void Apply()
211183
App.Overlay.CurrentLayoutSettings.Type = Type;
212184

213185
// update temp file
214-
App.FancyZonesEditorIO.SerializeAppliedLayouts();
186+
App.FancyZonesEditorIO.SerializeZoneSettings();
215187
}
216188
}
217189
}

0 commit comments

Comments
 (0)