Skip to content

Commit

Permalink
1、移除IME模块。
Browse files Browse the repository at this point in the history
2、重修AutoCAD无焦点输入丢首码问题。
3、优化获取坐标的逻辑。
  • Loading branch information
Techince committed Jan 9, 2024
1 parent ceff32c commit 914f44a
Show file tree
Hide file tree
Showing 21 changed files with 277 additions and 169 deletions.
2 changes: 1 addition & 1 deletion WeaselSetup/imesetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ int install(bool hant, bool silent)
{
std::wstring ime_src_path;
int retval = 0;
retval += install_ime_file(ime_src_path, L".ime", hant, silent, &register_ime);
// retval += install_ime_file(ime_src_path, L".ime", hant, silent, &register_ime);
retval += install_ime_file(ime_src_path, L".dll", hant, silent, &register_text_service);

// 写注册表
Expand Down
24 changes: 21 additions & 3 deletions WeaselTSF/CandidateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module;
#include "stdafx.h"
#include <WeaselUI.h>
#include <WeaselCommon.h>
#include "Globals.h"
#include "test.h"
#ifdef TEST
#define WEASEL_ENABLE_LOGGING
Expand Down Expand Up @@ -114,14 +115,33 @@ STDMETHODIMP CCandidateList::GetGUID(GUID* pguid)

STDMETHODIMP CCandidateList::Show(BOOL showCandidateWindow)
{
com_ptr<ITfCompartmentMgr> pCompartmentMgr;
if (_pContextDocument)
{
if (SUCCEEDED(_pContextDocument->QueryInterface(&pCompartmentMgr)))
{
// Update a hidden compartment to generate
// IMN_OPENCANDIDATE/IMN_CLOSECANDIDATE notifications for the
// application compatibility.
com_ptr<ITfCompartment> pCompartment;
if (SUCCEEDED(pCompartmentMgr->GetCompartment(WEASEL_CUAS_CANDIDATE_MESSAGE_COMPARTMENT, &pCompartment)))
{
VARIANT var{};
var.vt = VT_I4;
var.lVal = showCandidateWindow ? TRUE : FALSE;
pCompartment->SetValue(_tsf.GetClientId(), &var);
}
}
}

if (showCandidateWindow)
{
_ui.Show();
}
else
{
_ui.Hide();
}
}
#ifdef TEST
LOG(INFO) << std::format("From CCandidateList::Show. show = {}", showCandidateWindow);
#endif // TEST
Expand Down Expand Up @@ -370,7 +390,6 @@ void CCandidateList::UpdateUI(const Context& ctx, const Status& status)
// _flags = TF_RIUIE_CONTEXT | TF_RIUIE_STRING | TF_RIUIE_MAXREADINGSTRINGLENGTH | TF_RIUIE_VERTICALORDER;
// _flags = TF_RIUIE_STRING | TF_RIUIE_VERTICALORDER;
_UpdateUIElement();
PostMessage(_GetActiveWnd(), WM_IME_NOTIFY, IMN_CHANGECANDIDATE, 0x1);
}

