Skip to content

Commit 81bfdb0

Browse files
authored
Add additional behaviors in the switch input method configuration (#118)
1 parent 664c7dd commit 81bfdb0

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

src/rimeengine.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,22 @@ void RimeEngine::activate(const InputMethodEntry & /*entry*/,
482482

483483
void RimeEngine::deactivate(const InputMethodEntry &entry,
484484
InputContextEvent &event) {
485-
if (event.type() == EventType::InputContextSwitchInputMethod &&
486-
*config_.commitWhenDeactivate) {
485+
if (event.type() == EventType::InputContextSwitchInputMethod) {
487486
auto *inputContext = event.inputContext();
488487
auto *state = this->state(inputContext);
489-
state->commitPreedit(inputContext);
488+
switch (*config_.switchInputMethodBehavior) {
489+
case SwitchInputMethodBehavior::Clear:
490+
break;
491+
case SwitchInputMethodBehavior::CommitRawInput:
492+
state->commitInput(inputContext);
493+
break;
494+
case SwitchInputMethodBehavior::CommitComposingText:
495+
state->commitComposing(inputContext);
496+
break;
497+
case SwitchInputMethodBehavior::CommitCommitPreview:
498+
state->commitPreedit(inputContext);
499+
break;
500+
}
490501
}
491502
reset(entry, event);
492503
}

src/rimeengine.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ enum class PreeditMode { No, ComposingText, CommitPreview };
6363
FCITX_CONFIG_ENUM_NAME_WITH_I18N(PreeditMode, N_("Do not show"),
6464
N_("Composing text"), N_("Commit preview"))
6565

66+
enum class SwitchInputMethodBehavior {
67+
Clear,
68+
CommitRawInput,
69+
CommitComposingText,
70+
CommitCommitPreview
71+
};
72+
73+
FCITX_CONFIG_ENUM_NAME_WITH_I18N(SwitchInputMethodBehavior, N_("Clear"),
74+
N_("Commit raw input"),
75+
N_("Commit composing text"),
76+
N_("Commit commit preview"))
77+
6678
FCITX_CONFIGURATION(
6779
RimeEngineConfig,
6880
OptionWithAnnotation<PreeditMode, PreeditModeI18NAnnotation> preeditMode{
@@ -78,9 +90,12 @@ FCITX_CONFIGURATION(
7890
this, "PreeditCursorPositionAtBeginning",
7991
_("Fix embedded preedit cursor at the beginning of the preedit"),
8092
!isAndroid() && !isApple()};
81-
Option<bool> commitWhenDeactivate{
82-
this, "Commit when deactivate",
83-
_("Commit current text when deactivating"), true};
93+
OptionWithAnnotation<SwitchInputMethodBehavior,
94+
SwitchInputMethodBehaviorI18NAnnotation>
95+
switchInputMethodBehavior{
96+
this, "SwitchInputMethodBehavior",
97+
_("Action when switching input method"),
98+
SwitchInputMethodBehavior::CommitCommitPreview};
8499
ExternalOption userDataDir{
85100
this, "UserDataDir", _("User data dir"),
86101
stringutils::concat(

src/rimestate.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "rimesession.h"
1212
#include <algorithm>
1313
#include <cstdint>
14+
#include <cstring>
1415
#include <fcitx-utils/capabilityflags.h>
1516
#include <fcitx-utils/i18n.h>
1617
#include <fcitx-utils/key.h>
@@ -440,6 +441,30 @@ void RimeState::updateUI(InputContext *ic, bool keyRelease) {
440441

441442
void RimeState::release() { session_.reset(); }
442443

444+
void RimeState::commitInput(InputContext *ic) {
445+
if (auto *api = engine_->api()) {
446+
if (const char *input = api->get_input(this->session())) {
447+
if (std::strlen(input) > 0) {
448+
ic->commitString(input);
449+
}
450+
}
451+
}
452+
}
453+
454+
void RimeState::commitComposing(InputContext *ic) {
455+
if (auto *api = engine_->api()) {
456+
RIME_STRUCT(RimeContext, context);
457+
auto session = this->session();
458+
if (!api->get_context(session, &context)) {
459+
return;
460+
}
461+
if (context.composition.length > 0) {
462+
ic->commitString(context.composition.preedit);
463+
}
464+
api->free_context(&context);
465+
}
466+
}
467+
443468
void RimeState::commitPreedit(InputContext *ic) {
444469
if (auto *api = engine_->api()) {
445470
RIME_STRUCT(RimeContext, context);

src/rimestate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class RimeState : public InputContextProperty {
4343
void updatePreedit(InputContext *ic, const RimeContext &context);
4444
void updateUI(InputContext *ic, bool keyRelease);
4545
void release();
46+
void commitInput(InputContext *ic);
47+
void commitComposing(InputContext *ic);
4648
void commitPreedit(InputContext *ic);
4749
std::string subMode();
4850
std::string subModeLabel();

0 commit comments

Comments
 (0)