Skip to content
Merged
39 changes: 38 additions & 1 deletion src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using FancyZonesEditor.Utils;
using ManagedCommon;
Expand Down Expand Up @@ -87,7 +89,42 @@ private void OnStartup(object sender, StartupEventArgs e)
_themeManager = new ThemeManager(this);

FancyZonesEditorIO.ParseCommandLineArguments();
FancyZonesEditorIO.ParseDeviceInfoData();

var parseResult = FancyZonesEditorIO.ParseZoneSettings();

// 10ms retry loop with 1 second timeout
if (!parseResult.Item1)
{
CancellationTokenSource ts = new CancellationTokenSource();
Task t = Task.Run(() =>
{
while (!parseResult.Item1 && !ts.IsCancellationRequested)
{
Task.Delay(10).Wait();
parseResult = FancyZonesEditorIO.ParseZoneSettings();
}
});

try
{
bool result = t.Wait(1000, ts.Token);
ts.Cancel();
}
catch (OperationCanceledException)
{
ts.Dispose();
}
}

// Error message if parsing failed
if (!parseResult.Item1)
{
string message = parseResult.Item2 + Environment.NewLine + Environment.NewLine + FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_User_Choice;
if (MessageBox.Show(message, FancyZonesEditor.Properties.Resources.Error_Parsing_Zones_Settings_Title, MessageBoxButton.YesNo) == MessageBoxResult.No)
{
Environment.Exit(0);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to log the error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added data saving to the file with a notification message box, as it was done for other errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's a comment for future reference when we fully enable logging in the editor.

}
}

MainWindowSettingsModel settings = ((App)Current).MainWindowSettings;
settings.UpdateSelectedLayoutModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected void OnSaveApplyTemplate(object sender, RoutedEventArgs e)
model.Persist();
}

LayoutModel.SerializeDeletedCustomZoneSets();
App.FancyZonesEditorIO.SerializeZoneSettings();

_backToLayoutPicker = false;
Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void Apply()

