chore: clarify ownership in WindowSystem, deprecate GetWindow(name)

This commit is contained in:
goat 2023-01-27 00:58:04 +01:00
parent 83586fd46b
commit 905fefe877
No known key found for this signature in database
GPG key ID: 49E2AA8C6A76498B

View file

@ -62,6 +62,8 @@ public class WindowSystem
/// <summary> /// <summary>
/// Add a window to this <see cref="WindowSystem"/>. /// Add a window to this <see cref="WindowSystem"/>.
/// The window system doesn't own your window, it just renders it
/// You need to store a reference to it to use it later.
/// </summary> /// </summary>
/// <param name="window">The window to add.</param> /// <param name="window">The window to add.</param>
public void AddWindow(Window window) public void AddWindow(Window window)
@ -74,6 +76,7 @@ public class WindowSystem
/// <summary> /// <summary>
/// Remove a window from this <see cref="WindowSystem"/>. /// Remove a window from this <see cref="WindowSystem"/>.
/// Will not dispose your window, if it is disposable.
/// </summary> /// </summary>
/// <param name="window">The window to remove.</param> /// <param name="window">The window to remove.</param>
public void RemoveWindow(Window window) public void RemoveWindow(Window window)
@ -81,31 +84,21 @@ public class WindowSystem
if (!this.windows.Contains(window)) if (!this.windows.Contains(window))
throw new ArgumentException("This window is not registered on this WindowSystem."); throw new ArgumentException("This window is not registered on this WindowSystem.");
if (window is IDisposable disposable)
disposable.Dispose();
this.windows.Remove(window); this.windows.Remove(window);
} }
/// <summary> /// <summary>
/// Remove all windows from this <see cref="WindowSystem"/>. /// Remove all windows from this <see cref="WindowSystem"/>.
/// Will not dispose your windows, if they are disposable.
/// </summary> /// </summary>
public void RemoveAllWindows() public void RemoveAllWindows() => this.windows.Clear();
{
foreach (var window in this.windows)
{
if (window is IDisposable disposable)
disposable.Dispose();
}
this.windows.Clear();
}
/// <summary> /// <summary>
/// Get a window by name. /// Get a window by name.
/// </summary> /// </summary>
/// <param name="windowName">The name of the <see cref="Window"/>.</param> /// <param name="windowName">The name of the <see cref="Window"/>.</param>
/// <returns>The <see cref="Window"/> object with matching name or null.</returns> /// <returns>The <see cref="Window"/> object with matching name or null.</returns>
[Obsolete("WindowSystem does not own your window - you should store a reference to it and use that instead.")]
public Window? GetWindow(string windowName) => this.windows.FirstOrDefault(w => w.WindowName == windowName); public Window? GetWindow(string windowName) => this.windows.FirstOrDefault(w => w.WindowName == windowName);
/// <summary> /// <summary>