Cache the calculation of default color indices.

This commit is contained in:
James Holderness 2021-11-13 21:47:57 +00:00
parent 96dd85fd72
commit 6ebcb5eaed
12 changed files with 71 additions and 59 deletions

View file

@ -99,6 +99,7 @@ class TerminalCoreUnitTests::ConptyRoundtripTests final
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, INVALID_COLOR);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, INVALID_COLOR);
gci.SetFillAttribute(0x07); // DARK_WHITE on DARK_BLACK
gci.CalculateDefaultColorIndices();
m_state->PrepareNewTextBufferInfo(true, TerminalViewWidth, TerminalViewHeight);
auto& currentBuffer = gci.GetActiveOutputBuffer();

View file

@ -219,36 +219,6 @@ InputBuffer* const CONSOLE_INFORMATION::GetActiveInputBuffer() const
return pInputBuffer;
}
// Method Description:
// - Return the color table index of the default foreground color. If the settings
// are configured to have a default foreground color (separate from the color
// table), this will return the extended index TextColor::DEFAULT_FOREGROUND.
// Otherwise it will return an index in the range 0 to 15.
// Arguments:
// - <none>
// Return Value:
// - the color table index of the default foreground color.
size_t CONSOLE_INFORMATION::GetDefaultForegroundIndex() const noexcept
{
const auto fg = GetColorTableEntry(TextColor::DEFAULT_FOREGROUND);
return fg != INVALID_COLOR ? TextColor::DEFAULT_FOREGROUND : TextColor::TransposeLegacyIndex(LOBYTE(GetFillAttribute()) & FG_ATTRS);
}
// Method Description:
// - Return the color table index of the default background color. If the settings
// are configured to have a default background color (separate from the color
// table), this will return the extended index TextColor::DEFAULT_BACKGROUND.
// Otherwise it will return an index in the range 0 to 15.
// Arguments:
// - <none>
// Return Value:
// - the color table index of the default background color.
size_t CONSOLE_INFORMATION::GetDefaultBackgroundIndex() const noexcept
{
const auto bg = GetColorTableEntry(TextColor::DEFAULT_BACKGROUND);
return bg != INVALID_COLOR ? TextColor::DEFAULT_BACKGROUND : TextColor::TransposeLegacyIndex((LOBYTE(GetFillAttribute()) & BG_ATTRS) >> 4);
}
// Method Description:
// - Get the colors of a particular text attribute, using our color table,
// and our configured default attributes.
@ -257,26 +227,12 @@ size_t CONSOLE_INFORMATION::GetDefaultBackgroundIndex() const noexcept
// Return Value:
// - The color values of the attribute's foreground and background.
std::pair<COLORREF, COLORREF> CONSOLE_INFORMATION::LookupAttributeColors(const TextAttribute& attr) const noexcept
{
return LookupAttributeColors(attr, GetDefaultForegroundIndex(), GetDefaultBackgroundIndex());
}
// Method Description:
// - Get the colors of a particular text attribute, using our color table,
// and the given default color values.
// Arguments:
// - attr: the TextAttribute to retrieve the foreground and background color of.
// - defaultFgIndex: the color table index to use for a default foreground color.
// - defaultBgIndex: the color table index to use for a default background color.
// Return Value:
// - The color values of the attribute's foreground and background.
std::pair<COLORREF, COLORREF> CONSOLE_INFORMATION::LookupAttributeColors(const TextAttribute& attr, const size_t defaultFgIndex, const size_t defaultBgIndex) const noexcept
{
_blinkingState.RecordBlinkingUsage(attr);
return attr.CalculateRgbColors(
GetColorTable(),
defaultFgIndex,
defaultBgIndex,
GetDefaultForegroundIndex(),
GetDefaultBackgroundIndex(),
IsScreenReversed(),
_blinkingState.IsBlinkingFaint());
}

View file

@ -642,6 +642,17 @@ try
gci.SetColorTableEntry(tableIndex, color);
// If we're setting the default foreground or background colors
// we need to make sure the index is correctly set as well.
if (tableIndex == TextColor::DEFAULT_FOREGROUND)
{
gci.SetDefaultForegroundIndex(TextColor::DEFAULT_FOREGROUND);
}
if (tableIndex == TextColor::DEFAULT_BACKGROUND)
{
gci.SetDefaultBackgroundIndex(TextColor::DEFAULT_BACKGROUND);
}
// Update the screen colors if we're not a pty
// No need to force a redraw in pty mode.
if (g.pRender && !gci.IsInVtIoMode())

View file

