Skip to content

Commit

Permalink
refactor: drop HANT configuration for WeaselTSF, register both SUBLAN…
Browse files Browse the repository at this point in the history
…G_CHINESE_TRADITIONAL and SUBLANG_CHINESE_SIMPLIFIED in the same DLL.
  • Loading branch information
fxliang committed Apr 11, 2024
1 parent 9f9ec5d commit a153f6d
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 57 deletions.
15 changes: 14 additions & 1 deletion WeaselSetup/imesetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ int install_ime_file(std::wstring& srcPath,
WCHAR path[MAX_PATH];
GetModuleFileNameW(GetModuleHandle(NULL), path, _countof(path));

std::wstring srcFileName = (hant ? L"weaselt" : L"weasel");
std::wstring srcFileName = L"weasel";
if (ext == L"ime")
srcFileName = (hant ? L"weaselt" : L"weasel");

srcFileName += ext;
WCHAR drive[_MAX_DRIVE];
WCHAR dir[_MAX_DIR];
Expand Down Expand Up @@ -492,6 +495,16 @@ int register_text_service(const std::wstring& tsf_path,
// if (silent) // always silent
{ params = L" /s " + params; }

if (hant) {
if (!SetEnvironmentVariable(L"TEXTSERVICE_PROFILE", L"hant")) {
// bad luck
}
} else {
if (!SetEnvironmentVariable(L"TEXTSERVICE_PROFILE", L"hans")) {
// bad luck
}
}

std::wstring app = L"regsvr32.exe";
if (is_wowarm32) {
WCHAR sysarm32[MAX_PATH];
Expand Down
6 changes: 2 additions & 4 deletions WeaselTSF/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
#include "stdafx.h"
#include <WeaselCommon.h>

#ifdef WEASEL_HANT
#define TEXTSERVICE_LANGID MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
#else
#define TEXTSERVICE_LANGID_HANT \
MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
#define TEXTSERVICE_LANGID MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
#endif

#define TEXTSERVICE_DESC WEASEL_IME_NAME
#define TEXTSERVICE_DESC_A "Weasel"
Expand Down
34 changes: 28 additions & 6 deletions WeaselTSF/LanguageBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,29 @@ STDAPI CLangBarItemButton::Show(BOOL fShow) {
return S_OK;
}

static LANGID GetActiveProfileLangId() {
CComPtr<ITfInputProcessorProfileMgr> pInputProcessorProfileMgr;
HRESULT hr = pInputProcessorProfileMgr.CoCreateInstance(
CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_ALL);
if (FAILED(hr))
return MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);

TF_INPUTPROCESSORPROFILE profile;
hr = pInputProcessorProfileMgr->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,
&profile);
if (FAILED(hr))
return MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED);
return profile.langid;
}

STDAPI CLangBarItemButton::GetTooltipString(BSTR* pbstrToolTip) {
#ifdef WEASEL_HANT
*pbstrToolTip = SysAllocString(L"左鍵切換模式,右鍵打開菜單");
#else
*pbstrToolTip = SysAllocString(L"左键切换模式,右键打开菜单");
#endif
LANGID langid = GetActiveProfileLangId();
if (langid == TEXTSERVICE_LANGID) {
*pbstrToolTip = SysAllocString(L"左键切换模式,右键打开菜单");
} else {
*pbstrToolTip = SysAllocString(L"左鍵切換模式,右鍵打開菜單");
}

return (*pbstrToolTip == NULL) ? E_OUTOFMEMORY : S_OK;
}

