Skip to content

Commit

Permalink
(chord_composer): bind any key to commit_raw_input
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lotem committed Nov 5, 2024
1 parent b74f5fa commit c0f3dda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
49 changes: 32 additions & 17 deletions src/rime/gear/chord_composer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@
#include <rime/key_event.h>
#include <rime/schema.h>
#include <rime/gear/chord_composer.h>
#include <rime/gear/key_binding_processor.h>

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<ChordComposer>(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_);
Expand All @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/rime/gear/chord_composer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <rime/key_event.h>
#include <rime/processor.h>
#include <rime/algo/algebra.h>
#include <rime/gear/key_binding_processor.h>

namespace rime {

Expand All @@ -37,13 +38,16 @@ struct ChordingState {
}
};

class ChordComposer : public Processor {
class ChordComposer : public Processor,
public KeyBindingProcessor<ChordComposer> {
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);
Expand Down

0 comments on commit c0f3dda

Please sign in to comment.