private void OnClosing(object sender, EventArgs e)
{
LayoutModel.SerializeDeletedCustomZoneSets();
App.FancyZonesEditorIO.SerializeZoneSettings();
App.Overlay.CloseLayoutWindow();
App.Current.Shutdown();
}
Expand Down Expand Up @@ -250,19 +250,18 @@ private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e
private void Reset_Click(object sender, RoutedEventArgs e)
{
var overlay = App.Overlay;
MainWindowSettingsModel settings = ((App)Application.Current).MainWindowSettings;

if (overlay.CurrentDataContext is LayoutModel model)
{
model.IsSelected = false;
model.IsApplied = false;
}

overlay.CurrentLayoutSettings.ZonesetUuid = settings.BlankModel.Uuid;
overlay.CurrentLayoutSettings.ZonesetUuid = MainWindowSettingsModel.BlankModel.Uuid;
overlay.CurrentLayoutSettings.Type = LayoutType.Blank;
overlay.CurrentDataContext = settings.BlankModel;
overlay.CurrentDataContext = MainWindowSettingsModel.BlankModel;

App.FancyZonesEditorIO.SerializeAppliedLayouts();
App.FancyZonesEditorIO.SerializeZoneSettings();

if (!overlay.MultiMonitorMode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Windows;

namespace FancyZonesEditor.Models
Expand All @@ -14,7 +13,7 @@ namespace FancyZonesEditor.Models
public class CanvasLayoutModel : LayoutModel
{
// Non-localizable strings
private const string ModelTypeID = "canvas";
public const string ModelTypeID = "canvas";

public Rect CanvasRect { get; private set; }

Expand Down Expand Up @@ -83,84 +82,11 @@ public void RestoreTo(CanvasLayoutModel other)
}
}

private struct Zone
{
public int X { get; set; }

public int Y { get; set; }

public int Width { get; set; }

public int Height { get; set; }
}

private struct CanvasLayoutInfo
{
public int RefWidth { get; set; }

public int RefHeight { get; set; }

public Zone[] Zones { get; set; }
}

private struct CanvasLayoutJson
{
public string Uuid { get; set; }

public string Name { get; set; }

public string Type { get; set; }

public CanvasLayoutInfo Info { get; set; }
}

// PersistData
// Implements the LayoutModel.PersistData abstract method
protected override void PersistData()
{
AddCustomLayout(this);

var canvasRect = CanvasRect;
if (canvasRect.Width == 0 || canvasRect.Height == 0)
{
canvasRect = App.Overlay.WorkArea;
}

CanvasLayoutInfo layoutInfo = new CanvasLayoutInfo
{
RefWidth = (int)canvasRect.Width,
RefHeight = (int)canvasRect.Height,
Zones = new Zone[Zones.Count],
};

for (int i = 0; i < Zones.Count; ++i)
{
Zone zone = new Zone
{
X = Zones[i].X,
Y = Zones[i].Y,
Width = Zones[i].Width,
Height = Zones[i].Height,
};

layoutInfo.Zones[i] = zone;
}

CanvasLayoutJson jsonObj = new CanvasLayoutJson
{
Uuid = Uuid,
Name = Name,
Type = ModelTypeID,
Info = layoutInfo,
};
JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNamingPolicy = new DashCaseNamingPolicy(),
};

string jsonString = JsonSerializer.Serialize(jsonObj, options);
AddCustomLayoutJson(JsonSerializer.Deserialize<JsonElement>(jsonString));
SerializeCreatedCustomZonesets();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Windows;

namespace FancyZonesEditor.Models
{
Expand All @@ -14,7 +12,7 @@ namespace FancyZonesEditor.Models
public class GridLayoutModel : LayoutModel
{
// Non-localizable strings
private const string ModelTypeID = "grid";
public const string ModelTypeID = "grid";

// Rows - number of rows in the Grid
public int Rows
Expand Down Expand Up @@ -173,69 +171,11 @@ public void RestoreTo(GridLayoutModel layout)
layout.ColumnPercents = colPercents;
}

private struct GridLayoutInfo
{
public int Rows { get; set; }

public int Columns { get; set; }

public List<int> RowsPercentage { get; set; }

public List<int> ColumnsPercentage { get; set; }

public int[][] CellChildMap { get; set; }
}

private struct GridLayoutJson
{
public string Uuid { get; set; }

public string Name { get; set; }

public string Type { get; set; }

public GridLayoutInfo Info { get; set; }
}

// PersistData
// Implements the LayoutModel.PersistData abstract method
protected override void PersistData()
{
AddCustomLayout(this);

GridLayoutInfo layoutInfo = new GridLayoutInfo
{
Rows = Rows,
Columns = Columns,
RowsPercentage = RowPercents,
ColumnsPercentage = ColumnPercents,
CellChildMap = new int[Rows][],
};

for (int row = 0; row < Rows; row++)
{
layoutInfo.CellChildMap[row] = new int[Columns];
for (int col = 0; col < Columns; col++)
{
layoutInfo.CellChildMap[row][col] = CellChildMap[row, col];
}
}

GridLayoutJson jsonObj = new GridLayoutJson
{
Uuid = Uuid,
Name = Name,
Type = ModelTypeID,
Info = layoutInfo,
};
JsonSerializerOptions options = new JsonSerializerOptions
{
PropertyNamingPolicy = new DashCaseNamingPolicy(),
};

string jsonString = JsonSerializer.Serialize(jsonObj, options);
AddCustomLayoutJson(JsonSerializer.Deserialize<JsonElement>(jsonString));
SerializeCreatedCustomZonesets();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text.Json;

namespace FancyZonesEditor.Models
{
Expand Down Expand Up @@ -134,61 +131,36 @@ protected virtual void FirePropertyChanged([CallerMemberName] string propertyNam
// Removes this Layout from the registry and the loaded CustomModels list
public void Delete()
{
int i = _customModels.IndexOf(this);
var customModels = MainWindowSettingsModel.CustomModels;
int i = customModels.IndexOf(this);
if (i != -1)
{
_customModels.RemoveAt(i);
_deletedCustomModels.Add(Guid.ToString().ToUpper());
customModels.RemoveAt(i);
}
}

// Adds new custom Layout
public void AddCustomLayout(LayoutModel model)
{
bool updated = false;
for (int i = 0; i < _customModels.Count && !updated; i++)
var customModels = MainWindowSettingsModel.CustomModels;
for (int i = 0; i < customModels.Count && !updated; i++)
{
if (_customModels[i].Uuid == model.Uuid)
if (customModels[i].Uuid == model.Uuid)
{
_customModels[i] = model;
customModels[i] = model;
updated = true;
}
}

if (!updated)
{
_customModels.Add(model);
customModels.Add(model);
}
}

// Add custom layouts json data that would be serialized to a temp file
public void AddCustomLayoutJson(JsonElement json)
{
_createdCustomLayouts.Add(json);
}

public static void SerializeDeletedCustomZoneSets()
{
App.FancyZonesEditorIO.SerializeDeletedCustomZoneSets(_deletedCustomModels);
}

public static void SerializeCreatedCustomZonesets()
{
App.FancyZonesEditorIO.SerializeCreatedCustomZonesets(_createdCustomLayouts);
}

// Loads all the custom Layouts from tmp file passed by FancyZonesLib
public static ObservableCollection<LayoutModel> LoadCustomModels()
{
_customModels = new ObservableCollection<LayoutModel>();
App.FancyZonesEditorIO.ParseLayouts(ref _customModels, ref _deletedCustomModels);
return _customModels;
App.FancyZonesEditorIO.SerializeZoneSettings();
}

private static ObservableCollection<LayoutModel> _customModels;
private static List<string> _deletedCustomModels = new List<string>();
private static List<JsonElement> _createdCustomLayouts = new List<JsonElement>();

// Callbacks that the base LayoutModel makes to derived types
protected abstract void PersistData();

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

// update temp file
App.FancyZonesEditorIO.SerializeAppliedLayouts();
App.FancyZonesEditorIO.SerializeZoneSettings();
}
}
}
Loading