Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Blazor.Diagrams.Core/Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Blazor.Diagrams.Core;

public abstract class Behavior : IDisposable
public abstract class Behavior : IBehavior, IDisposable
{
public Behavior(Diagram diagram)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Blazor.Diagrams.Core/Behaviors/DragNewLinkBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Blazor.Diagrams.Core.Behaviors;

public class DragNewLinkBehavior : Behavior
public class DragNewLinkBehavior : Behavior, IDragNewLinkBehavior
{
private PositionAnchor? _targetPositionAnchor;

Expand Down
14 changes: 14 additions & 0 deletions src/Blazor.Diagrams.Core/Behaviors/IDragNewLinkBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Blazor.Diagrams.Core.Models.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blazor.Diagrams.Core.Behaviors;

public interface IDragNewLinkBehavior : IBehavior
{
BaseLinkModel? OngoingLink { get; }
void StartFrom(ILinkable source, double clientX, double clientY);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override ValueTask OnPointerDown(Diagram diagram, Model model, PointerEve
if (model is not BaseLinkModel link)
throw new DiagramsException("ArrowHeadControl only works for models of type BaseLinkModel");

var dnlb = diagram.GetBehavior<DragNewLinkBehavior>()!;
var dnlb = diagram.GetBehavior<IDragNewLinkBehavior>()!;
if (Source)
{
link.SetSource(link.Target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override ValueTask OnPointerDown(Diagram diagram, Model model, PointerEve
if (model is not NodeModel node || node.Locked)
return ValueTask.CompletedTask;

var behavior = diagram.GetBehavior<DragNewLinkBehavior>();
var behavior = diagram.GetBehavior<IDragNewLinkBehavior>();
if (behavior == null)
throw new DiagramsException($"DragNewLinkBehavior was not found");

Expand Down
16 changes: 8 additions & 8 deletions src/Blazor.Diagrams.Core/Diagram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Blazor.Diagrams.Core;

public abstract class Diagram
{
private readonly Dictionary<Type, Behavior> _behaviors;
private readonly Dictionary<Type, IBehavior> _behaviors;
private readonly List<SelectableModel> _orderedSelectables;

public event Action<Model?, PointerEventArgs>? PointerDown;
Expand All @@ -40,7 +40,7 @@ public abstract class Diagram

protected Diagram()
{
_behaviors = new Dictionary<Type, Behavior>();
_behaviors = new Dictionary<Type, IBehavior>();
_orderedSelectables = new List<SelectableModel>();

Nodes = new NodeLayer(this);
Expand All @@ -58,7 +58,7 @@ protected Diagram()

RegisterBehavior(new SelectionBehavior(this));
RegisterBehavior(new DragMovablesBehavior(this));
RegisterBehavior(new DragNewLinkBehavior(this));
RegisterBehavior(new DragNewLinkBehavior(this) as IDragNewLinkBehavior);
RegisterBehavior(new PanBehavior(this));
RegisterBehavior(new ZoomBehavior(this));
RegisterBehavior(new EventsBehavior(this));
Expand Down Expand Up @@ -170,22 +170,22 @@ public void UnselectAll()

#region Behaviors

public void RegisterBehavior(Behavior behavior)
public void RegisterBehavior<T>(T behavior) where T : IBehavior
{
var type = behavior.GetType();
var type = typeof(T);
if (_behaviors.ContainsKey(type))
throw new Exception($"Behavior '{type.Name}' already registered");

_behaviors.Add(type, behavior);
}

public T? GetBehavior<T>() where T : Behavior
public T? GetBehavior<T>() where T : IBehavior
{
var type = typeof(T);
return (T?)(_behaviors.ContainsKey(type) ? _behaviors[type] : null);
}

public void UnregisterBehavior<T>() where T : Behavior
public void UnregisterBehavior<T>() where T : IBehavior
{
var type = typeof(T);
if (!_behaviors.ContainsKey(type))
Expand Down Expand Up @@ -346,7 +346,7 @@ public int GetMaxOrder()
public void RefreshOrders(bool refresh = true)
{
_orderedSelectables.Sort((a, b) => a.Order.CompareTo(b.Order));

if (refresh)
{
Refresh();
Expand Down
11 changes: 11 additions & 0 deletions src/Blazor.Diagrams.Core/IBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blazor.Diagrams.Core;

public interface IBehavior : IDisposable
{
}
2 changes: 1 addition & 1 deletion src/Blazor.Diagrams/Components/LinkWidget.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
if (result == null)
return;

var dnlb = BlazorDiagram.GetBehavior<DragNewLinkBehavior>();
var dnlb = BlazorDiagram.GetBehavior<IDragNewLinkBehavior>();
var d = result.FullPath.ToString();
}

Expand Down