Skip to content

Commit

Permalink
Add additional behaviors in the switch input method configuration (fc…
Browse files Browse the repository at this point in the history
  • Loading branch information
EasternLi authored and mokapsing committed Sep 19, 2024
1 parent 7938591 commit 1292fb3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/rimeengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 18 additions & 3 deletions src/rimeengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, PreeditModeI18NAnnotation> preeditMode{
Expand All @@ -78,9 +90,12 @@ FCITX_CONFIGURATION(
this, "PreeditCursorPositionAtBeginning",
_("Fix embedded preedit cursor at the beginning of the preedit"),
!isAndroid() && !isApple()};
Option<bool> commitWhenDeactivate{
this, "Commit when deactivate",
_("Commit current text when deactivating"), true};
OptionWithAnnotation<SwitchInputMethodBehavior,
SwitchInputMethodBehaviorI18NAnnotation>
switchInputMethodBehavior{
this, "SwitchInputMethodBehavior",
_("Action when switching input method"),
SwitchInputMethodBehavior::CommitCommitPreview};
ExternalOption userDataDir{
this, "UserDataDir", _("User data dir"),
stringutils::concat(
Expand Down
25 changes: 25 additions & 0 deletions src/rimestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "rimesession.h"
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <fcitx-utils/capabilityflags.h>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/key.h>
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/rimestate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1292fb3

Please sign in to comment.