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

Code before:
    // - <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() };

                // Apply the new scheme.
                control.ColorScheme(scheme.ToCoreScheme());

                // Each control will emplace a revert into the
                // _restorePreviewFuncs for itself.
                _restorePreviewFuncs.emplace_back([=]() {
                    // On dismiss, restore the original scheme.
                    control.ColorScheme(originalScheme);
                });
            });
        }
    }

    void TerminalPage::_PreviewAdjustOpacity(const Settings::Model::AdjustOpacityArgs& args)
    {
        // Clear the saved preview funcs because we don't need to add a restore each time
        // the preview color changes, we only need to be able to restore the last one.
        _restorePreviewFuncs.clear();

        _ApplyToActiveControls([&](const auto& control) {
            // Stash a copy of the original scheme.
            auto originalOpacity{ control.BackgroundOpacity() };

            // Apply the new opacity
            control.AdjustOpacity(args.Opacity(), args.Relative());

            _restorePreviewFuncs.emplace_back([=]() {
                // On dismiss:
                // Don't adjust relatively, just set outright.
                control.AdjustOpacity(::base::saturated_cast<int>(originalOpacity * 100), false);
            });
        });
    }

    // Method Description:
    // - Handler for the CommandPalette::PreviewAction event. The Command
    //   Palette will raise this even when an action is selected, but _not_
    //   committed. This gives the Terminal a chance to display a "preview" of
    //   the action.
    // - This will be called with a null args before an action is dispatched, or
    //   when the palette is dismissed.
    // - For any actions that are to be previewed here, MAKE SURE TO RESTORE THE

Review comment (suggestion):
```suggestion
        // the preview changes, we only need to be able to restore the last one.
```

Code after:
    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() };

                // Apply the new scheme.
                control.ColorScheme(scheme.ToCoreScheme());

                // Each control will emplace a revert into the
                // _restorePreviewFuncs for itself.
                _restorePreviewFuncs.emplace_back([=]() {
                    // On dismiss, restore the original scheme.
                    control.ColorScheme(originalScheme);
                });
            });
        }
    }

    void TerminalPage::_PreviewAdjustOpacity(const Settings::Model::AdjustOpacityArgs& args)
    {
        // Clear the saved preview funcs because we don't need to add a restore each time
        // the preview changes, we only need to be able to restore the last one.
        _restorePreviewFuncs.clear();

        _ApplyToActiveControls([&](const auto& control) {
            // Stash a copy of the original opacity.
            auto originalOpacity{ control.BackgroundOpacity() };

            // Apply the new opacity
            control.AdjustOpacity(args.Opacity(), args.Relative());

            _restorePreviewFuncs.emplace_back([=]() {
                // On dismiss:
                // Don't adjust relatively, just set outright.
                control.AdjustOpacity(::base::saturated_cast<int>(originalOpacity * 100), false);
            });
        });
    }

    // Method Description:
    // - Handler for the CommandPalette::PreviewAction event. The Command
    //   Palette will raise this even when an action is selected, but _not_
    //   committed. This gives the Terminal a chance to display a "preview" of
    //   the action.
    // - This will be called with a null args before an action is dispatched, or
    //   when the palette is dismissed.
    // - For any actions that are to be previewed here, MAKE SURE TO RESTORE THE
    //   STATE IN `TerminalPage::_EndPreview`. That method is called to revert