diff --git a/source/CPDLCPlugin/Messages/ConnectedNotification.cs b/source/CPDLCPlugin/Messages/ConnectedNotification.cs index f23444e..c958715 100644 --- a/source/CPDLCPlugin/Messages/ConnectedNotification.cs +++ b/source/CPDLCPlugin/Messages/ConnectedNotification.cs @@ -15,6 +15,8 @@ public class ConnectedNotificationHandler( AdscReportStore adscReportStore, AdscTrackDisplay adscTrackDisplay, AtsuSettingsStore atsuSettingsStore, + IJurisdictionChecker jurisdictionChecker, + IMediator mediator, ILogger logger) : INotificationHandler { @@ -73,5 +75,12 @@ public async Task Handle(ConnectedNotification notification, CancellationToken c // Relay notification to UI WeakReferenceMessenger.Default.Send(notification); + + // If there are dialogues relevant to the controller, open the current messages window + var hasDialoguesToDisplay = dialogues.Any(d => !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d)); + if (hasDialoguesToDisplay) + { + await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true), cancellationToken); + } } } diff --git a/source/CPDLCPlugin/Messages/FdrOwnerChanged.cs b/source/CPDLCPlugin/Messages/FdrOwnerChanged.cs new file mode 100644 index 0000000..be83f22 --- /dev/null +++ b/source/CPDLCPlugin/Messages/FdrOwnerChanged.cs @@ -0,0 +1,3 @@ +namespace CPDLCPlugin.Messages; + +public record FdrOwnerChanged(string Callsign); diff --git a/source/CPDLCPlugin/Messages/OpenCurrentMessagesWindowRequest.cs b/source/CPDLCPlugin/Messages/OpenCurrentMessagesWindowRequest.cs index abfab4d..ff0d514 100644 --- a/source/CPDLCPlugin/Messages/OpenCurrentMessagesWindowRequest.cs +++ b/source/CPDLCPlugin/Messages/OpenCurrentMessagesWindowRequest.cs @@ -1,3 +1,4 @@ +using CommunityToolkit.Mvvm.Messaging; using CPDLCPlugin.Configuration; using CPDLCPlugin.ViewModels; using CPDLCPlugin.Windows; @@ -6,7 +7,8 @@ namespace CPDLCPlugin.Messages; -public record OpenCurrentMessagesWindowRequest : IRequest; +// When OpenedAutomatically is true, the window closes automatically when it has no messages. +public record OpenCurrentMessagesWindowRequest(bool OpenedAutomatically) : IRequest; public class OpenCurrentMessagesWindowRequestHandler( WindowManager windowManager, @@ -20,6 +22,9 @@ public class OpenCurrentMessagesWindowRequestHandler( { public Task Handle(OpenCurrentMessagesWindowRequest request, CancellationToken cancellationToken) { + // If the window is already open, tell it how it was opened + WeakReferenceMessenger.Default.Send(request); + windowManager.FocusOrCreateWindow( WindowKeys.CurrentMessages, "Current Messages", @@ -32,7 +37,8 @@ public Task Handle(OpenCurrentMessagesWindowRequest request, CancellationToken c guiInvoker, errorReporter, windowHandle, - jurisdictionChecker); + jurisdictionChecker, + closeWhenEmpty: request.OpenedAutomatically); return new CurrentMessagesView(viewModel, windowHandle); }, diff --git a/source/CPDLCPlugin/Plugin.cs b/source/CPDLCPlugin/Plugin.cs index 38494cc..15c8ebb 100644 --- a/source/CPDLCPlugin/Plugin.cs +++ b/source/CPDLCPlugin/Plugin.cs @@ -378,7 +378,7 @@ void AddToolbarItems() _workQueue.Enqueue(async () => { var mediator = ServiceProvider.GetRequiredService(); - await mediator.Send(new OpenCurrentMessagesWindowRequest()); + await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: false)); }); }; @@ -578,9 +578,15 @@ public void OnFDRUpdate(FDP2.FDR updated) var jurisdictionChecker = ServiceProvider.GetRequiredService(); + var previousRecord = jurisdictionChecker.GetOwnershipRecord(updated.Callsign); jurisdictionChecker.RecordFdrOwner(updated.Callsign, updated.ControllerTracking?.Callsign); var record = jurisdictionChecker.GetOwnershipRecord(updated.Callsign); + + // Tell the Current Messages Window that the dialogues it must show can be different + if (previousRecord?.CurrentOwner != record?.CurrentOwner) + WeakReferenceMessenger.Default.Send(new FdrOwnerChanged(updated.Callsign)); + if (record?.PreviousOwner != Network.Callsign) return; @@ -600,21 +606,22 @@ public void OnFDRUpdate(FDP2.FDR updated) // Re-build the CPDLC status label items _workQueue.Enqueue(() => RebuildCpdlcStatusLabelItems()); - // If this flight has any open dialogues that we have jurisdiction over, open the current messages window + // If this flight has any dialogues that we have jurisdiction over, open the current messages window // Ensures flights handed off to us open the window so we can see their requests + // A closed dialogue stays in the window until it is archived (e.g. a downlink that needs no response) _workQueue.Enqueue(async () => { var store = ServiceProvider.GetRequiredService(); var jurisdictionChecker = ServiceProvider.GetRequiredService(); - var hasOpenDialogues = (await store.All(CancellationToken.None)) + var hasDialoguesToDisplay = (await store.All(CancellationToken.None)) .Where(d => d.AircraftCallsign == updated.Callsign) - .Any(d => !d.IsClosed && !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d)); + .Any(d => !d.IsArchived && jurisdictionChecker.ShouldDisplayDialogue(d)); - if (hasOpenDialogues) + if (hasDialoguesToDisplay) { var mediator = ServiceProvider.GetRequiredService(); - await mediator.Send(new OpenCurrentMessagesWindowRequest()); + await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true)); } }); } @@ -716,7 +723,7 @@ public void Receive(DialogueChangedNotification dialogueChangedNotification) } var mediator = ServiceProvider.GetRequiredService(); - await mediator.Send(new OpenCurrentMessagesWindowRequest()); + await mediator.Send(new OpenCurrentMessagesWindowRequest(OpenedAutomatically: true)); }); } catch (Exception ex) diff --git a/source/CPDLCPlugin/ViewModels/CurrentMessagesViewModel.cs b/source/CPDLCPlugin/ViewModels/CurrentMessagesViewModel.cs index 4aea07a..8362b2b 100644 --- a/source/CPDLCPlugin/ViewModels/CurrentMessagesViewModel.cs +++ b/source/CPDLCPlugin/ViewModels/CurrentMessagesViewModel.cs @@ -10,7 +10,13 @@ namespace CPDLCPlugin.ViewModels; -public partial class CurrentMessagesViewModel : ObservableObject, IRecipient, IDisposable +public partial class CurrentMessagesViewModel : + ObservableObject, + IRecipient, + IRecipient, + IRecipient, + IRecipient, + IDisposable { readonly PluginConfiguration _configuration; readonly DialogueStore _dialogueStore; @@ -19,6 +25,7 @@ public partial class CurrentMessagesViewModel : ObservableObject, IRecipient(this); + WeakReferenceMessenger.Default.Register(this); + WeakReferenceMessenger.Default.Register(this); + WeakReferenceMessenger.Default.Register(this); // Initial load. The window stays open when there are no messages, so that it can open empty. _ = LoadDialoguesAsync(closeIfEmpty: false); @@ -50,6 +62,9 @@ public void Dispose() return; WeakReferenceMessenger.Default.Unregister(this); + WeakReferenceMessenger.Default.Unregister(this); + WeakReferenceMessenger.Default.Unregister(this); + WeakReferenceMessenger.Default.Unregister(this); _disposed = true; } @@ -557,8 +572,9 @@ async Task LoadDialoguesAsync(bool closeIfEmpty) Dialogues.Add(dialogueViewModel); } - // Close the window if there are no messages left - if (closeIfEmpty && Dialogues.Count == 0) + // Close the window if there are no messages left. + // Only a window that opened automatically closes automatically. + if (closeIfEmpty && _closeWhenEmpty && Dialogues.Count == 0) { _windowHandle.Close(); } @@ -583,6 +599,45 @@ public void Receive(DialogueChangedNotification message) }); } + // The jurisdiction of a flight can change, so reload the dialogues that we must show + public void Receive(FdrOwnerChanged message) + { + if (_disposed) + return; + + _guiInvoker.InvokeOnGUI(async _ => + { + if (_disposed) + return; + + await LoadDialoguesAsync(closeIfEmpty: true); + }); + } + + // The dialogues are loaded when we connect, so reload the dialogues that we must show + public void Receive(ConnectedNotification message) + { + if (_disposed) + return; + + _guiInvoker.InvokeOnGUI(async _ => + { + if (_disposed) + return; + + await LoadDialoguesAsync(closeIfEmpty: true); + }); + } + + // The window must not close automatically after the controller opens it manually + public void Receive(OpenCurrentMessagesWindowRequest message) + { + if (_disposed || message.OpenedAutomatically) + return; + + _guiInvoker.InvokeOnGUI(_ => _closeWhenEmpty = false); + } + [RelayCommand] async Task SendStandby(CurrentMessageViewModel currentMessageViewModel) {