Skip to content

Commit

Permalink
Merge IBaseData, IRenderData and IUiaData (#14427)
Browse files Browse the repository at this point in the history
My goal is to make `IRenderData` "snapshottable", so that we
can render a frame of text without holding the console lock.
To facilitate this, this commit merges our 3 data interfaces
into one. It includes no actual changes apart from renames.
  • Loading branch information
lhecker authored Dec 5, 2022
1 parent 391abaf commit 4d9a266
Show file tree
Hide file tree
Showing 32 changed files with 102 additions and 226 deletions.
40 changes: 20 additions & 20 deletions src/buffer/out/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ using namespace Microsoft::Console::Types;
// - Once you've found something, you can perform actions like .Select() or .Color()
// Arguments:
// - textBuffer - The screen text buffer to search through (the "haystack")
// - uiaData - The IUiaData type reference, it is for providing selection methods
// - renderData - The IRenderData type reference, it is for providing selection methods
// - str - The search term you want to find (the "needle")
// - direction - The direction to search (upward or downward)
// - sensitivity - Whether or not you care about case
Search::Search(IUiaData& uiaData,
Search::Search(Microsoft::Console::Render::IRenderData& renderData,
const std::wstring_view str,
const Direction direction,
const Sensitivity sensitivity) :
_direction(direction),
_sensitivity(sensitivity),
_needle(s_CreateNeedleFromString(str)),
_uiaData(uiaData),
_coordAnchor(s_GetInitialAnchor(uiaData, direction))
_renderData(renderData),
_coordAnchor(s_GetInitialAnchor(renderData, direction))
{
_coordNext = _coordAnchor;
}
Expand All @@ -41,12 +41,12 @@ Search::Search(IUiaData& uiaData,
// - Once you've found something, you can perform actions like .Select() or .Color()
// Arguments:
// - textBuffer - The screen text buffer to search through (the "haystack")
// - uiaData - The IUiaData type reference, it is for providing selection methods
// - renderData - The IRenderData type reference, it is for providing selection methods
// - str - The search term you want to find (the "needle")
// - direction - The direction to search (upward or downward)
// - sensitivity - Whether or not you care about case
// - anchor - starting search location in screenInfo
Search::Search(IUiaData& uiaData,
Search::Search(Microsoft::Console::Render::IRenderData& renderData,
const std::wstring_view str,
const Direction direction,
const Sensitivity sensitivity,
Expand All @@ -55,7 +55,7 @@ Search::Search(IUiaData& uiaData,
_sensitivity(sensitivity),
_needle(s_CreateNeedleFromString(str)),
_coordAnchor(anchor),
_uiaData(uiaData)
_renderData(renderData)
{
_coordNext = _coordAnchor;
}
Expand Down Expand Up @@ -99,10 +99,10 @@ void Search::Select() const
{
// Convert buffer selection offsets into the equivalent screen coordinates
// required by SelectNewRegion, taking line renditions into account.
const auto& textBuffer = _uiaData.GetTextBuffer();
const auto& textBuffer = _renderData.GetTextBuffer();
const auto selStart = textBuffer.BufferToScreenPosition(_coordSelStart);
const auto selEnd = textBuffer.BufferToScreenPosition(_coordSelEnd);
_uiaData.SelectNewRegion(selStart, selEnd);
_renderData.SelectNewRegion(selStart, selEnd);
}

// Routine Description:
Expand All @@ -114,7 +114,7 @@ void Search::Color(const TextAttribute attr) const
// Only select if we've found something.
if (_coordSelEnd >= _coordSelStart)
{
_uiaData.ColorSelection(_coordSelStart, _coordSelEnd, attr);
_renderData.ColorSelection(_coordSelStart, _coordSelEnd, attr);
}
}

Expand All @@ -135,19 +135,19 @@ std::pair<til::point, til::point> Search::GetFoundLocation() const noexcept
// - If the screen buffer given already has a selection in it, it will be used to determine the anchor.
// - Otherwise, we will choose one of the ends of the screen buffer depending on direction.
// Arguments:
// - uiaData - The reference to the IUiaData interface type object
// - renderData - The reference to the IRenderData interface type object
// - direction - The intended direction of the search
// Return Value:
// - Coordinate to start the search from.
til::point Search::s_GetInitialAnchor(const IUiaData& uiaData, const Direction direction)
til::point Search::s_GetInitialAnchor(const Microsoft::Console::Render::IRenderData& renderData, const Direction direction)
{
const auto& textBuffer = uiaData.GetTextBuffer();
const auto textBufferEndPosition = uiaData.GetTextBufferEndPosition();
if (uiaData.IsSelectionActive())
const auto& textBuffer = renderData.GetTextBuffer();
const auto textBufferEndPosition = renderData.GetTextBufferEndPosition();
if (renderData.IsSelectionActive())
{
// Convert the screen position of the selection anchor into an equivalent
// buffer position to start searching, taking line rendition into account.
auto anchor = textBuffer.ScreenToBufferPosition(uiaData.GetSelectionAnchor());
auto anchor = textBuffer.ScreenToBufferPosition(renderData.GetSelectionAnchor());

if (direction == Direction::Forward)
{
Expand Down Expand Up @@ -196,7 +196,7 @@ bool Search::_FindNeedleInHaystackAt(const til::point pos, til::point& start, ti
for (const auto& needleChars : _needle)
{
// Haystack is the buffer. Needle is the string we were given.
const auto hayIter = _uiaData.GetTextBuffer().GetTextDataAt(bufferPos);
const auto hayIter = _renderData.GetTextBuffer().GetTextDataAt(bufferPos);
const auto hayChars = *hayIter;

// If we didn't match at any point of the needle, return false.
Expand Down Expand Up @@ -269,7 +269,7 @@ wchar_t Search::_ApplySensitivity(const wchar_t wch) const noexcept
// - coord - Updated by function to increment one position (will wrap X and Y direction)
void Search::_IncrementCoord(til::point& coord) const noexcept
{
_uiaData.GetTextBuffer().GetSize().IncrementInBoundsCircular(coord);
_renderData.GetTextBuffer().GetSize().IncrementInBoundsCircular(coord);
}

// Routine Description:
Expand All @@ -278,7 +278,7 @@ void Search::_IncrementCoord(til::point& coord) const noexcept
// - coord - Updated by function to decrement one position (will wrap X and Y direction)
void Search::_DecrementCoord(til::point& coord) const noexcept
{
_uiaData.GetTextBuffer().GetSize().DecrementInBoundsCircular(coord);
_renderData.GetTextBuffer().GetSize().DecrementInBoundsCircular(coord);
}

// Routine Description:
Expand All @@ -305,7 +305,7 @@ void Search::_UpdateNextPosition()
// We put the next position to:
// Forward: (0, 0)
// Backward: the position of the end of the text buffer
const auto bufferEndPosition = _uiaData.GetTextBufferEndPosition();
const auto bufferEndPosition = _renderData.GetTextBufferEndPosition();

if (_coordNext.y > bufferEndPosition.y ||
(_coordNext.y == bufferEndPosition.y && _coordNext.x > bufferEndPosition.x))
Expand Down
11 changes: 5 additions & 6 deletions src/buffer/out/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ Revision History:

#pragma once

#include <WinConTypes.h>
#include "TextAttribute.hpp"
#include "textBuffer.hpp"
#include "../types/IUiaData.h"
#include "../renderer/inc/IRenderData.hpp"

// This used to be in find.h.
#define SEARCH_STRING_LENGTH (80)
Expand All @@ -40,12 +39,12 @@ class Search final
CaseSensitive
};

Search(Microsoft::Console::Types::IUiaData& uiaData,
Search(Microsoft::Console::Render::IRenderData& renderData,
const std::wstring_view str,
const Direction dir,
const Sensitivity sensitivity);

Search(Microsoft::Console::Types::IUiaData& uiaData,
Search(Microsoft::Console::Render::IRenderData& renderData,
const std::wstring_view str,
const Direction dir,
const Sensitivity sensitivity,
Expand All @@ -66,7 +65,7 @@ class Search final
void _IncrementCoord(til::point& coord) const noexcept;
void _DecrementCoord(til::point& coord) const noexcept;

static til::point s_GetInitialAnchor(const Microsoft::Console::Types::IUiaData& uiaData, const Direction dir);
static til::point s_GetInitialAnchor(const Microsoft::Console::Render::IRenderData& renderData, const Direction dir);

static std::vector<std::wstring> s_CreateNeedleFromString(const std::wstring_view wstr);

Expand All @@ -79,7 +78,7 @@ class Search final
const std::vector<std::wstring> _needle;
const Direction _direction;
const Sensitivity _sensitivity;
Microsoft::Console::Types::IUiaData& _uiaData;
Microsoft::Console::Render::IRenderData& _renderData;

#ifdef UNIT_TESTING
friend class SearchTests;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/PublicTerminalCore/HwndTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ void HwndTerminal::RegisterWriteCallback(const void _stdcall callback(wchar_t*))
_pfnWriteCallback = callback;
}

::Microsoft::Console::Types::IUiaData* HwndTerminal::GetUiaData() const noexcept
::Microsoft::Console::Render::IRenderData* HwndTerminal::GetRenderData() const noexcept
{
return _terminal.get();
}
Expand Down Expand Up @@ -312,7 +312,7 @@ IRawElementProviderSimple* HwndTerminal::_GetUiaProvider() noexcept
try
{
auto lock = _terminal->LockForWriting();
LOG_IF_FAILED(::Microsoft::WRL::MakeAndInitialize<HwndTerminalAutomationPeer>(&_uiaProvider, this->GetUiaData(), this));
LOG_IF_FAILED(::Microsoft::WRL::MakeAndInitialize<HwndTerminalAutomationPeer>(&_uiaProvider, this->GetRenderData(), this));
_uiaEngine = std::make_unique<::Microsoft::Console::Render::UiaEngine>(_uiaProvider.Get());
LOG_IF_FAILED(_uiaEngine->Enable());
_renderer->AddRenderEngine(_uiaEngine.get());
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/PublicTerminalCore/HwndTerminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct HwndTerminal : ::Microsoft::Console::Types::IControlAccessibilityInfo
HRESULT Refresh(const til::size windowSize, _Out_ til::size* dimensions);
void RegisterScrollCallback(std::function<void(int, int, int)> callback);
void RegisterWriteCallback(const void _stdcall callback(wchar_t*));
::Microsoft::Console::Types::IUiaData* GetUiaData() const noexcept;
::Microsoft::Console::Render::IRenderData* GetRenderData() const noexcept;
HWND GetHwnd() const noexcept;

static LRESULT CALLBACK HwndTerminalWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) noexcept;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return result;
}

::Microsoft::Console::Types::IUiaData* ControlCore::GetUiaData() const
::Microsoft::Console::Render::IRenderData* ControlCore::GetRenderData() const
{
return _terminal.get();
}
Expand Down Expand Up @@ -1453,7 +1453,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
Search::Sensitivity::CaseSensitive :
Search::Sensitivity::CaseInsensitive;

::Search search(*GetUiaData(), text.c_str(), direction, sensitivity);
::Search search(*GetRenderData(), text.c_str(), direction, sensitivity);
auto lock = _terminal->LockForWriting();
const auto foundMatch{ search.FindNext() };
if (foundMatch)
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
winrt::hstring HoveredUriText() const;
Windows::Foundation::IReference<Core::Point> HoveredCell() const;

::Microsoft::Console::Types::IUiaData* GetUiaData() const;
::Microsoft::Console::Render::IRenderData* GetRenderData() const;

void ColorSelection(const Control::SelectionColor& fg, const Control::SelectionColor& bg, Core::MatchMode matchMode);

Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalControl/ControlInteractivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return nullptr;
}

::Microsoft::Console::Types::IUiaData* ControlInteractivity::GetUiaData() const
::Microsoft::Console::Render::IRenderData* ControlInteractivity::GetRenderData() const
{
return _core->GetUiaData();
return _core->GetRenderData();
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/ControlInteractivity.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
Control::ControlCore Core();

Control::InteractivityAutomationPeer OnCreateAutomationPeer();
::Microsoft::Console::Types::IUiaData* GetUiaData() const;
::Microsoft::Console::Render::IRenderData* GetRenderData() const;

#pragma region Input Methods
void PointerPressed(Control::MouseButtonState buttonState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
InteractivityAutomationPeer::InteractivityAutomationPeer(Control::implementation::ControlInteractivity* owner) :
_interactivity{ owner }
{
THROW_IF_FAILED(::Microsoft::WRL::MakeAndInitialize<::Microsoft::Terminal::TermControlUiaProvider>(&_uiaProvider, _interactivity->GetUiaData(), this));
THROW_IF_FAILED(::Microsoft::WRL::MakeAndInitialize<::Microsoft::Terminal::TermControlUiaProvider>(&_uiaProvider, _interactivity->GetRenderData(), this));
};

void InteractivityAutomationPeer::SetControlBounds(const Windows::Foundation::Rect bounds)
Expand Down
11 changes: 2 additions & 9 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
#include "../../terminal/adapter/ITerminalApi.hpp"
#include "../../terminal/parser/StateMachine.hpp"
#include "../../terminal/input/terminalInput.hpp"

#include "../../types/inc/Viewport.hpp"
#include "../../types/inc/GlyphWidth.hpp"
#include "../../types/IUiaData.h"
#include "../../cascadia/terminalcore/ITerminalInput.hpp"

#include <til/ticket_lock.h>
Expand Down Expand Up @@ -57,8 +55,7 @@ namespace TerminalCoreUnitTests
class Microsoft::Terminal::Core::Terminal final :
public Microsoft::Console::VirtualTerminal::ITerminalApi,
public Microsoft::Terminal::Core::ITerminalInput,
public Microsoft::Console::Render::IRenderData,
public Microsoft::Console::Types::IUiaData
public Microsoft::Console::Render::IRenderData
{
using RenderSettings = Microsoft::Console::Render::RenderSettings;

Expand Down Expand Up @@ -172,17 +169,15 @@ class Microsoft::Terminal::Core::Terminal final :
std::optional<interval_tree::IntervalTree<til::point, size_t>::interval> GetHyperlinkIntervalFromViewportPosition(const til::point viewportPos);
#pragma endregion

#pragma region IBaseData(base to IRenderData and IUiaData)
#pragma region IRenderData
Microsoft::Console::Types::Viewport GetViewport() noexcept override;
til::point GetTextBufferEndPosition() const noexcept override;
const TextBuffer& GetTextBuffer() const noexcept override;
const FontInfo& GetFontInfo() const noexcept override;

void LockConsole() noexcept override;
void UnlockConsole() noexcept override;
#pragma endregion

#pragma region IRenderData
// These methods are defined in TerminalRenderData.cpp
til::point GetCursorPosition() const noexcept override;
bool IsCursorVisible() const noexcept override;
Expand All @@ -196,9 +191,7 @@ class Microsoft::Terminal::Core::Terminal final :
const std::wstring GetHyperlinkUri(uint16_t id) const override;
const std::wstring GetHyperlinkCustomId(uint16_t id) const override;
const std::vector<size_t> GetPatternId(const til::point location) const override;
#pragma endregion

#pragma region IUiaData
std::pair<COLORREF, COLORREF> GetAttributeColors(const TextAttribute& attr) const noexcept override;
std::vector<Microsoft::Console::Types::Viewport> GetSelectionRects() noexcept override;
const bool IsSelectionActive() const noexcept override;
Expand Down
7 changes: 0 additions & 7 deletions src/host/renderData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ using namespace Microsoft::Console::Types;
using namespace Microsoft::Console::Interactivity;
using Microsoft::Console::Interactivity::ServiceLocator;

#pragma region IBaseData
// Routine Description:
// - Retrieves the viewport that applies over the data available in the GetTextBuffer() call
// Return Value:
Expand Down Expand Up @@ -98,9 +97,6 @@ void RenderData::UnlockConsole() noexcept
::UnlockConsole();
}

#pragma endregion

#pragma region IRenderData
// Method Description:
// - Gets the cursor's position in the buffer, relative to the buffer origin.
// Arguments:
Expand Down Expand Up @@ -327,9 +323,7 @@ const std::vector<size_t> RenderData::GetPatternId(const til::point /*location*/
{
return {};
}
#pragma endregion

#pragma region IUiaData
// Routine Description:
// - Converts a text attribute into the RGB values that should be presented, applying
// relevant table translation information and preferences.
Expand Down Expand Up @@ -443,4 +437,3 @@ void RenderData::ColorSelection(const til::point coordSelectionStart, const til:
{
Selection::Instance().ColorSelection(coordSelectionStart, coordSelectionEnd, attr);
}
#pragma endregion
11 changes: 1 addition & 10 deletions src/host/renderData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ Author(s):
#pragma once

#include "../renderer/inc/IRenderData.hpp"
#include "../types/inc/colorTable.hpp"
#include "../types/IUiaData.h"

class RenderData final :
public Microsoft::Console::Render::IRenderData,
public Microsoft::Console::Types::IUiaData
public Microsoft::Console::Render::IRenderData
{
public:
#pragma region BaseData
Microsoft::Console::Types::Viewport GetViewport() noexcept override;
til::point GetTextBufferEndPosition() const noexcept override;
const TextBuffer& GetTextBuffer() const noexcept override;
Expand All @@ -33,9 +29,7 @@ class RenderData final :

void LockConsole() noexcept override;
void UnlockConsole() noexcept override;
#pragma endregion

#pragma region IRenderData
til::point GetCursorPosition() const noexcept override;
bool IsCursorVisible() const noexcept override;
bool IsCursorOn() const noexcept override;
Expand All @@ -54,9 +48,7 @@ class RenderData final :
const std::wstring GetHyperlinkCustomId(uint16_t id) const override;

const std::vector<size_t> GetPatternId(const til::point location) const override;
#pragma endregion

#pragma region IUiaData
std::pair<COLORREF, COLORREF> GetAttributeColors(const TextAttribute& attr) const noexcept override;
const bool IsSelectionActive() const override;
const bool IsBlockSelection() const noexcept override;
Expand All @@ -66,5 +58,4 @@ class RenderData final :
const til::point GetSelectionEnd() const noexcept;
void ColorSelection(const til::point coordSelectionStart, const til::point coordSelectionEnd, const TextAttribute attr);
const bool IsUiaDataInitialized() const noexcept override { return true; }
#pragma endregion
};
2 changes: 1 addition & 1 deletion src/host/ut_host/VtIoTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void VtIoTests::DtorTestStackAllocMany()
}
}

class MockRenderData : public IRenderData, IUiaData
class MockRenderData : public IRenderData
{
public:
Microsoft::Console::Types::Viewport GetViewport() noexcept override
Expand Down
Loading

0 comments on commit 4d9a266

Please sign in to comment.