// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System.Collections; using System.Collections.Generic; using Avalonia.Controls; namespace Avalonia { public class WindowCollection : IReadOnlyList { private readonly Application _application; private readonly List _windows = new List(); public WindowCollection(Application application) { _application = application; } /// /// /// Gets the number of elements in the collection. /// public int Count => _windows.Count; /// /// /// Gets the at the specified index. /// /// /// The . /// /// The index. /// public Window this[int index] => _windows[index]; /// /// /// Returns an enumerator that iterates through the collection. /// /// /// An enumerator that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() { return _windows.GetEnumerator(); } /// /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// /// Adds the specified window. /// /// The window. internal void Add(Window window) { if (window == null) { return; } _windows.Add(window); } /// /// Removes the specified window. /// /// The window. internal void Remove(Window window) { if (window == null) { return; } _windows.Remove(window); OnRemoveWindow(window); } /// /// Closes all windows and removes them from the underlying collection. /// internal void Clear() { while (_windows.Count > 0) { _windows[0].Close(true); } } private void OnRemoveWindow(Window window) { if (window == null) { return; } if (_application.IsShuttingDown) { return; } switch (_application.ShutdownMode) { case ShutdownMode.OnLastWindowClose: if (Count == 0) { _application.Shutdown(); } break; case ShutdownMode.OnMainWindowClose: if (window == _application.MainWindow) { _application.Shutdown(); } break; } } } }