[FancyZones] Custom canvas layouts scaling (#3644)

* Make canvas layout scaling sensitive

* Revert "Make canvas layout scaling sensitive"

This reverts commit 705dab7e36.

* Revert "Revert "Make canvas layout scaling sensitive""

This reverts commit b9dd27644c.

* Cleanup

* Minor refactoring

* Address PR comments
This commit is contained in:
stefansjfw
2020-05-29 09:38:29 +02:00
committed by GitHub
parent 3bb3c06456
commit ff328381af
15 changed files with 120 additions and 126 deletions

View File

@@ -21,7 +21,7 @@ namespace FancyZonesEditor
private void OnAddZone(object sender, RoutedEventArgs e)
{
_model.AddZone(new Int32Rect(_offset, _offset, (int)(_model.ReferenceWidth * 0.6), (int)(_model.ReferenceHeight * 0.6)));
_model.AddZone(new Int32Rect(_offset, _offset, (int)(Settings.WorkArea.Width * 0.6), (int)(Settings.WorkArea.Height * 0.6)));
_offset += 100;
}

View File

@@ -23,8 +23,6 @@ namespace FancyZonesEditor
Canvas.SetZIndex(this, zIndex++);
}
private readonly Settings _settings = ((App)Application.Current).ZoneSettings;
private CanvasLayoutModel model;
private int zoneIndex;
@@ -289,8 +287,8 @@ namespace FancyZonesEditor
// Corner dragging
private void Caption_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BothEdges, (int)_settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BothEdges, (int)_settings.WorkArea.Height);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BothEdges, (int)Settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BothEdges, (int)Settings.WorkArea.Height);
}
public CanvasLayoutModel Model { get => model; set => model = value; }
@@ -299,50 +297,50 @@ namespace FancyZonesEditor
private void NWResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)_settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)_settings.WorkArea.Height);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)Settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)Settings.WorkArea.Height);
}
private void NEResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)_settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)_settings.WorkArea.Height);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)Settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)Settings.WorkArea.Height);
}
private void SWResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)_settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)_settings.WorkArea.Height);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)Settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)Settings.WorkArea.Height);
}
private void SEResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)_settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)_settings.WorkArea.Height);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)Settings.WorkArea.Width);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)Settings.WorkArea.Height);
}
// Edge dragging
private void NResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = null;
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)_settings.WorkArea.Height);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.BottomEdge, (int)Settings.WorkArea.Height);
}
private void SResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = null;
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)_settings.WorkArea.Height);
snappyY = NewDefaultSnappyHelper(false, ResizeMode.TopEdge, (int)Settings.WorkArea.Height);
}
private void WResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)_settings.WorkArea.Width);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.BottomEdge, (int)Settings.WorkArea.Width);
snappyY = null;
}
private void EResize_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)_settings.WorkArea.Width);
snappyX = NewDefaultSnappyHelper(true, ResizeMode.TopEdge, (int)Settings.WorkArea.Width);
snappyY = null;
}
}

View File

@@ -69,10 +69,10 @@ namespace FancyZonesEditor
InitializeComponent();
Current = this;
Left = _settings.WorkArea.Left;
Top = _settings.WorkArea.Top;
Width = _settings.WorkArea.Width;
Height = _settings.WorkArea.Height;
Left = Settings.WorkArea.Left;
Top = Settings.WorkArea.Top;
Width = Settings.WorkArea.Width;
Height = Settings.WorkArea.Height;
}
private void OnLoaded(object sender, RoutedEventArgs e)

View File

@@ -16,6 +16,13 @@ namespace FancyZonesEditor
EditorOverlay mainEditor = EditorOverlay.Current;
if (mainEditor.DataContext is LayoutModel model)
{
// If new custom Canvas layout is created (i.e. edited Blank layout),
// it's type needs to be updated
if (model.Type == LayoutType.Blank)
{
model.Type = LayoutType.Custom;
}
model.Persist();
}

