Client storage window cleanup (#5772)

Although we save the window to save its position this avoids creating it for every single component even if we never open its UI.
This commit is contained in:
metalgearsloth
2021-12-14 08:32:28 +11:00
committed by GitHub
parent 70655dc8b0
commit 2eca9a94d3

View File

@@ -45,21 +45,32 @@ namespace Content.Client.Storage
public override IReadOnlyList<EntityUid> StoredEntities => _storedEntities; public override IReadOnlyList<EntityUid> StoredEntities => _storedEntities;
protected override void OnAdd() private StorageWindow GetOrCreateWindow()
{
if (_window == null)
{ {
base.OnAdd();
_window = new StorageWindow(this, _playerManager, _entityManager) _window = new StorageWindow(this, _playerManager, _entityManager)
{ {
Title = _entityManager.GetComponent<MetaDataComponent>(Owner).EntityName Title = _entityManager.GetComponent<MetaDataComponent>(Owner).EntityName
}; };
_window.EntityList.GenerateItem += GenerateButton; _window.EntityList.GenerateItem += GenerateButton;
_window.EntityList.ItemPressed += Interact; _window.EntityList.ItemPressed += Interact;
} }
return _window;
}
protected override void OnRemove() protected override void OnRemove()
{ {
_window?.Dispose(); if (_window is { Disposed: false })
{
_window.EntityList.GenerateItem -= GenerateButton;
_window.EntityList.ItemPressed -= Interact;
_window.Dispose();
}
_window = null;
base.OnRemove(); base.OnRemove();
} }
@@ -108,7 +119,7 @@ namespace Content.Client.Storage
_storedEntities = storageState.StoredEntities.ToList(); _storedEntities = storageState.StoredEntities.ToList();
StorageSizeUsed = storageState.StorageSizeUsed; StorageSizeUsed = storageState.StorageSizeUsed;
StorageCapacityMax = storageState.StorageSizeMax; StorageCapacityMax = storageState.StorageSizeMax;
_window?.BuildEntityList(storageState.StoredEntities.ToList()); GetOrCreateWindow().BuildEntityList(storageState.StoredEntities.ToList());
} }
/// <summary> /// <summary>
@@ -134,12 +145,12 @@ namespace Content.Client.Storage
/// </summary> /// </summary>
private void ToggleUI() private void ToggleUI()
{ {
if (_window == null) return; var window = GetOrCreateWindow();
if (_window.IsOpen) if (window.IsOpen)
_window.Close(); window.Close();
else else
_window.OpenCentered(); window.OpenCentered();
} }
private void CloseUI() private void CloseUI()