Repository: unoplatform/uno
File: src/Uno.UI.Runtime.Skia.Win32/UI/Xaml/Window/Win32WindowWrapper.cs (C#)

Code before:
	private void OnAppWindowTitleBarChanged(object? sender, EventArgs e)
	{
		if (_window is null)
		{
			throw new InvalidOperationException("Cannot extend client area before the Window is set.");
		}

		if (!WasShown)
		{
			return;
		}

		UpdateClientAreaExtension();
	}

	public static IEnumerable<HWND> GetHwnds() => _hwndToWrapper.Keys;

	private unsafe void OnSystemThemeChanged(object? _, EventArgs __)
	{
		BOOL value = Win32SystemThemeHelperExtension.Instance.GetSystemTheme() is SystemTheme.Dark;
		var hResult = PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, &value, (uint)Marshal.SizeOf(value));
		if (hResult.Failed)
		{
			this.LogError()?.Error($"{nameof(PInvoke.DwmSetWindowAttribute)} failed: {Win32Helper.GetErrorMessage(hResult)}");
		}
	}

	private unsafe HWND CreateWindow()
	{
		using var title = new Win32Helper.NativeNulTerminatedUtf16String("Uno Platform");

		var preferredWindowSize = ApplicationView.PreferredLaunchViewSize;
		if (preferredWindowSize.IsEmpty)
		{
			preferredWindowSize = new Size(InitialWidth, InitialHeight);
		}

		using var lpClassName = new Win32Helper.NativeNulTerminatedUtf16String(WindowClassName);

		_wrapperForNextCreateWindow = this;
		var hwnd = PInvoke.CreateWindowEx(
			0,
			lpClassName,
			title,
			WINDOW_STYLE.WS_OVERLAPPEDWINDOW,
			PInvoke.CW_USEDEFAULT,
			PInvoke.CW_USEDEFAULT,
			(int)preferredWindowSize.Width,
			(int)preferredWindowSize.Height,
			HWND.Null,
			HMENU.Null,

Review comment (suggestion):
wondering why it decided to materialize the Keys collection `ToList()` then gets the values using `TryGetValue()`

Why not just:

```csharp
foreach (var wrapper in _hwndToWrapper.Values)
{
    wrapper.CloseCore();
}
```

Code after:

	private void OnAppWindowTitleBarChanged(object? sender, EventArgs e)
	{
		if (_window is null)
		{
			throw new InvalidOperationException("Cannot extend client area before the Window is set.");
		}

		if (!WasShown)
		{
			return;
		}

		UpdateClientAreaExtension();
	}

	public static IEnumerable<HWND> GetHwnds() => _hwndToWrapper.Keys;

	public static void CloseAllWindows()
	{
		foreach (var hwnd in _hwndToWrapper.Keys.ToList())
		{
			if (_hwndToWrapper.TryGetValue(hwnd, out var wrapper))
			{
				wrapper.CloseCore();
			}
		}
	}

	private unsafe void OnSystemThemeChanged(object? _, EventArgs __)
	{
		BOOL value = Win32SystemThemeHelperExtension.Instance.GetSystemTheme() is SystemTheme.Dark;
		var hResult = PInvoke.DwmSetWindowAttribute(_hwnd, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, &value, (uint)Marshal.SizeOf(value));
		if (hResult.Failed)
		{
			this.LogError()?.Error($"{nameof(PInvoke.DwmSetWindowAttribute)} failed: {Win32Helper.GetErrorMessage(hResult)}");
		}
	}

	private unsafe HWND CreateWindow()
	{
		using var title = new Win32Helper.NativeNulTerminatedUtf16String("Uno Platform");

		var preferredWindowSize = ApplicationView.PreferredLaunchViewSize;
		if (preferredWindowSize.IsEmpty)
		{
			preferredWindowSize = new Size(InitialWidth, InitialHeight);
		}

		using var lpClassName = new Win32Helper.NativeNulTerminatedUtf16String(WindowClassName);