Skip to content

Commit

Permalink
FZ editor: Splitted zones positioning (#2158)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova committed Apr 20, 2020
1 parent 5cfa888 commit cab5a97
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 37 deletions.
35 changes: 24 additions & 11 deletions src/modules/fancyzones/editor/FancyZonesEditor/GridEditor.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -222,6 +222,7 @@ private void OnSplit(object o, SplitEventArgs e)
int newChildIndex = AddZone();

double offset = e.Offset;
double space = e.Space;

if (e.Orientation == Orientation.Vertical)
{
Expand Down Expand Up @@ -294,22 +295,28 @@ private void OnSplit(object o, SplitEventArgs e)
model.CellChildMap = newCellChildMap;

sourceCol = 0;
double newTotalExtent = ActualWidth - (space * (cols + 1));
for (int col = 0; col < cols; col++)
{
if (col == foundCol)
{
RowColInfo[] split = _colInfo[col].Split(offset);
RowColInfo[] split = _colInfo[col].Split(offset, space);
newColInfo[col] = split[0];
newColPercents[col] = split[0].Percent;
newColInfo[col++] = split[0];
newColPercents[col] = split[1].Percent;
col++;

newColInfo[col] = split[1];
sourceCol++;
newColPercents[col] = split[1].Percent;
}
else
{
newColInfo[col] = _colInfo[sourceCol];
newColInfo[col].RecalculatePercent(newTotalExtent);

newColPercents[col] = model.ColumnPercents[sourceCol];
newColInfo[col] = _colInfo[sourceCol++];
}

sourceCol++;
}

_colInfo = newColInfo;
Expand Down Expand Up @@ -389,22 +396,28 @@ private void OnSplit(object o, SplitEventArgs e)
model.CellChildMap = newCellChildMap;

sourceRow = 0;
double newTotalExtent = ActualHeight - (space * (rows + 1));
for (int row = 0; row < rows; row++)
{
if (row == foundRow)
{
RowColInfo[] split = _rowInfo[row].Split(offset);
RowColInfo[] split = _rowInfo[row].Split(offset, space);
newRowInfo[row] = split[0];
newRowPercents[row] = split[0].Percent;
newRowInfo[row++] = split[0];
newRowPercents[row] = split[1].Percent;
row++;

newRowInfo[row] = split[1];
sourceRow++;
newRowPercents[row] = split[1].Percent;
}
else
{
newRowInfo[row] = _rowInfo[sourceRow];
newRowInfo[row].RecalculatePercent(newTotalExtent);

newRowPercents[row] = model.RowPercents[sourceRow];
newRowInfo[row] = _rowInfo[sourceRow++];
}

sourceRow++;
}

_rowInfo = newRowInfo;
Expand Down
13 changes: 10 additions & 3 deletions src/modules/fancyzones/editor/FancyZonesEditor/GridZone.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -276,8 +276,15 @@ private void DoMergeComplete(MouseButtonEventArgs e)
}

private void DoSplit(Orientation orientation, double offset)
{
Split?.Invoke(this, new SplitEventArgs(orientation, offset));
{
int spacing = 0;
Settings settings = ((App)Application.Current).ZoneSettings;
if (settings.ShowSpacing)
{
spacing = settings.Spacing;
}

Split?.Invoke(this, new SplitEventArgs(orientation, offset, spacing));
}

private void FullSplit_Click(object sender, RoutedEventArgs e)
Expand Down
21 changes: 16 additions & 5 deletions src/modules/fancyzones/editor/FancyZonesEditor/RowColInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand Down Expand Up @@ -34,13 +34,24 @@ public double Recalculate(double start, double totalExtent)
return Extent;
}

public RowColInfo[] Split(double offset)
public void RecalculatePercent(double newTotalExtent)
{
Percent = (int)(Extent * _multiplier / newTotalExtent);
}

public RowColInfo[] Split(double offset, double space)
{
RowColInfo[] info = new RowColInfo[2];

int newPercent = (int)(Percent * offset / Extent);
info[0] = new RowColInfo(newPercent);
info[1] = new RowColInfo(Percent - newPercent);
double totalExtent = Extent * _multiplier / Percent;
totalExtent -= space;

int percent0 = (int)(offset * _multiplier / totalExtent);
int percent1 = (int)((Extent - space - offset) * _multiplier / totalExtent);

info[0] = new RowColInfo(percent0);
info[1] = new RowColInfo(percent1);

return info;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

Expand All @@ -13,15 +13,18 @@ public SplitEventArgs()
{
}

public SplitEventArgs(Orientation orientation, double offset)
public SplitEventArgs(Orientation orientation, double offset, double thickness)
{
Orientation = orientation;
Offset = offset;
Space = thickness;
}

public Orientation Orientation { get; }

public double Offset { get; }

public double Space { get; }
}

public delegate void SplitEventHandler(object sender, SplitEventArgs args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
Expand Down Expand Up @@ -161,23 +161,67 @@ public void MoveHorizontalSplitter()
public void CreateSplitter()
{
OpenCreatorWindow("Columns", "Custom table layout creator", "EditTemplateButton");
WindowsElement gridEditor = session.FindElementByClassName("GridEditor");
Assert.IsNotNull(gridEditor);
WaitSeconds(2);

ReadOnlyCollection<AppiumWebElement> thumbs = gridEditor.FindElementsByClassName("Thumb");
Assert.AreEqual(3, session.FindElementsByClassName("GridZone").Count);
Assert.AreEqual(2, thumbs.Count);
ReadOnlyCollection<WindowsElement> zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(3, zones.Count, "Zones count invalid");

new Actions(session).MoveToElement(thumbs[0]).MoveByOffset(-30, 0).Click().Perform();
Assert.AreEqual(3, gridEditor.FindElementsByClassName("Thumb").Count);
const int defaultSpacing = 16;
int splitPos = zones[0].Rect.Y + zones[0].Rect.Height / 2;

new Actions(session).MoveToElement(zones[0]).Click().Perform();

zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(4, zones.Count);

//check splitted zone
Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - splitPos + defaultSpacing / 2) <= 2);
Assert.IsTrue(Math.Abs(zones[3].Rect.Top - splitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[3].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);
}

[TestMethod]
public void TestSplitterShiftAfterCreation()
{
OpenCreatorWindow("Columns", "Custom table layout creator", "EditTemplateButton");
WaitSeconds(2);

ReadOnlyCollection<WindowsElement> zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(3, zones.Count, "Zones count invalid");

const int defaultSpacing = 16;

//create first split
int firstSplitPos = zones[0].Rect.Y + zones[0].Rect.Height / 4;
new Actions(session).MoveToElement(zones[0]).MoveByOffset(0, -(zones[0].Rect.Height / 4)).Click().Perform();

zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(4, zones.Count);

//check that zone was splitted horizontally
Assert.AreNotEqual(zones[0].Rect.Height, zones[1].Rect.Height);
Assert.AreNotEqual(zones[3].Rect.Height, zones[1].Rect.Height);
Assert.AreEqual(zones[1].Rect.Height, zones[2].Rect.Height);
Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - firstSplitPos + defaultSpacing / 2) <= 2);
Assert.IsTrue(Math.Abs(zones[3].Rect.Top - firstSplitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[3].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);

//create second split
int secondSplitPos = zones[3].Rect.Y + zones[3].Rect.Height / 2;
int expectedTop = zones[3].Rect.Top;

new Actions(session).MoveToElement(zones[3]).Click().Perform();

zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(5, zones.Count);

//check first split on same position
Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - firstSplitPos + defaultSpacing / 2) <= 2);

