Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/IslandWindow.cpp (C++)

Code before:
                auto isCurrentlyDark = Theme::IsSystemInDarkTheme();
                if (isCurrentlyDark != _currentSystemThemeIsDark)
                {
                    _currentSystemThemeIsDark = isCurrentlyDark;
                    updateSettings = true;
                }

                bool isCurrentlyHighContrast = []() {
                    HIGHCONTRAST hc = { 0 };
                    hc.cbSize = sizeof(HIGHCONTRAST);
                    if (SystemParametersInfo(SPI_GETHIGHCONTRAST, sizeof(hc), &hc, 0))
                    {
                        return (bool)(hc.dwFlags & HCF_HIGHCONTRASTON);
                    }
                    return false;
                }();

                if (isCurrentlyHighContrast != _currentHighContrastModeState)
                {
                    _currentHighContrastModeState = isCurrentlyHighContrast;
                    updateSettings = true;
                }

                if (updateSettings)
                {
                    UpdateSettingsRequested.raise(_currentHighContrastModeState);
                }
            }
        }
        break;
    }
    case WM_ENDSESSION:
    {
        // For WM_QUERYENDSESSION and WM_ENDSESSION, refer to:
        //
        // https://docs.microsoft.com/en-us/windows/win32/rstmgr/guidelines-for-applications
        //
        // The OS will send us a WM_QUERYENDSESSION when it's preparing an
        // update for our app. It will then send us a WM_ENDSESSION, which gives
        // us a small timeout (~30s) to actually shut down gracefully. After
        // that timeout, it will send us a WM_CLOSE. If we still don't close
        // after the WM_CLOSE, it'll force-kill us (causing a crash which will be
        // bucketed to MoAppHang).
        //
        // If we need to do anything to prepare for being told to shutdown,
        // start it in WM_QUERYENDSESSION. If (in the future) we need to prevent
        // logoff, we can return false there. (DefWindowProc returns true)
        //
        // The OS is going to shut us down here. We will manually start a quit,
        // so that we can persist the state. If we refuse to gracefully shut
        // down here, the OS will crash us to forcefully terminate us. We choose

Review comment (performance):
Why does the `IslandWindow` of all things need to remember whether we were in dark mode or not? Also, why does it result in `ReloadSettings` when none of the settings changed? An alternative idea:
* Add a `bool CascadiaSettings::ReloadingSystemConfig()` function (or similar).
  * It reads the dark-mode-ness and the high-contrast-ness of the OS.
  * It caches the 2 results inside (for instance) `GlobalAppSettings`.
  * If checks if either of 2 have changed and returns `true` if they did, `false` otherwise.
  * The cached values replace any use of `IsSystemInDarkTheme()` inside the settings code (currently in 2 locations).
* Add an alternative to `ReloadSettings()`, e.g. `ReloadingSystemConfigRelatedSettings()` (lol) and call that in `WindowEmperor::_windowRequestUpdateSettings()`.
* In `ReloadingSystemConfigRelatedSettings()` call `_settings.ReloadingSystemConfig()` and check if it returned `true`. If it did: Raise a `SettingsChanged` event, just like we do in `ReloadSettings`.

Code after:
// - Get the dimensions of our non-client area, as a rect where each component
//   represents that side.
// - The .left will be a negative number, to represent that the actual side of
//   the non-client area is outside the border of our window. It's roughly 8px (
//   * DPI scaling) to the left of the visible border.
// - The .right component will be positive, indicating that the nonclient border
//   is in the positive-x direction from the edge of our client area.
// - This will also include our titlebar! It's in the nonclient area for us.
// Arguments:
// - dpi: the scaling that we should use to calculate the border sizes.
// Return Value:
// - a til::rect whose components represent the margins of the nonclient area,
//   relative to the client area.
til::rect IslandWindow::GetNonClientFrame(const UINT dpi) const noexcept
{
    const auto windowStyle = static_cast<DWORD>(GetWindowLong(_window.get(), GWL_STYLE));
    RECT islandFrame{};

    // If we failed to get the correct window size for whatever reason, log
    // the error and go on. We'll use whatever the control proposed as the
    // size of our window, which will be at least close.
    LOG_IF_WIN32_BOOL_FALSE(AdjustWindowRectExForDpi(&islandFrame, windowStyle, false, 0, dpi));
    return til::rect{ islandFrame };
}

// Method Description:
// - Gets the difference between window and client area size.
// Arguments:
// - dpi: dpi of a monitor on which the window is placed
// Return Value
// - The size difference
til::size IslandWindow::GetTotalNonClientExclusiveSize(const UINT dpi) const noexcept
{
    const auto islandFrame{ GetNonClientFrame(dpi) };
    return {
        islandFrame.right - islandFrame.left,
        islandFrame.bottom - islandFrame.top
    };
}

void IslandWindow::OnAppInitialized()
{
    // Do a quick resize to force the island to paint
    const auto size = GetPhysicalSize();
    OnSize(size.width, size.height);
}

// Method Description:
// - Called when the app wants to change its theme. We'll update the root UI
//   element of the entire XAML tree, so that all UI elements get the theme
//   applied.