Skip to content

Commit

Permalink
refactor: hover, scroll, page with new librime API
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Feb 23, 2024
1 parent 9168147 commit c36d753
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 36 deletions.
24 changes: 24 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,30 @@ void RimeWithWeaselHandler::SelectCandidateOnCurrentPage(size_t index, UINT sess
api->select_candidate_on_current_page(session_id, index);
}

bool RimeWithWeaselHandler::HighlightCandidateOnCurrentPage(size_t index, UINT session_id, EatLine eat)
{
DLOG(INFO) << "highlight candidate on current page, session_id = " << session_id << ", index = " << index;
RimeApi* api = rime_get_api();
if(!api)
return false;
bool res = api->highlight_candidate_on_current_page(session_id, index);
_Respond(session_id, eat);
_UpdateUI(session_id);
return res;
}

bool RimeWithWeaselHandler::ChangePage(bool backward, UINT session_id, EatLine eat)
{
DLOG(INFO) << "change page, session_id = " << session_id << (backward? "backward" : "foreward");
RimeApi* api = rime_get_api();
if(!api)
return false;
bool res = api->change_page(session_id, backward);
_Respond(session_id, eat);
_UpdateUI(session_id);
return res;
}

void RimeWithWeaselHandler::FocusIn(DWORD client_caps, UINT session_id)
{
DLOG(INFO) << "Focus in: session_id = " << session_id << ", client_caps = " << client_caps;
Expand Down
26 changes: 26 additions & 0 deletions WeaselIPC/WeaselClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ bool ClientImpl::SelectCandidateOnCurrentPage(size_t index)
return ret != 0;
}

bool ClientImpl::HighlightCandidateOnCurrentPage(size_t index)
{
if(!_Active())
return false;
LRESULT ret = _SendMessage(WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE, index, session_id);
return ret != 0;
}

bool ClientImpl::ChangePage(bool backward)
{
if(!_Active())
return false;
LRESULT ret = _SendMessage(WEASEL_IPC_CHANGE_PAGE, backward, session_id);
return ret != 0;
}

void ClientImpl::UpdateInputPosition(RECT const& rc)
{
if (!_Active())
Expand Down Expand Up @@ -257,6 +273,16 @@ bool Client::SelectCandidateOnCurrentPage(size_t index)
return m_pImpl->SelectCandidateOnCurrentPage(index);
}

bool Client::HighlightCandidateOnCurrentPage(size_t index)
{
return m_pImpl->HighlightCandidateOnCurrentPage(index);
}

bool Client::ChangePage(bool backward)
{
return m_pImpl->ChangePage(backward);
}

void Client::UpdateInputPosition(RECT const& rc)
{
m_pImpl->UpdateInputPosition(rc);
Expand Down
2 changes: 2 additions & 0 deletions WeaselIPC/WeaselClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace weasel
bool CommitComposition();
bool ClearComposition();
bool SelectCandidateOnCurrentPage(size_t index);
bool HighlightCandidateOnCurrentPage(size_t index);
bool ChangePage(bool backward);
void UpdateInputPosition(RECT const& rc);
void FocusIn();
void FocusOut();
Expand Down
28 changes: 28 additions & 0 deletions WeaselIPCServer/WeaselServerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,32 @@ DWORD ServerImpl::OnSelectCandidateOnCurrentPage(WEASEL_IPC_COMMAND uMsg, DWORD
return 0;
}

DWORD ServerImpl::OnHighlightCandidateOnCurrentPage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if(m_pRequestHandler)
{
auto eat = [this](std::wstring &msg) -> bool {
*channel << msg;
return true;
};
m_pRequestHandler->HighlightCandidateOnCurrentPage(wParam, lParam, eat);
}
return 0;
}

DWORD ServerImpl::OnChangePage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
if(m_pRequestHandler)
{
auto eat = [this](std::wstring &msg) -> bool {
*channel << msg;
return true;
};
m_pRequestHandler->ChangePage(wParam, lParam, eat);
}
return 0;
}

#define MAP_PIPE_MSG_HANDLE(__msg, __wParam, __lParam) {\
auto lParam = __lParam;\
auto wParam = __wParam;\
Expand Down Expand Up @@ -351,6 +377,8 @@ void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp)
PIPE_MSG_HANDLE(WEASEL_IPC_COMMIT_COMPOSITION, OnCommitComposition)
PIPE_MSG_HANDLE(WEASEL_IPC_CLEAR_COMPOSITION, OnClearComposition);
PIPE_MSG_HANDLE(WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE, OnSelectCandidateOnCurrentPage);
PIPE_MSG_HANDLE(WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE, OnHighlightCandidateOnCurrentPage);
PIPE_MSG_HANDLE(WEASEL_IPC_CHANGE_PAGE, OnChangePage);
PIPE_MSG_HANDLE(WEASEL_IPC_TRAY_COMMAND, OnCommand);
END_MAP_PIPE_MSG_HANDLE(result);