@ -110,8 +110,6 @@ void RenderData::UnlockConsole() noexcept
const TextAttribute RenderData::GetDefaultBrushColors() noexcept
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
_defaultForegroundIndex = gci.GetDefaultForegroundIndex();
_defaultBackgroundIndex = gci.GetDefaultBackgroundIndex();
return gci.GetActiveOutputBuffer().GetAttributes();
}
@ -364,7 +362,7 @@ const std::vector<size_t> RenderData::GetPatternId(const COORD /*location*/) con
std::pair<COLORREF, COLORREF> RenderData::GetAttributeColors(const TextAttribute& attr) const noexcept
{
const CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
return gci.LookupAttributeColors(attr, _defaultForegroundIndex, _defaultBackgroundIndex);
return gci.LookupAttributeColors(attr);
}
#pragma endregion

View file

@ -72,8 +72,4 @@ public:
void ColorSelection(const COORD coordSelectionStart, const COORD coordSelectionEnd, const TextAttribute attr);
const bool IsUiaDataInitialized() const noexcept override { return true; }
#pragma endregion
private:
size_t _defaultForegroundIndex = 7;
size_t _defaultBackgroundIndex = 0;
};

View file

@ -124,10 +124,7 @@ public:
COOKED_READ_DATA& CookedReadData() noexcept;
void SetCookedReadData(COOKED_READ_DATA* readData) noexcept;
size_t GetDefaultForegroundIndex() const noexcept;
size_t GetDefaultBackgroundIndex() const noexcept;
std::pair<COLORREF, COLORREF> LookupAttributeColors(const TextAttribute& attr) const noexcept;
std::pair<COLORREF, COLORREF> LookupAttributeColors(const TextAttribute& attr, const size_t defaultFgIndex, const size_t defaultBgIndex) const noexcept;
void SetTitle(const std::wstring_view newTitle);
void SetTitlePrefix(const std::wstring_view newTitlePrefix);

View file

@ -56,6 +56,8 @@ Settings::Settings() :
_fScreenReversed(false),
// window size pixels initialized below
_fInterceptCopyPaste(0),
_defaultForegroundIndex(TextColor::DARK_WHITE),
_defaultBackgroundIndex(TextColor::DARK_BLACK),
_fUseDx(UseDx::Disabled),
_fCopyColor(false)
{
@ -356,6 +358,8 @@ void Settings::Validate()
// At this point the default fill attributes are fully initialized
// so we can pass on the final colors to the TextAttribute class.
TextAttribute::SetLegacyDefaultAttributes(_wFillAttribute);
// And calculate the position of the default colors in the color table.
CalculateDefaultColorIndices();
FAIL_FAST_IF(!(_dwWindowSize.X > 0));
FAIL_FAST_IF(!(_dwWindowSize.Y > 0));
@ -795,6 +799,37 @@ void Settings::SetInterceptCopyPaste(const bool interceptCopyPaste) noexcept
_fInterceptCopyPaste = interceptCopyPaste;
}
void Settings::CalculateDefaultColorIndices() noexcept
{
const auto foregroundColor = _colorTable.at(TextColor::DEFAULT_FOREGROUND);
const auto foregroundIndex = TextColor::TransposeLegacyIndex(_wFillAttribute & FG_ATTRS);
_defaultForegroundIndex = foregroundColor != INVALID_COLOR ? TextColor::DEFAULT_FOREGROUND : foregroundIndex;
const auto backgroundColor = _colorTable.at(TextColor::DEFAULT_BACKGROUND);
const auto backgroundIndex = TextColor::TransposeLegacyIndex((_wFillAttribute & BG_ATTRS) >> 4);
_defaultBackgroundIndex = backgroundColor != INVALID_COLOR ? TextColor::DEFAULT_BACKGROUND : backgroundIndex;
}
size_t Settings::GetDefaultForegroundIndex() const noexcept
{
return _defaultForegroundIndex;
}
void Settings::SetDefaultForegroundIndex(const size_t index) noexcept
{
_defaultForegroundIndex = index;
}
size_t Settings::GetDefaultBackgroundIndex() const noexcept
{
return _defaultBackgroundIndex;
}
void Settings::SetDefaultBackgroundIndex(const size_t index) noexcept
{
_defaultBackgroundIndex = index;
}
bool Settings::IsTerminalScrolling() const noexcept
{
return _TerminalScrolling;

View file

@ -186,6 +186,12 @@ public:
bool GetInterceptCopyPaste() const noexcept;
void SetInterceptCopyPaste(const bool interceptCopyPaste) noexcept;
void CalculateDefaultColorIndices() noexcept;
size_t GetDefaultForegroundIndex() const noexcept;
void SetDefaultForegroundIndex(const size_t index) noexcept;
size_t GetDefaultBackgroundIndex() const noexcept;
void SetDefaultBackgroundIndex(const size_t index) noexcept;
bool IsTerminalScrolling() const noexcept;
void SetTerminalScrolling(const bool terminalScrollingEnabled) noexcept;
@ -248,6 +254,9 @@ private:
bool _fInterceptCopyPaste;
size_t _defaultForegroundIndex;
size_t _defaultBackgroundIndex;
bool _TerminalScrolling;
friend class RegistrySerialization;
};

