Skip to content

Commit

Permalink
Account for diagram zoom when dragging resizers (#16)
Browse files Browse the repository at this point in the history
* add test

* add fix

* enable zoom for testing

* finish writing tests

* add fix for remaining resizers

* Revert "enable zoom for testing"

This reverts commit 69fa0aa.

* fix formatting

* make fields nullable

* fix nullables

* use file scoped namespaces
  • Loading branch information
Heathermcx committed Mar 5, 2024
1 parent a4d0828 commit 920dc36
Show file tree
Hide file tree
Showing 10 changed files with 642 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ namespace Blazor.Diagrams.Core.Positions.Resizing
{
public class BottomLeftResizerProvider : IResizerProvider
{
public string? Class => "bottomleft";
public string? Class => "bottomleft";

private Size? _originalSize;
private Point? _originalPosition;
private Point? _originalMousePosition;
private NodeModel? _nodeModel;
private Diagram? _diagram;

private Size _originalSize = null!;
private Point _originalPosition = null!;
private Point _originalMousePosition = null!;
private NodeModel _nodeModel = null!;

public Point? GetPosition(Model model)
{
Expand All @@ -29,22 +31,23 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr
{
_originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y);
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY);
_originalSize = nodeModel.Size!;
_originalSize = nodeModel.Size;
_nodeModel = nodeModel;
_diagram = diagram;
}
}

public void OnPointerMove(Model? model, PointerEventArgs args)
{
if (_nodeModel is null)
if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null)
{
return;
}

var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y);
var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X);
var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;
var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;

var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X);
var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;
var positionY = _originalPosition.Y;

if (width < _nodeModel.MinimumDimensions.Width)
Expand All @@ -65,10 +68,11 @@ public void OnPointerMove(Model? model, PointerEventArgs args)
public void OnResizeEnd(Model? model, PointerEventArgs args)
{
_nodeModel?.TriggerSizeChanged();
_originalSize = null!;
_originalPosition = null!;
_originalMousePosition = null!;
_nodeModel = null!;
_originalSize = null;
_originalPosition = null;
_originalMousePosition = null;
_nodeModel = null;
_diagram = null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ public class BottomRightResizerProvider : IResizerProvider
{
public string? Class => "bottomright";

private Size _originalSize = null!;
private Point _originalMousePosition = null!;
private NodeModel _nodeModel = null!;
private Size? _originalSize;
private Point? _originalMousePosition;
private NodeModel? _nodeModel;
private Diagram? _diagram;

public Point? GetPosition(Model model)
public Point? GetPosition(Model model)
{
if (model is NodeModel nodeModel && nodeModel.Size is not null)
{
Expand All @@ -27,20 +28,21 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr
if (model is NodeModel nodeModel)
{
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY);
_originalSize = nodeModel.Size!;
_originalSize = nodeModel.Size;
_nodeModel = nodeModel;
_diagram = diagram;
}
}

public void OnPointerMove(Model? model, PointerEventArgs args)
{
if (_nodeModel is null)
if (_originalSize is null || _originalMousePosition is null || _nodeModel is null || _diagram is null)
{
return;
}

var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y);
var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X);
var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;
var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;

if (width < _nodeModel.MinimumDimensions.Width)
{
Expand All @@ -57,9 +59,10 @@ public void OnPointerMove(Model? model, PointerEventArgs args)
public void OnResizeEnd(Model? model, PointerEventArgs args)
{
_nodeModel?.TriggerSizeChanged();
_originalSize = null!;
_originalMousePosition = null!;
_nodeModel = null!;
_originalSize = null;
_originalMousePosition = null;
_nodeModel = null;
_diagram = null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace Blazor.Diagrams.Core.Positions.Resizing
{
public class TopLeftResizerProvider : IResizerProvider
{
public string? Class => "topleft";
public string? Class => "topleft";

private Size _originalSize = null!;
private Point _originalPosition = null!;
private Point _originalMousePosition = null!;
private NodeModel _nodeModel = null!;
private Size? _originalSize;
private Point? _originalPosition;
private Point? _originalMousePosition;
private NodeModel? _nodeModel;
private Diagram? _diagram;

public Point? GetPosition(Model model)
{
Expand All @@ -29,23 +30,24 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr
{
_originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y);
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY);
_originalSize = nodeModel.Size!;
_originalSize = nodeModel.Size;
_nodeModel = nodeModel;
_diagram = diagram;
}
}

public void OnPointerMove(Model? model, PointerEventArgs args)
{
if (_nodeModel is null)
if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null)
{
return;
}

var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y);
var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X);
var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;
var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;

var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X);
var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y);
var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;
var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;

