[FancyZonesEditor] Introduce const strings in LayoutModel.cs (#5196)

* Introduce const strings in LayoutModel.cs

* Address PR comments - Update FirePropertyChanged
This commit is contained in:
stefansjfw
2020-07-27 10:12:35 +02:00
committed by GitHub
parent 5a2385c66b
commit 61669a73d9
3 changed files with 80 additions and 43 deletions

View File

@@ -27,7 +27,7 @@ namespace FancyZonesEditor.Models
if (_rows != value) if (_rows != value)
{ {
_rows = value; _rows = value;
FirePropertyChanged("Rows"); FirePropertyChanged();
} }
} }
} }
@@ -47,7 +47,7 @@ namespace FancyZonesEditor.Models
if (_cols != value) if (_cols != value)
{ {
_cols = value; _cols = value;
FirePropertyChanged("Columns"); FirePropertyChanged();
} }
} }
} }

View File

@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json; using System.Text.Json;
using System.Windows; using System.Windows;
@@ -27,11 +28,46 @@ namespace FancyZonesEditor.Models
// Manages common properties and base persistence // Manages common properties and base persistence
public abstract class LayoutModel : INotifyPropertyChanged public abstract class LayoutModel : INotifyPropertyChanged
{ {
public static void ShowExceptionMessageBox(string message, Exception ex) // Localizable strings
private const string ErrorMessageBoxTitle = "FancyZones Editor Exception Handler";
private const string ErrorMessageBoxMessage = "Please report the bug to ";
private const string ErrorSerializingDeletedLayouts = "Error serializing deleted layouts";
private const string ErrorLoadingCustomLayouts = "Error loading custom layouts";
private const string ErrorApplyingLayout = "Error applying layout";
// Non-localizable strings
private const string NameStr = "name";
private const string CustomZoneSetsJsonTag = "custom-zone-sets";
private const string TypeJsonTag = "type";
private const string UuidJsonTag = "uuid";
private const string InfoJsonTag = "info";
private const string GridJsonTag = "grid";
private const string RowsJsonTag = "rows";
private const string ColumnsJsonTag = "columns";
private const string RowsPercentageJsonTag = "rows-percentage";
private const string ColumnsPercentageJsonTag = "columns-percentage";
private const string CellChildMapJsonTag = "cell-child-map";
private const string ZonesJsonTag = "zones";
private const string CanvasJsonTag = "canvas";
private const string RefWidthJsonTag = "ref-width";
private const string RefHeightJsonTag = "ref-height";
private const string XJsonTag = "X";
private const string YJsonTag = "Y";
private const string WidthJsonTag = "width";
private const string HeightJsonTag = "height";
private const string FocusJsonTag = "focus";
private const string PriorityGridJsonTag = "priority-grid";
private const string CustomJsonTag = "custom";
public static void ShowExceptionMessageBox(string message, Exception exception = null)
{ {
string title = "FancyZones Editor Exception Handler"; string fullMessage = ErrorMessageBoxMessage + "https://github.com/microsoft/PowerToys/issues \n" + message;
string fullMessage = "Please report the bug to https://github.com/microsoft/PowerToys/issues \n" + message + ": " + ex.Message; if (exception != null)
MessageBox.Show(fullMessage, title); {
fullMessage += ": " + exception.Message;
}
MessageBox.Show(fullMessage, ErrorMessageBoxTitle);
} }
protected LayoutModel() protected LayoutModel()
@@ -74,7 +110,7 @@ namespace FancyZonesEditor.Models
if (_name != value) if (_name != value)
{ {
_name = value; _name = value;
FirePropertyChanged("Name"); FirePropertyChanged();
} }
} }
} }
@@ -107,7 +143,7 @@ namespace FancyZonesEditor.Models
if (_isSelected != value) if (_isSelected != value)
{ {
_isSelected = value; _isSelected = value;
FirePropertyChanged("IsSelected"); FirePropertyChanged();
} }
} }
} }
@@ -118,7 +154,7 @@ namespace FancyZonesEditor.Models
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
// FirePropertyChanged -- wrapper that calls INPC.PropertyChanged // FirePropertyChanged -- wrapper that calls INPC.PropertyChanged
protected virtual void FirePropertyChanged(string propertyName) protected virtual void FirePropertyChanged([CallerMemberName] string propertyName = null)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
@@ -158,7 +194,7 @@ namespace FancyZonesEditor.Models
} }
catch (Exception ex) catch (Exception ex)
{ {
ShowExceptionMessageBox("Error serializing deleted layouts", ex); ShowExceptionMessageBox(ErrorSerializingDeletedLayouts, ex);
} }
} }
@@ -171,36 +207,36 @@ namespace FancyZonesEditor.Models
{ {
FileStream inputStream = File.Open(Settings.FancyZonesSettingsFile, FileMode.Open); FileStream inputStream = File.Open(Settings.FancyZonesSettingsFile, FileMode.Open);
JsonDocument jsonObject = JsonDocument.Parse(inputStream, options: default); JsonDocument jsonObject = JsonDocument.Parse(inputStream, options: default);
JsonElement.ArrayEnumerator customZoneSetsEnumerator = jsonObject.RootElement.GetProperty("custom-zone-sets").EnumerateArray(); JsonElement.ArrayEnumerator customZoneSetsEnumerator = jsonObject.RootElement.GetProperty(CustomZoneSetsJsonTag).EnumerateArray();
while (customZoneSetsEnumerator.MoveNext()) while (customZoneSetsEnumerator.MoveNext())
{ {
var current = customZoneSetsEnumerator.Current; var current = customZoneSetsEnumerator.Current;
string name = current.GetProperty("name").GetString(); string name = current.GetProperty(NameStr).GetString();
string type = current.GetProperty("type").GetString(); string type = current.GetProperty(TypeJsonTag).GetString();
string uuid = current.GetProperty("uuid").GetString(); string uuid = current.GetProperty(UuidJsonTag).GetString();
var info = current.GetProperty("info"); var info = current.GetProperty(InfoJsonTag);
if (type.Equals("grid")) if (type.Equals(GridJsonTag))
{ {
int rows = info.GetProperty("rows").GetInt32(); int rows = info.GetProperty(RowsJsonTag).GetInt32();
int columns = info.GetProperty("columns").GetInt32(); int columns = info.GetProperty(ColumnsJsonTag).GetInt32();
List<int> rowsPercentage = new List<int>(rows); List<int> rowsPercentage = new List<int>(rows);
JsonElement.ArrayEnumerator rowsPercentageEnumerator = info.GetProperty("rows-percentage").EnumerateArray(); JsonElement.ArrayEnumerator rowsPercentageEnumerator = info.GetProperty(RowsPercentageJsonTag).EnumerateArray();
while (rowsPercentageEnumerator.MoveNext()) while (rowsPercentageEnumerator.MoveNext())
{ {
rowsPercentage.Add(rowsPercentageEnumerator.Current.GetInt32()); rowsPercentage.Add(rowsPercentageEnumerator.Current.GetInt32());
} }
List<int> columnsPercentage = new List<int>(columns); List<int> columnsPercentage = new List<int>(columns);
JsonElement.ArrayEnumerator columnsPercentageEnumerator = info.GetProperty("columns-percentage").EnumerateArray(); JsonElement.ArrayEnumerator columnsPercentageEnumerator = info.GetProperty(ColumnsPercentageJsonTag).EnumerateArray();
while (columnsPercentageEnumerator.MoveNext()) while (columnsPercentageEnumerator.MoveNext())
{ {
columnsPercentage.Add(columnsPercentageEnumerator.Current.GetInt32()); columnsPercentage.Add(columnsPercentageEnumerator.Current.GetInt32());
} }
int i = 0; int i = 0;
JsonElement.ArrayEnumerator cellChildMapRows = info.GetProperty("cell-child-map").EnumerateArray(); JsonElement.ArrayEnumerator cellChildMapRows = info.GetProperty(CellChildMapJsonTag).EnumerateArray();
int[,] cellChildMap = new int[rows, columns]; int[,] cellChildMap = new int[rows, columns];
while (cellChildMapRows.MoveNext()) while (cellChildMapRows.MoveNext())
{ {
@@ -216,19 +252,19 @@ namespace FancyZonesEditor.Models
_customModels.Add(new GridLayoutModel(uuid, name, LayoutType.Custom, rows, columns, rowsPercentage, columnsPercentage, cellChildMap)); _customModels.Add(new GridLayoutModel(uuid, name, LayoutType.Custom, rows, columns, rowsPercentage, columnsPercentage, cellChildMap));
} }
else if (type.Equals("canvas")) else if (type.Equals(CanvasJsonTag))
{ {
int lastWorkAreaWidth = info.GetProperty("ref-width").GetInt32(); int lastWorkAreaWidth = info.GetProperty(RefWidthJsonTag).GetInt32();
int lastWorkAreaHeight = info.GetProperty("ref-height").GetInt32(); int lastWorkAreaHeight = info.GetProperty(RefHeightJsonTag).GetInt32();
JsonElement.ArrayEnumerator zonesEnumerator = info.GetProperty("zones").EnumerateArray(); JsonElement.ArrayEnumerator zonesEnumerator = info.GetProperty(ZonesJsonTag).EnumerateArray();
IList<Int32Rect> zones = new List<Int32Rect>(); IList<Int32Rect> zones = new List<Int32Rect>();
while (zonesEnumerator.MoveNext()) while (zonesEnumerator.MoveNext())
{ {
int x = zonesEnumerator.Current.GetProperty("X").GetInt32(); int x = zonesEnumerator.Current.GetProperty(XJsonTag).GetInt32();
int y = zonesEnumerator.Current.GetProperty("Y").GetInt32(); int y = zonesEnumerator.Current.GetProperty(YJsonTag).GetInt32();
int width = zonesEnumerator.Current.GetProperty("width").GetInt32(); int width = zonesEnumerator.Current.GetProperty(WidthJsonTag).GetInt32();
int height = zonesEnumerator.Current.GetProperty("height").GetInt32(); int height = zonesEnumerator.Current.GetProperty(HeightJsonTag).GetInt32();
zones.Add(new Int32Rect(x, y, width, height)); zones.Add(new Int32Rect(x, y, width, height));
} }
@@ -240,7 +276,7 @@ namespace FancyZonesEditor.Models
} }
catch (Exception ex) catch (Exception ex)
{ {
ShowExceptionMessageBox("Error loading custom layouts", ex); ShowExceptionMessageBox(ErrorLoadingCustomLayouts, ex);
return new ObservableCollection<LayoutModel>(); return new ObservableCollection<LayoutModel>();
} }
@@ -291,22 +327,22 @@ namespace FancyZonesEditor.Models
switch (Type) switch (Type)
{ {
case LayoutType.Focus: case LayoutType.Focus:
activeZoneSet.Type = "focus"; activeZoneSet.Type = FocusJsonTag;
break; break;
case LayoutType.Rows: case LayoutType.Rows:
activeZoneSet.Type = "rows"; activeZoneSet.Type = RowsJsonTag;
break; break;
case LayoutType.Columns: case LayoutType.Columns:
activeZoneSet.Type = "columns"; activeZoneSet.Type = ColumnsJsonTag;
break; break;
case LayoutType.Grid: case LayoutType.Grid:
activeZoneSet.Type = "grid"; activeZoneSet.Type = GridJsonTag;
break; break;
case LayoutType.PriorityGrid: case LayoutType.PriorityGrid:
activeZoneSet.Type = "priority-grid"; activeZoneSet.Type = PriorityGridJsonTag;
break; break;
case LayoutType.Custom: case LayoutType.Custom:
activeZoneSet.Type = "custom"; activeZoneSet.Type = CustomJsonTag;
break; break;
} }
@@ -333,7 +369,7 @@ namespace FancyZonesEditor.Models
} }
catch (Exception ex) catch (Exception ex)
{ {
ShowExceptionMessageBox("Error applying layout", ex); ShowExceptionMessageBox(ErrorApplyingLayout, ex);
} }
} }
} }

