| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2017-2024 CSSlayer <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "candidate.h" |
| 9 | #include "engine.h" |
| 10 | #include "state.h" |
| 11 | #include <cstddef> |
| 12 | #include <fcitx-utils/i18n.h> |
| 13 | #include <fcitx-utils/utf8.h> |
| 14 | #include <fcitx/candidateaction.h> |
| 15 | #include <fcitx/candidatelist.h> |
| 16 | #include <fcitx/inputcontext.h> |
| 17 | #include <fcitx/text.h> |
| 18 | #include <libime/table/tablebaseddictionary.h> |
| 19 | #include <string> |
| 20 | #include <utility> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace fcitx { |
| 24 | |
| 25 | TableCandidateWord::TableCandidateWord(TableEngine *engine, Text text, |
| 26 | Text comment, bool spaceBeforeHint, |
| 27 | size_t idx) |
| 28 | : CandidateWord(std::move(text)), engine_(engine), idx_(idx) { |
| 29 | setComment(std::move(comment)); |
| 30 | setSpaceBetweenComment(spaceBeforeHint); |
| 31 | } |
| 32 | void TableCandidateWord::select(InputContext *inputContext) const { |
| 33 | auto *state = inputContext->propertyFor(&engine_->factory()); |
| 34 | // nullptr means use the last requested entry. |
| 35 | auto *context = state->updateContext(nullptr); |
| 36 | if (!context || idx_ >= context->candidates().size()) { |
| 37 | return; |
| 38 | } |
| 39 | if (state->mode() == TableMode::ForgetWord) { |
| 40 | state->forgetCandidateWord(idx_); |
| 41 | return; |
| 42 | } |
| 43 | { |
| 44 | const CommitAfterSelectWrapper commitAfterSelectRAII(state); |
| 45 | context->select(idx_); |
| 46 | } |
| 47 | if (context->selected()) { |
| 48 | state->commitBuffer(true); |
| 49 | } |
| 50 | state->updateUI(/*keepOldCursor=*/false, /*maybePredict=*/true); |
| 51 | } |
| 52 | TablePinyinCandidateWord::TablePinyinCandidateWord( |
| 53 | TableEngine *engine, std::string word, |
| 54 | const libime::TableBasedDictionary &dict, bool customHint, |
| 55 | std::string hintSeparator, bool spaceBeforeHint) |
| 56 | : engine_(engine), word_(std::move(word)) { |
| 57 | setText(Text(word_)); |
| 58 | if (utf8::lengthValidated(word_) == 1) { |
| 59 | if (auto code = dict.reverseLookup(word_); !code.empty()) { |
| 60 | Text comment; |
| 61 | comment.append(hintSeparator); |
| 62 | if (customHint) { |
| 63 | comment.append(dict.hint(code)); |
| 64 | } else { |
| 65 | comment.append(std::move(code)); |
| 66 | } |
| 67 | setComment(std::move(comment)); |
| 68 | } |
| 69 | } |
| 70 | setSpaceBetweenComment(spaceBeforeHint); |
| 71 | } |
| 72 | void TablePinyinCandidateWord::select(InputContext *inputContext) const { |
| 73 | auto *state = inputContext->propertyFor(&engine_->factory()); |
| 74 | inputContext->commitString(word_); |
| 75 | state->pushLastCommit("", word_); |
| 76 | state->resetAndPredict(); |
| 77 | } |
| 78 | TablePunctuationCandidateWord::TablePunctuationCandidateWord(TableState *state, |
| 79 | std::string word, |
| 80 | bool isHalf) |
| 81 | : state_(state), word_(std::move(word)) { |
| 82 | setText(Text(word_)); |
| 83 | if (isHalf) { |
| 84 | setComment(Text(_("(Half)"))); |
| 85 | } |
| 86 | } |
| 87 | void TablePunctuationCandidateWord::select(InputContext *inputContext) const { |
| 88 | state_->commitBuffer(true); |
| 89 | inputContext->commitString(word_); |
| 90 | state_->reset(); |
| 91 | } |
| 92 | |
| 93 | TablePredictCandidateWord::TablePredictCandidateWord(TableState *state, |
| 94 | std::string word) |
| 95 | : CandidateWord(Text(word)), state_(state), word_(std::move(word)) {} |
| 96 | void TablePredictCandidateWord::select(InputContext *inputContext) const { |
| 97 | state_->commitBuffer(true); |
| 98 | inputContext->commitString(word_); |
| 99 | state_->pushLastCommit("", word_); |
| 100 | state_->resetAndPredict(); |
| 101 | } |
| 102 | |
| 103 | TableActionableCandidateList::TableActionableCandidateList(TableState *state) |
| 104 | : state_(state) {} |
| 105 | |
| 106 | bool TableActionableCandidateList::hasAction( |
| 107 | const CandidateWord &candidate) const { |
| 108 | return dynamic_cast<const TableCandidateWord *>(&candidate); |
| 109 | } |
| 110 | std::vector<CandidateAction> TableActionableCandidateList::candidateActions( |
| 111 | const CandidateWord &candidate) const { |
| 112 | if (!hasAction(candidate)) { |
| 113 | return {}; |
| 114 | } |
| 115 | std::vector<CandidateAction> actions; |
| 116 | CandidateAction action; |
| 117 | action.setId(0); |
| 118 | action.setText(_("Forget word")); |
| 119 | actions.push_back(std::move(action)); |
| 120 | return actions; |
| 121 | } |
| 122 | |
| 123 | void TableActionableCandidateList::triggerAction(const CandidateWord &candidate, |
| 124 | int id) { |
| 125 | if (id != 0) { |
| 126 | return; |
| 127 | } |
| 128 | if (const auto *tableCandidate = |
| 129 | dynamic_cast<const TableCandidateWord *>(&candidate)) { |
| 130 | state_->forgetCandidateWord(tableCandidate->idx_); |
| 131 | } |
| 132 | } |
| 133 | } // namespace fcitx |