From 6116231fce925ae818762e5f58209f2e84ba64fc Mon Sep 17 00:00:00 2001 From: Don-Vito Date: Wed, 14 Apr 2021 20:11:19 +0300 Subject: [PATCH] Limit terminal warning bells to one per second (#9812) ## PR Checklist * [x] Closes https://github.com/microsoft/terminal/issues/9776 * [x] CLA signed. * [ ] Tests added/passed * [ ] Documentation updated. * [ ] Schema updated. * [ ] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments Use `ThrottledFunc` in `TermControl` to limit bell emission callback to one per second. --- src/cascadia/TerminalControl/TermControl.cpp | 17 ++++++++++++++++- src/cascadia/TerminalControl/TermControl.h | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index a0e56a650dea..f34d7013c02e 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -39,6 +39,9 @@ constexpr const auto TsfRedrawInterval = std::chrono::milliseconds(100); // The minimum delay between updating the locations of regex patterns constexpr const auto UpdatePatternLocationsInterval = std::chrono::milliseconds(500); +// The minimum delay between emitting warning bells +constexpr const auto TerminalWarningBellInterval = std::chrono::milliseconds(1000); + DEFINE_ENUM_FLAG_OPERATORS(winrt::Microsoft::Terminal::Control::CopyFormat); namespace winrt::Microsoft::Terminal::Control::implementation @@ -91,7 +94,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation // GH#8969: pre-seed working directory to prevent potential races _terminal->SetWorkingDirectory(_settings.StartingDirectory()); - auto pfnWarningBell = std::bind(&TermControl::_TerminalWarningBell, this); + auto pfnWarningBell = [this]() { + _playWarningBell->Run(); + }; _terminal->SetWarningBellCallback(pfnWarningBell); auto pfnTitleChanged = std::bind(&TermControl::_TerminalTitleChanged, this, std::placeholders::_1); @@ -167,6 +172,16 @@ namespace winrt::Microsoft::Terminal::Control::implementation UpdatePatternLocationsInterval, Dispatcher()); + _playWarningBell = std::make_shared>( + [weakThis = get_weak()]() { + if (auto control{ weakThis.get() }) + { + control->_TerminalWarningBell(); + } + }, + TerminalWarningBellInterval, + Dispatcher()); + _updateScrollBar = std::make_shared>( [weakThis = get_weak()](const auto& update) { if (auto control{ weakThis.get() }) diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index f38d8592eeb0..0ebbc9186e20 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -145,6 +145,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation std::shared_ptr> _updatePatternLocations; + std::shared_ptr> _playWarningBell; + struct ScrollBarUpdate { std::optional newValue;