Skip to content

Commit

Permalink
Experiments
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed Aug 9, 2024
1 parent d9e047b commit e4814d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
20 changes: 2 additions & 18 deletions src/host/_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ static void AdjustCursorPosition(SCREEN_INFORMATION& screenInfo, _In_ til::point
coordCursor.y = bufferSize.height - 1;
}

const auto cursorMovedPastViewport = coordCursor.y > screenInfo.GetViewport().BottomInclusive();

// if at right or bottom edge of window, scroll right or down one char.
if (cursorMovedPastViewport)
{
til::point WindowOrigin;
WindowOrigin.x = 0;
WindowOrigin.y = coordCursor.y - screenInfo.GetViewport().BottomInclusive();
LOG_IF_FAILED(screenInfo.SetViewportOrigin(false, WindowOrigin, true));
}

LOG_IF_FAILED(screenInfo.SetCursorPosition(coordCursor, false));
}

Expand Down Expand Up @@ -167,7 +156,7 @@ void WriteCharsLegacy(SCREEN_INFORMATION& screenInfo, const std::wstring_view& t
auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
auto writer = gci.GetVtWriterForBuffer(&screenInfo);

screenInfo.SnapOnOutput();
const auto snap = screenInfo.SnapOnOutput();

// If we enter this if condition, then someone wrote text in VT mode and now switched to non-VT mode.
// Since the Console APIs don't support delayed EOL wrapping, we need to first put the cursor back
Expand Down Expand Up @@ -208,7 +197,6 @@ void WriteCharsLegacy(SCREEN_INFORMATION& screenInfo, const std::wstring_view& t
writer.Submit();
}

screenInfo.SnapOnOutput();
return;
}

Expand Down Expand Up @@ -331,8 +319,6 @@ void WriteCharsLegacy(SCREEN_INFORMATION& screenInfo, const std::wstring_view& t
{
writer.Submit();
}

screenInfo.SnapOnOutput();
}

// This is the main entrypoint for conhost to write VT to the buffer.
Expand All @@ -345,7 +331,7 @@ void WriteCharsVT(SCREEN_INFORMATION& screenInfo, const std::wstring_view& str)
// may change, so get the VtIo reference now, just in case.
auto writer = gci.GetVtWriterForBuffer(&screenInfo);

screenInfo.SnapOnOutput();
const auto snap = screenInfo.SnapOnOutput();

stateMachine.ProcessString(str);

Expand Down Expand Up @@ -387,8 +373,6 @@ void WriteCharsVT(SCREEN_INFORMATION& screenInfo, const std::wstring_view& str)
write(offset, std::wstring_view::npos);
writer.Submit();
}

screenInfo.SnapOnOutput();
}

// Erases all contents of the given screenInfo, including the current screen and scrollback.
Expand Down
44 changes: 18 additions & 26 deletions src/host/screenInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1685,24 +1685,6 @@ static constexpr bool IsInputKey(WORD vkey)
}

void SCREEN_INFORMATION::MakeCursorVisible(til::point position)
{
_makeLocationVisible(position, true, true);
}

void SCREEN_INFORMATION::SnapOnInput(const WORD vkey)
{
if (IsInputKey(vkey))
{
_makeLocationVisible(_textBuffer->GetCursor().GetPosition(), true, false);
}
}

void SCREEN_INFORMATION::SnapOnOutput()
{
_makeLocationVisible(_textBuffer->GetCursor().GetPosition(), false, true);
}

void SCREEN_INFORMATION::_makeLocationVisible(til::point position, bool vertical, bool horizontal)
{
const auto viewportOrigin = _viewport.Origin();
const auto viewportSize = _viewport.Dimensions();
Expand All @@ -1713,21 +1695,31 @@ void SCREEN_INFORMATION::_makeLocationVisible(til::point position, bool vertical
position.x = std::clamp(position.x, 0, bufferSize.width - 1);
position.y = std::clamp(position.y, 0, bufferSize.height - 1);

if (vertical)
origin.y = std::min(origin.y, position.y); // shift up if above
origin.y = std::max(origin.y, position.y - (viewportSize.height - 1)); // shift down if below

origin.x = std::min(origin.x, position.x); // shift left if left
origin.x = std::max(origin.x, position.x - (viewportSize.width - 1)); // shift right if right

if (origin != viewportOrigin)
{
origin.y = std::min(origin.y, position.y); // shift up if above
origin.y = std::max(origin.y, position.y - (viewportSize.height - 1)); // shift down if below
std::ignore = SetViewportOrigin(true, origin, false);
}
}

if (horizontal)
void SCREEN_INFORMATION::SnapOnInput(const WORD vkey)
{
if (IsInputKey(vkey))
{
origin.x = std::min(origin.x, position.x); // shift left if left
origin.x = std::max(origin.x, position.x - (viewportSize.width - 1)); // shift right if right
_makeCursorVisible();
}
}

if (origin != viewportOrigin)
void SCREEN_INFORMATION::_makeCursorVisible()
{
if (_textBuffer->GetCursor().IsOn())
{
std::ignore = SetViewportOrigin(true, origin, false);
MakeCursorVisible(_textBuffer->GetCursor().GetPosition());
}
}

Expand Down
13 changes: 11 additions & 2 deletions src/host/screenInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,16 @@ class SCREEN_INFORMATION : public ConsoleObjectHeader, public Microsoft::Console
void MakeCurrentCursorVisible();
void MakeCursorVisible(til::point position);
void SnapOnInput(WORD vkey);
void SnapOnOutput();
auto SnapOnOutput()
{
const auto inBounds = _viewport.IsInBounds(_textBuffer->GetCursor().GetPosition());
return wil::scope_exit([this, inBounds]() {
if (inBounds)
{
_makeCursorVisible();
}
});
}

void ClipToScreenBuffer(_Inout_ til::inclusive_rect* const psrClip) const;

Expand Down Expand Up @@ -233,7 +242,7 @@ class SCREEN_INFORMATION : public ConsoleObjectHeader, public Microsoft::Console
void _CalculateViewportSize(const til::rect* const prcClientArea, _Out_ til::size* const pcoordSize);
void _AdjustViewportSize(const til::rect* const prcClientNew, const til::rect* const prcClientOld, const til::size* const pcoordSize);
void _InternalSetViewportSize(const til::size* pcoordSize, const bool fResizeFromTop, const bool fResizeFromLeft);
void _makeLocationVisible(til::point position, bool vertical, bool horizontal);
void _makeCursorVisible();

static void s_CalculateScrollbarVisibility(const til::rect* const prcClientArea,
const til::size* const pcoordBufferSize,
Expand Down

0 comments on commit e4814d9

Please sign in to comment.