| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _CLOUDPINYIN_CLOUDPINYIN_PUBLIC_H_ |
| 8 |
#define _CLOUDPINYIN_CLOUDPINYIN_PUBLIC_H_ |
| 9 |
|
| 10 |
#include <chrono> |
| 11 |
#include <fcitx-utils/key.h> |
| 12 |
#include <fcitx-utils/macros.h> |
| 13 |
#include <fcitx-utils/trackableobject.h> |
| 14 |
#include <fcitx/addoninstance.h> |
| 15 |
#include <fcitx/candidatelist.h> |
| 16 |
#include <fcitx/inputcontext.h> |
| 17 |
#include <fcitx/inputpanel.h> |
| 18 |
#include <fcitx/text.h> |
| 19 |
#include <fcitx/userinterface.h> |
| 20 |
#include <functional> |
| 21 |
#include <optional> |
| 22 |
#include <string> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
using CloudPinyinCallback = |
| 26 |
std::function<void(const std::string &pinyin, const std::string &hanzi)>; |
| 27 |
|
| 28 |
using CloudPinyinSelectedCallback = |
| 29 |
std::function<void(fcitx::InputContext *inputContext, |
| 30 |
const std::string &selected, const std::string &word)>; |
| 31 |
|
| 32 |
FCITX_ADDON_DECLARE_FUNCTION(CloudPinyin, request, |
| 33 |
void(const std::string &pinyin, |
| 34 |
CloudPinyinCallback)); |
| 35 |
FCITX_ADDON_DECLARE_FUNCTION(CloudPinyin, toggleKey, const fcitx::KeyList &()); |
| 36 |
FCITX_ADDON_DECLARE_FUNCTION(CloudPinyin, resetError, void()); |
| 37 |
|
| 38 |
class CloudPinyinCandidateWord |
| 39 |
: virtual public fcitx::CandidateWord, |
| 40 |
public fcitx::TrackableObject<CloudPinyinCandidateWord> { |
| 41 |
public: |
| 42 |
CloudPinyinCandidateWord(fcitx::AddonInstance *cloudpinyin_, |
| 43 |
const std::string &pinyin, |
| 44 |
std::string selectedSentence, bool keep, |
| 45 |
fcitx::InputContext *inputContext, |
| 46 |
CloudPinyinSelectedCallback callback) |
| 47 |
: selectedSentence_(std::move(selectedSentence)), |
| 48 |
inputContext_(inputContext), callback_(std::move(callback)), |
| 49 |
keep_(keep) { |
| 50 |
// use cloud unicode char |
| 51 |
setText(fcitx::Text("\xe2\x98\x81")); |
| 52 |
cloudpinyin_->call<fcitx::ICloudPinyin::request>( |
| 53 |
pinyin, [ref = watch()](const std::string &pinyin, |
| 54 |
const std::string &hanzi) { |
| 55 |
FCITX_UNUSED(pinyin); |
| 56 |
auto *self = ref.get(); |
| 57 |
if (self) { |
| 58 |
self->fill(hanzi); |
| 59 |
} |
| 60 |
}); |
| 61 |
constructor_ = false; |
| 62 |
} |
| 63 |
|
| 64 |
void select(fcitx::InputContext *inputContext) const override { |
| 65 |
if (!filled_ || word_.empty()) { |
| 66 |
// not filled, do nothing |
| 67 |
return; |
| 68 |
} |
| 69 |
callback_(inputContext, selectedSentence_, word_); |
| 70 |
} |
| 71 |
|
| 72 |
bool filled() const { return filled_; } |
| 73 |
const std::string &word() const { return word_; } |
| 74 |
fcitx::InputContext *inputContext() { return inputContext_; } |
| 75 |
|
| 76 |
private: |
| 77 |
static constexpr long int LOADING_TIME_QUICK_THRESHOLD = 1000; |
| 78 |
|
| 79 |
void fill(const std::string &hanzi) { |
| 80 |
setText(fcitx::Text(hanzi)); |
| 81 |
word_ = hanzi; |
| 82 |
filled_ = true; |
| 83 |
if (!constructor_) { |
| 84 |
update(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
void update() { |
| 89 |
auto *inputContext = inputContext_; |
| 90 |
auto candidateList = inputContext_->inputPanel().candidateList(); |
| 91 |
if (!candidateList) { |
| 92 |
return; |
| 93 |
} |
| 94 |
auto *modifiable = candidateList->toModifiable(); |
| 95 |
if (!modifiable) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
int idx = -1; |
| 100 |
std::optional<int> dupIndex; |
| 101 |
for (auto i = 0, e = modifiable->totalSize(); i < e; i++) { |
| 102 |
const auto &candidate = modifiable->candidateFromAll(i); |
| 103 |
if (static_cast<CandidateWord *>(this) == &candidate) { |
| 104 |
idx = i; |
| 105 |
} else { |
| 106 |
if (!dupIndex && word_ == candidate.text().toString()) { |
| 107 |
dupIndex = i; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
if (idx >= 0 && (dupIndex || word_.empty())) { |
| 112 |
auto ms = |
| 113 |
std::chrono::duration_cast<std::chrono::milliseconds>( |
| 114 |
std::chrono::high_resolution_clock::now() - timestamp_) |
| 115 |
.count(); |
| 116 |
// If user make cloudpinyin to be the default one, we should prefer |
| 117 |
// to move the dup to current. |
| 118 |
if (idx == 0) { |
| 119 |
if (dupIndex) { |
| 120 |
modifiable->remove(0); |
| 121 |
modifiable->move(*dupIndex - 1, 0); |
| 122 |
} else { |
| 123 |
// result is empty. |
| 124 |
// Remove empty. |
| 125 |
modifiable->remove(0); |
| 126 |
} |
| 127 |
|
| 128 |
} else { |
| 129 |
if (!keep_ && ms <= LOADING_TIME_QUICK_THRESHOLD) { |
| 130 |
modifiable->remove(idx); |
| 131 |
} else { |
| 132 |
setText(fcitx::Text("\xe2\x98\x81")); |
| 133 |
word_ = std::string(); |
| 134 |
setPlaceHolder(!keep_); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
// use stack variable inputContext, because it may be removed already |
| 139 |
inputContext->updateUserInterface( |
| 140 |
fcitx::UserInterfaceComponent::InputPanel); |
| 141 |
} |
| 142 |
|
| 143 |
std::chrono::high_resolution_clock::time_point timestamp_ = |
| 144 |
std::chrono::high_resolution_clock::now(); |
| 145 |
bool filled_ = false; |
| 146 |
std::string word_; |
| 147 |
std::string selectedSentence_; |
| 148 |
fcitx::InputContext *inputContext_; |
| 149 |
bool constructor_ = true; |
| 150 |
CloudPinyinSelectedCallback callback_; |
| 151 |
bool keep_; |
| 152 |
}; |
| 153 |
|
| 154 |
#endif // _CLOUDPINYIN_CLOUDPINYIN_PUBLIC_H_ |