mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 11:46:30 +02:00
[FancyZones] Custom canvas layouts scaling (#3644)
* Make canvas layout scaling sensitive * Revert "Make canvas layout scaling sensitive" This reverts commit705dab7e36. * Revert "Revert "Make canvas layout scaling sensitive"" This reverts commitb9dd27644c. * Cleanup * Minor refactoring * Address PR comments
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user