From 82434c81f5bde4e3869c450941da8a3bd5104cee Mon Sep 17 00:00:00 2001 From: Pankaj Bhojwani Date: Tue, 1 Jun 2021 11:31:13 -0700 Subject: [PATCH 1/2] cleanup --- src/cascadia/TerminalControl/TermControl.cpp | 40 ++++++++++++-------- src/cascadia/TerminalControl/TermControl.h | 4 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 9cec3b8100d..92487820184 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -58,8 +58,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation _lastAutoScrollUpdateTime{ std::nullopt }, _cursorTimer{}, _blinkTimer{}, - _searchBox{ nullptr }, - _bellLightAnimation{ Window::Current().Compositor().CreateScalarKeyFrameAnimation() } + _searchBox{ nullptr } { InitializeComponent(); @@ -168,11 +167,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation _autoScrollTimer.Interval(AutoScrollUpdateInterval); _autoScrollTimer.Tick({ this, &TermControl::_UpdateAutoScroll }); - // Add key frames and a duration to our bell light animation - _bellLightAnimation.InsertKeyFrame(0.0, 2.0); - _bellLightAnimation.InsertKeyFrame(1.0, 1.0); - _bellLightAnimation.Duration(winrt::Windows::Foundation::TimeSpan(std::chrono::milliseconds(TerminalWarningBellInterval))); - _ApplyUISettings(_settings); } @@ -2386,17 +2380,34 @@ namespace winrt::Microsoft::Terminal::Control::implementation void TermControl::BellLightOn() { + // Initialize the animation if it does not exist + // We only initialize here instead of in the ctor because depending on the bell style setting, + // we may never need this animation + if (!_bellLightAnimation) + { + _bellLightAnimation = Window::Current().Compositor().CreateScalarKeyFrameAnimation(); + // Add key frames and a duration to our bell light animation + _bellLightAnimation.InsertKeyFrame(0.0, 2.0); + _bellLightAnimation.InsertKeyFrame(1.0, 1.0); + _bellLightAnimation.Duration(winrt::Windows::Foundation::TimeSpan(std::chrono::milliseconds(TerminalWarningBellInterval))); + } + + // Similar to the animation, only initialize the timer here + if (!_bellLightTimer) + { + DispatcherTimer invertTimer; + invertTimer.Interval(std::chrono::milliseconds(TerminalWarningBellInterval)); + invertTimer.Tick({ get_weak(), &TermControl::_BellLightOff }); + _bellLightTimer = invertTimer; + } + Windows::Foundation::Numerics::float2 zeroSize{ 0, 0 }; // If the grid has 0 size or if the bell timer is // already active, do nothing - if (RootGrid().ActualSize() != zeroSize && !_bellLightTimer) + if (RootGrid().ActualSize() != zeroSize && !_bellLightTimer.IsEnabled()) { // Start the timer, when the timer ticks we switch off the light - DispatcherTimer invertTimer; - invertTimer.Interval(std::chrono::milliseconds(TerminalWarningBellInterval)); - invertTimer.Tick({ get_weak(), &TermControl::_BellLightOff }); - invertTimer.Start(); - _bellLightTimer.emplace(std::move(invertTimer)); + _bellLightTimer.Start(); // Switch on the light and animate the intensity to fade out VisualBellLight::SetIsTarget(RootGrid(), true); @@ -2410,8 +2421,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation if (_bellLightTimer) { // Stop the timer and switch off the light - _bellLightTimer->Stop(); - _bellLightTimer.reset(); + _bellLightTimer.Stop(); if (!_closing) { diff --git a/src/cascadia/TerminalControl/TermControl.h b/src/cascadia/TerminalControl/TermControl.h index 684ac3a1a29..0e09cb903fc 100644 --- a/src/cascadia/TerminalControl/TermControl.h +++ b/src/cascadia/TerminalControl/TermControl.h @@ -174,11 +174,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation Windows::UI::Xaml::DispatcherTimer _autoScrollTimer; std::optional _lastAutoScrollUpdateTime; - winrt::Windows::UI::Composition::ScalarKeyFrameAnimation _bellLightAnimation; + winrt::Windows::UI::Composition::ScalarKeyFrameAnimation _bellLightAnimation{ nullptr }; + Windows::UI::Xaml::DispatcherTimer _bellLightTimer{ nullptr }; std::optional _cursorTimer; std::optional _blinkTimer; - std::optional _bellLightTimer; event_token _coreOutputEventToken; From e52fed6be8b28729edc75e56c7c4231b770a57ed Mon Sep 17 00:00:00 2001 From: Pankaj Bhojwani Date: Wed, 2 Jun 2021 08:45:07 -0700 Subject: [PATCH 2/2] fix --- src/cascadia/TerminalControl/TermControl.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/cascadia/TerminalControl/TermControl.cpp b/src/cascadia/TerminalControl/TermControl.cpp index 92487820184..266b1bd3db7 100644 --- a/src/cascadia/TerminalControl/TermControl.cpp +++ b/src/cascadia/TerminalControl/TermControl.cpp @@ -2395,10 +2395,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation // Similar to the animation, only initialize the timer here if (!_bellLightTimer) { - DispatcherTimer invertTimer; - invertTimer.Interval(std::chrono::milliseconds(TerminalWarningBellInterval)); - invertTimer.Tick({ get_weak(), &TermControl::_BellLightOff }); - _bellLightTimer = invertTimer; + _bellLightTimer = {}; + _bellLightTimer.Interval(std::chrono::milliseconds(TerminalWarningBellInterval)); + _bellLightTimer.Tick({ get_weak(), &TermControl::_BellLightOff }); } Windows::Foundation::Numerics::float2 zeroSize{ 0, 0 };