Skip to content

Commit

Permalink
1、自动保存手动拖动候选框的位置,下次不使用「光标跟随」时会读取此设置
Browse files Browse the repository at this point in the history
2、不同进程切换时,状态更新的坐标使用当前cursor坐标
  • Loading branch information
Techince committed Mar 4, 2024
1 parent 413cdf1 commit ffdd951
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 17 deletions.
10 changes: 5 additions & 5 deletions WeaselTSF/CompartmentTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ HRESULT WeaselTSF::_InitGlobalCompartment()
{
if ((var.ulVal & 0xFF00'0000) != 0xFC00'0000)
{
if (!ReadConfiguration())
WriteConfiguration();
if (!ReadConfiguration(ConfigFlag::GLOBAL_COMPARTMENT))
WriteConfiguration(ConfigFlag::GLOBAL_COMPARTMENT);
var.ulVal = m_globalCompartment;
ret = _pGlobalCompartment->SetValue(_tfClientId, &var);
value = static_cast<unsigned char>(m_globalCompartment);
Expand All @@ -238,8 +238,8 @@ HRESULT WeaselTSF::_InitGlobalCompartment()
else
{
var.vt = VT_I4;
if (!ReadConfiguration())
WriteConfiguration();
if (!ReadConfiguration(ConfigFlag::GLOBAL_COMPARTMENT))
WriteConfiguration(ConfigFlag::GLOBAL_COMPARTMENT);
var.ulVal = m_globalCompartment;
ret = _pGlobalCompartment->SetValue(_tfClientId, &var);
value = static_cast<unsigned char>(m_globalCompartment);
Expand Down Expand Up @@ -268,7 +268,7 @@ void WeaselTSF::UpdateGlobalCompartment(bool in)
m_globalCompartment = var.ulVal;

_pGlobalCompartment->SetValue(_tfClientId, &var);
WriteConfiguration();
WriteConfiguration(ConfigFlag::GLOBAL_COMPARTMENT);
}
else if (SUCCEEDED(_pGlobalCompartment->GetValue(&var)))
{
Expand Down
9 changes: 9 additions & 0 deletions WeaselTSF/CompositionTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,13 @@ void WeaselTSF::SimulatingKeyboardEvents(unsigned short code)
inputs[1].ki = { code, 0, KEYEVENTF_KEYUP };

SendInput(inputs.size(), inputs.data(), sizeof(INPUT));
}

void WeaselTSF::SetRect(const RECT& rc)
{
m_rcFallback = rc;
if (!GetBit(WeaselFlag::CARET_FOLLOWING))
{
WriteConfiguration(ConfigFlag::FALLBACK_POSITION);
}
}
37 changes: 29 additions & 8 deletions WeaselTSF/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@ module;
#include "stdafx.h"
module WeaselTSF;

std::wstring subKey{ LR"(Software\Rime\Weasel)" };
std::wstring valueName{ L"GlobalCompartment" };
std::wstring tsfHKEY{ L"TextService" };
const std::wstring subKey{ LR"(Software\Rime\Weasel)" };
const std::wstring valueName{ L"GlobalCompartment" };
const std::wstring tsfHKEY{ L"TextService" };
const std::wstring fallbackPos{ L"FalbackPosition" };

bool WeaselTSF::ReadConfiguration()
bool WeaselTSF::ReadConfiguration(ConfigFlag flag)
{
HKEY hKey;
auto ret = ::RegOpenKeyEx(HKEY_CURRENT_USER, subKey.data(), 0, KEY_READ | KEY_WOW64_64KEY, &hKey);

if (ret != ERROR_SUCCESS)
return false;

DWORD dataSize = sizeof(DWORD);
ret = ::RegQueryValueEx(hKey, valueName.data(), nullptr, nullptr, (LPBYTE)&m_globalCompartment, &dataSize);
DWORD dataSize;
switch (flag)
{
case ConfigFlag::GLOBAL_COMPARTMENT:
dataSize = sizeof(DWORD);
ret = ::RegQueryValueEx(hKey, valueName.data(), nullptr, nullptr, (LPBYTE)&m_globalCompartment, &dataSize);
break;

case ConfigFlag::FALLBACK_POSITION:
dataSize = sizeof(RECT);
ret = ::RegQueryValueEx(hKey, fallbackPos.data(), nullptr, nullptr, (LPBYTE)&m_rcFallback, &dataSize);
break;
}
RegCloseKey(hKey);

if (ret != ERROR_SUCCESS)
Expand All @@ -24,15 +36,24 @@ bool WeaselTSF::ReadConfiguration()
return true;
}

bool WeaselTSF::WriteConfiguration()
bool WeaselTSF::WriteConfiguration(ConfigFlag flag)
{
HKEY hKey;
auto ret = ::RegOpenKeyEx(HKEY_CURRENT_USER, subKey.data(), 0, KEY_WRITE | KEY_WOW64_64KEY, &hKey);

if (ret != ERROR_SUCCESS)
return false;

ret = ::RegSetValueEx(hKey, valueName.data(), 0, REG_DWORD, (LPBYTE)&m_globalCompartment, sizeof(DWORD));
switch (flag)
{
case ConfigFlag::GLOBAL_COMPARTMENT:
ret = ::RegSetValueEx(hKey, valueName.data(), 0, REG_DWORD, (LPBYTE)&m_globalCompartment, sizeof(DWORD));
break;

case ConfigFlag::FALLBACK_POSITION:
ret = ::RegSetValueEx(hKey, fallbackPos.data(), 0, REG_BINARY, (LPBYTE)&m_rcFallback, sizeof(RECT));
break;
}
RegCloseKey(hKey);

if (ret != ERROR_SUCCESS)
Expand Down
4 changes: 4 additions & 0 deletions WeaselTSF/KeyEventSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ STDAPI WeaselTSF::OnPreservedKey(ITfContext* pContext, REFGUID rguid, BOOL* pfEa
{
Flip(WeaselFlag::CARET_FOLLOWING);
_cand->SetCaretFollowing(GetBit(WeaselFlag::CARET_FOLLOWING));
if (!GetBit(WeaselFlag::CARET_FOLLOWING))
{
ReadConfiguration(ConfigFlag::FALLBACK_POSITION);
}
*pfEaten = TRUE;
}
else if (IsEqualGUID(rguid, WEASEL_DAEMON_PRESERVED_KEY))
Expand Down
4 changes: 4 additions & 0 deletions WeaselTSF/LanguageBarTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ void WeaselTSF::_HandleLangBarMenuSelect(UINT wID)
case ID_STYLE_CARET_FOLLOWING:
_bitset.flip(static_cast<int>(WeaselFlag::CARET_FOLLOWING));
_cand->SetCaretFollowing(GetBit(WeaselFlag::CARET_FOLLOWING));
if (!GetBit(WeaselFlag::CARET_FOLLOWING))
{
ReadConfiguration(ConfigFlag::FALLBACK_POSITION);
}
break;

case ID_STYLE_PRESERVED_KEY_SWITCH:
Expand Down
10 changes: 9 additions & 1 deletion WeaselTSF/ThreadFocusSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ STDMETHODIMP WeaselTSF::OnSetThreadFocus()
if (m_client.Echo())
{
m_client.ProcessKeyEvent(0);
_UpdateComposition(_pEditSessionContext);
POINT pt{};
::GetCursorPos(&pt);
RECT rc{ pt.x, pt.y, pt.x, pt.y };
m_client.UpdateInputPosition(rc);
weasel::ResponseParser parser(NULL, NULL, &_status, NULL, &_cand->style());
bool ok = m_client.GetResponseData(std::ref(parser));
if (ok) {
_UpdateLanguageBar();
}
}
#ifdef TEST
buffer = std::format(L"From WeaselTSF::OnSetThreadFocus(). ascii_mode = {:s}\n", _status.ascii_mode);
Expand Down
6 changes: 6 additions & 0 deletions WeaselTSF/WeaselFlag.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ export
PLANTS_VS_ZOMBIES,
WEASEL_TSF_DEBUG
};

enum class ConfigFlag
{
GLOBAL_COMPARTMENT,
FALLBACK_POSITION
};
}
1 change: 1 addition & 0 deletions WeaselTSF/WeaselTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ void WeaselTSF::_InitWeaselData()
SetBit(WeaselFlag::GAME_MODE_SELF_REDRAW);
ResetBit(WeaselFlag::CARET_FOLLOWING);
_cand->SetCaretFollowing(GetBit(WeaselFlag::CARET_FOLLOWING));
ReadConfiguration(ConfigFlag::FALLBACK_POSITION);
}

if (name == L"PlantsVsZombies.exe")
Expand Down
6 changes: 3 additions & 3 deletions WeaselTSF/WeaselTSF.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export

bool execute(std::wstring_view cmd, std::wstring_view args = L"");
bool explore(std::wstring_view path);
void SetRect(const RECT& rc) { m_rcFallback = rc; }
void SetRect(const RECT& rc);
RECT GetRect() const { return m_rcFallback; }
WCHAR GetInput() const { return static_cast<WCHAR>(_keycode); }

Expand All @@ -155,8 +155,8 @@ export
void UpdateGlobalCompartment(bool in = true);
void SetHWND(HWND hwnd) { m_hwnd = hwnd; }

bool ReadConfiguration();
bool WriteConfiguration();
bool ReadConfiguration(ConfigFlag flag);
bool WriteConfiguration(ConfigFlag flag);
bool StoreTextServiceHandle(HKL hkl);

TfClientId GetClientId() const { return _tfClientId; }
Expand Down

0 comments on commit ffdd951

Please sign in to comment.