Skip to content

Commit

Permalink
feat(compartment): show IME disabled on language bar (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
nameoverflow authored Sep 3, 2018
1 parent c578997 commit 4015d18
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 15 deletions.
8 changes: 7 additions & 1 deletion WeaselTSF/CandidateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ void CandidateList::UpdateInputPosition(RECT const & rc)

void CandidateList::Destroy()
{
Show(false);
//_EndUI();
Show(FALSE);
_ui->Destroy();
_curp = NULL;
}
Expand Down Expand Up @@ -332,3 +333,8 @@ void WeaselTSF::_UpdateUI(const Context & ctx, const Status & status)
{
_cand->UpdateUI(ctx, status);
}

void WeaselTSF::_DeleteCandidateList()
{
_cand->Destroy();
}
137 changes: 136 additions & 1 deletion WeaselTSF/Compartment.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
#include "stdafx.h"
#include "WeaselTSF.h"
#include "Compartment.h"
#include <resource.h>
#include <ComPtr.h>
#include <functional>

STDAPI CCompartmentEventSink::QueryInterface(REFIID riid, _Outptr_ void **ppvObj)
{
if (ppvObj == nullptr)
return E_INVALIDARG;

*ppvObj = nullptr;

if (IsEqualIID(riid, IID_IUnknown) ||
IsEqualIID(riid, IID_ITfCompartmentEventSink))
{
*ppvObj = (CCompartmentEventSink *)this;
}

if (*ppvObj)
{
AddRef();
return S_OK;
}

return E_NOINTERFACE;
}

STDAPI_(ULONG) CCompartmentEventSink::AddRef()
{
return ++_refCount;
}

STDAPI_(ULONG) CCompartmentEventSink::Release()
{
LONG cr = --_refCount;

assert(_refCount >= 0);

if (_refCount == 0)
{
delete this;
}

return cr;
}

STDAPI CCompartmentEventSink::OnChange(_In_ REFGUID guidCompartment)
{
return _callback(guidCompartment);
}

HRESULT CCompartmentEventSink::_Advise(_In_ IUnknown *punk, _In_ REFGUID guidCompartment)
{
HRESULT hr = S_OK;
ITfCompartmentMgr* pCompartmentMgr = nullptr;
ITfSource* pSource = nullptr;

hr = punk->QueryInterface(IID_ITfCompartmentMgr, (void **)&pCompartmentMgr);
if (FAILED(hr))
{
return hr;
}

hr = pCompartmentMgr->GetCompartment(guidCompartment, &_compartment);
if (SUCCEEDED(hr))
{
hr = _compartment->QueryInterface(IID_ITfSource, (void **)&pSource);
if (SUCCEEDED(hr))
{
hr = pSource->AdviseSink(IID_ITfCompartmentEventSink, this, &_cookie);
pSource->Release();
}
}

pCompartmentMgr->Release();

return hr;
}
HRESULT CCompartmentEventSink::_Unadvise()
{
HRESULT hr = S_OK;
ITfSource* pSource = nullptr;

hr = _compartment->QueryInterface(IID_ITfSource, (void **)&pSource);
if (SUCCEEDED(hr))
{
hr = pSource->UnadviseSink(_cookie);
pSource->Release();
}

_compartment->Release();
_compartment = nullptr;
_cookie = 0;

return hr;
}


BOOL WeaselTSF::_IsKeyboardDisabled()
{
Expand Down Expand Up @@ -100,4 +197,42 @@ HRESULT WeaselTSF::_SetKeyboardOpen(BOOL fOpen)
}

return hr;
}
}

BOOL WeaselTSF::_InitCompartment()
{
using namespace std::placeholders;

auto callback = std::bind(&WeaselTSF::_HandleCompartment, this, _1);
_pKeyboardCompartmentSink = new CCompartmentEventSink(callback);
if (!_pKeyboardCompartmentSink)
return FALSE;
DWORD hr = _pKeyboardCompartmentSink->_Advise(
_pThreadMgr,
GUID_COMPARTMENT_KEYBOARD_OPENCLOSE
);
return SUCCEEDED(hr);
}

