|
| 1 | +using Alidade.Handlers.Map; |
| 2 | +using Alidade.Handlers.Selection; |
| 3 | +using Alidade.Handlers.Tool; |
| 4 | + |
| 5 | +namespace Alidade.Components; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Right-click context menu offering Square, Circularize, and Gridify for the target element. |
| 9 | +/// Positioned at the coordinates stored in <see cref="MapStateService"/> and dismissed by |
| 10 | +/// clicking the backdrop or selecting an action. |
| 11 | +/// </summary> |
| 12 | +public partial class ContextMenu( |
| 13 | + MapStateService mapState, |
| 14 | + SelectionStateService selectionState, |
| 15 | + IMediator mediator) : IDisposable |
| 16 | +{ |
| 17 | + /// <inheritdoc /> |
| 18 | + protected override void OnInitialized() |
| 19 | + => mapState.StateChanged += OnStateChanged; |
| 20 | + |
| 21 | + private void OnStateChanged(object? sender, EventArgs e) |
| 22 | + => StateHasChanged(); |
| 23 | + |
| 24 | + private async Task OnSquare() |
| 25 | + { |
| 26 | + await EnsureTargetSelected(); |
| 27 | + await mediator.Publish(new Square.Notification()); |
| 28 | + await mediator.Send(new HideContextMenu.Command()); |
| 29 | + } |
| 30 | + |
| 31 | + private async Task OnCircularize() |
| 32 | + { |
| 33 | + await EnsureTargetSelected(); |
| 34 | + await mediator.Send(new ToggleCircularizeDialog.Command()); |
| 35 | + await mediator.Send(new HideContextMenu.Command()); |
| 36 | + } |
| 37 | + |
| 38 | + private async Task OnGridify() |
| 39 | + { |
| 40 | + await EnsureTargetSelected(); |
| 41 | + await mediator.Send(new ToggleGridifyDialog.Command()); |
| 42 | + await mediator.Send(new HideContextMenu.Command()); |
| 43 | + } |
| 44 | + |
| 45 | + private async Task OnBackdropClick() |
| 46 | + => await mediator.Send(new HideContextMenu.Command()); |
| 47 | + |
| 48 | + private async Task EnsureTargetSelected() |
| 49 | + { |
| 50 | + OsmElementRef? target = mapState.State.ContextMenuTargetElement; |
| 51 | + if (target is not null && !selectionState.State.Selected.Contains(target)) |
| 52 | + { |
| 53 | + await mediator.Send(new Select.Command(target, false)); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + public void Dispose() |
| 59 | + => mapState.StateChanged -= OnStateChanged; |
| 60 | +} |
0 commit comments