Skip to content

Commit

Permalink
feat(chord_composer): accept escaped chording keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Dec 25, 2016
1 parent aa43e5e commit 79a32b2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 28 deletions.
9 changes: 5 additions & 4 deletions include/rime/gear/chord_composer.h
Expand Up @@ -9,6 +9,7 @@

#include <rime/common.h>
#include <rime/component.h>
#include <rime/key_event.h>
#include <rime/processor.h>
#include <rime/algo/algebra.h>

Expand All @@ -30,17 +31,17 @@ class ChordComposer : public Processor {
void OnContextUpdate(Context* ctx);
void OnUnhandledKey(Context* ctx, const KeyEvent& key);

string alphabet_;
KeySequence chording_keys_;
string delimiter_;
Projection algebra_;
Projection output_format_;
Projection prompt_format_;

set<char> pressed_;
set<char> chord_;
set<int> pressed_;
set<int> chord_;
bool pass_thru_ = false;
bool composing_ = false;
string sequence_;
string raw_sequence_;
connection update_connection_;
connection unhandled_key_connection_;
};
Expand Down
53 changes: 29 additions & 24 deletions src/gear/chord_composer.cc
Expand Up @@ -23,7 +23,9 @@ ChordComposer::ChordComposer(const Ticket& ticket) : Processor(ticket) {
if (!engine_)
return;
if (Config* config = engine_->schema()->config()) {
config->GetString("chord_composer/alphabet", &alphabet_);
string alphabet;
config->GetString("chord_composer/alphabet", &alphabet);
chording_keys_.Parse(alphabet);
config->GetString("speller/delimiter", &delimiter_);
algebra_.Load(config->GetList("chord_composer/algebra"));
output_format_.Load(config->GetList("chord_composer/output_format"));
Expand Down Expand Up @@ -54,38 +56,40 @@ ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
int ch = key_event.keycode();
Context* ctx = engine_->context();
if (!is_key_up && ch == XK_Return) {
if (!sequence_.empty()) {
if (!raw_sequence_.empty()) {
// commit raw input
ctx->set_input(sequence_);
ctx->set_input(raw_sequence_);
// then the sequence should not be used again
sequence_.clear();
raw_sequence_.clear();
}
ClearChord();
return kNoop;
}
if (!is_key_up && ch == XK_BackSpace) {
// invalidate sequence
sequence_.clear();
// invalidate raw sequence
raw_sequence_.clear();
ClearChord();
if (DeleteLastSyllable()) {
return kAccepted;
}
return kNoop;
}
if (!is_key_up && ch == XK_Escape) {
// to clear a sequence made of invalid combos
sequence_.clear();
// clear the raw sequence
raw_sequence_.clear();
ClearChord();
return kNoop;
}
if (!is_key_up && ch >= 0x20 && ch <= 0x7e) {
// save raw input
if (!ctx->IsComposing() || !sequence_.empty()) {
sequence_.push_back(ch);
DLOG(INFO) << "update sequence: " << sequence_;
if (!ctx->IsComposing() || !raw_sequence_.empty()) {
raw_sequence_.push_back(ch);
DLOG(INFO) << "update raw sequence: " << raw_sequence_;
}
}
if (alphabet_.find(ch) == string::npos) {
if (std::find(chording_keys_.begin(),
chording_keys_.end(),
KeyEvent{ch, 0}) == chording_keys_.end()) {
return chording ? kAccepted : kNoop;
}
// in alphabet
Expand All @@ -104,11 +108,12 @@ ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
}

string ChordComposer::SerializeChord() {
string code;
for (char ch : alphabet_) {
if (chord_.find(ch) != chord_.end())
code.push_back(ch);
KeySequence key_sequence;
for (KeyEvent key : chording_keys_) {
if (chord_.find(key.keycode()) != chord_.end())
key_sequence.push_back(key);
}
string code = key_sequence.repr();
algebra_.Apply(&code);
return code;
}
Expand Down Expand Up @@ -142,15 +147,15 @@ void ChordComposer::FinishChord() {
output_format_.Apply(&code);
ClearChord();

KeySequence sequence;
if (sequence.Parse(code) && !sequence.empty()) {
KeySequence key_sequence;
if (key_sequence.Parse(code) && !key_sequence.empty()) {
pass_thru_ = true;
for (const KeyEvent& key : sequence) {
for (const KeyEvent& key : key_sequence) {
if (!engine_->ProcessKey(key)) {
// direct commit
engine_->CommitText(string(1, key.keycode()));
// exclude the character (eg. space) from the following sequence
sequence_.clear();
// exclude the character (eg. space) from the raw sequence
raw_sequence_.clear();
}
}
pass_thru_ = false;
Expand Down Expand Up @@ -202,18 +207,18 @@ void ChordComposer::OnContextUpdate(Context* ctx) {
}
else if (composing_) {
composing_ = false;
sequence_.clear();
raw_sequence_.clear();
DLOG(INFO) << "clear sequence.";
}
}

void ChordComposer::OnUnhandledKey(Context* ctx, const KeyEvent& key) {
// directly committed ascii should not be captured into the sequence
// directly committed ascii should not be captured into the raw sequence
// test case:
// 3.14{Return} should not commit an extra sequence '14'
if ((key.modifier() & ~kShiftMask) == 0 &&
key.keycode() >= 0x20 && key.keycode() <= 0x7e) {
sequence_.clear();
raw_sequence_.clear();
DLOG(INFO) << "clear sequence.";
}
}
Expand Down

0 comments on commit 79a32b2

Please sign in to comment.