View File

@@ -106,8 +106,8 @@ namespace FancyZonesEditor
int spacing = settings.ShowSpacing ? settings.Spacing : 0;
int width = (int)settings.WorkArea.Width;
int height = (int)settings.WorkArea.Height;
int width = (int)Settings.WorkArea.Width;
int height = (int)Settings.WorkArea.Height;
double totalWidth = width - (spacing * (cols + 1));
double totalHeight = height - (spacing * (rows + 1));
@@ -265,8 +265,8 @@ namespace FancyZonesEditor
Body.Children.Add(viewbox);
Canvas frame = new Canvas
{
Width = canvas.ReferenceWidth,
Height = canvas.ReferenceHeight,
Width = Settings.WorkArea.Width,
Height = Settings.WorkArea.Height,
};
viewbox.Child = frame;
foreach (Int32Rect zone in canvas.Zones)

View File

@@ -31,7 +31,7 @@ namespace FancyZonesEditor
KeyUp += MainWindow_KeyUp;
if (_settings.WorkArea.Height < 900)
if (Settings.WorkArea.Height < 900)
{
SizeToContent = SizeToContent.WidthAndHeight;
WrapPanelItemSize = 180;
@@ -146,6 +146,7 @@ namespace FancyZonesEditor
private void Apply_Click(object sender, RoutedEventArgs e)
{
EditorOverlay mainEditor = EditorOverlay.Current;
if (mainEditor.DataContext is LayoutModel model)
{
if (model is GridLayoutModel)
@@ -154,7 +155,7 @@ namespace FancyZonesEditor
}
else
{
model.Apply();
model.Persist();
}
Close();

View File

@@ -14,69 +14,33 @@ namespace FancyZonesEditor.Models
// Free form Layout Model, which specifies independent zone rects
public class CanvasLayoutModel : LayoutModel
{
public CanvasLayoutModel(string uuid, string name, LayoutType type, int referenceWidth, int referenceHeight, IList<Int32Rect> zones)
public CanvasLayoutModel(string uuid, string name, LayoutType type, IList<Int32Rect> zones, int workAreaWidth, int workAreaHeight)
: base(uuid, name, type)
{
_referenceWidth = referenceWidth;
_referenceHeight = referenceHeight;
Zones = zones;
lastWorkAreaWidth = workAreaWidth;
lastWorkAreaHeight = workAreaHeight;
if (ShouldScaleLayout())
{
ScaleLayout(zones);
}
else
{
Zones = zones;
}
}
public CanvasLayoutModel(string name, LayoutType type, int referenceWidth, int referenceHeight)
public CanvasLayoutModel(string name, LayoutType type)
: base(name, type)
{
// Initialize Reference Size
_referenceWidth = referenceWidth;
_referenceHeight = referenceHeight;
}
public CanvasLayoutModel(string name)
: base(name)
{
}
// ReferenceWidth - the reference width for the layout rect that all Zones are relative to
public int ReferenceWidth
{
get
{
return _referenceWidth;
}
set
{
if (_referenceWidth != value)
{
_referenceWidth = value;
FirePropertyChanged("ReferenceWidth");
}
}
}
private int _referenceWidth;
// ReferenceHeight - the reference height for the layout rect that all Zones are relative to
public int ReferenceHeight
{
get
{
return _referenceHeight;
}
set
{
if (_referenceHeight != value)
{
_referenceHeight = value;
FirePropertyChanged("ReferenceHeight");
}
}
}
private int _referenceHeight;
// Zones - the list of all zones in this layout, described as independent rectangles
public IList<Int32Rect> Zones { get; } = new List<Int32Rect>();
public IList<Int32Rect> Zones { get; private set; } = new List<Int32Rect>();
private int lastWorkAreaWidth = (int)Settings.WorkArea.Width;
private int lastWorkAreaHeight = (int)Settings.WorkArea.Height;
// RemoveZoneAt
// Removes the specified index from the Zones list, and fires a property changed notification for the Zones property
@@ -99,11 +63,7 @@ namespace FancyZonesEditor.Models
// Clones the data from this CanvasLayoutModel to a new CanvasLayoutModel
public override LayoutModel Clone()
{
CanvasLayoutModel layout = new CanvasLayoutModel(Name)
{
ReferenceHeight = ReferenceHeight,
ReferenceWidth = ReferenceWidth,
};
CanvasLayoutModel layout = new CanvasLayoutModel(Name, Type);
foreach (Int32Rect zone in Zones)
{
@@ -122,6 +82,33 @@ namespace FancyZonesEditor.Models
}
}
private bool ShouldScaleLayout()
{
// Scale if:
// - at least one dimension changed
// - orientation remained the same
return (lastWorkAreaHeight != Settings.WorkArea.Height || lastWorkAreaWidth != Settings.WorkArea.Width) &&
((lastWorkAreaHeight > lastWorkAreaWidth && Settings.WorkArea.Height > Settings.WorkArea.Width) ||
(lastWorkAreaWidth > lastWorkAreaHeight && Settings.WorkArea.Width > Settings.WorkArea.Height));
}
private void ScaleLayout(IList<Int32Rect> zones)
{
foreach (Int32Rect zone in zones)
{
double widthFactor = (double)Settings.WorkArea.Width / lastWorkAreaWidth;
double heightFactor = (double)Settings.WorkArea.Height / lastWorkAreaHeight;
int scaledX = (int)(zone.X * widthFactor);
int scaledY = (int)(zone.Y * heightFactor);
int scaledWidth = (int)(zone.Width * widthFactor);
int scaledHeight = (int)(zone.Height * heightFactor);
Zones.Add(new Int32Rect(scaledX, scaledY, scaledWidth, scaledHeight));
}
lastWorkAreaHeight = (int)Settings.WorkArea.Height;
lastWorkAreaWidth = (int)Settings.WorkArea.Width;
}
private struct Zone
{
public int X { get; set; }
@@ -159,8 +146,9 @@ namespace FancyZonesEditor.Models
{
CanvasLayoutInfo layoutInfo = new CanvasLayoutInfo
{
RefWidth = _referenceWidth,
RefHeight = _referenceHeight,
RefWidth = lastWorkAreaWidth,
RefHeight = lastWorkAreaHeight,
Zones = new Zone[Zones.Count],
};
for (int i = 0; i < Zones.Count; ++i)

View File

@@ -43,7 +43,6 @@ namespace FancyZonesEditor.Models
protected LayoutModel(string name)
: this()
{
_guid = Guid.NewGuid();
Name = name;
}
@@ -220,8 +219,9 @@ namespace FancyZonesEditor.Models
}
else if (type.Equals("canvas"))
{
int referenceWidth = info.GetProperty("ref-width").GetInt32();
int referenceHeight = info.GetProperty("ref-height").GetInt32();
int lastWorkAreaWidth = info.GetProperty("ref-width").GetInt32();
int lastWorkAreaHeight = info.GetProperty("ref-height").GetInt32();
JsonElement.ArrayEnumerator zonesEnumerator = info.GetProperty("zones").EnumerateArray();
IList<Int32Rect> zones = new List<Int32Rect>();
while (zonesEnumerator.MoveNext())
@@ -233,7 +233,7 @@ namespace FancyZonesEditor.Models
zones.Add(new Int32Rect(x, y, width, height));
}
_customModels.Add(new CanvasLayoutModel(uuid, name, LayoutType.Custom, referenceWidth, referenceHeight, zones));
_customModels.Add(new CanvasLayoutModel(uuid, name, LayoutType.Custom, zones, lastWorkAreaWidth, lastWorkAreaHeight));
}
}

View File

@@ -44,6 +44,8 @@ namespace FancyZonesEditor
public const ushort _blankCustomModelId = 0xFFFA;
public const ushort _lastDefinedId = _blankCustomModelId;
private const int _defaultDPI = 96;
// hard coded data for all the "Priority Grid" configurations that are unique to "Grid"
private static readonly byte[][] _priorityData = new byte[][]
{
@@ -84,7 +86,7 @@ namespace FancyZonesEditor
// Initialize the five default layout models: Focus, Columns, Rows, Grid, and PriorityGrid
DefaultModels = new List<LayoutModel>(5);
_focusModel = new CanvasLayoutModel("Focus", LayoutType.Focus, (int)_workArea.Width, (int)_workArea.Height);
_focusModel = new CanvasLayoutModel("Focus", LayoutType.Focus);
DefaultModels.Add(_focusModel);
_columnsModel = new GridLayoutModel("Columns", LayoutType.Columns)
@@ -107,7 +109,7 @@ namespace FancyZonesEditor
_priorityGridModel = new GridLayoutModel("Priority Grid", LayoutType.PriorityGrid);
DefaultModels.Add(_priorityGridModel);
_blankCustomModel = new CanvasLayoutModel("Create new custom", LayoutType.Blank, (int)_workArea.Width, (int)_workArea.Height);
_blankCustomModel = new CanvasLayoutModel("Create new custom", LayoutType.Blank);
UpdateLayoutModels();
}
@@ -213,12 +215,7 @@ namespace FancyZonesEditor
private bool _isCtrlKeyPressed;
public Rect WorkArea
{
get { return _workArea; }
}
private Rect _workArea;
public static Rect WorkArea { get; private set; }
public static uint Monitor { get; private set; }
@@ -252,7 +249,7 @@ namespace FancyZonesEditor
public static string WorkAreaKey { get; private set; }
// UpdateLayoutModels
// Update the five default layouts based on the new ZoneCount
// Update the five default layouts based on the new ZoneCount
private void UpdateLayoutModels()
{
// Update the "Focus" Default Layout
@@ -264,9 +261,9 @@ namespace FancyZonesEditor
ZoneCount = 3;
}
Int32Rect focusZoneRect = new Int32Rect((int)(_focusModel.ReferenceWidth * 0.1), (int)(_focusModel.ReferenceHeight * 0.1), (int)(_focusModel.ReferenceWidth * 0.6), (int)(_focusModel.ReferenceHeight * 0.6));
int focusRectXIncrement = (ZoneCount <= 1) ? 0 : (int)(_focusModel.ReferenceWidth * 0.2) / (ZoneCount - 1);
int focusRectYIncrement = (ZoneCount <= 1) ? 0 : (int)(_focusModel.ReferenceHeight * 0.2) / (ZoneCount - 1);
Int32Rect focusZoneRect = new Int32Rect((int)(WorkArea.Width * 0.1), (int)(WorkArea.Height * 0.1), (int)(WorkArea.Width * 0.6), (int)(WorkArea.Height * 0.6));
int focusRectXIncrement = (ZoneCount <= 1) ? 0 : (int)(WorkArea.Width * 0.2) / (ZoneCount - 1);
int focusRectYIncrement = (ZoneCount <= 1) ? 0 : (int)(WorkArea.Height * 0.2) / (ZoneCount - 1);
for (int i = 0; i < ZoneCount; i++)
{
@@ -416,7 +413,7 @@ namespace FancyZonesEditor
private void ParseCommandLineArgs()
{
_workArea = SystemParameters.WorkArea;
WorkArea = SystemParameters.WorkArea;
Monitor = 0;
string[] args = Environment.GetCommandLineArgs();
@@ -433,7 +430,7 @@ namespace FancyZonesEditor
var width = int.Parse(parsedLocation[2]);
var height = int.Parse(parsedLocation[3]);
_workArea = new Rect(x, y, width, height);
WorkArea = new Rect(x, y, width, height);
WorkAreaKey = args[(int)CmdArgs.ResolutionKey];