Expand All @@ -126,7 +143,12 @@ STDAPI CLangBarItemButton::OnClick(TfLBIClick click,
/* Open menu */
HWND hwnd = _pTextService->_GetFocusedContextWindow();
if (hwnd != NULL) {
HMENU menu = LoadMenuW(g_hInst, MAKEINTRESOURCE(IDR_MENU_POPUP));
LANGID langid = GetActiveProfileLangId();

HMENU menu =
((langid == TEXTSERVICE_LANGID)
? LoadMenuW(g_hInst, MAKEINTRESOURCE(IDR_MENU_POPUP))
: LoadMenuW(g_hInst, MAKEINTRESOURCE(IDR_MENU_POPUP_HANT)));
HMENU popupMenu = GetSubMenu(menu, 0);
UINT wID = TrackPopupMenuEx(
popupMenu, TPM_NONOTIFY | TPM_RETURNCMD | TPM_HORPOSANIMATION, pt.x,
Expand Down
56 changes: 22 additions & 34 deletions WeaselTSF/Register.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,6 @@ static const char c_szTipKeyPrefix[] = "Software\\Microsft\\CTF\\TIP\\";
static const char c_szInProcSvr32[] = "InprocServer32";
static const char c_szModelName[] = "ThreadingModel";

HKL FindIME() {
HKL hKL = NULL;
WCHAR key[9];
HKEY hKey;
LSTATUS ret =
RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts", 0,
KEY_READ, &hKey);
if (ret == ERROR_SUCCESS) {
for (DWORD id = (0xE0200000 | TEXTSERVICE_LANGID);
hKL == NULL && id <= (0xE0FF0000 | TEXTSERVICE_LANGID);
id += 0x10000) {
StringCchPrintfW(key, _countof(key), L"%08X", id);
HKEY hSubKey;
ret = RegOpenKeyExW(hKey, key, 0, KEY_READ, &hSubKey);
if (ret == ERROR_SUCCESS) {
WCHAR data[32];
DWORD type;
DWORD size = sizeof data;
ret = RegQueryValueExW(hSubKey, L"Ime File", NULL, &type, (LPBYTE)data,
&size);
if (ret == ERROR_SUCCESS && type == REG_SZ &&
_wcsicmp(data, L"weasel.ime") == 0)
hKL = (HKL)id;
}
RegCloseKey(hSubKey);
}
}
RegCloseKey(hKey);
return hKL;
}

BOOL RegisterProfiles() {
WCHAR achIconFile[MAX_PATH];
ULONG cchIconFile =
Expand All @@ -53,11 +21,29 @@ BOOL RegisterProfiles() {
CLSID_TF_InputProcessorProfiles, NULL, CLSCTX_ALL);
if (FAILED(hr))
return FALSE;

WCHAR szProfile[100];
DWORD dwSize = GetEnvironmentVariable(L"TEXTSERVICE_PROFILE", szProfile,
ARRAYSIZE(szProfile));
BOOL hansEnable = FALSE;
BOOL hantEnable = FALSE;

if (dwSize > 0 && wcscmp(szProfile, L"hans") == 0)
hansEnable = TRUE;
hr = pInputProcessorProfileMgr->RegisterProfile(
c_clsidTextService, TEXTSERVICE_LANGID, c_guidProfile, TEXTSERVICE_DESC,
(ULONG)wcslen(TEXTSERVICE_DESC), achIconFile, cchIconFile,
TEXTSERVICE_ICON_INDEX, FindIME(), 0, TRUE, 0);
TEXTSERVICE_ICON_INDEX, LoadKeyboardLayout(L"00000804", 0), 0,
hansEnable, 0);
if (FAILED(hr))
return FALSE;

if (dwSize > 0 && wcscmp(szProfile, L"hant") == 0)
hantEnable = TRUE;
hr = pInputProcessorProfileMgr->RegisterProfile(
c_clsidTextService, TEXTSERVICE_LANGID_HANT, c_guidProfile,
TEXTSERVICE_DESC, (ULONG)wcslen(TEXTSERVICE_DESC), achIconFile,
cchIconFile, TEXTSERVICE_ICON_INDEX, LoadKeyboardLayout(L"00000404", 0),
0, hantEnable, 0);
if (FAILED(hr))
return FALSE;
}
Expand All @@ -77,6 +63,8 @@ void UnregisterProfiles() {

hr = pInputProcessorProfileMgr->UnregisterProfile(
c_clsidTextService, TEXTSERVICE_LANGID, c_guidProfile, 0);
hr = pInputProcessorProfileMgr->UnregisterProfile(
c_clsidTextService, TEXTSERVICE_LANGID_HANT, c_guidProfile, 0);
}
}

Expand Down
Binary file modified WeaselTSF/WeaselTSF.rc
Binary file not shown.
Binary file modified include/resource.h
Binary file not shown.
8 changes: 0 additions & 8 deletions output/install.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,6 @@ program_files:
File /nonfatal "weaselARM64.dll"
File /nonfatal "weaselARM64X.dll"
${EndIf}
File "weaselt.dll"
${If} ${RunningX64}
File "weaseltx64.dll"
${EndIf}
${If} ${IsNativeARM64}
File /nonfatal "weaseltARM.dll"
File /nonfatal "weaseltARM64.dll"
${EndIf}
File "weasel.ime"
${If} ${RunningX64}
File "weaselx64.ime"
Expand Down
4 changes: 0 additions & 4 deletions weasel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ Global
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.Release|x64.ActiveCfg = Release|x64
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.Release|x64.Build.0 = Release|x64
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|ARM.ActiveCfg = ReleaseHant|ARM
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|ARM.Build.0 = ReleaseHant|ARM
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|ARM64.ActiveCfg = ReleaseHant|ARM64
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|ARM64.Build.0 = ReleaseHant|ARM64
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|Win32.ActiveCfg = ReleaseHant|Win32
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|Win32.Build.0 = ReleaseHant|Win32
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|x64.ActiveCfg = ReleaseHant|x64
{FF9B3625-BBD6-4972-8F23-7BA57F3127E8}.ReleaseHant|x64.Build.0 = ReleaseHant|x64
{10B3B8BF-7294-4661-9A8A-2FFC920FA2F4}.Debug|ARM.ActiveCfg = Debug|ARM
{10B3B8BF-7294-4661-9A8A-2FFC920FA2F4}.Debug|ARM.Build.0 = Debug|ARM
{10B3B8BF-7294-4661-9A8A-2FFC920FA2F4}.Debug|ARM64.ActiveCfg = Debug|ARM64
Expand Down

0 comments on commit a153f6d

Please sign in to comment.