#ifdef TEST
Expand Down Expand Up @@ -492,7 +511,6 @@ void CCandidateList::StartUI()
return;
}
auto ret = pUIElementMgr->EndUIElement(uiid);
PostMessage(_GetActiveWnd(), WM_IME_STARTCOMPOSITION, 0, 0);
#ifdef TEST
LOG(INFO) << std::format("From CCandidateList::StartUI. ret = 0x{:X}", _pbShow, (unsigned)ret);
#endif // TEST
Expand Down
61 changes: 35 additions & 26 deletions WeaselTSF/Composition.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// #include <immdev.h>
#include <WeaselCommon.h>
#include "cmath"
#include <UIAutomation.h>
#include "test.h"
#ifdef TEST
#define WEASEL_ENABLE_LOGGING
Expand Down Expand Up @@ -175,7 +174,7 @@ STDAPI CStartCompositionEditSession::DoEditSession(TfEditCookie ec)
STDAPI CEndCompositionEditSession::DoEditSession(TfEditCookie ec)
{
/* Clear the dummy text we set before, if any. */
if (_pComposition == nullptr) return S_OK;
if (_pComposition == nullptr) return S_OK;

if (_pTextService->GetBit(WeaselFlag::SUPPORT_DISPLAY_ATTRIBUTE))
{
Expand All @@ -185,6 +184,10 @@ STDAPI CEndCompositionEditSession::DoEditSession(TfEditCookie ec)
com_ptr<ITfRange> pCompositionRange;
if (_clear && _pComposition->GetRange(&pCompositionRange) == S_OK)
{
/*if (_pTextService->GetBit(WeaselFlag::INLINE_PREEDIT))
{
ClearReadingProperties(_pContext, pCompositionRange, ec);
}*/
pCompositionRange->SetText(ec, 0, L"", 0);
}

Expand All @@ -208,27 +211,33 @@ STDAPI CGetTextExtentEditSession::DoEditSession(TfEditCookie ec)
// composition end
// note: selection.range is always an empty range
hr = _pContext->GetEnd(ec, &pRange);
}
}

HWND hwnd;
_pContextView->GetWnd(&hwnd);
_pTextService->SetHWND(hwnd);
_pContextView->GetScreenExt(&rcExt);
hr = _pContextView->GetTextExt(ec, pRange.p, &rc, &fClipped);

/*auto himc = ImmGetContext(hwnd);
COMPOSITIONFORM candidateFrom{};
bool ret = ImmRequestMessage(himc, IMR_COMPOSITIONWINDOW, (LPARAM)&candidateFrom);
POINT pt = candidateFrom.ptCurrentPos;
RECT rc1 = candidateFrom.rcArea;*/

#ifdef TEST
std::wstring name{};
name.reserve(512);
GetClassName(hwnd, name.data(), name.capacity());
name = name.data();

_pContextView->GetScreenExt(&rcExt);
hr = _pContextView->GetTextExt(ec, pRange, &rc, &fClipped);

#ifdef TEST
LOG(INFO) << std::format("From CGetTextExtentEditSession::DoEditSession. rc.left = {}, rc.top = {}, hr = {:#x}, fClipped = {:s}, className = {}, rcExt = ({}, {}, {}, {})",
rc.left, rc.top, (unsigned)hr, (bool)fClipped, to_string(name, CP_UTF8), rcExt.left, rcExt.top, rcExt.right, rcExt.bottom);
#endif // TEST
if (hr == 0x80040057)
if ((hr == 0x80040057) ||
(hr == 0x80070057 && _pTextService->GetBit(WeaselFlag::AUTOCAD)))
{
_pTextService->OnCompositionTerminated(ec, _pComposition);
_pTextService->_AbortComposition(true);
return hr;
}

Expand Down Expand Up @@ -262,8 +271,8 @@ void CGetTextExtentEditSession::CalculatePosition(const RECT& rcExt, RECT& rc)
}
else
{
rc.right = rc.left = rcExt.left + (rcExt.right - rcExt.left) / 3;
rc.bottom = rc.top = rcExt.top + (rcExt.bottom - rcExt.top) / 3 * 2;
rc.left = rc.right = rc.right / 3.0;
rc.top = rc.bottom = rc.bottom / 3.0 * 2;
}

if (_pTextService->GetBit(WeaselFlag::INLINE_PREEDIT)) // 仅嵌入式候选框记录首码坐标
Expand Down Expand Up @@ -300,28 +309,28 @@ STDAPI CInlinePreeditEditSession::DoEditSession(TfEditCookie ec)
#endif // TEST
std::wstring preedit = _context->preedit.str;

com_ptr<ITfRange> pRangeComposition;
com_ptr<ITfRange> pRangeComposition;
if ((_pComposition->GetRange(&pRangeComposition)) != S_OK)
return E_FAIL;

