Skip to content

Commit

Permalink
Set cursor option for pinyin mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Nov 8, 2020
1 parent 2411713 commit 8cf55ff
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions im/table/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,62 +348,68 @@ bool TableState::handlePinyinMode(KeyEvent &event) {
reset();
}
}
if (needUpdate) {
auto &inputPanel = ic_->inputPanel();
ic_->inputPanel().reset();

if (!pinyinModeBuffer_.empty()) {
const auto &dict = engine_->pinyinDict();
const auto &lm = engine_->pinyinModel();
auto pinyin = libime::PinyinEncoder::encodeOneUserPinyin(
pinyinModeBuffer_.userInput());
if (!needUpdate) {
return false;
}
auto &inputPanel = ic_->inputPanel();
ic_->inputPanel().reset();

auto candidateList = std::make_unique<CommonCandidateList>();
candidateList->setSelectionKey(*context_->config().selection);
candidateList->setPageSize(*context_->config().pageSize);
std::vector<std::pair<std::string, float>> pinyinWords;

dict.matchWords(pinyin.data(), pinyin.size(),
[&pinyinWords, &lm](std::string_view,
std::string_view hanzi, float) {
pinyinWords.emplace_back(
hanzi, lm.singleWordScore(hanzi));
return true;
});

std::sort(pinyinWords.begin(), pinyinWords.end(),
[](const auto &lhs, const auto &rhs) {
return lhs.second > rhs.second;
});

for (auto &p : pinyinWords) {
candidateList->append<TablePinyinCandidateWord>(
engine_, std::move(p.first), context_->dict(),
*context_->config().displayCustomHint);
}
if (!pinyinModeBuffer_.empty()) {
const auto &dict = engine_->pinyinDict();
const auto &lm = engine_->pinyinModel();
const auto &config = context_->config();
auto pinyin = libime::PinyinEncoder::encodeOneUserPinyin(
pinyinModeBuffer_.userInput());

auto candidateList = std::make_unique<CommonCandidateList>();
candidateList->setLayoutHint(*config.candidateLayoutHint);
candidateList->setCursorPositionAfterPaging(
CursorPositionAfterPaging::ResetToFirst);
candidateList->setSelectionKey(*config.selection);
candidateList->setPageSize(*config.pageSize);

std::vector<std::pair<std::string, float>> pinyinWords;

dict.matchWords(pinyin.data(), pinyin.size(),
[&pinyinWords, &lm](std::string_view,
std::string_view hanzi, float) {
pinyinWords.emplace_back(hanzi,
lm.singleWordScore(hanzi));
return true;
});

std::sort(pinyinWords.begin(), pinyinWords.end(),
[](const auto &lhs, const auto &rhs) {
return lhs.second > rhs.second;
});

for (auto &p : pinyinWords) {
candidateList->append<TablePinyinCandidateWord>(
engine_, std::move(p.first), context_->dict(),
*config.displayCustomHint);
}

if (candidateList->size()) {
candidateList->setGlobalCursorIndex(0);
inputPanel.setCandidateList(std::move(candidateList));
}
} else {
if (!lastSegment_.empty()) {
inputPanel.setAuxDown(Text(lastSegment_));
}
if (candidateList->size()) {
candidateList->setGlobalCursorIndex(0);
inputPanel.setCandidateList(std::move(candidateList));
}
Text preeditText;
preeditText.append(pinyinModePrefix_);
preeditText.append(pinyinModeBuffer_.userInput());
if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) {
preeditText.setCursor(0);
inputPanel.setClientPreedit(preeditText);
} else {
preeditText.setCursor(preeditText.textLength());
inputPanel.setPreedit(preeditText);
} else {
if (!lastSegment_.empty()) {
inputPanel.setAuxDown(Text(lastSegment_));
}
ic_->updatePreedit();
ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
}
Text preeditText;
preeditText.append(pinyinModePrefix_);
preeditText.append(pinyinModeBuffer_.userInput());
if (ic_->capabilityFlags().test(CapabilityFlag::Preedit)) {
preeditText.setCursor(0);
inputPanel.setClientPreedit(preeditText);
} else {
preeditText.setCursor(preeditText.textLength());
inputPanel.setPreedit(preeditText);
}
ic_->updatePreedit();
ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
return true;
}

Expand Down Expand Up @@ -977,6 +983,8 @@ void TableState::updateUI() {
candidateList->setLayoutHint(*config.candidateLayoutHint);
candidateList->setCursorPositionAfterPaging(
CursorPositionAfterPaging::ResetToFirst);
candidateList->setSelectionKey(*config.selection);
candidateList->setPageSize(*config.pageSize);

for (const auto &candidate : candidates) {
auto candidateString = candidate.toString();
Expand All @@ -995,8 +1003,6 @@ void TableState::updateUI() {
std::move(text), idx);
idx++;
}
candidateList->setSelectionKey(*config.selection);
candidateList->setPageSize(*config.pageSize);
if (candidateList->size()) {
candidateList->setGlobalCursorIndex(0);
}
Expand Down

0 comments on commit 8cf55ff

Please sign in to comment.