[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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;
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; }
@ -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];

View File

@ -487,7 +487,6 @@ void FancyZones::ToggleEditor() noexcept
const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance();
fancyZonesData.CustomZoneSetsToJsonFile(ZoneWindowUtils::GetCustomZoneSetsTmpPath());
// Do not scale window params by the dpi, that will be done in the editor - see LayoutModel.Apply
const auto taskbar_x_offset = mi.rcWork.left - mi.rcMonitor.left;
const auto taskbar_y_offset = mi.rcWork.top - mi.rcMonitor.top;
const auto x = mi.rcMonitor.left + taskbar_x_offset;

View File

@ -853,9 +853,9 @@ namespace JSONHelpers
CanvasLayoutInfo info;
int j = 5;
info.referenceWidth = data[j] * 256 + data[j + 1];
info.lastWorkAreaWidth = data[j] * 256 + data[j + 1];
j += 2;
info.referenceHeight = data[j] * 256 + data[j + 1];
info.lastWorkAreaHeight = data[j] * 256 + data[j + 1];
j += 2;
int count = data[j++];
@ -1023,8 +1023,9 @@ namespace JSONHelpers
json::JsonObject CanvasLayoutInfo::ToJson(const CanvasLayoutInfo& canvasInfo)
{
json::JsonObject infoJson{};
infoJson.SetNamedValue(L"ref-width", json::value(canvasInfo.referenceWidth));
infoJson.SetNamedValue(L"ref-height", json::value(canvasInfo.referenceHeight));
infoJson.SetNamedValue(L"ref-width", json::value(canvasInfo.lastWorkAreaWidth));
infoJson.SetNamedValue(L"ref-height", json::value(canvasInfo.lastWorkAreaHeight));
json::JsonArray zonesJson;
for (const auto& [x, y, width, height] : canvasInfo.zones)
@ -1045,8 +1046,9 @@ namespace JSONHelpers
try
{
CanvasLayoutInfo info;
info.referenceWidth = static_cast<int>(infoJson.GetNamedNumber(L"ref-width"));
info.referenceHeight = static_cast<int>(infoJson.GetNamedNumber(L"ref-height"));
info.lastWorkAreaWidth = static_cast<int>(infoJson.GetNamedNumber(L"ref-width"));
info.lastWorkAreaHeight = static_cast<int>(infoJson.GetNamedNumber(L"ref-height"));
json::JsonArray zonesJson = infoJson.GetNamedArray(L"zones");
uint32_t size = zonesJson.Size();
info.zones.reserve(size);

View File

@ -45,8 +45,9 @@ namespace JSONHelpers
struct CanvasLayoutInfo
{
int referenceWidth;
int referenceHeight;
int lastWorkAreaWidth;
int lastWorkAreaHeight;
struct Rect
{
int x;

View File

@ -576,7 +576,7 @@ bool ZoneSet::CalculateUniquePriorityGridLayout(Rect workArea, int zoneCount, in
bool ZoneSet::CalculateCustomLayout(Rect workArea, int spacing) noexcept
{
wil::unique_cotaskmem_string guidStr;
if (SUCCEEDED_LOG(StringFromCLSID(m_config.Id, &guidStr)))
if (SUCCEEDED(StringFromCLSID(m_config.Id, &guidStr)))
{
const std::wstring guid = guidStr.get();

View File

@ -10,6 +10,7 @@
#include <dwmapi.h>
#include <ProjectTelemetry.h>
#include <shellapi.h>
#include <ShellScalingApi.h>
#include <strsafe.h>
#include <TraceLoggingActivity.h>
#include <wil\resource.h>

View File

@ -208,8 +208,8 @@ namespace FancyZonesUnitTests
TEST_METHOD (ToJson)
{
CanvasLayoutInfo info;
info.referenceWidth = 123;
info.referenceHeight = 321;
info.lastWorkAreaWidth = 123;
info.lastWorkAreaHeight = 321;
info.zones = { CanvasLayoutInfo::Rect{ 11, 22, 33, 44 }, CanvasLayoutInfo::Rect{ 55, 66, 77, 88 } };
auto actual = CanvasLayoutInfo::ToJson(info);
@ -219,15 +219,15 @@ namespace FancyZonesUnitTests
TEST_METHOD (FromJson)
{
CanvasLayoutInfo expected;
expected.referenceWidth = 123;
expected.referenceHeight = 321;
expected.lastWorkAreaWidth = 123;
expected.lastWorkAreaHeight = 321;
expected.zones = { CanvasLayoutInfo::Rect{ 11, 22, 33, 44 }, CanvasLayoutInfo::Rect{ 55, 66, 77, 88 } };
auto actual = CanvasLayoutInfo::FromJson(m_json);
Assert::IsTrue(actual.has_value());
Assert::AreEqual(expected.referenceHeight, actual->referenceHeight);
Assert::AreEqual(expected.referenceWidth, actual->referenceWidth);
Assert::AreEqual(expected.lastWorkAreaHeight, actual->lastWorkAreaHeight);
Assert::AreEqual(expected.lastWorkAreaWidth, actual->lastWorkAreaWidth);
Assert::AreEqual(expected.zones.size(), actual->zones.size());
for (int i = 0; i < expected.zones.size(); i++)
{
@ -596,8 +596,8 @@ namespace FancyZonesUnitTests
auto expectedGrid = std::get<CanvasLayoutInfo>(expected.data.info);
auto actualGrid = std::get<CanvasLayoutInfo>(actual->data.info);
Assert::AreEqual(expectedGrid.referenceWidth, actualGrid.referenceWidth);
Assert::AreEqual(expectedGrid.referenceHeight, actualGrid.referenceHeight);
Assert::AreEqual(expectedGrid.lastWorkAreaWidth, actualGrid.lastWorkAreaWidth);
Assert::AreEqual(expectedGrid.lastWorkAreaHeight, actualGrid.lastWorkAreaHeight);
}
TEST_METHOD (FromJsonGridInvalidUuid)
@ -1397,8 +1397,8 @@ namespace FancyZonesUnitTests
{
auto expectedInfo = std::get<CanvasLayoutInfo>(expected->data.info);
auto actualInfo = std::get<CanvasLayoutInfo>(actual.info);
Assert::AreEqual(expectedInfo.referenceWidth, actualInfo.referenceWidth, L"canvas width");
Assert::AreEqual(expectedInfo.referenceHeight, actualInfo.referenceHeight, L"canvas height");
Assert::AreEqual(expectedInfo.lastWorkAreaWidth, actualInfo.lastWorkAreaWidth, L"canvas width");
Assert::AreEqual(expectedInfo.lastWorkAreaHeight, actualInfo.lastWorkAreaHeight, L"canvas height");
}
iter.MoveNext();