forked from Blazor-Diagrams/Blazor.Diagrams
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add VirtualizationBehavior and VirtualizationOptions and Visible prop…
…erty on Model
- Loading branch information
Showing
15 changed files
with
155 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Blazor.Diagrams.Core/Behaviors/VirtualizationBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Blazor.Diagrams.Core.Models.Base; | ||
|
||
namespace Blazor.Diagrams.Core.Behaviors; | ||
|
||
public class VirtualizationBehavior : Behavior | ||
{ | ||
public VirtualizationBehavior(Diagram diagram) : base(diagram) | ||
{ | ||
Diagram.ZoomChanged += CheckVisibility; | ||
Diagram.PanChanged += CheckVisibility; | ||
Diagram.ContainerChanged += CheckVisibility; | ||
} | ||
|
||
private void CheckVisibility() | ||
{ | ||
if (!Diagram.Options.Virtualization.Enabled) | ||
return; | ||
|
||
if (Diagram.Container == null) | ||
return; | ||
|
||
if (Diagram.Options.Virtualization.OnNodes) | ||
{ | ||
foreach (var node in Diagram.Nodes) | ||
{ | ||
CheckVisibility(node); | ||
} | ||
} | ||
|
||
if (Diagram.Options.Virtualization.OnGroups) | ||
{ | ||
foreach (var group in Diagram.Groups) | ||
{ | ||
CheckVisibility(group); | ||
} | ||
} | ||
|
||
if (Diagram.Options.Virtualization.OnLinks) | ||
{ | ||
foreach (var link in Diagram.Links) | ||
{ | ||
CheckVisibility(link); | ||
} | ||
} | ||
} | ||
|
||
private void CheckVisibility(Model model) | ||
{ | ||
if (model is not IHasBounds ihb) | ||
return; | ||
|
||
var bounds = ihb.GetBounds(); | ||
if (bounds == null) | ||
return; | ||
|
||
var left = bounds.Left * Diagram.Zoom + Diagram.Pan.X; | ||
var top = bounds.Top * Diagram.Zoom + Diagram.Pan.Y; | ||
var right = left + bounds.Width * Diagram.Zoom; | ||
var bottom = top + bounds.Height * Diagram.Zoom; | ||
model.Visible = right > 0 && left < Diagram.Container!.Width && bottom > 0 && top < Diagram.Container.Height; | ||
} | ||
|
||
public override void Dispose() | ||
{ | ||
Diagram.ZoomChanged -= CheckVisibility; | ||
Diagram.PanChanged -= CheckVisibility; | ||
Diagram.ContainerChanged -= CheckVisibility; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Blazor.Diagrams.Core/Options/DiagramVirtualizationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Blazor.Diagrams.Core.Options; | ||
|
||
public class DiagramVirtualizationOptions | ||
{ | ||
public bool Enabled { get; set; } | ||
public bool OnNodes { get; set; } = true; | ||
public bool OnGroups { get; set; } | ||
public bool OnLinks { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.