Skip to content

Commit

Permalink
fix(tsf): fix candidate behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nameoverflow committed Apr 2, 2018
1 parent 11be1c4 commit 9e2f9f1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 61 deletions.
103 changes: 57 additions & 46 deletions WeaselTSF/CandidateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "WeaselTSF.h"
#include "CandidateList.h"
#include <ComPtr.h>

using namespace std;
using namespace weasel;

CandidateList::CandidateList(WeaselTSF * pTextService)
: _ui(make_unique<UI>())
, _curp(NULL)
{
//_ui->Create(NULL);
_cRef = 1;
Expand All @@ -30,14 +32,15 @@ STDMETHODIMP CandidateList::QueryInterface(REFIID riid, void ** ppvObj)
*ppvObj = nullptr;

if (IsEqualIID(riid, IID_ITfUIElement) ||
IsEqualIID(riid, IID_ITfCandidateListUIElement))
IsEqualIID(riid, IID_ITfCandidateListUIElement) ||
IsEqualIID(riid, IID_ITfCandidateListUIElementBehavior))
{
*ppvObj = (ITfCandidateListUIElement*)this;
*ppvObj = (ITfCandidateListUIElementBehavior*)this;
}
else if (IsEqualIID(riid, IID_IUnknown) ||
IsEqualIID(riid, IID_ITfCandidateListUIElementBehavior))
IsEqualIID(riid, __uuidof(ITfIntegratableCandidateListUIElement)))
{
*ppvObj = (ITfCandidateListUIElementBehavior*)this;
*ppvObj = (ITfIntegratableCandidateListUIElement*)this;
}

if (*ppvObj)
Expand Down Expand Up @@ -87,29 +90,10 @@ STDMETHODIMP CandidateList::GetGUID(GUID * pguid)

STDMETHODIMP CandidateList::Show(BOOL showCandidateWindow)
{
BOOL pbShow = true;
ITfUIElementMgr *emgr = nullptr;

if (FAILED(_tsf->_pThreadMgr->QueryInterface(IID_ITfUIElementMgr, (void **)&emgr)) || emgr == nullptr) {
return E_FAIL;
}

emgr->BeginUIElement(this, &pbShow, &uiid);
if (!pbShow) {
emgr->UpdateUIElement(uiid);
}

if (pbShow && showCandidateWindow)
if (showCandidateWindow)
_ui->Show();
else
_ui->Hide();

if (!showCandidateWindow) {
emgr->EndUIElement(uiid);
}

emgr->Release();

return S_OK;
}

Expand Down Expand Up @@ -205,6 +189,7 @@ STDMETHODIMP CandidateList::Finalize(void)
STDMETHODIMP CandidateList::Abort(void)
{
_tsf->_AbortComposition(true);
Destroy();
return S_OK;
}

Expand Down Expand Up @@ -248,17 +233,13 @@ void CandidateList::UpdateUI(const Context & ctx, const Status & status)

/// In UWP, candidate window will only be shown
/// if it is owned by active view window
HWND actw = _GetActiveWnd();
if (actw != _curp) {
UIStyle sty = _ui->style();
_ui->Destroy();
_ui->Create(actw);
_curp = actw;
_ui->style() = sty;
}
_UpdateOwner();
_ui->Update(ctx, status);

Show(status.composing);
if (status.composing)
_StartUI();
else
_EndUI();
}

void CandidateList::UpdateStyle(const UIStyle & sty)
Expand All @@ -283,37 +264,67 @@ UIStyle & CandidateList::style()
return _ui->style();
}

void CandidateList::_UpdateOwner()
{
HWND actw = _GetActiveWnd();
if (actw != _curp) {
UIStyle sty = _ui->style();
_ui->Destroy();
_ui->Create(actw);
_curp = actw;
_ui->style() = sty;
}
}

