From c380242f40c2b97ea96b42098e202ca4407d9639 Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 11 Sep 2024 14:41:22 +0800 Subject: [PATCH 1/3] feat: GUID_COMPARTMENT_KEYBOARD_OPENCLOSE actions configurable. - WeaselTSF default toggle ascii_mode on keyboard open/close messages(esp., Ctrl+Space), if `HKEY_CURRENT_USER\SOFTWARE\Rime\Weasel\ToggleImeOnOpenClose` is not set `yes` - WeaselSetup parameters, `/toggleime` to enable toggle ime on keyboard open/close message, `/toggleascii` to toggle ascii_mode when those messages come. --- WeaselSetup/WeaselSetup.cpp | 8 ++++++++ WeaselTSF/Compartment.cpp | 25 +++++++++++++++++++------ WeaselTSF/KeyEventSink.cpp | 5 ++++- WeaselTSF/LanguageBar.h | 1 + WeaselTSF/WeaselTSF.cpp | 4 ++++ WeaselTSF/WeaselTSF.h | 1 + 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/WeaselSetup/WeaselSetup.cpp b/WeaselSetup/WeaselSetup.cpp index bef216a7c..259af11f7 100644 --- a/WeaselSetup/WeaselSetup.cpp +++ b/WeaselSetup/WeaselSetup.cpp @@ -161,6 +161,14 @@ static int Run(LPTSTR lpCmdLine) { L"CheckForUpdates", L"0", REG_SZ); } + if (!wcscmp(L"/toggleime", lpCmdLine)) { + return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel", + L"ToggleImeOnOpenClose", L"yes", REG_SZ); + } + if (!wcscmp(L"/toggleascii", lpCmdLine)) { + return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel", + L"ToggleImeOnOpenClose", L"no", REG_SZ); + } if (!wcscmp(L"/testing", lpCmdLine)) { return SetRegKeyValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel", L"UpdateChannel", L"testing", REG_SZ); diff --git a/WeaselTSF/Compartment.cpp b/WeaselTSF/Compartment.cpp index a3b6a25a2..beb5225b4 100644 --- a/WeaselTSF/Compartment.cpp +++ b/WeaselTSF/Compartment.cpp @@ -5,6 +5,7 @@ #include #include "ResponseParser.h" #include "CandidateList.h" +#include "LanguageBar.h" STDAPI CCompartmentEventSink::QueryInterface(REFIID riid, _Outptr_ void** ppvObj) { @@ -242,13 +243,25 @@ void WeaselTSF::_UninitCompartment() { HRESULT WeaselTSF::_HandleCompartment(REFGUID guidCompartment) { if (IsEqualGUID(guidCompartment, GUID_COMPARTMENT_KEYBOARD_OPENCLOSE)) { - BOOL isOpen = _IsKeyboardOpen(); - // clear composition when close keyboard - if (!isOpen && _pEditSessionContext) { - m_client.ClearComposition(); - _EndComposition(_pEditSessionContext, true); + if (_isToOpenClose) { + BOOL isOpen = _IsKeyboardOpen(); + // clear composition when close keyboard + if (!isOpen && _pEditSessionContext) { + m_client.ClearComposition(); + _EndComposition(_pEditSessionContext, true); + } + _EnableLanguageBar(isOpen); + _UpdateLanguageBar(_status); + } else { + _status.ascii_mode = !_status.ascii_mode; + _SetKeyboardOpen(true); + if (_pLangBarButton && _pLangBarButton->IsLangBarDisabled()) + _EnableLanguageBar(true); + _HandleLangBarMenuSelect(_status.ascii_mode + ? ID_WEASELTRAY_ENABLE_ASCII + : ID_WEASELTRAY_DISABLE_ASCII); + _UpdateLanguageBar(_status); } - _EnableLanguageBar(isOpen); } else if (IsEqualGUID(guidCompartment, GUID_COMPARTMENT_KEYBOARD_INPUTMODE_CONVERSION)) { BOOL isOpen = _IsKeyboardOpen(); diff --git a/WeaselTSF/KeyEventSink.cpp b/WeaselTSF/KeyEventSink.cpp index 55dde4f98..a2b565381 100644 --- a/WeaselTSF/KeyEventSink.cpp +++ b/WeaselTSF/KeyEventSink.cpp @@ -9,7 +9,10 @@ static BOOL prevfEaten = FALSE; static int keyCountToSimulate = 0; void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) { - if (!_IsKeyboardOpen() || _IsKeyboardDisabled()) { + if (_isToOpenClose && (!_IsKeyboardOpen() || _IsKeyboardDisabled())) { + *pfEaten = FALSE; + return; + } else if (!_isToOpenClose && _IsKeyboardDisabled()) { *pfEaten = FALSE; return; } diff --git a/WeaselTSF/LanguageBar.h b/WeaselTSF/LanguageBar.h index 667eab3fd..2c0513ab3 100644 --- a/WeaselTSF/LanguageBar.h +++ b/WeaselTSF/LanguageBar.h @@ -30,6 +30,7 @@ class CLangBarItemButton : public ITfLangBarItemButton, public ITfSource { /* ITfSource */ STDMETHODIMP AdviseSink(REFIID riid, IUnknown* punk, DWORD* pdwCookie); STDMETHODIMP UnadviseSink(DWORD dwCookie); + BOOL IsLangBarDisabled() { return (_status & TF_LBI_STATUS_DISABLED); } void UpdateWeaselStatus(weasel::Status stat); void SetLangbarStatus(DWORD dwStatus, BOOL fSet); diff --git a/WeaselTSF/WeaselTSF.cpp b/WeaselTSF/WeaselTSF.cpp index 0564ebdc1..236e3daf8 100644 --- a/WeaselTSF/WeaselTSF.cpp +++ b/WeaselTSF/WeaselTSF.cpp @@ -171,6 +171,10 @@ STDAPI WeaselTSF::ActivateEx(ITfThreadMgr* pThreadMgr, } STDMETHODIMP WeaselTSF::OnSetThreadFocus() { + std::wstring _ToggleImeOnOpenClose{}; + RegGetStringValue(HKEY_CURRENT_USER, L"Software\\Rime\\weasel", + L"ToggleImeOnOpenClose", _ToggleImeOnOpenClose); + _isToOpenClose = (_ToggleImeOnOpenClose == L"yes"); if (m_client.Echo()) { m_client.ProcessKeyEvent(0); weasel::ResponseParser parser(NULL, NULL, &_status, NULL, &_cand->style()); diff --git a/WeaselTSF/WeaselTSF.h b/WeaselTSF/WeaselTSF.h index 12ae30d17..696d3cce4 100644 --- a/WeaselTSF/WeaselTSF.h +++ b/WeaselTSF/WeaselTSF.h @@ -232,4 +232,5 @@ class WeaselTSF : public ITfTextInputProcessorEx, TfGuidAtom _gaDisplayAttributeInput; BOOL _async_edit = false; BOOL _committed = false; + BOOL _isToOpenClose = false; }; From 6b97bffdd4cc7c3c09d73cdd2efe18322003ca8e Mon Sep 17 00:00:00 2001 From: fxliang Date: Wed, 11 Sep 2024 21:49:10 +0800 Subject: [PATCH 2/3] simplify the condiction --- WeaselTSF/KeyEventSink.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/WeaselTSF/KeyEventSink.cpp b/WeaselTSF/KeyEventSink.cpp index a2b565381..e0d431410 100644 --- a/WeaselTSF/KeyEventSink.cpp +++ b/WeaselTSF/KeyEventSink.cpp @@ -9,10 +9,9 @@ static BOOL prevfEaten = FALSE; static int keyCountToSimulate = 0; void WeaselTSF::_ProcessKeyEvent(WPARAM wParam, LPARAM lParam, BOOL* pfEaten) { - if (_isToOpenClose && (!_IsKeyboardOpen() || _IsKeyboardDisabled())) { - *pfEaten = FALSE; - return; - } else if (!_isToOpenClose && _IsKeyboardDisabled()) { + // when _IsKeyboardDisabled don't eat the key, + // when keyboard closable and keyboard closed, don't eat the key + if ((_isToOpenClose && !_IsKeyboardOpen()) || _IsKeyboardDisabled()) { *pfEaten = FALSE; return; } From e99df45f124363d59695799ffb0a5a08dfb53872 Mon Sep 17 00:00:00 2001 From: fxliang Date: Thu, 12 Sep 2024 22:48:39 +0800 Subject: [PATCH 3/3] clear composition when toggle ascii_mode open/close --- WeaselTSF/Compartment.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/WeaselTSF/Compartment.cpp b/WeaselTSF/Compartment.cpp index beb5225b4..a45e0d1f9 100644 --- a/WeaselTSF/Compartment.cpp +++ b/WeaselTSF/Compartment.cpp @@ -260,6 +260,8 @@ HRESULT WeaselTSF::_HandleCompartment(REFGUID guidCompartment) { _HandleLangBarMenuSelect(_status.ascii_mode ? ID_WEASELTRAY_ENABLE_ASCII : ID_WEASELTRAY_DISABLE_ASCII); + if (_pEditSessionContext) + m_client.ClearComposition(); _UpdateLanguageBar(_status); } } else if (IsEqualGUID(guidCompartment,