//check second split
Assert.AreEqual(zones[3].Rect.Top, expectedTop);
Assert.IsTrue(Math.Abs(zones[3].Rect.Bottom - secondSplitPos + defaultSpacing / 2) <= 2);
Assert.IsTrue(Math.Abs(zones[4].Rect.Top - secondSplitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[4].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);
}

[TestMethod]
Expand Down
10 changes: 6 additions & 4 deletions src/tests/win-app-driver/FancyZonesTests/FancyZonesEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ protected static void ResetSettings()
protected static void OpenEditor()
{
new Actions(session).KeyDown(OpenQA.Selenium.Keys.Command).SendKeys("`").KeyUp(OpenQA.Selenium.Keys.Command).Perform();
WaitSeconds(2);
//editorWindow = WaitElementByXPath("//Window[@Name=\"FancyZones Editor\"]");
editorWindow = WaitElementByName("FancyZones Editor");
//may not find editor by name in 0.16.1
editorWindow = WaitElementByAccessibilityId("MainWindow1");
//editorWindow = WaitElementByAccessibilityId("MainWindow1");
Assert.IsNotNull(editorWindow, "Couldn't find editor window");
}

Expand Down Expand Up @@ -57,10 +59,10 @@ protected static void OpenTemplates()
protected static void OpenCreatorWindow(string tabName, string creatorWindowName, string buttonId = "EditCustomButton")
{
string elementXPath = "//Text[@Name=\"" + tabName + "\"]";
session.FindElementByXPath(elementXPath).Click();
session.FindElementByAccessibilityId(buttonId).Click();
WaitElementByXPath(elementXPath).Click();
WaitElementByAccessibilityId(buttonId).Click();

WindowsElement creatorWindow = session.FindElementByName(creatorWindowName);
WindowsElement creatorWindow = WaitElementByName(creatorWindowName);
Assert.IsNotNull(creatorWindow, "Creator window didn't open");
}

Expand Down

0 comments on commit cab5a97

Please sign in to comment.