HWND CandidateList::_GetActiveWnd()
{
ITfDocumentMgr *dmgr = nullptr;
ITfContext *ctx = nullptr;
ITfContextView *view = nullptr;
ComPtr<ITfDocumentMgr> dmgr;
ComPtr<ITfContext> ctx;
ComPtr<ITfContextView> view;
HWND w = NULL;

if (FAILED(_tsf->_pThreadMgr->GetFocus(&dmgr))) {
if (FAILED(_tsf->_pThreadMgr->GetFocus(dmgr.GetAddressOf()))) {
goto Exit;
}
if (FAILED(dmgr->GetTop(&ctx))) {
if (FAILED(dmgr->GetTop(ctx.GetAddressOf()))) {
goto Exit;
}
if (FAILED(ctx->GetActiveView(&view))) {
if (FAILED(ctx->GetActiveView(view.GetAddressOf()))) {
goto Exit;
}

view->GetWnd(&w);

Exit:
if (dmgr)
dmgr->Release();
if (ctx)
ctx->Release();
if (view)
view->Release();

if (w == NULL) w = ::GetFocus();
return w;
}

void CandidateList::_StartUI()
{
BOOL pbShow = TRUE;
ComPtr<ITfUIElementMgr> emgr;
_tsf->_pThreadMgr->QueryInterface(emgr.GetAddressOf());

if (emgr) {
if (!_ui->IsShown())
emgr->BeginUIElement(this, &pbShow, &uiid);
emgr->UpdateUIElement(uiid);
}

Show(pbShow);
}

void CandidateList::_EndUI()
{
ComPtr<ITfUIElementMgr> emgr;
_tsf->_pThreadMgr->QueryInterface(emgr.GetAddressOf());
if (emgr)
emgr->EndUIElement(uiid);
if (_ui->IsShown())
Show(false);
}

void WeaselTSF::_UpdateUI(const Context & ctx, const Status & status)
{
_cand->UpdateUI(ctx, status);
Expand Down
4 changes: 4 additions & 0 deletions WeaselTSF/CandidateList.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ namespace weasel {
UIStyle &style();

private:
void _UpdateOwner();
HWND _GetActiveWnd();

void _StartUI();
void _EndUI();

std::unique_ptr<UI> _ui;
DWORD _cRef;
WeaselTSF *_tsf;
Expand Down
2 changes: 1 addition & 1 deletion WeaselTSF/Composition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ void WeaselTSF::_AbortComposition(bool clear)
if (_IsComposing()) {
_EndComposition(_pEditSessionContext, clear);
}
_cand->Show(false);
_cand->Destroy();
}

void WeaselTSF::_FinalizeComposition()
Expand Down
14 changes: 3 additions & 11 deletions WeaselTSF/EditSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
weasel::Status status;
weasel::Config config;
auto context = std::make_shared<weasel::Context>();

auto _NewComposition = [this, &config]() {
_UpdateCompositionWindow(_pEditSessionContext);
_StartComposition(_pEditSessionContext, _fCUASWorkaroundEnabled && !config.inline_preedit);
};

weasel::ResponseParser parser(&commit, context.get(), &status, &config, &_cand->style());

bool ok = m_client.GetResponseData(std::ref(parser));
Expand All @@ -29,22 +23,20 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
// For auto-selecting, commit and preedit can both exist.
// Commit and close the original composition first.
if (!_IsComposing()) {
_NewComposition();
_StartComposition(_pEditSessionContext, _fCUASWorkaroundEnabled && !config.inline_preedit);
}
_InsertText(_pEditSessionContext, commit);
_EndComposition(_pEditSessionContext, false);
}
if (status.composing && !_IsComposing())
{
_NewComposition();
_StartComposition(_pEditSessionContext, _fCUASWorkaroundEnabled && !config.inline_preedit);
}
else if (!status.composing && _IsComposing())
{
_EndComposition(_pEditSessionContext, true);
}
if (status.composing) {
_UpdateCompositionWindow(_pEditSessionContext);
}
_UpdateCompositionWindow(_pEditSessionContext);
if (_IsComposing() && config.inline_preedit)
{
_ShowInlinePreedit(_pEditSessionContext, context);
Expand Down
2 changes: 2 additions & 0 deletions WeaselTSF/WeaselTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ STDAPI WeaselTSF::Deactivate()

_tfClientId = TF_CLIENTID_NULL;

_cand->Destroy();

return S_OK;
}

Expand Down
7 changes: 4 additions & 3 deletions WeaselUI/WeaselUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ bool UI::Create(HWND parent)

void UI::Destroy()
{
if (pimpl_ && pimpl_->panel.IsWindow())
if (pimpl_)
{
pimpl_->panel.DestroyWindow();
if (pimpl_->panel.IsWindow())
pimpl_->panel.DestroyWindow();
delete pimpl_;
pimpl_ = 0;
}
Expand Down Expand Up @@ -99,7 +100,7 @@ void UI::Refresh()

void UI::UpdateInputPosition(RECT const& rc)
{
if (pimpl_)
if (pimpl_ && pimpl_->panel.IsWindow())
{
pimpl_->panel.MoveTo(rc);
}
Expand Down

0 comments on commit 9e2f9f1

Please sign in to comment.