diff --git a/src/rimeengine.cpp b/src/rimeengine.cpp index f40c11a..55fe2ea 100644 --- a/src/rimeengine.cpp +++ b/src/rimeengine.cpp @@ -482,11 +482,22 @@ void RimeEngine::activate(const InputMethodEntry & /*entry*/, void RimeEngine::deactivate(const InputMethodEntry &entry, InputContextEvent &event) { - if (event.type() == EventType::InputContextSwitchInputMethod && - *config_.commitWhenDeactivate) { + if (event.type() == EventType::InputContextSwitchInputMethod) { auto *inputContext = event.inputContext(); auto *state = this->state(inputContext); - state->commitPreedit(inputContext); + switch (*config_.switchInputMethodBehavior) { + case SwitchInputMethodBehavior::Clear: + break; + case SwitchInputMethodBehavior::CommitRawInput: + state->commitInput(inputContext); + break; + case SwitchInputMethodBehavior::CommitComposingText: + state->commitComposing(inputContext); + break; + case SwitchInputMethodBehavior::CommitCommitPreview: + state->commitPreedit(inputContext); + break; + } } reset(entry, event); } diff --git a/src/rimeengine.h b/src/rimeengine.h index 345212d..a5ea6fa 100644 --- a/src/rimeengine.h +++ b/src/rimeengine.h @@ -63,6 +63,18 @@ enum class PreeditMode { No, ComposingText, CommitPreview }; FCITX_CONFIG_ENUM_NAME_WITH_I18N(PreeditMode, N_("Do not show"), N_("Composing text"), N_("Commit preview")) +enum class SwitchInputMethodBehavior { + Clear, + CommitRawInput, + CommitComposingText, + CommitCommitPreview +}; + +FCITX_CONFIG_ENUM_NAME_WITH_I18N(SwitchInputMethodBehavior, N_("Clear"), + N_("Commit raw input"), + N_("Commit composing text"), + N_("Commit commit preview")) + FCITX_CONFIGURATION( RimeEngineConfig, OptionWithAnnotation preeditMode{ @@ -78,9 +90,12 @@ FCITX_CONFIGURATION( this, "PreeditCursorPositionAtBeginning", _("Fix embedded preedit cursor at the beginning of the preedit"), !isAndroid() && !isApple()}; - Option commitWhenDeactivate{ - this, "Commit when deactivate", - _("Commit current text when deactivating"), true}; + OptionWithAnnotation + switchInputMethodBehavior{ + this, "SwitchInputMethodBehavior", + _("Action when switching input method"), + SwitchInputMethodBehavior::CommitCommitPreview}; ExternalOption userDataDir{ this, "UserDataDir", _("User data dir"), stringutils::concat( diff --git a/src/rimestate.cpp b/src/rimestate.cpp index 5d46726..e63b26d 100644 --- a/src/rimestate.cpp +++ b/src/rimestate.cpp @@ -11,6 +11,7 @@ #include "rimesession.h" #include #include +#include #include #include #include @@ -471,6 +472,30 @@ void RimeState::updateUI(InputContext *ic, bool keyRelease) { void RimeState::release() { session_.reset(); } +void RimeState::commitInput(InputContext *ic) { + if (auto *api = engine_->api()) { + if (const char *input = api->get_input(this->session())) { + if (std::strlen(input) > 0) { + ic->commitString(input); + } + } + } +} + +void RimeState::commitComposing(InputContext *ic) { + if (auto *api = engine_->api()) { + RIME_STRUCT(RimeContext, context); + auto session = this->session(); + if (!api->get_context(session, &context)) { + return; + } + if (context.composition.length > 0) { + ic->commitString(context.composition.preedit); + } + api->free_context(&context); + } +} + void RimeState::commitPreedit(InputContext *ic) { if (auto *api = engine_->api()) { RIME_STRUCT(RimeContext, context); diff --git a/src/rimestate.h b/src/rimestate.h index 0418600..3a176f7 100644 --- a/src/rimestate.h +++ b/src/rimestate.h @@ -44,6 +44,8 @@ class RimeState : public InputContextProperty { void updatePreedit(InputContext *ic, const RimeContext &context); void updateUI(InputContext *ic, bool keyRelease); void release(); + void commitInput(InputContext *ic); + void commitComposing(InputContext *ic); void commitPreedit(InputContext *ic); std::string subMode(); std::string subModeLabel();