Skip to content

Commit

Permalink
Merge pull request #20 from WiseTechGlobal/M71/WI00642713/NCN-Cursors
Browse files Browse the repository at this point in the history
Introduced cursor change based on allow panning.
  • Loading branch information
shams-azam-wtg authored Jan 17, 2024
2 parents 5f6626c + 4f97ed2 commit 81a7838
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="diagram-canvas @Class"
tabindex="-1"
style="@Style"
@ref="elementReference"
@onpointerdown="OnPointerDown"
@onpointermove="OnPointerMove"
Expand Down
2 changes: 2 additions & 0 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public partial class DiagramCanvas : IDisposable
{
private DotNetObjectReference<DiagramCanvas>? _reference;
private bool _shouldRender;
private string? Style;

protected ElementReference elementReference;

Expand Down Expand Up @@ -52,6 +53,7 @@ protected override void OnInitialized()

_reference = DotNetObjectReference.Create(this);
BlazorDiagram.Changed += OnDiagramChanged;
Style = BlazorDiagram.Options.AllowPanning ? "cursor: grab; cursor: -webkit-grab;" : "cursor: default;";
}

protected override async Task OnAfterRenderAsync(bool firstRender)
Expand Down
2 changes: 0 additions & 2 deletions src/Blazor.Diagrams/wwwroot/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions tests/Blazor.Diagrams.Tests/Components/DiagramCursorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using AngleSharp.Css.Dom;
using Blazor.Diagrams.Components;
using Blazor.Diagrams.Core.Geometry;
using Bunit;
using FluentAssertions;
using Xunit;

namespace Blazor.Diagrams.Core.Tests.Behaviors
{
public class DiagramCursorTests
{
[Fact]
public void Behavior_WhenPanningOptionIsAllowed_CursorShouldBeGrab()
{
// Arrange
using var ctx = new TestContext();
var diagram = new BlazorDiagram();
diagram.Options.AllowPanning = true;
ctx.JSInterop.Setup<Rectangle>("ZBlazorDiagrams.getBoundingClientRect", _ => true);

// Act
var cut = ctx.RenderComponent<DiagramCanvas>(parameters => parameters
.Add(n => n.BlazorDiagram, diagram));
var diagramCanvas = cut.Find(".diagram-canvas");

// Assert
diagramCanvas.ToMarkup().Should().Contain("cursor: grab; cursor: -webkit-grab;");
}

[Fact]
public void Behavior_WhenPanningOptionIsNotAllowed_CursorShouldBeDefault()
{
// Arrange
using var ctx = new TestContext();
var diagram = new BlazorDiagram();
diagram.Options.AllowPanning = false;
ctx.JSInterop.Setup<Rectangle>("ZBlazorDiagrams.getBoundingClientRect", _ => true);

// Act
var cut = ctx.RenderComponent<DiagramCanvas>(parameters => parameters
.Add(n => n.BlazorDiagram, diagram));
var diagramCanvas = cut.Find(".diagram-canvas");
var canvasStyle = diagramCanvas.GetStyle().CssText;

// Assert
canvasStyle.Should().Contain("cursor: default");
}
}
}

0 comments on commit 81a7838

Please sign in to comment.