@@ -84,6 +84,16 @@ inline static int get_base_layer_key_code(const KeyEvent& key_event) {
8484 : ch;
8585}
8686
87+ inline static bool finish_chord_on_all_keys_released (
88+ const ChordingState& state) {
89+ return state.pressed_keys .empty ();
90+ }
91+
92+ bool ChordComposer::FinishChordConditionIsMet () const {
93+ return finish_chord_on_first_key_release_ ||
94+ finish_chord_on_all_keys_released (state_);
95+ }
96+
8797ProcessResult ChordComposer::ProcessChordingKey (const KeyEvent& key_event) {
8898 if (key_event.ctrl () || key_event.alt () || key_event.super () ||
8999 key_event.caps ()) {
@@ -106,19 +116,14 @@ ProcessResult ChordComposer::ProcessChordingKey(const KeyEvent& key_event) {
106116 editing_chord_ = true ;
107117 bool is_key_up = key_event.release ();
108118 if (is_key_up) {
109- if (finish_chord_on_first_key_release_) {
110- if (pressed_. find (ch) != pressed_. end ()) {
111- FinishChord ();
112- pressed_. clear ();
113- }
114- } else if (pressed_. erase (ch) != 0 && pressed_. empty ( )) {
115- FinishChord ( );
119+ if (state_. ReleaseKey (ch) && FinishChordConditionIsMet () &&
120+ !state_. recognized_chord . empty ()) {
121+ FinishChord (state_. recognized_chord );
122+ }
123+ } else { // key down, ignore repeated key down events
124+ if (state_. PressKey (ch) && state_. AddKeyToChord (ch )) {
125+ UpdateChord (state_. recognized_chord );
116126 }
117- } else { // key down
118- pressed_.insert (ch);
119- bool updated = chord_.insert (ch).second ;
120- if (updated)
121- UpdateChord ();
122127 }
123128 editing_chord_ = false ;
124129 return kAccepted ;
@@ -147,23 +152,23 @@ ProcessResult ChordComposer::ProcessKeyEvent(const KeyEvent& key_event) {
147152 return ProcessFunctionKey (key_event);
148153}
149154
150- string ChordComposer::SerializeChord () {
155+ string ChordComposer::SerializeChord (const Chord& chord ) {
151156 KeySequence key_sequence;
152157 for (KeyEvent key : chording_keys_) {
153- if (chord_ .find (key.keycode ()) != chord_ .end ())
158+ if (chord .find (key.keycode ()) != chord .end ())
154159 key_sequence.push_back (key);
155160 }
156161 string code = key_sequence.repr ();
157162 algebra_.Apply (&code);
158163 return code;
159164}
160165
161- void ChordComposer::UpdateChord () {
166+ void ChordComposer::UpdateChord (const Chord& chord ) {
162167 if (!engine_)
163168 return ;
164169 Context* ctx = engine_->context ();
165170 Composition& comp = ctx->composition ();
166- string code = SerializeChord ();
171+ string code = SerializeChord (chord );
167172 prompt_format_.Apply (&code);
168173 if (comp.empty ()) {
169174 // add a placeholder segment
@@ -178,10 +183,10 @@ void ChordComposer::UpdateChord() {
178183 last_segment.prompt = code;
179184}
180185
181- void ChordComposer::FinishChord () {
186+ void ChordComposer::FinishChord (const Chord& chord ) {
182187 if (!engine_)
183188 return ;
184- string code = SerializeChord ();
189+ string code = SerializeChord (chord );
185190 output_format_.Apply (&code);
186191 ClearChord ();
187192
@@ -201,8 +206,7 @@ void ChordComposer::FinishChord() {
201206}
202207
203208void ChordComposer::ClearChord () {
204- pressed_.clear ();
205- chord_.clear ();
209+ state_.ClearChord ();
206210 if (!engine_)
207211 return ;
208212 Context* ctx = engine_->context ();
0 commit comments