com_ptr<ITfProperty> pProperty;
VARIANT var{};
_pContext->GetProperty(GUID_PROP_COMPOSING, &pProperty);
auto ret = pProperty->GetValue(ec, pRangeComposition, &var);
HRESULT hr;
if (_pTextService->GetBit(WeaselFlag::INLINE_PREEDIT_LOST_FIRST_KEY))
{
hr = pRangeComposition->SetText(ec, 0, L"", 0);
}
else
{
hr = pRangeComposition->SetText(ec, 0, preedit.c_str(), preedit.length());
}

// SetReadingProperties(_pContext, pRangeComposition, preedit, ec);

HRESULT hr = pRangeComposition->SetText(ec, TF_ST_CORRECTION, preedit.c_str(), preedit.length());
#ifdef TEST
LOG(INFO) << std::format("From CInlinePreeditEditSession::DoEditSession. preedit = {}, hr = 0x{:X}, ret = 0x{:X}, isComposition = {}", to_string(preedit), (unsigned)hr, (unsigned)ret, var.lVal);
LOG(INFO) << std::format("From CInlinePreeditEditSession::DoEditSession. preedit = {}, hr = 0x{:X}", to_string(preedit), (unsigned)hr);
#endif // TEST
if (FAILED(hr))
{
// _pTextService->_AbortComposition();
if (_pTextService->GetBit(WeaselFlag::AUTOCAD))
{
_pTextService->ResetBit(WeaselFlag::FOCUS_CHANGED);
_pTextService->SetBit(WeaselFlag::RETRY_COMPOSITION);
}
_pTextService->OnCompositionTerminated(ec, _pComposition);
_pTextService->_AbortComposition(false);
return hr;
}

Expand Down Expand Up @@ -349,7 +358,7 @@ STDAPI CInlinePreeditEditSession::DoEditSession(TfEditCookie ec)
tfSelection.range = pRangeComposition;
tfSelection.style.ase = TF_AE_NONE;
tfSelection.style.fInterimChar = FALSE;
_pContext->SetSelection(ec, 1, &tfSelection);
hr = _pContext->SetSelection(ec, 1, &tfSelection);

