Skip to content
Open
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
37 changes: 36 additions & 1 deletion src/tsf/Implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ HRESULT Implementation::Initialize()
RETURN_IF_FAILED(_contextSource->AdviseSink(IID_ITfTextEditSink, static_cast<ITfTextEditSink*>(this), &_cookieTextEditSink));

RETURN_IF_FAILED(_documentMgr->Push(_context.get()));

RETURN_IF_FAILED(_threadMgrEx.query<ITfSource>()->AdviseSink(IID_ITfThreadFocusSink, static_cast<ITfThreadFocusSink*>(this), &_cookieThreadFocusSink));
return S_OK;
}

Expand All @@ -110,6 +112,10 @@ void Implementation::Uninitialize() noexcept
std::ignore = _threadMgrEx->AssociateFocus(_associatedHwnd, nullptr, prev.addressof());
}

if (_cookieThreadFocusSink != TF_INVALID_COOKIE)
{
std::ignore = _threadMgrEx.query<ITfSource>()->UnadviseSink(_cookieThreadFocusSink);
}
if (_cookieTextEditSink != TF_INVALID_COOKIE)
{
std::ignore = _contextSource->UnadviseSink(_cookieTextEditSink);
Expand Down Expand Up @@ -219,7 +225,12 @@ void Implementation::Unfocus(IDataProvider* provider)

_provider.reset();

if (_compositions > 0 && _ownerCompositionServices)
// Leave the composition to the IME if the whole window was deactivated, see GH#20936
if (_compositions > 0 && !GetActiveWindow())
{
_refocusOnThreadFocus = true;
}
else if (_compositions > 0 && _ownerCompositionServices)
{
std::ignore = _ownerCompositionServices->TerminateComposition(nullptr);
}
Expand Down Expand Up @@ -272,6 +283,10 @@ STDMETHODIMP Implementation::QueryInterface(REFIID riid, void** ppvObj) noexcept
{
*ppvObj = static_cast<ITfTextEditSink*>(this);
}
else if (IsEqualGUID(riid, IID_ITfThreadFocusSink))
{
*ppvObj = static_cast<ITfThreadFocusSink*>(this);
}
else if (IsEqualGUID(riid, IID_IUnknown))
{
*ppvObj = static_cast<IUnknown*>(static_cast<ITfContextOwner*>(this));
Expand Down Expand Up @@ -459,6 +474,26 @@ CATCH_RETURN()

#pragma endregion ITfTextEditSink

#pragma region ITfThreadFocusSink

STDMETHODIMP Implementation::OnSetThreadFocus() noexcept
{
// Refocus our document if the IME committed the composition left to it while the thread had no TSF focus, see GH#20936
if (std::exchange(_refocusOnThreadFocus, false) && _provider)
{
std::ignore = _threadMgrEx->SetFocus(nullptr);
std::ignore = _threadMgrEx->SetFocus(_documentMgr.get());
}
return S_OK;
}

STDMETHODIMP Implementation::OnKillThreadFocus() noexcept
{
return S_OK;
}

#pragma endregion ITfThreadFocusSink

Implementation::EditSessionProxyBase::EditSessionProxyBase(Implementation* self) noexcept :
self{ self }
{
Expand Down
8 changes: 7 additions & 1 deletion src/tsf/Implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft::Console::TSF
{
struct IDataProvider;

struct Implementation : ITfContextOwner, ITfContextOwnerCompositionSink, ITfTextEditSink
struct Implementation : ITfContextOwner, ITfContextOwnerCompositionSink, ITfTextEditSink, ITfThreadFocusSink
{
static void AvoidBuggyTSFConsoleFlags() noexcept;
static void SetDefaultScopeAlphanumericHalfWidth(bool enable) noexcept;
Expand Down Expand Up @@ -51,6 +51,10 @@ namespace Microsoft::Console::TSF
// ITfTextEditSink methods
STDMETHODIMP OnEndEdit(ITfContext* pic, TfEditCookie ecReadOnly, ITfEditRecord* pEditRecord) noexcept override;

// ITfThreadFocusSink methods
STDMETHODIMP OnSetThreadFocus() noexcept override;
STDMETHODIMP OnKillThreadFocus() noexcept override;

private:
struct EditSessionProxyBase : ITfEditSession
{
Expand Down Expand Up @@ -125,10 +129,12 @@ namespace Microsoft::Console::TSF
wil::com_ptr<ITfSource> _contextSource;
DWORD _cookieContextOwner = TF_INVALID_COOKIE;
DWORD _cookieTextEditSink = TF_INVALID_COOKIE;
DWORD _cookieThreadFocusSink = TF_INVALID_COOKIE;
TfClientId _clientId = TF_CLIENTID_NULL;

EditSessionProxy<&Implementation::_doCompositionUpdate> _editSessionCompositionUpdate{ this };
int _compositions = 0;
bool _refocusOnThreadFocus = false;

AnsiInputScope _ansiInputScope{ this };
};
Expand Down