Skip to content

Commit

Permalink
Update window background when the color table changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
j4james committed May 2, 2022
1 parent aa7bd41 commit 50c87e3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 40 deletions.
2 changes: 0 additions & 2 deletions src/cascadia/PublicTerminalCore/HwndTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ HRESULT HwndTerminal::Initialize()

_renderEngine = std::move(dxEngine);

_terminal->SetBackgroundCallback([](auto) {});

_terminal->Create(COORD{ 80, 25 }, 1000, *_renderer);
_terminal->SetWriteInputCallback([=](std::wstring_view input) noexcept { _WriteTextToConnection(input); });
localPointerToThread->EnablePainting();
Expand Down
28 changes: 8 additions & 20 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
auto pfnTabColorChanged = std::bind(&ControlCore::_terminalTabColorChanged, this, std::placeholders::_1);
_terminal->SetTabColorChangedCallback(pfnTabColorChanged);

auto pfnBackgroundColorChanged = std::bind(&ControlCore::_terminalBackgroundColorChanged, this, std::placeholders::_1);
_terminal->SetBackgroundCallback(pfnBackgroundColorChanged);

auto pfnScrollPositionChanged = std::bind(&ControlCore::_terminalScrollPositionChanged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
_terminal->SetScrollPositionChangedCallback(pfnScrollPositionChanged);

Expand Down Expand Up @@ -128,6 +125,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
const auto& renderSettings = _terminal->GetRenderSettings();
_renderer = std::make_unique<::Microsoft::Console::Render::Renderer>(renderSettings, _terminal.get(), nullptr, 0, std::move(renderThread));

_renderer->SetBackgroundColorChangedCallback([this]() { _rendererBackgroundColorChanged(); });

_renderer->SetRendererEnteredErrorStateCallback([weakThis = get_weak()]() {
if (auto strongThis{ weakThis.get() })
{
Expand Down Expand Up @@ -1153,21 +1152,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_TabColorChangedHandlers(*this, nullptr);
}

// Method Description:
// - Called for the Terminal's BackgroundColorChanged callback. This will
// re-raise a new winrt TypedEvent that can be listened to.
// - The listeners to this event will re-query the control for the current
// value of BackgroundColor().
// Arguments:
// - <unused>
// Return Value:
// - <none>
void ControlCore::_terminalBackgroundColorChanged(const COLORREF /*color*/)
{
// Raise a BackgroundColorChanged event
_BackgroundColorChangedHandlers(*this, nullptr);
}

// Method Description:
// - Update the position and size of the scrollbar to match the given
// viewport top, viewport height, and buffer size.
Expand Down Expand Up @@ -1360,6 +1344,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_SwapChainChangedHandlers(*this, nullptr);
}

void ControlCore::_rendererBackgroundColorChanged()
{
_BackgroundColorChangedHandlers(*this, nullptr);
}

void ControlCore::BlinkAttributeTick()
{
auto lock = _terminal->LockForWriting();
Expand Down Expand Up @@ -1684,8 +1673,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

_renderEngine->SetSelectionBackground(til::color{ _settings->SelectionBackground() });

_renderer->TriggerRedrawAll();
_BackgroundColorChangedHandlers(*this, nullptr);
_renderer->TriggerRedrawAll(true);
}

bool ControlCore::HasUnfocusedAppearance() const
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 @@ -265,7 +265,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void _terminalWarningBell();
void _terminalTitleChanged(std::wstring_view wstr);
void _terminalTabColorChanged(const std::optional<til::color> color);
void _terminalBackgroundColorChanged(const COLORREF color);
void _terminalScrollPositionChanged(const int viewTop,
const int viewHeight,
const int bufferSize);
Expand All @@ -277,6 +276,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
#pragma region RendererCallbacks
void _rendererWarning(const HRESULT hr);
void _renderEngineSwapChainChanged();
void _rendererBackgroundColorChanged();
#pragma endregion

void _raiseReadOnlyWarning();
Expand Down
9 changes: 0 additions & 9 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,15 +1301,6 @@ void Terminal::SetCursorPositionChangedCallback(std::function<void()> pfn) noexc
_pfnCursorPositionChanged.swap(pfn);
}

// Method Description:
// - Allows setting a callback for when the background color is changed
// Arguments:
// - pfn: a function callback that takes a color
void Terminal::SetBackgroundCallback(std::function<void(const til::color)> pfn) noexcept
{
_pfnBackgroundColorChanged.swap(pfn);
}

// Method Description:
// - Allows settings a callback for settings the taskbar progress indicator
// Arguments:
Expand Down
2 changes: 0 additions & 2 deletions src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ class Microsoft::Terminal::Core::Terminal final :
void SetCopyToClipboardCallback(std::function<void(std::wstring_view)> pfn) noexcept;
void SetScrollPositionChangedCallback(std::function<void(const int, const int, const int)> pfn) noexcept;
void SetCursorPositionChangedCallback(std::function<void()> pfn) noexcept;
void SetBackgroundCallback(std::function<void(const til::color)> pfn) noexcept;
void TaskbarProgressChangedCallback(std::function<void()> pfn) noexcept;
void SetShowWindowCallback(std::function<void(bool)> pfn) noexcept;

Expand Down Expand Up @@ -268,7 +267,6 @@ class Microsoft::Terminal::Core::Terminal final :
#endif

std::function<void(const int, const int, const int)> _pfnScrollPositionChanged;
std::function<void(const til::color)> _pfnBackgroundColorChanged;
std::function<void()> _pfnCursorPositionChanged;
std::function<void(const std::optional<til::color>)> _pfnTabColorChanged;
std::function<void()> _pfnTaskbarProgressChanged;
Expand Down
20 changes: 18 additions & 2 deletions src/renderer/base/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,22 @@ void Renderer::TriggerRedrawCursor(const COORD* const pcoord)
// - Called when something that changes the output state has occurred and the entire frame is now potentially invalid.
// - NOTE: Use sparingly. Try to reduce the refresh region where possible. Only use when a global state change has occurred.
// Arguments:
// - <none>
// - backgroundChanged - Set to true if the background color has changed.
// Return Value:
// - <none>
void Renderer::TriggerRedrawAll()
void Renderer::TriggerRedrawAll(const bool backgroundChanged)
{
FOREACH_ENGINE(pEngine)
{
LOG_IF_FAILED(pEngine->InvalidateAll());
}

NotifyPaintFrame();

if (backgroundChanged && _pfnBackgroundColorChanged)
{
_pfnBackgroundColorChanged();
}
}

// Method Description:
Expand Down Expand Up @@ -1331,6 +1336,17 @@ void Renderer::AddRenderEngine(_In_ IRenderEngine* const pEngine)
THROW_HR_MSG(E_UNEXPECTED, "engines array is full");
}

