Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close organelle menu when leaving cell editor #5775

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 66 additions & 1 deletion src/gui_common/ModalManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public partial class ModalManager : NodeWithInput
/// </summary>
private readonly Dictionary<TopLevelContainer, Node> originalParents = new();

/// <summary>
/// Contains the callable that is used to delete the modal when the parent leaves the tree.
/// </summary>
private readonly Dictionary<TopLevelContainer, Callable> parentLostCallables = new();

private readonly Deque<TopLevelContainer> modalStack = new();
private readonly Queue<TopLevelContainer> demotedModals = new();

Expand Down Expand Up @@ -85,7 +90,15 @@ public void MakeModal(TopLevelContainer popup)
if (modalStack.Contains(popup))
return;

originalParents[popup] = popup.GetParent();
var parent = popup.GetParent();
originalParents[popup] = parent;

// Listen for when the parent is removed from the tree so the modal can be removed as well.
var parentLostCallable = Callable.From(() => OnParentLost(popup));
parent.Connect(Node.SignalName.TreeExiting, parentLostCallable,
(uint)ConnectFlags.OneShot);
parentLostCallables[popup] = parentLostCallable;

modalStack.AddToFront(popup);
modalsDirty = true;

Expand Down Expand Up @@ -153,12 +166,28 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

/// <summary>
/// Parent lost signals are added when a modal is created so that the modal can be automatically deleted.
/// </summary>
private void DeleteParentLostSignal(TopLevelContainer modal)
{
if (!originalParents.TryGetValue(modal, out var parent))
return;
if (!parentLostCallables.TryGetValue(modal, out var parentLostCallable))
return;

parent.Disconnect(Node.SignalName.TreeExiting, parentLostCallable);
parentLostCallables.Remove(modal);
}

private void UpdateModals()
{
while (demotedModals.Count > 0)
{
var modal = demotedModals.Dequeue();

DeleteParentLostSignal(modal);

if (!originalParents.TryGetValue(modal, out var parent))
{
modal.ReParent(this);
Expand Down Expand Up @@ -272,4 +301,40 @@ private void OnModalLost(TopLevelContainer popup)

modalsDirty = true;
}

/// <summary>
/// Called when the parent of a <paramref name="popup"/> is deleted from the scene tree.
/// </summary>
/// <remarks>
/// <para>
/// If a popup is demoted but its parent is invalid, an exception will occur.
/// Deleting the modal here and removing references to it will avoid the exception.
/// </para>
/// </remarks>
private void OnParentLost(TopLevelContainer popup)
{
// Remove the original parent since the reference will now be invalid
var popupWasInDictionary = originalParents.Remove(popup);

// Guard against duplicate signals
if (!popupWasInDictionary)
return;

// Delete the popup
popup.Close();
modalStack.Remove(popup);
popup.QueueFree();
DeleteParentLostSignal(popup);

// Remove the popup from the demoted modals since it is now destroyed
var demotedModalsCount = demotedModals.Count;
for (int i = 0; i < demotedModalsCount; ++i)
{
var item = demotedModals.Dequeue();
if (item != popup)
demotedModals.Enqueue(item);
}

modalsDirty = true;
}
}