Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/MainPage.cpp (C++)

Code before:
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_UpdateBackgroundForMica()
    {
        // DWMWA_SYSTEMBACKDROP_TYPE, which we use for mica, is only available
        // on builds >=2261
        static const bool isMicaAvailable = []() -> bool {
            OSVERSIONINFOEXW osver{};
            osver.dwOSVersionInfoSize = sizeof(osver);
            osver.dwBuildNumber = 22621;
            DWORDLONG dwlConditionMask = 0;
            VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_GREATER_EQUAL);
            return VerifyVersionInfoW(&osver, VER_BUILDNUMBER, dwlConditionMask) != FALSE;
        }();

        if (!isMicaAvailable)
        {
            return;
        }

        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();

        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                               L"SettingsPageMicaBackground" :
                               L"SettingsPageBackground";

        if (const auto bgColor = Resources().TryLookup(winrt::box_value(bgKey)))
        {
            SettingsNav().Background(winrt::WUX::Media::SolidColorBrush(winrt::unbox_value<Windows::UI::Color>(bgColor)));
        }
    }

}


Review comment (security):
I believe it'd be better long-term to come up with a way how we can get to the return value of `DwmSetWindowAttribute` in `IslandWindow::UseMica` instead. Otherwise this code has a hard to understand and maintain, secret dependency to `IslandWindow::UseMica`'s success.

Code after:

    IObservableVector<IInspectable> MainPage::Breadcrumbs() noexcept
    {
        return _breadcrumbs;
    }

    winrt::Windows::UI::Xaml::Media::Brush MainPage::BackgroundBrush()
    {
        return SettingsNav().Background();
    }

    // If the theme asks for Mica, then drop out our background, so that we
    // can have mica too.
    void MainPage::_UpdateBackgroundForMica()
    {
        bool isMicaAvailable = false;

        // Check to see if our hosting window supports Mica at all. We'll check
        // to see if the window has Mica enabled - if it does, then we can
        // assume that it supports Mica.
        //
        // We're doing this instead of checking if we're on Windows build 22621
        // or higher.
        if (_hostingHwnd.has_value())
        {
            int attribute = DWMSBT_NONE;
            const auto hr = DwmGetWindowAttribute(*_hostingHwnd, DWMWA_SYSTEMBACKDROP_TYPE, &attribute, sizeof(attribute));
            if (SUCCEEDED(hr))
            {
                isMicaAvailable = attribute == DWMSBT_MAINWINDOW;
            }
        }

        if (!isMicaAvailable)
        {
            return;
        }

        const auto& theme = _settingsSource.GlobalSettings().CurrentTheme();
        const auto& requestedTheme = _settingsSource.GlobalSettings().CurrentTheme().RequestedTheme();

        RequestedTheme(requestedTheme);

        const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) ?
                               L"SettingsPageMicaBackground" :
                               L"SettingsPageBackground";

        // remember to use ThemeLookup to get the actual correct color for the
        // currently requested theme.
        if (const auto bgColor = ThemeLookup(Resources(), requestedTheme, winrt::box_value(bgKey)))
        {