Expand Down
2 changes: 2 additions & 0 deletions WeaselIPCServer/WeaselServerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ namespace weasel
DWORD OnCommitComposition(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnClearComposition(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnSelectCandidateOnCurrentPage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnHighlightCandidateOnCurrentPage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnChangePage(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);

public:
ServerImpl();
Expand Down
60 changes: 25 additions & 35 deletions WeaselTSF/CandidateList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,39 +422,41 @@ void WeaselTSF::_SelectCandidateOnCurrentPage(size_t index)
{
m_client.SelectCandidateOnCurrentPage(index);
// simulate a VK_SELECT presskey to get data back and DoEditSession
// the simulated keycode must be the one make TranslateKeycode Non-Zero return
// fix me: are there any better ways?
INPUT inputs[2];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki = {VK_SELECT, 0,0,0,0};
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki = {VK_SELECT, 0,KEYEVENTF_KEYUP,0,0};
::SendInput(sizeof(inputs) / sizeof(INPUT), inputs, sizeof(INPUT));
// the simulated keycode must be the one make TranslateKeycode Non-Zero return
// fix me: are there any better ways?
INPUT inputs[2];
inputs[0].type = INPUT_KEYBOARD;
inputs[0].ki = {VK_SELECT, 0,0,0,0};
inputs[1].type = INPUT_KEYBOARD;
inputs[1].ki = {VK_SELECT, 0,KEYEVENTF_KEYUP,0,0};
::SendInput(sizeof(inputs) / sizeof(INPUT), inputs, sizeof(INPUT));
}

void WeaselTSF::_HandleMousePageEvent( bool* const nextPage, bool* const scrollNextPage)
{
// ToDo: if feature new api comes, replace the processes bellow
weasel::KeyEvent ke{ 0, 0 };
// from scrolling event
if ( scrollNextPage ) {
if(_cand->style().paging_on_scroll)
ke.keycode = *scrollNextPage ? ibus::Page_Down : ibus::Page_Up;
else
{
ke.keycode = *scrollNextPage ? ibus::Down : ibus::Up;
if(_cand->GetIsReposition())
{
if(ke.keycode == ibus::Up)
ke.keycode = ibus::Down;
else if(ke.keycode == ibus::Down)
ke.keycode = ibus::Up;
m_client.ChangePage(!(*scrollNextPage));
else {
UINT current_select = 0, cand_count = 0;
_cand->GetSelection(&current_select);
_cand->GetCount(&cand_count);
bool is_reposition = _cand->GetIsReposition();
int offset = *scrollNextPage ? 1 : -1;
offset = offset * (is_reposition ? -1 : 1);
int index = (int)current_select + offset;
if (index >= 0 && index < cand_count)
m_client.HighlightCandidateOnCurrentPage((size_t)index);
else {
KeyEvent ke{0, 0};
ke.keycode = (index < 0) ? ibus::Up : ibus::Down;
m_client.ProcessKeyEvent(ke);
}
}
} else { // from click event
ke.keycode = *nextPage ? ibus::Page_Down : ibus::Page_Up;
m_client.ChangePage(!(*nextPage));
}
m_client.ProcessKeyEvent(ke);
_UpdateComposition(_pEditSessionContext);
}

Expand All @@ -465,19 +467,7 @@ void WeaselTSF::_HandleMouseHoverEvent(const size_t index)

if(index != current_select)
{
// simuulate change current_select in librime
// maybe replace with new api in the future
weasel::KeyEvent ke{ 0, 0 };
ke.keycode = current_select < index ? ibus::Down : ibus::Up;
int inc = index > current_select ? 1 : -1;
if (_cand->GetIsReposition()) inc = -inc;
UINT gap = abs((INT)((INT)index - (INT)current_select));
for(UINT i=0; i < gap; i++)
{
m_client.ProcessKeyEvent(ke);
}
// simuulate change current_select end
_cand->SetSelection(current_select + inc * gap);
m_client.HighlightCandidateOnCurrentPage(index);
_UpdateComposition(_pEditSessionContext);
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/RimeWithWeasel.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class RimeWithWeaselHandler :
virtual void CommitComposition(UINT session_id);
virtual void ClearComposition(UINT session_id);
virtual void SelectCandidateOnCurrentPage(size_t index, UINT session_id);
virtual bool HighlightCandidateOnCurrentPage(size_t index, UINT session_id, EatLine eat);
virtual bool ChangePage(bool backward, UINT session_id, EatLine eat);
virtual void FocusIn(DWORD param, UINT session_id);
virtual void FocusOut(DWORD param, UINT session_id);
virtual void UpdateInputPosition(RECT const& rc, UINT session_id);
Expand Down
10 changes: 9 additions & 1 deletion include/WeaselIPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ enum WEASEL_IPC_COMMAND
WEASEL_IPC_END_MAINTENANCE,
WEASEL_IPC_COMMIT_COMPOSITION,
WEASEL_IPC_CLEAR_COMPOSITION,
WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE,
WEASEL_IPC_TRAY_COMMAND,
WEASEL_IPC_SELECT_CANDIDATE_ON_CURRENT_PAGE,
WEASEL_IPC_HIGHLIGHT_CANDIDATE_ON_CURRENT_PAGE,
WEASEL_IPC_CHANGE_PAGE,
WEASEL_IPC_LAST_COMMAND
};

Expand Down Expand Up @@ -78,6 +80,8 @@ namespace weasel
virtual void CommitComposition(UINT session_id) {}
virtual void ClearComposition(UINT session_id) {}
virtual void SelectCandidateOnCurrentPage(size_t index, UINT session_id) {}
virtual bool HighlightCandidateOnCurrentPage(size_t index, UINT session_id, EatLine eat) {return false;}
virtual bool ChangePage(bool backward, UINT session_id, EatLine eat) {return false;}
virtual void FocusIn(DWORD param, UINT session_id) {}
virtual void FocusOut(DWORD param, UINT session_id) {}
virtual void UpdateInputPosition(RECT const& rc, UINT session_id) {}
Expand Down Expand Up @@ -135,6 +139,10 @@ namespace weasel
bool ClearComposition();
// 选择当前页面编号为index的候选
bool SelectCandidateOnCurrentPage(size_t index);
// 高亮当前页面编号为index的候选
bool HighlightCandidateOnCurrentPage(size_t index);
// 高亮当前页面编号为index的候选
bool ChangePage(bool backward);
// 更新输入位置
void UpdateInputPosition(RECT const& rc);
// 输入窗口获得焦点
Expand Down

0 comments on commit c36d753

Please sign in to comment.