fix(dock): move dock math to core to fix test suite breakage
This commit is contained in:
@@ -0,0 +1,33 @@
|
|||||||
|
namespace Ghost.Editor.Core.Controls.Internal.Docking;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the possible dock positions for a drop operation.
|
||||||
|
/// </summary>
|
||||||
|
public enum DockPosition
|
||||||
|
{
|
||||||
|
Center,
|
||||||
|
Top,
|
||||||
|
Bottom,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Helper class for docking-related calculations.
|
||||||
|
/// </summary>
|
||||||
|
public static class DockMath
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Calculates the dock position based on the relative position within a target element.
|
||||||
|
/// Precedence: Left/Right win over Top/Bottom at corners.
|
||||||
|
/// </summary>
|
||||||
|
public static DockPosition CalculateDockPosition(double width, double height, double x, double y, double threshold)
|
||||||
|
{
|
||||||
|
if (x < width * threshold) return DockPosition.Left;
|
||||||
|
if (x > width * (1 - threshold)) return DockPosition.Right;
|
||||||
|
if (y < height * threshold) return DockPosition.Top;
|
||||||
|
if (y > height * (1 - threshold)) return DockPosition.Bottom;
|
||||||
|
return DockPosition.Center;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -25,8 +25,6 @@ public sealed partial class DockLayout : Control
|
|||||||
private FrameworkElement? _dropTargetOverlay;
|
private FrameworkElement? _dropTargetOverlay;
|
||||||
private readonly HashSet<DockNode> _subscribedNodes = new();
|
private readonly HashSet<DockNode> _subscribedNodes = new();
|
||||||
|
|
||||||
public enum DockPosition { Center, Top, Bottom, Left, Right, None }
|
|
||||||
|
|
||||||
public DockLayout()
|
public DockLayout()
|
||||||
{
|
{
|
||||||
DefaultStyleKey = typeof(DockLayout);
|
DefaultStyleKey = typeof(DockLayout);
|
||||||
@@ -34,19 +32,6 @@ public sealed partial class DockLayout : Control
|
|||||||
Unloaded += OnUnloaded;
|
Unloaded += OnUnloaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Calculates the dock position based on the relative position within a target element.
|
|
||||||
/// Precedence: Left/Right win over Top/Bottom at corners.
|
|
||||||
/// </summary>
|
|
||||||
public static DockPosition CalculateDockPosition(double width, double height, double x, double y, double threshold)
|
|
||||||
{
|
|
||||||
if (x < width * threshold) return DockPosition.Left;
|
|
||||||
if (x > width * (1 - threshold)) return DockPosition.Right;
|
|
||||||
if (y < height * threshold) return DockPosition.Top;
|
|
||||||
if (y > height * (1 - threshold)) return DockPosition.Bottom;
|
|
||||||
return DockPosition.Center;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnLoaded(object sender, RoutedEventArgs e)
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (Root != null)
|
if (Root != null)
|
||||||
@@ -327,7 +312,7 @@ public sealed partial class DockLayout : Control
|
|||||||
e.AcceptedOperation = global::Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
|
e.AcceptedOperation = global::Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
|
||||||
|
|
||||||
var position = e.GetPosition(targetElement);
|
var position = e.GetPosition(targetElement);
|
||||||
var newPosition = CalculateDockPosition(targetElement.ActualWidth, targetElement.ActualHeight, position.X, position.Y, DROP_EDGE_THRESHOLD);
|
var newPosition = DockMath.CalculateDockPosition(targetElement.ActualWidth, targetElement.ActualHeight, position.X, position.Y, DROP_EDGE_THRESHOLD);
|
||||||
|
|
||||||
if (newPosition != _currentDropPosition)
|
if (newPosition != _currentDropPosition)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using Ghost.Editor.View.Controls;
|
using Ghost.Editor.Core.Controls.Internal.Docking;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
namespace Ghost.UnitTest;
|
namespace Ghost.UnitTest;
|
||||||
@@ -12,36 +12,36 @@ public class DockLayoutTest
|
|||||||
public void TestCalculateDockPosition_Center()
|
public void TestCalculateDockPosition_Center()
|
||||||
{
|
{
|
||||||
// 100x100, threshold 0.25 -> Center is [25, 75]
|
// 100x100, threshold 0.25 -> Center is [25, 75]
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 50, 50, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 50, 50, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Center, pos);
|
Assert.AreEqual(DockPosition.Center, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCalculateDockPosition_Left()
|
public void TestCalculateDockPosition_Left()
|
||||||
{
|
{
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 10, 50, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 10, 50, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Left, pos);
|
Assert.AreEqual(DockPosition.Left, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCalculateDockPosition_Right()
|
public void TestCalculateDockPosition_Right()
|
||||||
{
|
{
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 90, 50, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 90, 50, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Right, pos);
|
Assert.AreEqual(DockPosition.Right, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCalculateDockPosition_Top()
|
public void TestCalculateDockPosition_Top()
|
||||||
{
|
{
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 50, 10, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 50, 10, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Top, pos);
|
Assert.AreEqual(DockPosition.Top, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCalculateDockPosition_Bottom()
|
public void TestCalculateDockPosition_Bottom()
|
||||||
{
|
{
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 50, 90, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 50, 90, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Bottom, pos);
|
Assert.AreEqual(DockPosition.Bottom, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@@ -49,14 +49,14 @@ public class DockLayoutTest
|
|||||||
{
|
{
|
||||||
// (10, 10) is in both Left and Top zones.
|
// (10, 10) is in both Left and Top zones.
|
||||||
// Current implementation: Left/Right win over Top/Bottom.
|
// Current implementation: Left/Right win over Top/Bottom.
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 10, 10, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 10, 10, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Left, pos);
|
Assert.AreEqual(DockPosition.Left, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void TestCalculateDockPosition_CornerPrecedence_RightBottom()
|
public void TestCalculateDockPosition_CornerPrecedence_RightBottom()
|
||||||
{
|
{
|
||||||
var pos = DockLayout.CalculateDockPosition(100, 100, 90, 90, THRESHOLD);
|
var pos = DockMath.CalculateDockPosition(100, 100, 90, 90, THRESHOLD);
|
||||||
Assert.AreEqual(DockLayout.DockPosition.Right, pos);
|
Assert.AreEqual(DockPosition.Right, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\Editor\Ghost.Editor.Core\Ghost.Editor.Core.csproj" />
|
<ProjectReference Include="..\..\Editor\Ghost.Editor.Core\Ghost.Editor.Core.csproj" />
|
||||||
<ProjectReference Include="..\..\Editor\Ghost.Editor\Ghost.Editor.csproj" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user