Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Move timers from TermControl to Core/Interactivity #15969

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 92 additions & 2 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
core->_ScrollPositionChangedHandlers(*core, update);
}
});

_cursorTimer = _dispatcher.CreateTimer();
_blinkTimer = _dispatcher.CreateTimer();

_cursorTimer.IsRepeating(true);
_blinkTimer.IsRepeating(true);

_cursorTimer.Tick({ get_weak(), &ControlCore::_cursorTimerTick });
_blinkTimer.Tick({ get_weak(), &ControlCore::_blinkTimerTick });
}

ControlCore::~ControlCore()
Expand All @@ -237,6 +246,16 @@ namespace winrt::Microsoft::Terminal::Control::implementation
shared->tsfTryRedrawCanvas.reset();
shared->updatePatternLocations.reset();
shared->updateScrollBar.reset();
if (_cursorTimer)
{
_cursorTimer.Stop();
_cursorTimer = nullptr;
}
if (_blinkTimer)
{
_blinkTimer.Stop();
_blinkTimer = nullptr;
}
}

void ControlCore::AttachToNewControl(const Microsoft::Terminal::Control::IKeyBindings& keyBindings)
Expand Down Expand Up @@ -410,6 +429,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_initializedTerminal.store(true, std::memory_order_relaxed);
} // scope for TerminalLock

// Set the original intervals and start the timers
_updateTimers();

// Start the connection outside of lock, because it could
// start writing output immediately.
_connection.Start();
Expand Down Expand Up @@ -634,6 +656,13 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
}

// Force the cursor to be visible
if (_cursorTimer)
{
CursorOn(SelectionMode() != SelectionInteractionMode::Mark);
_cursorTimer.Start();
}

// If the terminal translated the key, mark the event as handled.
// This will prevent the system from trying to get the character out
// of it and sending us a CharacterReceived event.
Expand Down Expand Up @@ -1438,6 +1467,46 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return _terminal->GetBufferHeight();
}

void ControlCore::_updateTimers()
{
if (_cursorTimer)
{
_cursorTimer.Interval(_cursorBlinkTime);
if (_cursorBlinkTime == std::chrono::milliseconds(0))
{
_cursorTimer.Stop();
}
else
{
_cursorTimer.Start();
}
}
if (_blinkTimer)
{
_blinkTimer.Interval(_cursorBlinkTime);
if (_cursorBlinkTime == std::chrono::milliseconds(0) || !_blinkAnimationEnabled)
{
_blinkTimer.Stop();
}
else
{
_blinkTimer.Start();
}
}
}

void ControlCore::CursorBlinkTime(Windows::Foundation::TimeSpan t)
{
_cursorBlinkTime = t;
_updateTimers();
}

void ControlCore::VtBlinkEnabled(bool b)
{
_blinkAnimationEnabled = b;
_updateTimers();
}

void ControlCore::_terminalWarningBell()
{
// Since this can only ever be triggered by output from the connection,
Expand Down Expand Up @@ -1648,6 +1717,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_closing = true;

_cursorTimer.Stop();
_blinkTimer.Stop();

// Ensure Close() doesn't hang, waiting for MidiAudio to finish playing an hour long song.
_midiAudio.BeginSkip();

Expand Down Expand Up @@ -1703,15 +1775,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_TabColorChangedHandlers(*this, nullptr);
}

void ControlCore::BlinkAttributeTick()
void ControlCore::_blinkTimerTick(const IInspectable&, const IInspectable&)
{
const auto lock = _terminal->LockForWriting();

auto& renderSettings = _terminal->GetRenderSettings();
renderSettings.ToggleBlinkRendition(*_renderer);
}