if (width < _nodeModel.MinimumDimensions.Width)
{
Expand All @@ -65,10 +67,11 @@ public void OnPointerMove(Model? model, PointerEventArgs args)
public void OnResizeEnd(Model? model, PointerEventArgs args)
{
_nodeModel?.TriggerSizeChanged();
_originalSize = null!;
_originalPosition = null!;
_originalMousePosition = null!;
_nodeModel = null!;
_originalSize = null;
_originalPosition = null;
_originalMousePosition = null;
_nodeModel = null;
_diagram = null;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace Blazor.Diagrams.Core.Positions.Resizing
{
public class TopRightResizerProvider : IResizerProvider
{
public string? Class => "topright";
public string? Class => "topright";

private Size _originalSize = null!;
private Point _originalPosition = null!;
private Point _originalMousePosition = null!;
private NodeModel _nodeModel = null!;
private Size? _originalSize;
private Point? _originalPosition;
private Point? _originalMousePosition;
private NodeModel? _nodeModel;
private Diagram? _diagram;

public Point? GetPosition(Model model)
{
Expand All @@ -29,22 +30,24 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr
{
_originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y);
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY);
_originalSize = nodeModel.Size!;
_originalSize = nodeModel.Size;
_nodeModel = nodeModel;
_diagram = diagram;
}
}

public void OnPointerMove(Model? model, PointerEventArgs args)
{
if (_nodeModel is null)
if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null)
{
return;
}
var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y);
var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X);

var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;
var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom;

var positionX = _originalPosition.X;
var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y);
var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom;

if (width < _nodeModel.MinimumDimensions.Width)
{
Expand All @@ -64,11 +67,12 @@ public void OnPointerMove(Model? model, PointerEventArgs args)
public void OnResizeEnd(Model? model, PointerEventArgs args)
{
_nodeModel?.TriggerSizeChanged();
_originalSize = null!;
_originalPosition = null!;
_originalMousePosition = null!;
_nodeModel = null!;
_originalSize = null;
_originalPosition = null;
_originalMousePosition = null;
_nodeModel = null;
_diagram = null;
}

}
}
}
103 changes: 51 additions & 52 deletions tests/Blazor.Diagrams.Core.Tests/Controls/ResizeControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,70 @@
using Moq;
using Xunit;

namespace Blazor.Diagrams.Core.Tests.Controls
namespace Blazor.Diagrams.Core.Tests.Controls;

public class ResizeControlTests
{
public class ResizeControlTests
[Fact]
public void GetPosition_ShouldUseResizeProviderGetPosition()
{
[Fact]
public void GetPosition_ShouldUseResizeProviderGetPosition()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var model = new Mock<Model>();
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var model = new Mock<Model>();

control.GetPosition(model.Object);
control.GetPosition(model.Object);

resizeProvider.Verify(m => m.GetPosition(model.Object), Times.Once);
}
resizeProvider.Verify(m => m.GetPosition(model.Object), Times.Once);
}

[Fact]
public void OnPointerDown_ShouldInvokeResizeStart()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = Mock.Of<Diagram>();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);
[Fact]
public void OnPointerDown_ShouldInvokeResizeStart()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = Mock.Of<Diagram>();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);

control.OnPointerDown(diagram, model, eventArgs);
control.OnPointerDown(diagram, model, eventArgs);

resizeProvider.Verify(m => m.OnResizeStart(diagram, model, eventArgs), Times.Once);
}
resizeProvider.Verify(m => m.OnResizeStart(diagram, model, eventArgs), Times.Once);
}

[Fact]
public void OnPointerDown_ShouldAddEventHandlers()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = new TestDiagram();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);
[Fact]
public void OnPointerDown_ShouldAddEventHandlers()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = new TestDiagram();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);

control.OnPointerDown(diagram, model, eventArgs);
diagram.TriggerPointerMove(model, eventArgs);
resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Once);
control.OnPointerDown(diagram, model, eventArgs);

diagram.TriggerPointerMove(model, eventArgs);
resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Once);

diagram.TriggerPointerUp(model, eventArgs);
resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once);
}
diagram.TriggerPointerUp(model, eventArgs);
resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once);
}

[Fact]
public void OnPointerUp_ShouldRemoveEventHandlers()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = new TestDiagram();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);
[Fact]
public void OnPointerUp_ShouldRemoveEventHandlers()
{
var resizeProvider = new Mock<IResizerProvider>();
var control = new ResizeControl(resizeProvider.Object);
var diagram = new TestDiagram();
var model = Mock.Of<Model>();
var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);

control.OnPointerDown(diagram, model, eventArgs);
diagram.TriggerPointerUp(model, eventArgs);
control.OnPointerDown(diagram, model, eventArgs);
diagram.TriggerPointerUp(model, eventArgs);

diagram.TriggerPointerMove(model, eventArgs);
resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Never);
diagram.TriggerPointerMove(model, eventArgs);
resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Never);

diagram.TriggerPointerUp(model, eventArgs);
resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once);
}
diagram.TriggerPointerUp(model, eventArgs);
resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once);
}
}
Loading

0 comments on commit 920dc36

Please sign in to comment.