// Method Description:
// - Registers a callback for when the background color is changed
// Arguments:
// - pfn: the callback
// Return Value:
// - <none>
void Renderer::SetBackgroundColorChangedCallback(std::function<void()> pfn)
{
_pfnBackgroundColorChanged = std::move(pfn);
}

// Method Description:
// - Registers a callback that will be called when this renderer gives up.
// An application consuming a renderer can use this to display auxiliary Retry UI
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/base/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace Microsoft::Console::Render
void TriggerRedraw(const Microsoft::Console::Types::Viewport& region);
void TriggerRedraw(const COORD* const pcoord);
void TriggerRedrawCursor(const COORD* const pcoord);
void TriggerRedrawAll();
void TriggerRedrawAll(const bool backgroundChanged = false);
void TriggerTeardown() noexcept;

void TriggerSelection();
Expand Down Expand Up @@ -84,6 +84,7 @@ namespace Microsoft::Console::Render

void AddRenderEngine(_In_ IRenderEngine* const pEngine);

void SetBackgroundColorChangedCallback(std::function<void()> pfn);
void SetRendererEnteredErrorStateCallback(std::function<void()> pfn);
void ResetErrorStateAndResume();

Expand Down Expand Up @@ -121,6 +122,7 @@ namespace Microsoft::Console::Render
Microsoft::Console::Types::Viewport _viewport;
std::vector<Cluster> _clusterBuffer;
std::vector<SMALL_RECT> _previousSelection;
std::function<void()> _pfnBackgroundColorChanged;
std::function<void()> _pfnRendererEnteredErrorState;
bool _destructing = false;

Expand Down
8 changes: 5 additions & 3 deletions src/terminal/adapter/adaptDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2234,12 +2234,14 @@ bool AdaptDispatch::SetColorTableEntry(const size_t tableIndex, const DWORD dwCo
return false;
}

// TODO: In the Terminal we also need to call the _pfnBackgroundColorChanged
// handler when this is going to affect the background color.
// If we're updating the background color, we need to let the renderer
// know, since it may want to repaint the window background to match.
const auto backgroundIndex = _renderSettings.GetColorAliasIndex(ColorAlias::DefaultBackground);
const auto backgroundChanged = (tableIndex == backgroundIndex);

// Update the screen colors if we're not a pty
// No need to force a redraw in pty mode.
_renderer.TriggerRedrawAll();
_renderer.TriggerRedrawAll(backgroundChanged);
return true;
}

Expand Down

0 comments on commit 50c87e3

Please sign in to comment.