From 63a9d46cc49e1f6b6ae8214ae2b806c5dfe25736 Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Sun, 29 Sep 2024 23:15:52 +0300 Subject: [PATCH 1/6] AlwaysOn, Multiple control containers --- .../Controls/ControlsBehavior.cs | 98 ++++++------- .../Controls/ControlsContainer.cs | 4 +- .../Controls/ControlsLayer.cs | 137 +++++++++++------- .../Controls/ControlsType.cs | 3 +- src/Blazor.Diagrams.Core/Layers/LinkLayer.cs | 3 +- .../Controls/ControlsLayerRenderer.razor | 56 +++---- .../Controls/ControlsLayerRenderer.razor.cs | 2 +- 7 files changed, 160 insertions(+), 143 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsBehavior.cs b/src/Blazor.Diagrams.Core/Controls/ControlsBehavior.cs index 05148b5e6..19172c02e 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsBehavior.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsBehavior.cs @@ -5,57 +5,49 @@ namespace Blazor.Diagrams.Core.Controls; public class ControlsBehavior : Behavior { - public ControlsBehavior(Diagram diagram) : base(diagram) - { - Diagram.PointerEnter += OnPointerEnter; - Diagram.PointerLeave += OnPointerLeave; - Diagram.SelectionChanged += OnSelectionChanged; - } - - private void OnSelectionChanged(SelectableModel model) - { - var controls = Diagram.Controls.GetFor(model); - if (controls is not { Type: ControlsType.OnSelection }) - return; - - if (model.Selected) - { - controls.Show(); - } - else - { - controls.Hide(); - } - } - - private void OnPointerEnter(Model? model, PointerEventArgs e) - { - if (model == null) - return; - - var controls = Diagram.Controls.GetFor(model); - if (controls is not { Type: ControlsType.OnHover }) - return; - - controls.Show(); - } - - private void OnPointerLeave(Model? model, PointerEventArgs e) - { - if (model == null) - return; - - var controls = Diagram.Controls.GetFor(model); - if (controls is not { Type: ControlsType.OnHover }) - return; - - controls.Hide(); - } - - public override void Dispose() - { - Diagram.PointerEnter -= OnPointerEnter; - Diagram.PointerLeave -= OnPointerLeave; - Diagram.SelectionChanged -= OnSelectionChanged; - } + public ControlsBehavior(Diagram diagram) : base(diagram) + { + Diagram.PointerEnter += OnPointerEnter; + Diagram.PointerLeave += OnPointerLeave; + Diagram.SelectionChanged += OnSelectionChanged; + } + + private void OnSelectionChanged(SelectableModel model) + { + var controls = Diagram.Controls.GetFor(model, ControlsType.OnSelection); + if (controls is null) + return; + + if (model.Selected) + { + controls.Show(); + } + else + { + controls.Hide(); + } + } + + private void OnPointerEnter(Model? model, PointerEventArgs e) + { + if (model == null) + return; + + Diagram.Controls.GetFor(model, ControlsType.OnHover)?.Show(); + } + + private void OnPointerLeave(Model? model, PointerEventArgs e) + { + if (model == null) + return; + + Diagram.Controls.GetFor(model, ControlsType.OnHover)?.Hide(); + } + + public override void Dispose() + { + Diagram.PointerEnter -= OnPointerEnter; + Diagram.PointerLeave -= OnPointerLeave; + Diagram.SelectionChanged -= OnSelectionChanged; + } } \ No newline at end of file diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs b/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs index 5ebc16b46..16dfa3e25 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs @@ -1,6 +1,4 @@ -using System; using System.Collections; -using System.Collections.Generic; using Blazor.Diagrams.Core.Models.Base; namespace Blazor.Diagrams.Core.Controls; @@ -19,7 +17,7 @@ public ControlsContainer(Model model, ControlsType type = ControlsType.OnSelecti } public Model Model { get; } - public ControlsType Type { get; set; } + public ControlsType Type { get; init; } public bool Visible { get; private set; } public void Show() diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs index 39a331d76..191c13746 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs @@ -1,62 +1,89 @@ -using System; -using System.Collections.Generic; using Blazor.Diagrams.Core.Models.Base; namespace Blazor.Diagrams.Core.Controls; public class ControlsLayer { - private readonly Dictionary _containers; - - public event Action? ChangeCaused; - - public ControlsLayer() - { - _containers = new Dictionary(); - } - - public IReadOnlyCollection Models => _containers.Keys; - - public ControlsContainer AddFor(Model model, ControlsType type = ControlsType.OnSelection) - { - if (_containers.ContainsKey(model)) - return _containers[model]; - - var container = new ControlsContainer(model, type); - container.VisibilityChanged += OnVisibilityChanged; - container.ControlsChanged += RefreshIfVisible; - model.Changed += RefreshIfVisible; - _containers.Add(model, container); - return container; - } - - public ControlsContainer? GetFor(Model model) - { - return _containers.TryGetValue(model, out var container) ? container : null; - } - - public bool RemoveFor(Model model) - { - if (!_containers.TryGetValue(model, out var container)) - return false; - - container.VisibilityChanged -= OnVisibilityChanged; - container.ControlsChanged -= RefreshIfVisible; - model.Changed -= RefreshIfVisible; - _containers.Remove(model); - ChangeCaused?.Invoke(model); - return true; - } - - public bool AreVisibleFor(Model model) => GetFor(model)?.Visible ?? false; - - private void RefreshIfVisible(Model cause) - { - if (!AreVisibleFor(cause)) - return; - - ChangeCaused?.Invoke(cause); - } - - private void OnVisibilityChanged(Model cause) => ChangeCaused?.Invoke(cause); + private readonly Dictionary<(Model Model, ControlsType Type), ControlsContainer> _containers = new(); + + public event Action? ChangeCaused; + + public IEnumerable Models => _containers.Keys.Select(key => key.Model); + public IEnumerable<(Model Model, ControlsType Type)> ContainersKeys => _containers.Keys; + + public ControlsContainer AddFor(Model model, ControlsType type = ControlsType.OnSelection) + { + var key = (model, type); + if (_containers.TryGetValue(key, out ControlsContainer? container)) + return container; + + container = new(model, type); + container.VisibilityChanged += OnVisibilityChanged; + container.ControlsChanged += RefreshIfVisible; + model.Changed += RefreshIfVisible; + + _containers.Add(key, container); + + return container; + } + + public ControlsContainer? GetFor(Model model, ControlsType type) + { + if (_containers.TryGetValue((model, type), out ControlsContainer? container)) + return container; + + return null; + } + + public bool RemoveFor(Model model, ControlsType type) + { + var key = (model, type); + if (!_containers.TryGetValue(key, out var container)) + return false; + + container.VisibilityChanged -= OnVisibilityChanged; + container.ControlsChanged -= RefreshIfVisible; + model.Changed -= RefreshIfVisible; + _containers.Remove(key); + ChangeCaused?.Invoke(model); + return true; + } + + public bool RemoveFor(Model model) + { + bool removed = false; + foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) + { + var key = (model, type); + if (_containers.TryGetValue(key, out var container)) + { + container.VisibilityChanged -= OnVisibilityChanged; + container.ControlsChanged -= RefreshIfVisible; + model.Changed -= RefreshIfVisible; + _containers.Remove(key); + ChangeCaused?.Invoke(model); + removed = true; + } + + } + + return removed; + } + + public bool AreVisibleFor(Model model, ControlsType type) => GetFor(model, type)?.Visible ?? false; + + private void RefreshIfVisible(Model cause) + { + foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) + { + if (AreVisibleFor(cause, type)) + { + ChangeCaused?.Invoke(cause); + return; + } + } + + } + + private void OnVisibilityChanged(Model cause) => ChangeCaused?.Invoke(cause); } \ No newline at end of file diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsType.cs b/src/Blazor.Diagrams.Core/Controls/ControlsType.cs index 5997e72c9..641a79fdd 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsType.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsType.cs @@ -3,5 +3,6 @@ namespace Blazor.Diagrams.Core.Controls; public enum ControlsType { OnHover, - OnSelection + OnSelection, + AlwaysOn } \ No newline at end of file diff --git a/src/Blazor.Diagrams.Core/Layers/LinkLayer.cs b/src/Blazor.Diagrams.Core/Layers/LinkLayer.cs index b1e8983e4..db9c93b3f 100644 --- a/src/Blazor.Diagrams.Core/Layers/LinkLayer.cs +++ b/src/Blazor.Diagrams.Core/Layers/LinkLayer.cs @@ -1,6 +1,5 @@ using Blazor.Diagrams.Core.Anchors; using Blazor.Diagrams.Core.Models.Base; -using System.Linq; namespace Blazor.Diagrams.Core.Layers; @@ -30,7 +29,7 @@ protected override void OnItemRemoved(BaseLinkModel link) link.TargetChanged -= OnLinkTargetChanged; Diagram.Controls.RemoveFor(link); - Remove(link.Links.ToList()); + Remove(link.Links); } private static void OnLinkSourceChanged(BaseLinkModel link, Anchor old, Anchor @new) diff --git a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor index 7146c2316..e5f3f0a57 100644 --- a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor +++ b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor @@ -1,33 +1,33 @@ -@foreach (var model in BlazorDiagram.Controls.Models) +@foreach (var key in BlazorDiagram.Controls.ContainersKeys) { - var controls = BlazorDiagram.Controls.GetFor(model)!; - if (!controls.Visible || controls.Count == 0) - continue; + var controls = BlazorDiagram.Controls.GetFor(key.Model, key.Type)!; + if (!controls.Visible || controls.Count == 0) + continue; - if (Svg && model.IsSvg()) - { - - @foreach (var control in controls) - { - var position = control.GetPosition(model); - if (position == null) - continue; + if (Svg && key.Model.IsSvg()) + { + + @foreach (var control in controls) + { + var position = control.GetPosition(key.Model); + if (position == null) + continue; - @RenderControl(model, control, position, true) - } - - } - else if (!Svg && !model.IsSvg()) - { -
- @foreach (var control in controls) - { - var position = control.GetPosition(model); - if (position == null) - continue; + @RenderControl(key.Model, control, position, true) + } + + } + else if (!Svg && !key.Model.IsSvg()) + { +
+ @foreach (var control in controls) + { + var position = control.GetPosition(key.Model); + if (position == null) + continue; - @RenderControl(model, control, position, false) - } -
- } + @RenderControl(key.Model, control, position, false) + } +
+ } } \ No newline at end of file diff --git a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs index e3e8f4a64..337741cbf 100644 --- a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs +++ b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs @@ -26,7 +26,7 @@ public void Dispose() protected override void OnInitialized() { BlazorDiagram.Controls.ChangeCaused += OnControlsChangeCaused; - } + } protected override bool ShouldRender() { From fd74f2f64609f29816a76f1f189c54acbfe2d21f Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Sun, 29 Sep 2024 23:23:40 +0300 Subject: [PATCH 2/6] GetFor multiple containers --- .../Controls/ControlsLayer.cs | 184 ++++++++++-------- 1 file changed, 102 insertions(+), 82 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs index 191c13746..a08cab279 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs @@ -4,86 +4,106 @@ namespace Blazor.Diagrams.Core.Controls; public class ControlsLayer { - private readonly Dictionary<(Model Model, ControlsType Type), ControlsContainer> _containers = new(); - - public event Action? ChangeCaused; - - public IEnumerable Models => _containers.Keys.Select(key => key.Model); - public IEnumerable<(Model Model, ControlsType Type)> ContainersKeys => _containers.Keys; - - public ControlsContainer AddFor(Model model, ControlsType type = ControlsType.OnSelection) - { - var key = (model, type); - if (_containers.TryGetValue(key, out ControlsContainer? container)) - return container; - - container = new(model, type); - container.VisibilityChanged += OnVisibilityChanged; - container.ControlsChanged += RefreshIfVisible; - model.Changed += RefreshIfVisible; - - _containers.Add(key, container); - - return container; - } - - public ControlsContainer? GetFor(Model model, ControlsType type) - { - if (_containers.TryGetValue((model, type), out ControlsContainer? container)) - return container; - - return null; - } - - public bool RemoveFor(Model model, ControlsType type) - { - var key = (model, type); - if (!_containers.TryGetValue(key, out var container)) - return false; - - container.VisibilityChanged -= OnVisibilityChanged; - container.ControlsChanged -= RefreshIfVisible; - model.Changed -= RefreshIfVisible; - _containers.Remove(key); - ChangeCaused?.Invoke(model); - return true; - } - - public bool RemoveFor(Model model) - { - bool removed = false; - foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) - { - var key = (model, type); - if (_containers.TryGetValue(key, out var container)) - { - container.VisibilityChanged -= OnVisibilityChanged; - container.ControlsChanged -= RefreshIfVisible; - model.Changed -= RefreshIfVisible; - _containers.Remove(key); - ChangeCaused?.Invoke(model); - removed = true; - } - - } - - return removed; - } - - public bool AreVisibleFor(Model model, ControlsType type) => GetFor(model, type)?.Visible ?? false; - - private void RefreshIfVisible(Model cause) - { - foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) - { - if (AreVisibleFor(cause, type)) - { - ChangeCaused?.Invoke(cause); - return; - } - } - - } - - private void OnVisibilityChanged(Model cause) => ChangeCaused?.Invoke(cause); + private readonly Dictionary<(Model Model, ControlsType Type), ControlsContainer> _containers = new(); + + public event Action? ChangeCaused; + + public IEnumerable Models => _containers.Keys.Select(key => key.Model); + public IEnumerable<(Model Model, ControlsType Type)> ContainersKeys => _containers.Keys; + + public ControlsContainer AddFor(Model model, ControlsType type = ControlsType.OnSelection) + { + var key = (model, type); + if (_containers.TryGetValue(key, out ControlsContainer? container)) + return container; + + container = new(model, type); + container.VisibilityChanged += OnVisibilityChanged; + container.ControlsChanged += RefreshIfVisible; + model.Changed += RefreshIfVisible; + + _containers.Add(key, container); + + return container; + } + + public ControlsContainer? GetFor(Model model, ControlsType type) + { + if (_containers.TryGetValue((model, type), out ControlsContainer? container)) + return container; + + return null; + } + + /// + /// Will return ALL registered containers for model. Null if no containers registered + /// + /// + /// + public IReadOnlyCollection? GetFor(Model model) + { + List? containers = new(); + foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) + { + if (_containers.TryGetValue((model, type), out ControlsContainer? container)) + containers.Add(container); + } + + if (containers.Count == 0) + return null; + + return containers.AsReadOnly(); + } + + public bool RemoveFor(Model model, ControlsType type) + { + var key = (model, type); + if (!_containers.TryGetValue(key, out var container)) + return false; + + container.VisibilityChanged -= OnVisibilityChanged; + container.ControlsChanged -= RefreshIfVisible; + model.Changed -= RefreshIfVisible; + _containers.Remove(key); + ChangeCaused?.Invoke(model); + return true; + } + + public bool RemoveFor(Model model) + { + bool removed = false; + foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) + { + var key = (model, type); + if (_containers.TryGetValue(key, out var container)) + { + container.VisibilityChanged -= OnVisibilityChanged; + container.ControlsChanged -= RefreshIfVisible; + model.Changed -= RefreshIfVisible; + _containers.Remove(key); + ChangeCaused?.Invoke(model); + removed = true; + } + + } + + return removed; + } + + public bool AreVisibleFor(Model model, ControlsType type) => GetFor(model, type)?.Visible ?? false; + + private void RefreshIfVisible(Model cause) + { + foreach (ControlsType type in (ControlsType[])Enum.GetValues(typeof(ControlsType))) + { + if (AreVisibleFor(cause, type)) + { + ChangeCaused?.Invoke(cause); + return; + } + } + + } + + private void OnVisibilityChanged(Model cause) => ChangeCaused?.Invoke(cause); } \ No newline at end of file From dca24f54c4b293f455183cecd06ecb5f0b89c95c Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Mon, 30 Sep 2024 00:34:54 +0300 Subject: [PATCH 3/6] Removed unnecessary if --- src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs index a08cab279..f098f177a 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsLayer.cs @@ -29,10 +29,9 @@ public ControlsContainer AddFor(Model model, ControlsType type = ControlsType.On public ControlsContainer? GetFor(Model model, ControlsType type) { - if (_containers.TryGetValue((model, type), out ControlsContainer? container)) - return container; + _containers.TryGetValue((model, type), out ControlsContainer? container); - return null; + return container; } /// From 28b51736a3052cc500f0a35c387217d120f3081f Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Mon, 30 Sep 2024 14:19:29 +0300 Subject: [PATCH 4/6] Fixed "AlwaysOn" was not set to visible at start --- site/Site/Pages/Documentation/Controls/Customization.razor | 1 + src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/site/Site/Pages/Documentation/Controls/Customization.razor b/site/Site/Pages/Documentation/Controls/Customization.razor index fd33dcf7a..6211efd70 100644 --- a/site/Site/Pages/Documentation/Controls/Customization.razor +++ b/site/Site/Pages/Documentation/Controls/Customization.razor @@ -156,6 +156,7 @@ public class AlertControl : ExecutableControl cNode2.Title = "Hover on me"; _cDiagram.Controls.AddFor(cNode1, ControlsType.OnSelection).Add(new NodeInformationControl()); _cDiagram.Controls.AddFor(cNode2, ControlsType.OnHover).Add(new NodeInformationControl()); + _cDiagram.Controls.AddFor(cNode2, ControlsType.AlwaysOn).Add(new NodeInformationControl()); _cDiagram.SelectModel(cNode1, false); // Executable Control diff --git a/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs b/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs index 16dfa3e25..e741e2726 100644 --- a/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs +++ b/src/Blazor.Diagrams.Core/Controls/ControlsContainer.cs @@ -14,6 +14,8 @@ public ControlsContainer(Model model, ControlsType type = ControlsType.OnSelecti { Model = model; Type = type; + + Visible = type == ControlsType.AlwaysOn; } public Model Model { get; } @@ -24,7 +26,7 @@ public void Show() { if (Visible) return; - + Visible = true; VisibilityChanged?.Invoke(Model); } @@ -33,7 +35,7 @@ public void Hide() { if (!Visible) return; - + Visible = false; VisibilityChanged?.Invoke(Model); } From 618b347325a8d330f7c55d40f76301a247cf470b Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Tue, 1 Oct 2024 16:14:16 +0300 Subject: [PATCH 5/6] Fixed strange behavior of AlwaysOn controls --- .../Controls/ControlsLayerRenderer.razor | 4 +- .../Controls/ControlsLayerRenderer.razor.cs | 56 +++++++++---------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor index e5f3f0a57..ea336bc75 100644 --- a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor +++ b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor @@ -6,7 +6,7 @@ if (Svg && key.Model.IsSvg()) { - + @foreach (var control in controls) { var position = control.GetPosition(key.Model); @@ -19,7 +19,7 @@ } else if (!Svg && !key.Model.IsSvg()) { -
+
@foreach (var control in controls) { var position = control.GetPosition(key.Model); diff --git a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs index 337741cbf..c7f80cbe1 100644 --- a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs +++ b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs @@ -47,38 +47,36 @@ private void OnControlsChangeCaused(Model cause) } private RenderFragment RenderControl(Model model, Control control, Point position, bool svg) - { - var componentType = BlazorDiagram.GetComponent(control.GetType()); - if (componentType == null) - throw new BlazorDiagramsException( - $"A component couldn't be found for the user action {control.GetType().Name}"); + { + var componentType = BlazorDiagram.GetComponent(control.GetType()) ?? throw new BlazorDiagramsException($"A component couldn't be found for the user action {control.GetType().Name}"); - return builder => - { - builder.OpenElement(0, svg ? "g" : "div"); - builder.AddAttribute(1, "class", - $"{(control is ExecutableControl ? "executable " : "")}diagram-control {control.GetType().Name}"); - if (svg) - builder.AddAttribute(2, "transform", - $"translate({position.X.ToInvariantString()} {position.Y.ToInvariantString()})"); - else - builder.AddAttribute(2, "style", - $"top: {position.Y.ToInvariantString()}px; left: {position.X.ToInvariantString()}px"); + return builder => + { + var index = 0; + builder.OpenElement(index, svg ? "g" : "div"); + builder.AddAttribute(++index, "class", $"{(control is ExecutableControl ? "executable " : "")}diagram-control {control.GetType().Name}"); + if (svg) + { + builder.AddAttribute(++index, "transform", $"translate({position.X.ToInvariantString()} {position.Y.ToInvariantString()})"); + } + else + { + builder.AddAttribute(++index, "style", $"top: {position.Y.ToInvariantString()}px; left: {position.X.ToInvariantString()}px"); + } - if (control is ExecutableControl ec) - { - builder.AddAttribute(3, "onpointerdown", - EventCallback.Factory.Create(this, e => OnPointerDown(e, model, ec))); - builder.AddEventStopPropagationAttribute(4, "onpointerdown", true); - } + if (control is ExecutableControl ec) + { + builder.AddAttribute(++index, "onpointerdown", EventCallback.Factory.Create(this, e => OnPointerDown(e, model, ec))); + builder.AddEventStopPropagationAttribute(++index, "onpointerdown", true); + } - builder.OpenComponent(5, componentType); - builder.AddAttribute(6, "Control", control); - builder.AddAttribute(7, "Model", model); - builder.CloseComponent(); - builder.CloseElement(); - }; - } + builder.OpenComponent(++index, componentType); + builder.AddAttribute(++index, "Control", control); + builder.AddAttribute(++index, "Model", model); + builder.CloseComponent(); + builder.CloseElement(); + }; + } private async Task OnPointerDown(PointerEventArgs e, Model model, ExecutableControl control) { From ed4729248ebfad2bbdde9cf916cbb0ad6de658d1 Mon Sep 17 00:00:00 2001 From: Kyctarnik Date: Wed, 2 Oct 2024 20:45:50 +0300 Subject: [PATCH 6/6] forgot about https://learn.microsoft.com/en-us/aspnet/core/blazor/advanced-scenarios?view=aspnetcore-8.0 --- .../Controls/ControlsLayerRenderer.razor.cs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs index c7f80cbe1..420617cd4 100644 --- a/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs +++ b/src/Blazor.Diagrams/Components/Controls/ControlsLayerRenderer.razor.cs @@ -52,27 +52,26 @@ private RenderFragment RenderControl(Model model, Control control, Point positio return builder => { - var index = 0; - builder.OpenElement(index, svg ? "g" : "div"); - builder.AddAttribute(++index, "class", $"{(control is ExecutableControl ? "executable " : "")}diagram-control {control.GetType().Name}"); + builder.OpenElement(0, svg ? "g" : "div"); + builder.AddAttribute(1, "class", $"{(control is ExecutableControl ? "executable " : "")}diagram-control {control.GetType().Name}"); if (svg) { - builder.AddAttribute(++index, "transform", $"translate({position.X.ToInvariantString()} {position.Y.ToInvariantString()})"); + builder.AddAttribute(2, "transform", $"translate({position.X.ToInvariantString()} {position.Y.ToInvariantString()})"); } else { - builder.AddAttribute(++index, "style", $"top: {position.Y.ToInvariantString()}px; left: {position.X.ToInvariantString()}px"); + builder.AddAttribute(3, "style", $"top: {position.Y.ToInvariantString()}px; left: {position.X.ToInvariantString()}px"); } if (control is ExecutableControl ec) { - builder.AddAttribute(++index, "onpointerdown", EventCallback.Factory.Create(this, e => OnPointerDown(e, model, ec))); - builder.AddEventStopPropagationAttribute(++index, "onpointerdown", true); + builder.AddAttribute(4, "onpointerdown", EventCallback.Factory.Create(this, e => OnPointerDown(e, model, ec))); + builder.AddEventStopPropagationAttribute(5, "onpointerdown", true); } - builder.OpenComponent(++index, componentType); - builder.AddAttribute(++index, "Control", control); - builder.AddAttribute(++index, "Model", model); + builder.OpenComponent(6, componentType); + builder.AddAttribute(7, "Control", control); + builder.AddAttribute(8, "Model", model); builder.CloseComponent(); builder.CloseElement(); };