Debian Codegraph @

im/pinyin/pinyincandidate.cpp

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: 2024-2024 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8 #include "pinyincandidate.h"
9 #include "../../modules/cloudpinyin/cloudpinyin_public.h"
10 #include "pinyin.h"
11 #include <algorithm>
12 #include <cassert>
13 #include <cstddef>
14 #include <cstdint>
15 #include <ctime>
16 #include <fcitx-utils/event.h>
17 #include <fcitx-utils/eventloopinterface.h>
18 #include <fcitx-utils/i18n.h>
19 #include <fcitx-utils/log.h>
20 #include <fcitx/candidateaction.h>
21 #include <fcitx/candidatelist.h>
22 #include <fcitx/inputcontext.h>
23 #include <fcitx/text.h>
24 #include <fcitx/userinterface.h>
25 #include <format>
26 #include <libime/core/historybigram.h>
27 #include <libime/core/lattice.h>
28 #include <libime/pinyin/pinyincontext.h>
29 #include <libime/pinyin/pinyindecoder.h>
30 #include <libime/pinyin/pinyindictionary.h>
31 #include <libime/pinyin/pinyinencoder.h>
32 #include <libime/pinyin/shuangpinprofile.h>
33 #include <map>
34 #include <optional>
35 #include <span>
36 #include <string>
37 #include <tuple>
38 #include <utility>
39 #include <vector>
40
41 namespace fcitx {
42
43 namespace {
44
45 // Helper function to produce the full pinyin string that matches the best to
46 // the encoded candidate pinyin.
47 std::string bestMatchPinyin(const std::string &pinyin,
48 const std::string &candidatePinyin,
49 libime::PinyinContext &context) {
50
51 libime::MatchedPinyinSyllablesWithFuzzyFlags syls;
52 syls = context.useShuangpin()
53 ? libime::PinyinEncoder::shuangpinToSyllablesWithFuzzyFlags(
54 pinyin, *context.ime()->shuangpinProfile(),
55 context.ime()->fuzzyFlags())
56 : libime::PinyinEncoder::stringToSyllablesWithFuzzyFlags(
57 pinyin, context.ime()->correctionProfile().get(),
58 context.ime()->fuzzyFlags());
59 std::string actualPinyin;
60 std::optional<libime::PinyinSyllable> syl;
61
62 if (!syls.empty() && !syls.front().second.empty() &&
63 candidatePinyin.size() >= 2) {
64 auto candidateInitial =
65 static_cast<libime::PinyinInitial>(candidatePinyin[0]);
66 auto candidateFinal =
67 static_cast<libime::PinyinFinal>(candidatePinyin[1]);
68
69 for (const auto &initial : syls) {
70 for (const auto &[final, fuzzy] : initial.second) {
71 if (candidateInitial == initial.first) {
72 if (candidateFinal == final ||
73 (final == libime::PinyinFinal::Invalid && !syl)) {
74 syl.emplace(initial.first, final);
75 }
76 }
77 }
78 }
79 }
80
81 if (syl) {
82 actualPinyin = libime::PinyinEncoder::initialFinalToPinyinString(
83 syl->initial(), syl->final());
84 } else {
85 actualPinyin = pinyin;
86 }
87 return actualPinyin;
88 }
89
90 bool isSinglePinyin(const libime::PinyinContext &context, size_t idx) {
91 if (idx >= context.candidatesToCursor().size()) {
92 return false;
93 }
94 const auto &result = context.candidatesToCursor()[idx];
95 size_t totalSize = 0;
96 for (const auto &node : result.sentence()) {
97 totalSize +=
98 node->as<libime::PinyinLatticeNode>().encodedPinyin().size();
99 if (totalSize > 2) {
100 return false;
101 }
102 }
103 return totalSize == 2;
104 }
105
106 } // namespace
107
108 PinyinPredictCandidateWord::PinyinPredictCandidateWord(PinyinEngine *engine,
109 std::string word)
110 : CandidateWord(Text(word)), engine_(engine), word_(std::move(word)) {}
111
112 void PinyinPredictCandidateWord::select(InputContext *inputContext) const {
113 inputContext->commitString(word_);
114 auto *state = inputContext->propertyFor(&engine_->factory());
115 if (!state->predictWords_) {
116 state->predictWords_.emplace();
117 }
118 auto &predictWords = *state->predictWords_;
119 predictWords.push_back({word_, ""});
120 // Max history size.
121 constexpr size_t maxHistorySize = 5;
122 if (predictWords.size() > maxHistorySize) {
123 predictWords.erase(predictWords.begin(), predictWords.begin() +
124 predictWords.size() -
125 maxHistorySize);
126 }
127 engine_->updatePredict(inputContext);
128 }
129
130 PinyinPredictDictCandidateWord::PinyinPredictDictCandidateWord(
131 PinyinEngine *engine, std::string word)
132 : CandidateWord(Text(word)), engine_(engine), word_(std::move(word)) {}
133
134 void PinyinPredictDictCandidateWord::select(InputContext *inputContext) const {
135 inputContext->commitString(word_);
136 auto *state = inputContext->propertyFor(&engine_->factory());
137 if (!state->predictWords_) {
138 state->predictWords_.emplace();
139 }
140 // Append to last word, instead of push back.
141 if (!state->predictWords_->empty()) {
142 state->predictWords_->back().first.append(word_);
143 state->predictWords_->back().second.clear();
144 }
145 engine_->updatePredict(inputContext);
146 }
147
148 InsertableAsCustomPhraseInterface::~InsertableAsCustomPhraseInterface() =
149 default;
150
151 PinyinCandidateIndexInterface::~PinyinCandidateIndexInterface() = default;
152
153 PinyinAbstractCandidateWord::PinyinAbstractCandidateWord(size_t selectLength,
154 CandidateOrder order)
155 : selectLength_(selectLength), order_(std::move(order)) {}
156
157 PinyinAbstractCandidateWord::~PinyinAbstractCandidateWord() = default;
158
159 StrokeCandidateWord::StrokeCandidateWord(PinyinEngine *engine, std::string hz,
160 const std::string &py,
161 size_t selectLength,
162 CandidateOrder order)
163 : PinyinAbstractCandidateWord(selectLength, order), engine_(engine),
164 hz_(std::move(hz)) {
165 setText(Text(hz_));
166 if (!py.empty()) {
167 setComment(Text(std::format("({})", py)));
168 }
169 }
170
171 void StrokeCandidateWord::select(InputContext *inputContext) const {
172 inputContext->commitString(hz_);
173 engine_->doReset(inputContext);
174 }
175
176 CustomPhraseCandidateWord::CustomPhraseCandidateWord(
177 PinyinEngine *engine, size_t selectLength, CandidateOrder order,
178 std::string value, std::string customPhraseString)
179 : PinyinAbstractCandidateWord(selectLength, order), engine_(engine),
180 customPhraseString_(std::move(customPhraseString)) {
181 setText(Text(std::move(value)));
182 }
183
184 void CustomPhraseCandidateWord::select(InputContext *inputContext) const {
185 auto *state = inputContext->propertyFor(&engine_->factory());
186 auto &context = state->context_;
187 context.selectCustom(selectLength_, text().toString());
188 engine_->updateUI(inputContext);
189 }
190
191 PinyinPunctuationCandidateWord::PinyinPunctuationCandidateWord(
192 const PinyinEngine *engine, std::string word, bool isHalf)
193 : engine_(engine), word_(std::move(word)) {
194 setText(Text(word_));
195 if (isHalf) {
196 setComment(Text(_("(Half)")));
197 }
198 }
199
200 void PinyinPunctuationCandidateWord::select(InputContext *inputContext) const {
201 inputContext->commitString(word_);
202 engine_->doReset(inputContext);
203 }
204
205 ForgetCandidateWord::ForgetCandidateWord(PinyinEngine *engine, Text text,
206 size_t index)
207 : engine_(engine), index_(index) {
208 setText(std::move(text));
209 }
210
211 void ForgetCandidateWord::select(InputContext *inputContext) const {
212 engine_->forgetCandidate(inputContext, index_);
213 }
214
215 LuaCandidateWord::LuaCandidateWord(PinyinEngine *engine, size_t selectLength,
216 std::string word)
217 : engine_(engine), selectLength_(selectLength), word_(std::move(word)) {
218 setText(Text(word_));
219 }
220
221 void LuaCandidateWord::select(InputContext *inputContext) const {
222 auto *state = inputContext->propertyFor(&engine_->factory());
223 auto segmentLength =
224 state->context_.size() - state->context_.selectedLength();
225 segmentLength = std::min(segmentLength, selectLength_);
226 state->context_.selectCustom(segmentLength, word_);
227 engine_->updateUI(inputContext);
228 }
229
230 SymbolCandidateWord::SymbolCandidateWord(PinyinEngine *engine,
231 std::string symbol,
232 std::string encodedPinyin,
233 size_t selectLength, bool isFull,
234 int pinyinCandidateIndex)
235 : engine_(engine), symbol_(std::move(symbol)),
236 candidateSegmentLength_(selectLength), isFull_(isFull),
237 encodedPinyin_(std::move(encodedPinyin)),
238 pinyinCandidateIndex_(pinyinCandidateIndex) {
239 setText(Text(symbol_));
240 }
241
242 std::string SymbolCandidateWord::customPhraseString() const {
243 if (isFull_) {
244 return symbol_;
245 }
246 return "";
247 }
248
249 void SymbolCandidateWord::select(InputContext *inputContext) const {
250 auto *state = inputContext->propertyFor(&engine_->factory());
251 auto segmentLength =
252 state->context_.size() - state->context_.selectedLength();
253 segmentLength = std::min(segmentLength, candidateSegmentLength_);
254 state->context_.selectCustom(segmentLength, symbol_, encodedPinyin_);
255 engine_->updateUI(inputContext);
256 }
257
258 SpellCandidateWord::SpellCandidateWord(PinyinEngine *engine, std::string word,
259 size_t inputLength, CandidateOrder order)
260 : PinyinAbstractCandidateWord(inputLength, order), engine_(engine),
261 word_(std::move(word)) {
262 setText(Text(word_));
263 }
264
265 void SpellCandidateWord::select(InputContext *inputContext) const {
266 auto *state = inputContext->propertyFor(&engine_->factory());
267 auto &context = state->context_;
268 context.selectCustom(selectLength_, word_);
269 engine_->updateUI(inputContext);
270 }
271
272 PinyinCandidateWord::PinyinCandidateWord(PinyinEngine *engine,
273 InputContext *inputContext, Text text,
274 size_t selectLength, size_t idx,
275 CandidateOrder order)
276 : PinyinAbstractCandidateWord(selectLength, order), engine_(engine),
277 inputContext_(inputContext), idx_(idx) {
278 setText(std::move(text));
279 }
280
281 void PinyinCandidateWord::select(InputContext *inputContext) const {
282 if (inputContext != inputContext_) {
283 return;
284 }
285 auto *state = inputContext->propertyFor(&engine_->factory());
286 auto &context = state->context_;
287 if (idx_ >= context.candidatesToCursor().size()) {
288 return;
289 }
290 context.selectCandidatesToCursor(idx_);
291 engine_->updateUI(inputContext);
292 }
293
294 std::string PinyinCandidateWord::customPhraseString() const {
295 auto *state = inputContext_->propertyFor(&engine_->factory());
296 auto &context = state->context_;
297 if (idx_ >= context.candidatesToCursor().size()) {
298 return "";
299 }
300 const auto candidatePyLength =
301 context.candidatesToCursor()[idx_].sentence().back()->to()->index();
302 const auto selectedLength = state->context_.selectedLength();
303 const auto currentSearch = (state->context_.cursor() == selectedLength)
304 ? state->context_.size()
305 : state->context_.cursor();
306 if (currentSearch == candidatePyLength + selectedLength) {
307 return text().toString();
308 }
309 return "";
310 }
311
312 void PinyinCandidateWord::setPinyinInComment() {
313 auto *state = inputContext_->propertyFor(&engine_->factory());
314 auto &context = state->context_;
315 if (idx_ >= context.candidatesToCursor().size()) {
316 return;
317 }
318 const auto &result = context.candidatesToCursor()[idx_];
319 auto fullPinyin = context.candidateFullPinyin(result);
320 if (!fullPinyin.empty()) {
321 setComment(Text(std::format("({})", fullPinyin)));
322 }
323 }
324
325 CustomCloudPinyinCandidateWord::CustomCloudPinyinCandidateWord(
326 PinyinEngine *engine, const std::string &pinyin,
327 const std::string &selectedSentence, InputContext *inputContext,
328 CloudPinyinSelectedCallback callback, CandidateOrder order)
329 : CloudPinyinCandidateWord(engine->cloudpinyin(), pinyin, selectedSentence,
330 *engine->config().keepCloudPinyinPlaceHolder,
331 inputContext, std::move(callback)),
332 PinyinAbstractCandidateWord(pinyin.size(), order) {
333 if (filled() || !*engine->config().cloudPinyinAnimation) {
334 return;
335 }
336 setText(Text(std::string(ProgerssString[tick_])));
337 // This should be high accuracy since it's per 120ms.
338 timeEvent_ = engine->instance()->eventLoop().addTimeEvent(
339 CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + TickPeriod, 1000,
340 [this, ref = this->watch()](EventSourceTime *, uint64_t time) {
341 if (!ref.isValid()) {
342 return true;
343 }
344 if (filled()) {
345 timeEvent_.reset();
346 return true;
347 }
348 tick_ = (time / TickPeriod) % ProgerssString.size();
349 setText(Text(std::string(ProgerssString[tick_])));
350 this->inputContext()->updateUserInterface(
351 UserInterfaceComponent::InputPanel);
352 timeEvent_->setTime(timeEvent_->time() + TickPeriod);
353 timeEvent_->setOneShot();
354 return true;
355 });
356 }
357
358 void CustomCloudPinyinCandidateWord::select(InputContext *inputContext) const {
359 if ((!filled() || word().empty()) && order() == 0) {
360 auto candidateList = inputContext->inputPanel().candidateList();
361 for (int i = 0; i < candidateList->size(); i++) {
362 if (&candidateList->candidate(i) != this) {
363 candidateList->candidate(i).select(inputContext);
364 return;
365 }
366 }
367 }
368 CloudPinyinCandidateWord::select(inputContext);
369 }
370
371 enum { PINYIN_FORGET, PINYIN_CUSTOMPHRASE, PINYIN_DELETE_CUSTOMPHRASE };
372
373 PinyinActionableCandidateList::PinyinActionableCandidateList(
374 PinyinEngine *engine, InputContext *inputContext)
375 : engine_(engine), inputContext_(inputContext) {}
376
377 bool PinyinActionableCandidateList::hasAction(
378 const CandidateWord &candidate) const {
379 return isPinyinCandidate(candidate) ||
380 canBeInsertedAsCustomPhrase(candidate);
381 }
382
383 std::vector<CandidateAction> PinyinActionableCandidateList::candidateActions(
384 const CandidateWord &candidate) const {
385 std::vector<CandidateAction> result;
386 if (isPinyinCandidate(candidate)) {
387 CandidateAction action;
388 action.setId(PINYIN_FORGET);
389 action.setText(_("Forget candidate"));
390 result.push_back(std::move(action));
391 }
392 // If it's not custom phrase, or order is not at the top.
393 const auto *customPhrase =
394 dynamic_cast<const PinyinAbstractCandidateWord *>(&candidate);
395 if (canBeInsertedAsCustomPhrase(candidate) &&
396 (!customPhrase || !customPhrase->isCustomPhrase() ||
397 customPhrase->order() != 0)) {
398 CandidateAction action;
399 action.setId(PINYIN_CUSTOMPHRASE);
400 action.setText(_("Pin to top as custom phrase"));
401 result.push_back(std::move(action));
402 }
403 if (isCustomPhrase(candidate)) {
404 CandidateAction action;
405 action.setId(PINYIN_DELETE_CUSTOMPHRASE);
406 action.setText(_("Delete from custom phrase"));
407 result.push_back(std::move(action));
408 }
409
410 return result;
411 }
412
413 void PinyinActionableCandidateList::triggerAction(
414 const CandidateWord &candidate, int id) {
415 switch (id) {
416 case PINYIN_FORGET: {
417 if (const auto *pinyinCandidate =
418 dynamic_cast<const PinyinCandidateWord *>(&candidate)) {
419 engine_->forgetCandidate(inputContext_,
420 pinyinCandidate->candidateIndex());
421 }
422 } break;
423 case PINYIN_CUSTOMPHRASE: {
424 if (const auto *insertable =
425 dynamic_cast<const InsertableAsCustomPhraseInterface *>(
426 &candidate)) {
427 auto customPhraseString = insertable->customPhraseString();
428 if (!customPhraseString.empty()) {
429 engine_->pinCustomPhrase(inputContext_, customPhraseString);
430 }
431 }
432 } break;
433 case PINYIN_DELETE_CUSTOMPHRASE: {
434 if (const auto *customPhrase =
435 dynamic_cast<const PinyinAbstractCandidateWord *>(&candidate);
436 customPhrase && customPhrase->isCustomPhrase()) {
437 if (const auto *insertable =
438 dynamic_cast<const InsertableAsCustomPhraseInterface *>(
439 &candidate)) {
440 auto customPhraseString = insertable->customPhraseString();
441 if (!customPhraseString.empty()) {
442 engine_->deleteCustomPhrase(inputContext_,
443 customPhraseString);
444 }
445 }
446 }
447 } break;
448 default:
449 break;
450 }
451 }
452
453 PinyinTabbedCandidateList::PinyinTabbedCandidateList(
454 PinyinEngine *engine, InputContext *inputContext,
455 CommonCandidateList *candidateList)
456 : engine_(engine), inputContext_(inputContext),
457 candidateList_(candidateList) {}
458
459 std::span<const CandidateAction> PinyinTabbedCandidateList::tabActions() {
460 auto *state = inputContext_->propertyFor(&engine_->factory());
461 if (state->mode_ == PinyinMode::StrokeFilter) {
462 return strokeActions_;
463 }
464 if (!actions_) {
465 buildTabActions();
466 }
467 return *actions_;
468 }
469
470 void PinyinTabbedCandidateList::buildTabActions() {
471 if (actions_) {
472 return;
473 }
474 std::vector<CandidateAction> actions;
475 actionIdToCandidates_.clear();
476
477 auto *state = inputContext_->propertyFor(&engine_->factory());
478 auto &context = state->context_;
479 auto selectedLen = context.selectedLength();
480 const auto &fullInput = context.userInput();
481
482 std::map<std::tuple<std::string, std::string>, int> syllableToId;
483
484 if (!candidateList_) {
485 actions_ = std::move(actions);
486 return;
487 }
488
489 for (size_t i = 0; i < candidateList_->originSize(); i++) {
490 const auto *candidate = &candidateList_->originCandidate(i);
491 const auto *pinyinCandidate =
492 dynamic_cast<const PinyinCandidateWord *>(candidate);
493 if (!pinyinCandidate || !pinyinCandidate->isPinyinCandidate()) {
494 continue;
495 }
496 if (static_cast<size_t>(pinyinCandidate->candidateIndex()) >=
497 context.candidatesToCursor().size()) {
498 continue;
499 }
500 const auto &result =
501 context.candidatesToCursor()[pinyinCandidate->candidateIndex()];
502 const auto &sentence = result.sentence();
503 if (sentence.empty()) {
504 continue;
505 }
506 const auto &path = sentence[0]->path();
507 if (path.size() < 2) {
508 continue;
509 }
510 auto start = selectedLen + path[0]->index();
511 auto end = selectedLen + path[1]->index();
512
513 auto syllable = fullInput.substr(start, end - start);
514 auto actualPinyin = bestMatchPinyin(
515 syllable,
516 sentence[0]->as<libime::PinyinLatticeNode>().encodedPinyin(),
517 context);
518
519 auto [it, inserted] = syllableToId.emplace(
520 std::tuple{syllable, actualPinyin}, actions.size());
521 if (inserted) {
522 CandidateAction action;
523 action.setId(actions.size());
524 action.setText(actualPinyin);
525 action.setCheckable(true);
526 action.setChecked(checkedPinyinActionId_ == action.id());
527 actions.push_back(std::move(action));
528 actionIdToCandidates_.emplace_back();
529 }
530 actionIdToCandidates_[it->second].insert(
531 pinyinCandidate->candidateIndex());
532 }
533
534 CandidateAction separator;
535 separator.setId(SEPARATOR_ACTION);
536 separator.setSeparator(true);
537 actions.push_back(separator);
538
539 CandidateAction action;
540 action.setId(SINGLE_ACTION);
541 action.setText("单字");
542 action.setCheckable(true);
543 action.setChecked(checkedSingleAction_);
544 actions.push_back(std::move(action));
545
546 actions.push_back(std::move(separator));
547
548 CandidateAction stroke;
549 stroke.setId(STROKE_ACTION);
550 stroke.setText("笔画");
551 actions.push_back(std::move(stroke));
552
553 actions_ = std::move(actions);
554
555 strokeActions_.clear();
556 strokeActions_.emplace_back();
557 strokeActions_.back().setId(STROKE_SUB_ACTION_H);
558 strokeActions_.back().setText("一");
559 strokeActions_.emplace_back();
560 strokeActions_.back().setId(STROKE_SUB_ACTION_S);
561 strokeActions_.back().setText("丨");
562 strokeActions_.emplace_back();
563 strokeActions_.back().setId(STROKE_SUB_ACTION_P);
564 strokeActions_.back().setText("ノ");
565 strokeActions_.emplace_back();
566 strokeActions_.back().setId(STROKE_SUB_ACTION_N);
567 strokeActions_.back().setText("㇏");
568 strokeActions_.emplace_back();
569 strokeActions_.back().setId(STROKE_SUB_ACTION_Z);
570 strokeActions_.back().setText("𠃍");
571 strokeActions_.emplace_back();
572 strokeActions_.back().setId(SEPARATOR_ACTION);
573 strokeActions_.back().setSeparator(true);
574 strokeActions_.emplace_back();
575 strokeActions_.back().setId(STROKE_SUB_ACTION_RETURN);
576 strokeActions_.back().setText("返回");
577 }
578
579 void PinyinTabbedCandidateList::triggerTabAction(int id) {
580
581 auto currentCandidateList = inputContext_->inputPanel().candidateList();
582 if (!currentCandidateList || currentCandidateList.get() != candidateList_) {
583 return;
584 }
585
586 auto *state = inputContext_->propertyFor(&engine_->factory());
587 if (state->mode_ == PinyinMode::StrokeFilter) {
588 triggerStrokeAction(state, id);
589 } else {
590 triggerMainAction(state, id);
591 }
592 }
593
594 void PinyinTabbedCandidateList::triggerStrokeAction(PinyinState *state,
595 int id) {
596 assert(state->mode_ == PinyinMode::StrokeFilter);
597
598 switch (id) {
599 case STROKE_SUB_ACTION_H:
600 case STROKE_SUB_ACTION_S:
601 case STROKE_SUB_ACTION_P:
602 case STROKE_SUB_ACTION_N:
603 case STROKE_SUB_ACTION_Z:
604 state->strokeBuffer_.type(STROKE_SUB_ACTION_H - id + '1');
605 break;
606 case STROKE_SUB_ACTION_RETURN:
607 engine_->resetStroke(inputContext_);
608 break;
609 default:
610 return;
611 }
612 engine_->updateFilter(inputContext_);
613 }
614
615 std::optional<int> PinyinTabbedCandidateList::idToActionIndex(int id) const {
616 assert(actions_.has_value() && actions_->size() >= 2);
617 if (id < 0) {
618 if (id == SINGLE_ACTION) {
619 return actionIdToCandidates_.size() + 1;
620 }
621 } else if (id >= 0 && id < static_cast<int>(actionIdToCandidates_.size())) {
622 return id;
623 }
624 return std::nullopt;
625 }
626
627 void PinyinTabbedCandidateList::triggerMainAction(PinyinState *state, int id) {
628 if (!actions_) {
629 return;
630 }
631 std::optional<int> checkableActionIndex;
632 // negative id is special action.
633 if (id == STROKE_ACTION) {
634 state->mode_ = PinyinMode::StrokeFilter;
635 } else if (checkableActionIndex = idToActionIndex(id);
636 !checkableActionIndex) {
637 return;
638 }
639
640 // If triggered action is checkable, update the action checked state
641 // correspondingly.
642 if (checkableActionIndex) {
643 if (id == SINGLE_ACTION) {
644 checkedSingleAction_ = !checkedSingleAction_;
645 (*actions_)[*checkableActionIndex].setChecked(checkedSingleAction_);
646 } else {
647 if (checkedPinyinActionId_) {
648 if (auto oldIndex = idToActionIndex(*checkedPinyinActionId_)) {
649 (*actions_)[*oldIndex].setChecked(false);
650 }
651 }
652
653 if (checkedPinyinActionId_ == id) {
654 // If the same action is triggered, uncheck it.
655 checkedPinyinActionId_.reset();
656 } else {
657 checkedPinyinActionId_ = id;
658 (*actions_)[*checkableActionIndex].setChecked(true);
659 }
660 }
661 }
662 engine_->updateFilter(inputContext_);
663 }
664
665 bool PinyinTabbedCandidateList::filter(const CandidateWord &candidate) const {
666 if (!checked()) {
667 return true;
668 }
669
670 const auto *pinyinCandidate =
671 dynamic_cast<const PinyinCandidateIndexInterface *>(&candidate);
672 if (!pinyinCandidate) {
673 return false;
674 }
675
676 if (checkedSingleAction_) {
677 auto *state = inputContext_->propertyFor(&engine_->factory());
678 auto &context = state->context_;
679 if (!isSinglePinyin(context, pinyinCandidate->candidateIndex())) {
680 return false;
681 }
682 }
683
684 if (checkedPinyinActionId_) {
685 const auto id = checkedPinyinActionId_.value();
686 if (id < 0 || id >= static_cast<int>(actionIdToCandidates_.size())) {
687 return false;
688 }
689 const auto &candidateSet = actionIdToCandidates_[id];
690 if (!candidateSet.contains(pinyinCandidate->candidateIndex())) {
691 return false;
692 }
693 }
694
695 return true;
696 }
697
698 } // namespace fcitx