Repository: microsoft/terminal
File: src/cascadia/TerminalApp/ActionPreviewHandlers.cpp (C++)

Code before:
        {
        case ShortcutAction::SetColorScheme:
        case ShortcutAction::AdjustOpacity:
        {
            _RunRestorePreviews();
            break;
        }
        }
        _lastPreviewedCommand = nullptr;
    }

    // Method Description:
    // - Revert any changes from the preview action. This will run the restore
    //   function that the preview added to _restorePreviewFuncs
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TerminalPage::_RunRestorePreviews()
    {
        // Apply the reverts in reverse order - If we had multiple previews
        // stacked on top of each other, then this will ensure the first one in
        // is the last one out.
        for (auto i{ _restorePreviewFuncs.rbegin() }; i < _restorePreviewFuncs.rend(); i++)
        {
            auto f = *i;
            f();
        }
        _restorePreviewFuncs.clear();
    }

    // Method Description:
    // - Preview handler for the SetColorScheme action.
    // - This method will stash functions to reset the settings of the selected controls in
    //   _restorePreviewFuncs. Then it will create a new TerminalSettings object
    //   with only the properties from the ColorScheme set. It'll _insert_ a
    //   TerminalSettings between the control's root settings (built from
    //   CascadiaSettings) and the control's runtime settings. That'll cause the
    //   control to use _that_ table as the active color scheme.
    // Arguments:
    // - args: The SetColorScheme args with the name of the scheme to use.
    // Return Value:
    // - <none>
    void TerminalPage::_PreviewColorScheme(const Settings::Model::SetColorSchemeArgs& args)
    {
        if (const auto& scheme{ _settings.GlobalSettings().ColorSchemes().TryLookup(args.SchemeName()) })
        {
            _ApplyToActiveControls([&](const auto& control) {
                // Stash a copy of the current scheme.
                auto originalScheme{ control.ColorScheme() };


Review comment (bug):
Just in case a `f()` call below fails. This ensures the correct behavior for
```cpp
const auto backup = _restorePreviewFuncs.empty();
```

Code after:
        {
        case ShortcutAction::SetColorScheme:
        case ShortcutAction::AdjustOpacity:
        {
            _RunRestorePreviews();
            break;
        }
        }
        _lastPreviewedCommand = nullptr;
    }

    // Method Description:
    // - Revert any changes from the preview action. This will run the restore
    //   function that the preview added to _restorePreviewFuncs
    // Arguments:
    // - <none>
    // Return Value:
    // - <none>
    void TerminalPage::_RunRestorePreviews()
    {
        // Apply the reverts in reverse order - If we had multiple previews
        // stacked on top of each other, then this will ensure the first one in
        // is the last one out.
        const auto cleanup = wil::scope_exit([this]() {
            _restorePreviewFuncs.clear();
        });

        for (const auto& f : _restorePreviewFuncs)
        {
            f();
        }
    }

    // Method Description:
    // - Preview handler for the SetColorScheme action.
    // - This method will stash functions to reset the settings of the selected controls in
    //   _restorePreviewFuncs. Then it will create a new TerminalSettings object
    //   with only the properties from the ColorScheme set. It'll _insert_ a
    //   TerminalSettings between the control's root settings (built from
    //   CascadiaSettings) and the control's runtime settings. That'll cause the
    //   control to use _that_ table as the active color scheme.
    // Arguments:
    // - args: The SetColorScheme args with the name of the scheme to use.
    // Return Value:
    // - <none>
    void TerminalPage::_PreviewColorScheme(const Settings::Model::SetColorSchemeArgs& args)
    {
        if (const auto& scheme{ _settings.GlobalSettings().ColorSchemes().TryLookup(args.SchemeName()) })
        {
            const auto backup = _restorePreviewFuncs.empty();