View file

@ -70,6 +70,7 @@ class ConptyOutputTests
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, INVALID_COLOR);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, INVALID_COLOR);
gci.SetFillAttribute(0x07); // DARK_WHITE on DARK_BLACK
gci.CalculateDefaultColorIndices();
m_state->PrepareNewTextBufferInfo(true, TerminalViewWidth, TerminalViewHeight);
auto& currentBuffer = gci.GetActiveOutputBuffer();

View file

@ -62,6 +62,7 @@ class ScreenBufferTests
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, INVALID_COLOR);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, INVALID_COLOR);
gci.SetFillAttribute(0x07); // DARK_WHITE on DARK_BLACK
gci.CalculateDefaultColorIndices();
m_state->PrepareNewTextBufferInfo();
auto& currentBuffer = gci.GetActiveOutputBuffer();
@ -1390,6 +1391,7 @@ void ScreenBufferTests::VtScrollMarginsNewlineColor()
const COLORREF magenta = RGB(255, 0, 255);
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, yellow);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
const TextAttribute defaultAttrs = {};
si.SetAttributes(defaultAttrs);
@ -2261,6 +2263,7 @@ void ScreenBufferTests::SetDefaultsIndividuallyBothDefault()
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, yellow);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
Log::Comment(NoThrowString().Format(L"Write 6 X's:"));
@ -2363,6 +2366,7 @@ void ScreenBufferTests::SetDefaultsTogether()
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, yellow);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
Log::Comment(NoThrowString().Format(L"Write 6 X's:"));
@ -2434,6 +2438,7 @@ void ScreenBufferTests::ReverseResetWithDefaultBackground()
gci.SetColorTableEntry(TextColor::DEFAULT_FOREGROUND, INVALID_COLOR);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
Log::Comment(NoThrowString().Format(L"Write 3 X's:"));
@ -2502,6 +2507,7 @@ void ScreenBufferTests::BackspaceDefaultAttrs()
COLORREF magenta = RGB(255, 0, 255);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
Log::Comment(NoThrowString().Format(L"Write 2 X's, then backspace one."));
@ -2565,6 +2571,7 @@ void ScreenBufferTests::BackspaceDefaultAttrsWriteCharsLegacy()
COLORREF magenta = RGB(255, 0, 255);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
Log::Comment(NoThrowString().Format(L"Write 2 X's, then backspace one."));
@ -2633,6 +2640,7 @@ void ScreenBufferTests::BackspaceDefaultAttrsInPrompt()
COLORREF magenta = RGB(255, 0, 255);
gci.SetColorTableEntry(TextColor::DEFAULT_BACKGROUND, magenta);
gci.CalculateDefaultColorIndices();
si.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });
TextAttribute expectedDefaults{};

View file

@ -208,10 +208,8 @@ void Clipboard::StoreSelectionToClipboard(bool const copyFormatting)
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
const auto& buffer = gci.GetActiveOutputBuffer().GetTextBuffer();
const auto defaultForegroundIndex = gci.GetDefaultForegroundIndex();
const auto defaultBackgroundIndex = gci.GetDefaultBackgroundIndex();
const auto GetAttributeColors = [=, &gci](const auto& attr) {
return gci.LookupAttributeColors(attr, defaultForegroundIndex, defaultBackgroundIndex);
return gci.LookupAttributeColors(attr);
};
bool includeCRLF, trimTrailingWhitespace;

View file

@ -584,6 +584,8 @@ void Menu::s_PropertiesUpdate(PCONSOLE_STATE_INFO pStateInfo)
// Make sure the updated fill attributes are passed on to the TextAttribute class.
TextAttribute::SetLegacyDefaultAttributes(pStateInfo->ScreenAttributes);
// And recalculate the position of the default colors in the color table.
gci.CalculateDefaultColorIndices();
// Set the screen info's default text attributes to defaults -
ScreenInfo.SetDefaultAttributes({}, TextAttribute{ gci.GetPopupFillAttribute() });