void WeaselTSF::_UninitCompartment()
{
if (_pKeyboardCompartmentSink) {
_pKeyboardCompartmentSink->_Unadvise();
_pKeyboardCompartmentSink->Release();
_pKeyboardCompartmentSink = NULL;
}

}

HRESULT WeaselTSF::_HandleCompartment(REFGUID guidCompartment)
{
if (IsEqualGUID(guidCompartment, GUID_COMPARTMENT_KEYBOARD_OPENCLOSE))
{
BOOL isOpen = _IsKeyboardOpen();
if (isOpen) {
m_client.TrayCommand(ID_WEASELTRAY_DISABLE_ASCII);
}
_EnableLanguageBar(isOpen);
}
return S_OK;
}
32 changes: 32 additions & 0 deletions WeaselTSF/Compartment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include <ComPtr.h>
#include <functional>
#include <msctf.h>

class CCompartmentEventSink : public ITfCompartmentEventSink
{
public:
using Callback = std::function<HRESULT(REFGUID guidCompartment)>;
CCompartmentEventSink(Callback callback)
: _callback(callback), _refCount(1) {};
~CCompartmentEventSink() = default;

// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, _Outptr_ void **ppvObj);
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP_(ULONG) Release(void);

// ITfCompartmentEventSink
STDMETHODIMP OnChange(_In_ REFGUID guid);

// function
HRESULT _Advise(_In_ IUnknown *punk, _In_ REFGUID guidCompartment);
HRESULT _Unadvise();

private:
ITfCompartment * _compartment;
DWORD _cookie;
Callback _callback;

LONG _refCount;
};
11 changes: 5 additions & 6 deletions WeaselTSF/EditSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
{
// get commit string from server
std::wstring commit;
weasel::Status status;
weasel::Config config;
auto context = std::make_shared<weasel::Context>();
weasel::ResponseParser parser(&commit, context.get(), &status, &config, &_cand->style());
weasel::ResponseParser parser(&commit, context.get(), &_status, &config, &_cand->style());

bool ok = m_client.GetResponseData(std::ref(parser));

_UpdateUI(*context, status);
_UpdateLanguageBar(status);
_UpdateUI(*context, _status);
_UpdateLanguageBar(_status);

if (ok)
{
Expand All @@ -29,11 +28,11 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
_InsertText(_pEditSessionContext, commit);
_EndComposition(_pEditSessionContext, false);
}
if (status.composing && !_IsComposing())
if (_status.composing && !_IsComposing())
{
_StartComposition(_pEditSessionContext, _fCUASWorkaroundEnabled && !config.inline_preedit);
}
else if (!status.composing && _IsComposing())
else if (!_status.composing && _IsComposing())
{
_EndComposition(_pEditSessionContext, true);
}
Expand Down
3 changes: 3 additions & 0 deletions WeaselTSF/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ const GUID GUID_TFCAT_TIPCAP_SYSTRAYSUPPORT =

const GUID GUID_LBI_INPUTMODE =
{ 0x2C77A81E, 0x41CC, 0x4178, { 0xA3, 0xA7, 0x5F, 0x8A, 0x98, 0x75, 0x68, 0xE6 } };

const GUID GUID_IME_MODE_PRESERVED_KEY =
{ 0x0bd899fc, 0xa8f7, 0x4b42, { 0xa9, 0x6d, 0xce, 0xc7, 0xc5, 0x0e, 0x0e, 0xae } };
1 change: 1 addition & 0 deletions WeaselTSF/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ extern const GUID GUID_TFCAT_TIPCAP_SYSTRAYSUPPORT;
#endif

extern const GUID GUID_LBI_INPUTMODE;
extern const GUID GUID_IME_MODE_PRESERVED_KEY;

19 changes: 19 additions & 0 deletions WeaselTSF/KeyEventSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,25 @@ void WeaselTSF::_UninitKeyEventSink()
BOOL WeaselTSF::_InitPreservedKey()
{
return TRUE;
#if 0
ComPtr<ITfKeystrokeMgr> pKeystrokeMgr;
if (_pThreadMgr->QueryInterface(pKeystrokeMgr.GetAddressOf()) != S_OK)
{
return FALSE;
}
TF_PRESERVEDKEY preservedKeyImeMode;

/* Define SHIFT ONLY for now */
preservedKeyImeMode.uVKey = VK_SHIFT;
preservedKeyImeMode.uModifiers = TF_MOD_ON_KEYUP;

auto hr = pKeystrokeMgr->PreserveKey(
_tfClientId,
GUID_IME_MODE_PRESERVED_KEY,
&preservedKeyImeMode, L"", 0);

return SUCCEEDED(hr);
#endif
}

void WeaselTSF::_UninitPreservedKey()
Expand Down
51 changes: 49 additions & 2 deletions WeaselTSF/LanguageBar.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include <resource.h>
#include "WeaselTSF.h"
#include "Compartment.h"

static const DWORD LANGBARITEMSINK_COOKIE = 0x42424242;

Expand Down Expand Up @@ -60,16 +61,19 @@ class CLangBarItemButton: public ITfLangBarItemButton, public ITfSource
STDMETHODIMP UnadviseSink(DWORD dwCookie);

void UpdateStatus(weasel::Status stat);
void SetStatus(DWORD dwStatus, BOOL fSet);

private:
GUID _guid;
WeaselTSF *_pTextService;
ITfLangBarItemSink *_pLangBarItemSink;
LONG _cRef; /* COM Reference count */
DWORD _status;
bool ascii_mode;
};

