| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef _TABLE_TABLE_H_ |
| 8 |
#define _TABLE_TABLE_H_ |
| 9 |
|
| 10 |
#include "ime.h" |
| 11 |
#include <fcitx-config/configuration.h> |
| 12 |
#include <fcitx-config/enum.h> |
| 13 |
#include <fcitx-config/iniparser.h> |
| 14 |
#include <fcitx-config/option.h> |
| 15 |
#include <fcitx-config/rawconfig.h> |
| 16 |
#include <fcitx-utils/event.h> |
| 17 |
#include <fcitx-utils/eventloopinterface.h> |
| 18 |
#include <fcitx-utils/handlertable.h> |
| 19 |
#include <fcitx-utils/i18n.h> |
| 20 |
#include <fcitx-utils/key.h> |
| 21 |
#include <fcitx-utils/misc.h> |
| 22 |
#include <fcitx/action.h> |
| 23 |
#include <fcitx/addoninstance.h> |
| 24 |
#include <fcitx/addonmanager.h> |
| 25 |
#include <fcitx/event.h> |
| 26 |
#include <fcitx/inputcontextproperty.h> |
| 27 |
#include <fcitx/inputmethodengine.h> |
| 28 |
#include <fcitx/instance.h> |
| 29 |
#include <libime/core/languagemodel.h> |
| 30 |
#include <libime/pinyin/pinyindictionary.h> |
| 31 |
#include <map> |
| 32 |
#include <memory> |
| 33 |
#include <string> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
namespace fcitx { |
| 37 |
|
| 38 |
class TableState; |
| 39 |
|
| 40 |
enum class LookupShuangpinProfileEnum { |
| 41 |
No, |
| 42 |
Ziranma, |
| 43 |
MS, |
| 44 |
Ziguang, |
| 45 |
ABC, |
| 46 |
Zhongwenzhixing, |
| 47 |
PinyinJiajia, |
| 48 |
Xiaohe, |
| 49 |
Custom |
| 50 |
}; |
| 51 |
|
| 52 |
FCITX_CONFIG_ENUM_NAME_WITH_I18N(LookupShuangpinProfileEnum, |
| 53 |
N_("Do not display"), N_("Ziranma"), N_("MS"), |
| 54 |
N_("Ziguang"), N_("ABC"), |
| 55 |
N_("Zhongwenzhixing"), N_("PinyinJiajia"), |
| 56 |
N_("Xiaohe"), N_("Custom")) |
| 57 |
|
| 58 |
FCITX_CONFIGURATION( |
| 59 |
TableGlobalConfig, KeyListOption modifyDictionary{this, |
| 60 |
"ModifyDictionaryKey", |
| 61 |
_("Modify dictionary"), |
| 62 |
{Key("Control+8")}, |
| 63 |
KeyListConstrain()}; |
| 64 |
KeyListOption forgetWord{this, |
| 65 |
"ForgetWord", |
| 66 |
_("Forget word"), |
| 67 |
{Key("Control+7")}, |
| 68 |
KeyListConstrain()}; |
| 69 |
KeyListOption lookupPinyin{this, |
| 70 |
"LookupPinyinKey", |
| 71 |
_("Look up pinyin"), |
| 72 |
{Key("Control+Alt+E")}, |
| 73 |
KeyListConstrain()}; |
| 74 |
OptionWithAnnotation<LookupShuangpinProfileEnum, |
| 75 |
LookupShuangpinProfileEnumI18NAnnotation> |
| 76 |
shuangpinProfile{this, "ShuangpinProfile", |
| 77 |
_("Display Shuangpin when looking up pinyin"), |
| 78 |
LookupShuangpinProfileEnum::No}; |
| 79 |
|
| 80 |
Option<bool> predictionEnabled{this, "Prediction", _("Enable Prediction"), |
| 81 |
isAndroid()}; |
| 82 |
Option<int, IntConstrain> predictionSize{this, "PredictionSize", |
| 83 |
_("Prediction Size"), 10, |
| 84 |
IntConstrain(3, 100)};); |
| 85 |
|
| 86 |
class TableEngine final : public InputMethodEngine { |
| 87 |
public: |
| 88 |
TableEngine(Instance *instance); |
| 89 |
~TableEngine(); |
| 90 |
Instance *instance() { return instance_; } |
| 91 |
void activate(const InputMethodEntry &entry, |
| 92 |
InputContextEvent &event) override; |
| 93 |
void deactivate(const InputMethodEntry &entry, |
| 94 |
InputContextEvent &event) override; |
| 95 |
void keyEvent(const InputMethodEntry &entry, KeyEvent &keyEvent) override; |
| 96 |
std::string subMode(const InputMethodEntry &entry, |
| 97 |
InputContext &ic) override; |
| 98 |
void reloadConfig() override; |
| 99 |
void reset(const InputMethodEntry &entry, |
| 100 |
InputContextEvent &event) override; |
| 101 |
void save() override; |
| 102 |
auto &factory() { return factory_; } |
| 103 |
|
| 104 |
TableIME *ime() { return ime_.get(); } |
| 105 |
auto &config() { return config_; } |
| 106 |
const Configuration *getConfig() const override { return &config_; } |
| 107 |
void setConfig(const RawConfig &config) override { |
| 108 |
config_.load(config, true); |
| 109 |
populateConfig(); |
| 110 |
saveConfig(); |
| 111 |
} |
| 112 |
void populateConfig(); |
| 113 |
void setSubConfig(const std::string &path, |
| 114 |
const RawConfig & ) override; |
| 115 |
|
| 116 |
const Configuration * |
| 117 |
getConfigForInputMethod(const InputMethodEntry &entry) const override; |
| 118 |
void setConfigForInputMethod(const InputMethodEntry &entry, |
| 119 |
const RawConfig &config) override; |
| 120 |
|
| 121 |
const libime::PinyinDictionary &pinyinDict(); |
| 122 |
const libime::LanguageModel &pinyinModel(); |
| 123 |
const auto *reverseShuangPinTable() const { |
| 124 |
return reverseShuangPinTable_.get(); |
| 125 |
} |
| 126 |
|
| 127 |
FCITX_ADDON_DEPENDENCY_LOADER(fullwidth, instance_->addonManager()); |
| 128 |
FCITX_ADDON_DEPENDENCY_LOADER(punctuation, instance_->addonManager()); |
| 129 |
FCITX_ADDON_DEPENDENCY_LOADER(quickphrase, instance_->addonManager()); |
| 130 |
FCITX_ADDON_DEPENDENCY_LOADER(pinyinhelper, instance_->addonManager()); |
| 131 |
FCITX_ADDON_DEPENDENCY_LOADER(chttrans, instance_->addonManager()); |
| 132 |
|
| 133 |
private: |
| 134 |
void cloudTableSelected(InputContext *inputContext, |
| 135 |
const std::string &selected, |
| 136 |
const std::string &word); |
| 137 |
void saveConfig() { safeSaveAsIni(config_, "conf/table.conf"); } |
| 138 |
|
| 139 |
void releaseStates(); |
| 140 |
void reloadDict(); |
| 141 |
void preload(); |
| 142 |
|
| 143 |
Instance *instance_; |
| 144 |
std::unique_ptr<TableIME> ime_; |
| 145 |
std::vector<std::unique_ptr<HandlerTableEntry<EventHandler>>> events_; |
| 146 |
SimpleAction predictionAction_; |
| 147 |
FactoryFor<TableState> factory_; |
| 148 |
|
| 149 |
TableGlobalConfig config_; |
| 150 |
std::unique_ptr<std::multimap<std::string, std::string>> |
| 151 |
reverseShuangPinTable_; |
| 152 |
libime::PinyinDictionary pinyinDict_; |
| 153 |
bool pinyinLoaded_ = false; |
| 154 |
std::unique_ptr<libime::LanguageModel> pinyinLM_; |
| 155 |
std::unique_ptr<EventSource> preloadEvent_; |
| 156 |
}; |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
#endif |