void ControlCore::BlinkCursor()
void ControlCore::_cursorTimerTick(const IInspectable&, const IInspectable&)
{
const auto lock = _terminal->LockForWriting();
_terminal->BlinkCursor();
Expand Down Expand Up @@ -2214,12 +2286,30 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void ControlCore::GotFocus()
{
_focusChanged(true);
if (_cursorTimer)
{
CursorOn(SelectionMode() != SelectionInteractionMode::Mark);
_cursorTimer.Start();
}
if (_blinkTimer)
{
_blinkTimer.Start();
}
}

// See GotFocus.
void ControlCore::LostFocus()
{
_focusChanged(false);
if (_cursorTimer && !false /*TODO DH*/)
{
_cursorTimer.Stop();
CursorOn(false);
}
if (_blinkTimer)
{
_blinkTimer.Stop();
}
}

void ControlCore::_focusChanged(bool focused)
Expand Down
16 changes: 14 additions & 2 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation

#pragma endregion

void BlinkAttributeTick();
void BlinkCursor();
bool CursorOn() const;
void CursorOn(const bool isCursorOn);

Expand Down Expand Up @@ -245,6 +243,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
bool ShouldShowSelectCommand();
bool ShouldShowSelectOutput();

Windows::Foundation::TimeSpan CursorBlinkTime() { return _cursorBlinkTime; }
bool VtBlinkEnabled() { return _blinkAnimationEnabled; }
void CursorBlinkTime(Windows::Foundation::TimeSpan v);
void VtBlinkEnabled(bool v);

RUNTIME_SETTING(double, Opacity, _settings->Opacity());
RUNTIME_SETTING(bool, UseAcrylic, _settings->UseAcrylic());

Expand Down Expand Up @@ -337,6 +340,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
uint64_t _owningHwnd{ 0 };

winrt::Windows::System::DispatcherQueue _dispatcher{ nullptr };
winrt::Windows::System::DispatcherQueueTimer _cursorTimer{ nullptr };
winrt::Windows::System::DispatcherQueueTimer _blinkTimer{ nullptr };
til::shared_mutex<SharedState> _shared;

til::point _contextMenuBufferPosition{ 0, 0 };
Expand Down Expand Up @@ -375,6 +380,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
MidiAudio _midiAudio;
winrt::Windows::System::DispatcherQueueTimer _midiAudioSkipTimer{ nullptr };

winrt::Windows::Foundation::TimeSpan _cursorBlinkTime{ std::chrono::milliseconds(500) };
bool _blinkAnimationEnabled{ true };

#pragma region RendererCallbacks
void _rendererWarning(const HRESULT hr);
winrt::fire_and_forget _renderEngineSwapChainChanged(const HANDLE handle);
Expand All @@ -400,6 +408,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

bool _clickedOnMark(const til::point& pos, bool (*filter)(const ::ScrollMark&));

void _updateTimers();
void _cursorTimerTick(const IInspectable&, const IInspectable&);
void _blinkTimerTick(const IInspectable&, const IInspectable&);

inline bool _IsClosing() const noexcept
{
#ifndef NDEBUG
Expand Down
5 changes: 3 additions & 2 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ namespace Microsoft.Terminal.Control

Microsoft.Terminal.Core.Point CursorPosition { get; };
void ResumeRendering();
void BlinkAttributeTick();

void Search(String text, Boolean goForward, Boolean caseSensitive);
void ClearSearch();
Expand All @@ -141,7 +140,6 @@ namespace Microsoft.Terminal.Control
String HoveredUriText { get; };
Windows.Foundation.IReference<Microsoft.Terminal.Core.Point> HoveredCell { get; };

void BlinkCursor();
Boolean IsInReadOnlyMode { get; };
Boolean CursorOn;
void EnablePainting();
Expand All @@ -159,6 +157,9 @@ namespace Microsoft.Terminal.Control
Boolean ShouldShowSelectCommand();
Boolean ShouldShowSelectOutput();

Windows.Foundation.TimeSpan CursorBlinkTime { get; set; };
Boolean VtBlinkEnabled { get; set; };

// These events are called from some background thread
event Windows.Foundation.TypedEventHandler<Object, CopyToClipboardEventArgs> CopyToClipboard;
event Windows.Foundation.TypedEventHandler<Object, TitleChangedEventArgs> TitleChanged;
Expand Down
Loading
Loading