View File

@@ -7,6 +7,7 @@ using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json; using System.Text.Json;
using System.Windows; using System.Windows;
using FancyZonesEditor.Models; using FancyZonesEditor.Models;
@@ -169,7 +170,7 @@ namespace FancyZonesEditor
{ {
_zoneCount = value; _zoneCount = value;
UpdateLayoutModels(); UpdateLayoutModels();
FirePropertyChanged("ZoneCount"); FirePropertyChanged();
} }
} }
} }
@@ -189,7 +190,7 @@ namespace FancyZonesEditor
if (_spacing != value) if (_spacing != value)
{ {
_spacing = value; _spacing = value;
FirePropertyChanged("Spacing"); FirePropertyChanged();
} }
} }
} }
@@ -209,7 +210,7 @@ namespace FancyZonesEditor
if (_showSpacing != value) if (_showSpacing != value)
{ {
_showSpacing = value; _showSpacing = value;
FirePropertyChanged("ShowSpacing"); FirePropertyChanged();
} }
} }
} }
@@ -229,7 +230,7 @@ namespace FancyZonesEditor
if (_isShiftKeyPressed != value) if (_isShiftKeyPressed != value)
{ {
_isShiftKeyPressed = value; _isShiftKeyPressed = value;
FirePropertyChanged("IsShiftKeyPressed"); FirePropertyChanged();
} }
} }
} }
@@ -249,7 +250,7 @@ namespace FancyZonesEditor
if (_isCtrlKeyPressed != value) if (_isCtrlKeyPressed != value)
{ {
_isCtrlKeyPressed = value; _isCtrlKeyPressed = value;
FirePropertyChanged("IsCtrlKeyPressed"); FirePropertyChanged();
} }
} }
} }
@@ -521,7 +522,7 @@ namespace FancyZonesEditor
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
// FirePropertyChanged -- wrapper that calls INPC.PropertyChanged // FirePropertyChanged -- wrapper that calls INPC.PropertyChanged
protected virtual void FirePropertyChanged(string propertyName) protected virtual void FirePropertyChanged([CallerMemberName] string propertyName = null)
{ {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }