diff --git a/src/rime/gear/chord_composer.cc b/src/rime/gear/chord_composer.cc index e3b3a4a573..e45c9bfdc2 100644 --- a/src/rime/gear/chord_composer.cc +++ b/src/rime/gear/chord_composer.cc @@ -12,16 +12,25 @@ #include #include #include +#include namespace rime { -ChordComposer::ChordComposer(const Ticket& ticket) : Processor(ticket) { +static ChordComposer::ActionDef action_definitions[] = { + {"commit_raw_input", &ChordComposer::CommitRawInput}, + ChordComposer::kActionNoop, +}; + +ChordComposer::ChordComposer(const Ticket& ticket) + : Processor(ticket), + KeyBindingProcessor(action_definitions) { if (!engine_) return; if (Config* config = engine_->schema()->config()) { string alphabet; config->GetString("chord_composer/alphabet", &alphabet); chording_keys_.Parse(alphabet); + KeyBindingProcessor::LoadConfig(config, "chord_composer"); config->GetBool("chord_composer/use_control", &use_control_); config->GetBool("chord_composer/use_alt", &use_alt_); config->GetBool("chord_composer/use_shift", &use_shift_); @@ -47,26 +56,32 @@ ChordComposer::~ChordComposer() { unhandled_key_connection_.disconnect(); } +bool ChordComposer::CommitRawInput(Context* ctx) { + if (raw_sequence_.empty()) { + return false; + } + // commit raw input + engine_->context()->set_input(raw_sequence_); + // then the sequence should not be used again + raw_sequence_.clear(); + // discard composition and commit input + ctx->ClearNonConfirmedComposition(); + ctx->Commit(); + return true; +} + ProcessResult ChordComposer::ProcessFunctionKey(const KeyEvent& key_event) { - if (key_event.release()) { - return kNoop; + Context* ctx = engine_->context(); + auto result = KeyBindingProcessor::ProcessKeyEvent(key_event, ctx, 0); + if (result != kNoop) { + return result; } - int ch = key_event.keycode(); - if (ch == XK_Return) { - if (!raw_sequence_.empty()) { - // commit raw input - engine_->context()->set_input(raw_sequence_); - // then the sequence should not be used again + if (!key_event.release()) { + int ch = key_event.keycode(); + if (ch == XK_BackSpace || ch == XK_Escape) { + // clear the raw sequence raw_sequence_.clear(); } - ClearChord(); - state_.Clear(); - - } else if (ch == XK_BackSpace || ch == XK_Escape) { - // clear the raw sequence - raw_sequence_.clear(); - ClearChord(); - state_.Clear(); } return kNoop; } diff --git a/src/rime/gear/chord_composer.h b/src/rime/gear/chord_composer.h index dcb2ccd0ad..35537f2444 100644 --- a/src/rime/gear/chord_composer.h +++ b/src/rime/gear/chord_composer.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace rime { @@ -37,13 +38,16 @@ struct ChordingState { } }; -class ChordComposer : public Processor { +class ChordComposer : public Processor, + public KeyBindingProcessor { public: ChordComposer(const Ticket& ticket); ~ChordComposer(); virtual ProcessResult ProcessKeyEvent(const KeyEvent& key_event); + Handler CommitRawInput; + protected: bool FinishChordConditionIsMet() const; ProcessResult ProcessChordingKey(const KeyEvent& key_event);