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

Code before:
            if (args)
            {
                if (const auto& realArgs = args.ActionArgs().try_as<SaveTaskArgs>())
                {
                    if (realArgs.Commandline().empty())
                    {
                        if (const auto termControl{ _GetActiveControl() })
                        {
                            if (termControl.HasSelection())
                            {
                                const auto selections{ termControl.SelectedText(true) };
                                const auto selection = std::accumulate(selections.begin(), selections.end(), std::wstring());
                                realArgs.Commandline(selection);
                            }
                        }
                    }

                    try
                    {
                        winrt::Microsoft::Terminal::Control::KeyChord keyChord = nullptr;
                        if (!realArgs.KeyChord().empty())
                        {
                            keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
                        }
                        _settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), realArgs.Commandline(), keyChord);
                        _settings.WriteSettingsToDisk();
                        ActionSaved(realArgs.Commandline(), realArgs.Name(), realArgs.KeyChord());
                    }
                    catch (const winrt::hresult_error& ex)
                    {
                        auto code = ex.code();
                        auto message = ex.message();
                        ActionSaveFailed(message);
                        args.Handled(true);
                        return;
                    }

                    args.Handled(true);
                }
            }
        }
    }

    void TerminalPage::_HandleSelectCommand(const IInspectable& /*sender*/,
                                            const ActionEventArgs& args)
    {
        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SelectCommandArgs>())
            {
                const auto res = _ApplyToActiveControls([&](auto& control) {

Review comment (suggestion):
❌ `WriteSettingsToDisk` writes the file on _this_ thread. We definitely can't be doing this on the UI thread. 

You might be able to just do a 

```c++
auto saveSettings = [settings=_settings]() -> winrt::fire_and_forget {
  co_await winrt::resume_background();
  settings.WriteSettingsToDisk();
};
saveSettings();
```

to basically make a lambda that does the fire_and_forget thing, but then also just call it immediately. I haven't tested it, so I'm sure there's _spooky coroutine reasons_ that's a bad idea, but 🤷 



Code after:
            return;
        }

        if (args)
        {
            if (const auto& realArgs = args.ActionArgs().try_as<SaveSnippetArgs>())
            {
                auto commandLine = realArgs.Commandline();
                if (commandLine.empty())
                {
                    if (const auto termControl{ _GetActiveControl() })
                    {
                        if (termControl.HasSelection())
                        {
                            const auto selections{ termControl.SelectedText(true) };
                            const auto selection = std::accumulate(selections.begin(), selections.end(), std::wstring());
                            commandLine = selection;
                        }
                    }
                }

                if (commandLine.empty())
                {
                    ActionSaveFailed(L"CommandLine is Required");
                    return;
                }

                try
                {
                    KeyChord keyChord = nullptr;
                    if (!realArgs.KeyChord().empty())
                    {
                        keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
                    }
                    _settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), commandLine, keyChord);
                    _settings.WriteSettingsToDisk();
                    ActionSaved(commandLine, realArgs.Name(), realArgs.KeyChord());
                }
                catch (const winrt::hresult_error& ex)
                {
                    auto code = ex.code();
                    auto message = ex.message();
                    ActionSaveFailed(message);
                    args.Handled(true);
                    return;
                }

                args.Handled(true);
            }
        }
    }