return S_OK;
}
Expand Down
66 changes: 45 additions & 21 deletions WeaselTSF/CompositionTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ void WeaselTSF::_StartComposition(ITfContext* pContext, bool not_inline_preedit)
#endif // TEST
com_ptr<CStartCompositionEditSession> pStartCompositionEditSession;
pStartCompositionEditSession.Attach(new CStartCompositionEditSession(this, pContext, not_inline_preedit));
_cand->StartUI();
if (pStartCompositionEditSession != nullptr)
{
HRESULT hr;
auto ret = pContext->RequestEditSession(_tfClientId, pStartCompositionEditSession, TF_ES_ASYNCDONTCARE | TF_ES_READWRITE, &hr);
if (!GetBit(WeaselFlag::DONT_DESTROY_UI))
_cand->StartUI();
else
ResetBit(WeaselFlag::DONT_DESTROY_UI);
SetBit(WeaselFlag::FIRST_KEY_COMPOSITION);
#ifdef TEST
LOG(INFO) << std::format("From _StartComposition. hr = {:#x}, ret = {:#x}", (unsigned)hr, (unsigned)ret);
#endif // TEST

if (SUCCEEDED(ret) && FAILED(hr))
{
m_client.ClearComposition();
Expand All @@ -47,8 +49,13 @@ void WeaselTSF::_EndComposition(ITfContext* pContext, BOOL clear)
{
pContext->RequestEditSession(_tfClientId, pEditSession, TF_ES_ASYNCDONTCARE | TF_ES_READWRITE, &hr);
}
if (GetBit(WeaselFlag::DONT_DESTROY_UI) && SUCCEEDED(hr))
{
return;
}
// after pEditSession, less flicker
_cand->EndUI();
ResetBit(WeaselFlag::DONT_DESTROY_UI);
#ifdef TEST
LOG(INFO) << std::format("From _EndComposition. hr = 0x{:X}", (unsigned)hr);
#endif // TEST
Expand All @@ -57,6 +64,11 @@ void WeaselTSF::_EndComposition(ITfContext* pContext, BOOL clear)
/* Composition Window Handling */
BOOL WeaselTSF::_UpdateCompositionWindow(ITfContext* pContext)
{
if (GetBit(WeaselFlag::NOT_INLINE_PREEDIT_LOST_FIRST_KEY))
{
_AbortComposition(true);
return TRUE;
}
com_ptr<ITfContextView> pContextView;
HRESULT hr;

Expand Down Expand Up @@ -166,15 +178,7 @@ STDAPI WeaselTSF::OnCompositionTerminated(TfEditCookie ecWrite, ITfComposition*

if (!GetBit(WeaselFlag::FOCUS_CHANGED))
{
if (GetBit(WeaselFlag::AUTOCAD)) // 无焦点输入时,AutoCAD会终止首码的合成
{
if (GetBit(WeaselFlag::RETRY_COMPOSITION))
{
ResetBit(WeaselFlag::RETRY_COMPOSITION);
RetryKey();
}
}
else if (GetBit(WeaselFlag::FIREFOX) && GetBit(WeaselFlag::FIRST_KEY_COMPOSITION) && !GetBit(WeaselFlag::PREDICTION)) // 重发意外终止的首码事件
if (GetBit(WeaselFlag::FIREFOX) && GetBit(WeaselFlag::FIRST_KEY_COMPOSITION) && !GetBit(WeaselFlag::PREDICTION)) // 重发意外终止的首码事件
{
SetBit(WeaselFlag::RETRY_COMPOSITION);
RetryKey();
Expand All @@ -186,12 +190,31 @@ STDAPI WeaselTSF::OnCompositionTerminated(TfEditCookie ecWrite, ITfComposition*

void WeaselTSF::_AbortComposition(bool clear)
{
m_client.ClearComposition();
BOOL eaten{};
if (GetBit(WeaselFlag::INLINE_PREEDIT_LOST_FIRST_KEY))
{
ResetBit(WeaselFlag::INLINE_PREEDIT_LOST_FIRST_KEY);
SetBit(WeaselFlag::DONT_DESTROY_UI);
_EndComposition(_pEditSessionContext, false);
if (m_context->preedit.str.size() > 1)
{
_ProcessKeyEvent(VK_BACK, 0x1, &eaten);
}
return;
}

m_client.ClearComposition();
if (_IsComposing()) {
_EndComposition(_pEditSessionContext, clear);
if (GetBit(WeaselFlag::NOT_INLINE_PREEDIT_LOST_FIRST_KEY))
{
ResetBit(WeaselFlag::NOT_INLINE_PREEDIT_LOST_FIRST_KEY);
_ProcessKeyEvent(_lastKey, 0x1, &eaten);
SetBit(WeaselFlag::DONT_DESTROY_UI);
}
_EndComposition(_pEditSessionContext, clear);
return;
}
_cand->Destroy();
_cand->Destroy();
}

void WeaselTSF::_FinalizeComposition()
Expand Down Expand Up @@ -222,15 +245,16 @@ bool WeaselTSF::RetryKey()
return send == 1;
}

//获取程序当前所在显示器的分辨率大小,可以动态的获取程序所在显示器的分辨率
SIZE WeaselTSF::GetScreenResolution() {
// 获取程序当前所在显示器的分辨率大小,可以动态的获取程序所在显示器的分辨率
SIZE WeaselTSF::GetScreenResolution()
{
SIZE size{};
if (!m_hwnd)
return size;

//MONITOR_DEFAULTTONEAREST 返回值是最接近该点的屏幕句柄
//MONITOR_DEFAULTTOPRIMARY 返回值是主屏幕的句柄
//如果其中一个屏幕包含该点,则返回值是该屏幕的HMONITOR句柄。如果没有一个屏幕包含该点,则返回值取决于dwFlags的值
// MONITOR_DEFAULTTONEAREST 返回值是最接近该点的屏幕句柄
// MONITOR_DEFAULTTOPRIMARY 返回值是主屏幕的句柄
// 如果其中一个屏幕包含该点,则返回值是该屏幕的HMONITOR句柄。如果没有一个屏幕包含该点,则返回值取决于dwFlags的值
HMONITOR hMonitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFOEX miex;
miex.cbSize = sizeof(miex);
Expand All @@ -241,8 +265,8 @@ SIZE WeaselTSF::GetScreenResolution() {
dm.dmSize = sizeof(dm);
dm.dmDriverExtra = 0;

//ENUM_CURRENT_SETTINGS 检索显示设备的当前设置
//ENUM_REGISTRY_SETTINGS 检索当前存储在注册表中的显示设备的设置
// ENUM_CURRENT_SETTINGS 检索显示设备的当前设置
// ENUM_REGISTRY_SETTINGS 检索当前存储在注册表中的显示设备的设置
if (!EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm))
return size;

Expand Down
10 changes: 5 additions & 5 deletions WeaselTSF/EditSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
// get commit string from server
std::wstring commit;
weasel::Config config;
auto context = std::make_shared<weasel::Context>();
weasel::ResponseParser parser(&commit, context.get(), &_status, &config, &_cand->style());
// auto context = std::make_shared<weasel::Context>();
weasel::ResponseParser parser(&commit, m_context.get(), &_status, &config, &_cand->style());

bool ok = m_client.GetResponseData(std::ref(parser));
auto state = _UpdateLanguageBar();
Expand Down Expand Up @@ -50,7 +50,7 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
}
else
{
m_preedit = !context->preedit.empty();
m_preedit = !m_context->preedit.empty();
if (!commit.empty())
{
// For auto-selecting, commit and preedit can both exist.
Expand All @@ -72,7 +72,7 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
}
if (_IsComposing() && (config.inline_preedit || GetBit(WeaselFlag::GAME_MODE)))
{
_ShowInlinePreedit(_pEditSessionContext, context);
_ShowInlinePreedit(_pEditSessionContext, m_context);
SetBit(WeaselFlag::INLINE_PREEDIT);
}
if (GetBit(WeaselFlag::CARET_FOLLOWING))
Expand All @@ -85,7 +85,7 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
}
}
}
_UpdateUI(*context, _status);
_UpdateUI(*m_context, _status);

return S_OK;
}
Expand Down
7 changes: 7 additions & 0 deletions WeaselTSF/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ static const GUID WEASEL_CARET_FOLLOWING_PRESERVED_KEY =
// {BDD3F624-2AF4-4E70-8C8F-63A8293FE3CB}
static const GUID WEASEL_DAEMON_PRESERVED_KEY =
{ 0xbdd3f624, 0x2af4, 0x4e70, { 0x8c, 0x8f, 0x63, 0xa8, 0x29, 0x3f, 0xe3, 0xcb } };

// This GUID is used in Windows Vista/7/8 by MS_IME to represents if the
// candidate window is visible or not.
// TODO: Make sure if it is safe to use this GUID.
// {745537CC-8130-4056-920B-018457A4EC06}
static const GUID WEASEL_CUAS_CANDIDATE_MESSAGE_COMPARTMENT =
{ 0x745537cc, 0x8130, 0x4056, { 0x92, 0xb, 0x1, 0x84, 0x57, 0xa4, 0xec, 0x6 } };
3 changes: 2 additions & 1 deletion WeaselTSF/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ extern const GUID WEASEL_COMPARTMENT_PUNCTUATION;
extern const GUID WEASEL_COMPARTMENT_GLOAL_DEAMON;
extern const GUID WEASEL_UILESS_MODE_PRESERVED_KEY;
extern const GUID WEASEL_CARET_FOLLOWING_PRESERVED_KEY;
extern const GUID WEASEL_DAEMON_PRESERVED_KEY;
extern const GUID WEASEL_DAEMON_PRESERVED_KEY;
extern const GUID WEASEL_CUAS_CANDIDATE_MESSAGE_COMPARTMENT;
Loading

0 comments on commit 914f44a

Please sign in to comment.