Repository: microsoft/terminal (language: cpp)

Issue:
Suppressed application title overwritten after screen lock
### Windows Terminal version

1.17.11461.0

### Windows build number

10.0.22000.0

### Other Software

_No response_

### Steps to reproduce

1. Open a new terminal, and override the application title:
  ```
  wt --suppressApplicationTitle --title example powershell
  ```
2. Observe that the terminal has a tab named `example` 
2. Lock your screen (`Windows` + `L`)
3. Sign back in
4. Observe that the tab name has changed from `example` to `Windows PowerShell`

### Expected Behavior

After unlocking, I would expect the tab name to be unchanged.

### Actual Behavior

After unlocking, the tab name is changed from `example` to `Windows PowerShell`.

Note: This only happens when using the commandline to set the tab title. If you manually override it by double-clicking the tab and entering something there, the tab will keep its name after locking and unlocking.
Suppressed application title overwritten after screen lock
### Windows Terminal version

1.17.11461.0

### Windows build number

10.0.22000.0

### Other Software

_No response_

### Steps to reproduce

1. Open a new terminal, and override the application title:
  ```
  wt --suppressApplicationTitle --title example powershell
  ```
2. Observe that the terminal has a tab named `example` 
2. Lock your screen (`Windows` + `L`)
3. Sign back in
4. Observe that the tab name has changed from `example` to `Windows PowerShell`

### Expected Behavior

After unlocking, I would expect the tab name to be unchanged.

### Actual Behavior

After unlocking, the tab name is changed from `example` to `Windows PowerShell`.

Note: This only happens when using the commandline to set the tab title. If you manually override it by double-clicking the tab and entering something there, the tab will keep its name after locking and unlocking.


Discussion / hints:
Another obvious way of triggering this is setting a fontFace to `garbage` or some other non-existent font, then opening a admin application. The UAC will trigger a "desktop change"[^1]. That'll do the same thing as a lock screen. 

Debugger shows that we get another "ImmersiveColorSet" message, with `wparam=0`. Great. We're using that as a signal that the OS theme changed. When we get that, we reload the whole settings.[^2] 

A stackoverflow post pointed me at maybe [`UISettings.ColorValuesChanged`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.uisettings.colorvalueschanged?view=winrt-22621)

related, but not actually: #11522

[^1]: okay that's not exactly what's happening, on mobile so can't look up the right term. 
[^2]: reloading the settings overwrites the title from the NewTabArgs. That's not great, but I think that's _technically_ tracked in  
Another obvious way of triggering this is setting a fontFace to `garbage` or some other non-existent font, then opening a admin application. The UAC will trigger a "desktop change"[^1]. That'll do the same thing as a lock screen. 

Debugger shows that we get another "ImmersiveColorSet" message, with `wparam=0`. Great. We're using that as a signal that the OS theme changed. When we get that, we reload the whole settings.[^2] 

A stackoverflow post pointed me at maybe [`UISettings.ColorValuesChanged`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.viewmanagement.uisettings.colorvalueschanged?view=winrt-22621)

related, but not actually: #11522

[^1]: okay that's not exactly what's happening, on mobile so can't look up the right term. 
[^2]: reloading the settings overwrites the title from the NewTabArgs. That's not great, but I think that's _technically_ tracked in  

Fix patch:
diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp
index 87ec8d5dbce..9bea05acfe9 100644
--- a/src/cascadia/WindowsTerminal/IslandWindow.cpp
+++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp
@@ -199,6 +199,8 @@ void IslandWindow::_HandleCreateWindow(const WPARAM, const LPARAM lParam) noexce
     UpdateWindow(_window.get());
 
     UpdateWindowIconForActiveMetrics(_window.get());
+
+    _currentSystemThemeIsDark = Theme::IsSystemInDarkTheme();
 }
 
 // Method Description:
@@ -745,7 +747,16 @@ long IslandWindow::_calculateTotalSize(const bool isWidth, const long clientSize
             // themes, color schemes that might depend on the OS theme
             if (param == L"ImmersiveColorSet")
             {
-                _UpdateSettingsRequestedHandlers();
+                // GH#15732: Don't update the settings, unless the theme
+                // _actually_ changed. ImmersiveColorSet gets sent more often
+                // than just on a theme change. It notably gets sent when the PC
+                // is locked, or the UAC prompt opens.
+                auto isCurrentlyDark = Theme::IsSystemInDarkTheme();
+                if (isCurrentlyDark != _currentSystemThemeIsDark)
+                {
+                    _currentSystemThemeIsDark = isCurrentlyDark;
+                    _UpdateSettingsRequestedHandlers();
+                }
             }
         }
         break;
diff --git a/src/cascadia/WindowsTerminal/IslandWindow.h b/src/cascadia/WindowsTerminal/IslandWindow.h
index c0abd343e1b..eee013c529a 100644
--- a/src/cascadia/WindowsTerminal/IslandWindow.h
+++ b/src/cascadia/WindowsTerminal/IslandWindow.h
@@ -117,6 +117,7 @@ class IslandWindow :
     RECT _rcWindowBeforeFullscreen{};
     RECT _rcWorkBeforeFullscreen{};
     UINT _dpiBeforeFullscreen{ 96 };
+    bool _currentSystemThemeIsDark{ true };
 
     void _coldInitialize();
     void _warmInitialize();


Test patch: