| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef _TABLE_STATE_H_ |
| 8 |
#define _TABLE_STATE_H_ |
| 9 |
|
| 10 |
#include "context.h" |
| 11 |
#include "engine.h" |
| 12 |
#include "ime.h" |
| 13 |
#include <cstddef> |
| 14 |
#include <fcitx-utils/inputbuffer.h> |
| 15 |
#include <fcitx/candidatelist.h> |
| 16 |
#include <fcitx/event.h> |
| 17 |
#include <fcitx/inputcontextproperty.h> |
| 18 |
#include <fcitx/inputmethodentry.h> |
| 19 |
#include <list> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <utility> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace fcitx { |
| 26 |
|
| 27 |
enum class TableMode { |
| 28 |
Normal, |
| 29 |
ModifyDictionary, |
| 30 |
ForgetWord, |
| 31 |
Pinyin, |
| 32 |
LookupPinyin, |
| 33 |
Punctuation, |
| 34 |
}; |
| 35 |
|
| 36 |
struct EventSourceTime; |
| 37 |
|
| 38 |
class TableState : public InputContextProperty { |
| 39 |
public: |
| 40 |
TableState(InputContext *ic, TableEngine *engine) |
| 41 |
: ic_(ic), engine_(engine) {} |
| 42 |
|
| 43 |
InputContext *ic_; |
| 44 |
TableEngine *engine_; |
| 45 |
bool lastIsPunc_ = false; |
| 46 |
std::unique_ptr<EventSourceTime> cancelLastEvent_; |
| 47 |
|
| 48 |
TableContext *updateContext(const InputMethodEntry *entry); |
| 49 |
void release(); |
| 50 |
void reset(const InputMethodEntry *entry = nullptr); |
| 51 |
void resetAndPredict(); |
| 52 |
void predict(); |
| 53 |
|
| 54 |
void keyEvent(const InputMethodEntry &entry, KeyEvent &event); |
| 55 |
|
| 56 |
void commitBuffer(bool commitCode, bool noRealCommit = false); |
| 57 |
void updateUI(bool keepOldCursor, bool maybePredict); |
| 58 |
void updatePuncCandidate(InputContext *inputContext, |
| 59 |
const std::string &original, |
| 60 |
const std::vector<std::string> &candidates); |
| 61 |
void updatePuncPreedit(InputContext *inputContext); |
| 62 |
void pushLastCommit(const std::string &code, |
| 63 |
const std::string &lastSegment); |
| 64 |
|
| 65 |
void commitAfterSelect(int commitFrom); |
| 66 |
|
| 67 |
auto mode() const { return mode_; } |
| 68 |
|
| 69 |
void forgetCandidateWord(size_t idx); |
| 70 |
void handle2nd3rdCandidate(KeyEvent &event) { |
| 71 |
auto *context = context_.get(); |
| 72 |
if (!context) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
const auto &config = context->config(); |
| 77 |
handle2nd3rdCandidate(config, event); |
| 78 |
} |
| 79 |
|
| 80 |
auto context() const { return context_.get(); } |
| 81 |
|
| 82 |
private: |
| 83 |
bool handle2nd3rdCandidate(const TableConfig &config, KeyEvent &event); |
| 84 |
bool handlePuncCandidate(const TableConfig &config, KeyEvent &event); |
| 85 |
bool handleCandidateList(const TableConfig &config, KeyEvent &event); |
| 86 |
bool handleForgetWord(KeyEvent &event); |
| 87 |
bool handlePinyinMode(KeyEvent &event); |
| 88 |
bool handleLookupPinyinOrModifyDictionaryMode(KeyEvent &event); |
| 89 |
|
| 90 |
bool isContextEmpty() const; |
| 91 |
bool autoSelectCandidate() const; |
| 92 |
|
| 93 |
bool isComposeTableMode() const; |
| 94 |
|
| 95 |
std::unique_ptr<CandidateList> |
| 96 |
predictCandidateList(const std::vector<std::string> &words); |
| 97 |
std::string commitSegements(size_t from, size_t to); |
| 98 |
|
| 99 |
TableMode mode_ = TableMode::Normal; |
| 100 |
std::string pinyinModePrefix_; |
| 101 |
InputBuffer pinyinModeBuffer_{ |
| 102 |
{InputBufferOption::AsciiOnly, InputBufferOption::FixedCursor}}; |
| 103 |
size_t lookupPinyinIndex_ = 0; |
| 104 |
std::vector<std::pair<std::string, std::string>> lookupPinyinString_; |
| 105 |
std::string lastContext_; |
| 106 |
std::list<std::pair<std::string, std::string>> lastCommit_; |
| 107 |
std::string lastSegment_; |
| 108 |
std::list<std::pair<std::string, std::string>> autoPhraseBuffer_; |
| 109 |
std::unique_ptr<TableContext> context_; |
| 110 |
|
| 111 |
int keyReleased_ = -1; |
| 112 |
int keyReleasedIndex_ = -2; |
| 113 |
uint64_t lastKeyPressedTime_ = 0; |
| 114 |
}; |
| 115 |
|
| 116 |
class CommitAfterSelectWrapper { |
| 117 |
public: |
| 118 |
CommitAfterSelectWrapper(TableState *state) : state_(state) { |
| 119 |
if (auto *context = state->updateContext(nullptr)) { |
| 120 |
commitFrom_ = static_cast<int>(context->selectedSize()); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
~CommitAfterSelectWrapper() { |
| 125 |
if (commitFrom_ >= 0) { |
| 126 |
state_->commitAfterSelect(commitFrom_); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
private: |
| 131 |
TableState *state_; |
| 132 |
int commitFrom_ = -1; |
| 133 |
}; |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
#endif |