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

Restore contents when a screen info is closed #17853

Merged
merged 1 commit into from
Sep 6, 2024
Merged
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
49 changes: 49 additions & 0 deletions src/host/VtIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <til/unicode.h>

#include "directio.h"
#include "handle.h" // LockConsole
#include "output.h" // CloseConsoleProcessState
#include "../interactivity/inc/ServiceLocator.hpp"
Expand Down Expand Up @@ -790,3 +791,51 @@ void VtIo::Writer::WriteInfos(til::point target, std::span<const CHAR_INFO> info
} while (--repeat);
}
}

void VtIo::Writer::WriteScreenInfo(SCREEN_INFORMATION& newContext, til::size oldSize) const
{
const auto area = static_cast<size_t>(oldSize.width * oldSize.height);

auto& main = newContext.GetMainBuffer();
auto& alt = newContext.GetActiveBuffer();
const auto hasAltBuffer = &alt != &main;

// TODO GH#5094: This could use xterm's XTWINOPS "\e[8;<height>;<width>t" escape sequence here.
if (oldSize != main.GetBufferSize().Dimensions())
{
THROW_IF_NTSTATUS_FAILED(main.ResizeTraditional(oldSize));
main.SetViewportSize(&oldSize);
}
if (hasAltBuffer && oldSize != alt.GetBufferSize().Dimensions())
{
THROW_IF_NTSTATUS_FAILED(alt.ResizeTraditional(oldSize));
alt.SetViewportSize(&oldSize);
}
Comment on lines +804 to +813
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two blocks have changed slightly, to avoid reflow when the size hasn't changed (the common case I assume). All other code remains unchanged.


const auto request = Viewport::FromDimensions({}, oldSize);
Viewport read;
til::small_vector<CHAR_INFO, 1024> infos;
infos.resize(area, CHAR_INFO{ L' ', FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED });

const auto dumpScreenInfo = [&](SCREEN_INFORMATION& screenInfo) {
THROW_IF_FAILED(ReadConsoleOutputWImplHelper(screenInfo, infos, request, read));
for (til::CoordType i = 0; i < oldSize.height; i++)
{
WriteInfos({ 0, i }, { infos.begin() + i * oldSize.width, static_cast<size_t>(oldSize.width) });
}

WriteCUP(screenInfo.GetTextBuffer().GetCursor().GetPosition());
WriteAttributes(screenInfo.GetAttributes());
WriteDECTCEM(screenInfo.GetTextBuffer().GetCursor().IsVisible());
WriteDECAWM(WI_IsFlagSet(screenInfo.OutputMode, ENABLE_WRAP_AT_EOL_OUTPUT));
};

WriteASB(false);
dumpScreenInfo(main);

if (hasAltBuffer)
{
WriteASB(true);
dumpScreenInfo(alt);
}
}
1 change: 1 addition & 0 deletions src/host/VtIo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Microsoft::Console::VirtualTerminal
void WriteWindowTitle(std::wstring_view title) const;
void WriteAttributes(const TextAttribute& attributes) const;
void WriteInfos(til::point target, std::span<const CHAR_INFO> infos) const;
void WriteScreenInfo(SCREEN_INFORMATION& newContext, til::size oldSize) const;

private:
VtIo* _io = nullptr;
Expand Down
45 changes: 2 additions & 43 deletions src/host/getset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,49 +487,8 @@ void ApiRoutines::SetConsoleActiveScreenBufferImpl(SCREEN_INFORMATION& newContex

if (auto writer = gci.GetVtWriter())
{
const auto viewport = gci.GetActiveOutputBuffer().GetBufferSize();
const auto size = viewport.Dimensions();
const auto area = static_cast<size_t>(viewport.Width() * viewport.Height());

auto& main = newContext.GetMainBuffer();
auto& alt = newContext.GetActiveBuffer();
const auto hasAltBuffer = &alt != &main;

// TODO GH#5094: This could use xterm's XTWINOPS "\e[8;<height>;<width>t" escape sequence here.
THROW_IF_NTSTATUS_FAILED(main.ResizeTraditional(size));
main.SetViewportSize(&size);
if (hasAltBuffer)
{
THROW_IF_NTSTATUS_FAILED(alt.ResizeTraditional(size));
alt.SetViewportSize(&size);
}

Viewport read;
til::small_vector<CHAR_INFO, 1024> infos;
infos.resize(area, CHAR_INFO{ L' ', FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED });

const auto dumpScreenInfo = [&](SCREEN_INFORMATION& screenInfo) {
THROW_IF_FAILED(ReadConsoleOutputWImpl(screenInfo, infos, viewport, read));
for (til::CoordType i = 0; i < size.height; i++)
{
writer.WriteInfos({ 0, i }, { infos.begin() + i * size.width, static_cast<size_t>(size.width) });
}

writer.WriteCUP(screenInfo.GetTextBuffer().GetCursor().GetPosition());
writer.WriteAttributes(screenInfo.GetAttributes());
writer.WriteDECTCEM(screenInfo.GetTextBuffer().GetCursor().IsVisible());
writer.WriteDECAWM(WI_IsFlagSet(screenInfo.OutputMode, ENABLE_WRAP_AT_EOL_OUTPUT));
};

writer.WriteASB(false);
dumpScreenInfo(main);

if (hasAltBuffer)
{
writer.WriteASB(true);
dumpScreenInfo(alt);
}

const auto oldSize = gci.GetActiveOutputBuffer().GetBufferSize().Dimensions();
writer.WriteScreenInfo(newContext, oldSize);
writer.Submit();
}

Expand Down
11 changes: 11 additions & 0 deletions src/server/ObjectHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,18 @@ INPUT_READ_HANDLE_DATA* ConsoleHandleData::GetClientInput() const
LOG_IF_FAILED(pScreenInfo->FreeIoHandle(this));
if (!pScreenInfo->HasAnyOpenHandles())
{
auto& gci = Microsoft::Console::Interactivity::ServiceLocator::LocateGlobals().getConsoleInformation();
const auto oldSize = gci.GetActiveOutputBuffer().GetBufferSize().Dimensions();
auto writer = gci.GetVtWriter();

SCREEN_INFORMATION::s_RemoveScreenBuffer(pScreenInfo);

if (writer && gci.HasActiveOutputBuffer())
{
auto& newContext = gci.GetActiveOutputBuffer();
writer.WriteScreenInfo(newContext, oldSize);
writer.Submit();
}
}

return S_OK;
Expand Down
Loading