CLangBarItemButton::CLangBarItemButton(WeaselTSF *pTextService, REFGUID guid)
: _status(0)
{
DllAddRef();

Expand Down Expand Up @@ -130,13 +134,14 @@ STDAPI CLangBarItemButton::GetInfo(TF_LANGBARITEMINFO *pInfo)

STDAPI CLangBarItemButton::GetStatus(DWORD *pdwStatus)
{
*pdwStatus = 0;
*pdwStatus = _status;
return S_OK;
}

STDAPI CLangBarItemButton::Show(BOOL fShow)
{
return E_NOTIMPL;
SetStatus(TF_LBI_STATUS_HIDDEN, fShow ? FALSE : TRUE);
return S_OK;
}

STDAPI CLangBarItemButton::GetTooltipString(BSTR *pbstrToolTip)
Expand Down Expand Up @@ -233,6 +238,34 @@ void CLangBarItemButton::UpdateStatus(weasel::Status stat)
}
}

void CLangBarItemButton::SetStatus(DWORD dwStatus, BOOL fSet)
{
BOOL isChange = FALSE;

if (fSet)
{
if (!(_status & dwStatus))
{
_status |= dwStatus;
isChange = TRUE;
}
}
else
{
if (_status & dwStatus)
{
_status &= ~dwStatus;
isChange = TRUE;
}
}

if (isChange && _pLangBarItemSink)
{
_pLangBarItemSink->OnUpdate(TF_LBI_STATUS | TF_LBI_ICON);
}

return;
}


void WeaselTSF::_HandleLangBarMenuSelect(UINT wID)
Expand Down Expand Up @@ -288,6 +321,7 @@ BOOL WeaselTSF::_InitLanguageBar()
goto Exit;
}

_pLangBarButton->Show(TRUE);
fRet = TRUE;

Exit:
Expand Down Expand Up @@ -317,3 +351,16 @@ void WeaselTSF::_UpdateLanguageBar(weasel::Status stat)
if (!_pLangBarButton) return;
_pLangBarButton->UpdateStatus(stat);
}

void WeaselTSF::_ShowLanguageBar(BOOL show)
{
if (!_pLangBarButton) return;
_pLangBarButton->Show(show);

}

void WeaselTSF::_EnableLanguageBar(BOOL enable)
{
if (!_pLangBarButton) return;
_pLangBarButton->SetStatus(TF_LBI_STATUS_DISABLED, !enable);
}
Loading

0 comments on commit 4015d18

Please sign in to comment.