Skip to content

Commit

Permalink
client preedit
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Oct 12, 2024
1 parent baaf765 commit 57e379b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
31 changes: 23 additions & 8 deletions src/hallelujah.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ class HallelujahCandidateWord : public CandidateWord {
}

void select(InputContext *inputContext) const override {
auto config = static_cast<const HallelujahEngineConfig *>(
state_->engine()->getConfig());
auto config = state_->engine()->config();
auto word = text().toString();
if (*config->commitWithSpace) {
if (*config.commitWithSpace) {
word += " ";
}
inputContext->commitString(word);
Expand Down Expand Up @@ -96,6 +95,22 @@ class HallelujahCandidateList : public CandidateList,
int cursor_ = 0;
};

void HallelujahState::updatePreedit(InputContext *ic, const Text &preedit) {
PreeditMode mode = ic->capabilityFlags().test(CapabilityFlag::Preedit)
? *engine_->config().preeditMode
: PreeditMode::No;
auto &inputPanel = ic->inputPanel();
switch (mode) {
case PreeditMode::No:
inputPanel.setPreedit(preedit);
inputPanel.setClientPreedit(Text());
break;
case PreeditMode::ComposingText:
inputPanel.setClientPreedit(preedit);
break;
}
}

void HallelujahState::updateUI(InputContext *ic,
const std::vector<std::string> &words,
const std::vector<std::string> &comments) {
Expand All @@ -111,7 +126,8 @@ void HallelujahState::updateUI(InputContext *ic,
if (!userInput.empty()) {
preedit.append(userInput);
}
inputPanel.setPreedit(preedit);
updatePreedit(ic, preedit);
ic->updatePreedit();
ic->updateUserInterface(UserInterfaceComponent::InputPanel);
}

Expand Down Expand Up @@ -211,13 +227,12 @@ void HallelujahState::keyEvent(KeyEvent &event) {
std::copy(userInput.begin(), userInput.end(), word.begin());
}
if (iter != words_->end()) {
auto config = static_cast<const HallelujahEngineConfig *>(
engine_->getConfig());
std::string comment = *config->showIPA ? iter->second.ipa_ : "";
auto config = engine_->config();
std::string comment = *config.showIPA ? iter->second.ipa_ : "";
if (!comment.empty()) {
comment = fmt::format("[{}] ", comment);
}
if (*config->showTranslation) {
if (*config.showTranslation) {
comment += fmt::format(
"{}", fmt::join(iter->second.translation_, " "));
}
Expand Down
23 changes: 16 additions & 7 deletions src/hallelujah.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@
#include <unordered_map>

namespace fcitx {
FCITX_CONFIGURATION(HallelujahEngineConfig,
Option<bool> showIPA{this, "ShowIPA", _("Show IPA"), true};
Option<bool> showTranslation{this, "ShowTranslation",
_("Show translation"), true};
Option<bool> commitWithSpace{this, "CommitWithSpace",
_("Commit with space"),
false};);
enum class PreeditMode { No, ComposingText };

FCITX_CONFIG_ENUM_NAME_WITH_I18N(PreeditMode, N_("Do not show"),
N_("Composing text"))

FCITX_CONFIGURATION(
HallelujahEngineConfig,
OptionWithAnnotation<PreeditMode, PreeditModeI18NAnnotation> preeditMode{
this, "PreeditMode", _("Preedit Mode"), PreeditMode::ComposingText};
Option<bool> showIPA{this, "ShowIPA", _("Show IPA"), true};
Option<bool> showTranslation{this, "ShowTranslation", _("Show translation"),
true};
Option<bool> commitWithSpace{this, "CommitWithSpace",
_("Commit with space"), false};);

struct HallelujahWord {
HallelujahWord(const std::vector<std::string> &translation,
Expand Down Expand Up @@ -48,6 +55,7 @@ class HallelujahState : public InputContextProperty {
HallelujahEngine *engine() { return engine_; }

private:
void updatePreedit(InputContext *ic, const Text &preedit);
HallelujahEngine *engine_;
InputContext *ic_;
InputBuffer buffer_{
Expand All @@ -67,6 +75,7 @@ class HallelujahEngine final : public InputMethodEngine {
const Configuration *getConfig() const override { return &config_; }
void setConfig(const RawConfig &config) override;
void reloadConfig() override;
const HallelujahEngineConfig &config() const { return config_; }
FCITX_ADDON_DEPENDENCY_LOADER(spell, instance_->addonManager());

private:
Expand Down

0 comments on commit 57e379b

Please sign in to comment.