Skip to content

Commit

Permalink
There's no reason that this should have entirely broken moving panes.…
Browse files Browse the repository at this point in the history
… That doesn't make sense.
  • Loading branch information
zadjii-msft committed Mar 28, 2023
1 parent 45374e8 commit 31e904a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
23 changes: 18 additions & 5 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,19 +1555,32 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_RendererWarningHandlers(*this, winrt::make<RendererWarningArgs>(hr));
}

void ControlCore::_renderEngineSwapChainChanged(const HANDLE handle)
winrt::fire_and_forget ControlCore::_renderEngineSwapChainChanged(const HANDLE sourceHandle)
{
// `handle` is a weak ref to a HANDLE that's ultimately owned by the
// `sourceHandle` is a weak ref to a HANDLE that's ultimately owned by the
// render engine's own unique_handle. We'll add another ref to it here.
// This will make sure that we always have a valid HANDLE to give to
// callers of our own SwapChainHandle method, even if the renderer is
// currently in the process of discarding this value and creating a new
// one. Callers should have already set up the SwapChainChanged
// callback, so this all works out.
_lastSwapChainHandle.attach(handle);

// Now bubble the event up to the control.
_SwapChainChangedHandlers(*this, winrt::box_value<uint64_t>(reinterpret_cast<uint64_t>(handle)));
winrt::handle duplicatedHandle;
const auto processHandle = GetCurrentProcess();
THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(processHandle, sourceHandle, processHandle, duplicatedHandle.put(), 0, FALSE, DUPLICATE_SAME_ACCESS));

const auto weakThis{ get_weak() };

co_await wil::resume_foreground(_dispatcher);

if (auto core{ weakThis.get() })
{
// `this` is safe to use now

_lastSwapChainHandle = (std::move(duplicatedHandle));
// Now bubble the event up to the control.
_SwapChainChangedHandlers(*this, winrt::box_value<uint64_t>(reinterpret_cast<uint64_t>(_lastSwapChainHandle.get())));
}
}

void ControlCore::_rendererBackgroundColorChanged()
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 @@ -328,7 +328,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

#pragma region RendererCallbacks
void _rendererWarning(const HRESULT hr);
void _renderEngineSwapChainChanged(const HANDLE handle);
winrt::fire_and_forget _renderEngineSwapChainChanged(const HANDLE handle);
void _rendererBackgroundColorChanged();
void _rendererTabColorChanged();
#pragma endregion
Expand Down
36 changes: 13 additions & 23 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -866,28 +866,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return _core.ConnectionState();
}

winrt::fire_and_forget TermControl::RenderEngineSwapChainChanged(IInspectable /*sender*/, IInspectable args)
void TermControl::RenderEngineSwapChainChanged(IInspectable /*sender*/, IInspectable args)
{
// This event comes in on the render thread, not the UI thread.

const auto weakThis{ get_weak() };

winrt::handle handle;

// Add a ref to the handle passed to us, so that the HANDLE will remain
// valid to the other side of the co_await.
handle.attach(reinterpret_cast<HANDLE>(winrt::unbox_value<uint64_t>(args)));

co_await wil::resume_foreground(Dispatcher());

if (auto control{ weakThis.get() })
{
_AttachDxgiSwapChainToXaml(handle.get());
}
// Detach from the handle. If you don't do this, we'll CloseHandle() on
// the handle when `handle` goes out of scope, resulting in the swap
// chain being closed.
handle.detach();
// This event comes in on the UI thread
HANDLE h = reinterpret_cast<HANDLE>(winrt::unbox_value<uint64_t>(args));
_AttachDxgiSwapChainToXaml(h);
}

// Method Description:
Expand Down Expand Up @@ -1818,7 +1801,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// GH#5421: Enable the UiaEngine before checking for the SearchBox
// That way, new selections are notified to automation clients.
// The _uiaEngine lives in _interactivity, so call into there to enable it.
_interactivity.GotFocus();

if (_interactivity)
{
_interactivity.GotFocus();
}

// If the searchbox is focused, we don't want TSFInputControl to think
// it has focus so it doesn't intercept IME input. We also don't want the
Expand Down Expand Up @@ -1873,7 +1860,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation

// This will disable the accessibility notifications, because the
// UiaEngine lives in ControlInteractivity
_interactivity.LostFocus();
if (_interactivity)
{
_interactivity.LostFocus();
}

if (TSFInputControl() != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void ToggleShaderEffects();

winrt::fire_and_forget RenderEngineSwapChainChanged(IInspectable sender, IInspectable args);
void RenderEngineSwapChainChanged(IInspectable sender, IInspectable args);
void _AttachDxgiSwapChainToXaml(HANDLE swapChainHandle);
winrt::fire_and_forget _RendererEnteredErrorState(IInspectable sender, IInspectable args);

Expand Down

0 comments on commit 31e904a

Please sign in to comment.