From c0f3dda0fbd9a7eee2b85f247eeb82fab426de5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Wed, 6 Nov 2024 00:31:40 +0800 Subject: [PATCH] (chord_composer): bind any key to commit_raw_input the raw input key sequence before converting to chord is kept in the ChordComposer. previously the Return key binding is hardcoded. leverage KeyBindingProcessor to allow Return used for other purposes like committing text, and allow other keys be used to commit_raw_input. --- src/rime/gear/chord_composer.cc | 49 +++++++++++++++++++++------------ src/rime/gear/chord_composer.h | 6 +++- 2 files changed, 37 insertions(+), 18 deletions(-) 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);