Skip to content

Commit

Permalink
fix(selector): do not match fallback modifier
Browse files Browse the repository at this point in the history
Tab = Shift+Right should be skipped instead of treated as Right.

Fixes #609
  • Loading branch information
lotem committed Feb 5, 2023
1 parent a94739f commit 21adf97
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/rime/gear/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ ProcessResult Editor::ProcessKeyEvent(const KeyEvent& key_event) {
int ch = key_event.keycode();
Context* ctx = engine_->context();
if (ctx->IsComposing()) {
auto result = KeyBindingProcessor::ProcessKeyEvent(key_event, ctx);
auto result = KeyBindingProcessor::ProcessKeyEvent(
key_event, ctx, 0, FallbackOptions::All);
if (result != kNoop) {
return result;
}
Expand Down
10 changes: 9 additions & 1 deletion src/rime/gear/key_binding_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class KeyBindingProcessor {
typedef bool Handler(Context* ctx);
using HandlerPtr = bool (T::*)(Context* ctx);

enum FallbackOptions {
None = 0,
ShiftAsControl = (1 << 0),
IgnoreShift = (1 << 1),
All = ShiftAsControl | IgnoreShift,
};

struct ActionDef {
const char* name;
HandlerPtr action;
Expand All @@ -31,7 +38,8 @@ class KeyBindingProcessor {

ProcessResult ProcessKeyEvent(const KeyEvent& key_event,
Context* ctx,
int keymap_selector = 0);
int keymap_selector = 0,
int fallback_options = FallbackOptions::None);
void LoadConfig(Config* config,
const string& section,
int kemap_selector = 0);
Expand Down
35 changes: 21 additions & 14 deletions src/rime/gear/key_binding_processor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,37 @@ const typename KeyBindingProcessor<T, N>::ActionDef

template <class T, int N>
ProcessResult KeyBindingProcessor<T, N>::ProcessKeyEvent(
const KeyEvent& key_event, Context* ctx, int keymap_selector) {
const KeyEvent& key_event,
Context* ctx,
int keymap_selector,
int fallback_options) {
auto& keymap = get_keymap(keymap_selector);
// exact match
if (Accept(key_event, ctx, keymap)) {
return kAccepted;
}
// fallback: compatible modifiers
// try to match the fallback options
if (key_event.ctrl() || key_event.alt()) {
return kNoop;
}
if (key_event.shift()) {
KeyEvent shift_as_ctrl{
key_event.keycode(),
(key_event.modifier() & ~kShiftMask) | kControlMask
};
if (Accept(shift_as_ctrl, ctx, keymap)) {
return kAccepted;
if ((fallback_options & ShiftAsControl) != 0) {
KeyEvent shift_as_control{
key_event.keycode(),
(key_event.modifier() & ~kShiftMask) | kControlMask
};
if (Accept(shift_as_control, ctx, keymap)) {
return kAccepted;
}
}
KeyEvent ignore_shift{
key_event.keycode(),
key_event.modifier() & ~kShiftMask
};
if (Accept(ignore_shift, ctx, keymap)) {
return kAccepted;
if ((fallback_options & IgnoreShift) != 0) {
KeyEvent ignore_shift{
key_event.keycode(),
key_event.modifier() & ~kShiftMask
};
if (Accept(ignore_shift, ctx, keymap)) {
return kAccepted;
}
}
}
// not handled
Expand Down
3 changes: 2 additions & 1 deletion src/rime/gear/navigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ ProcessResult Navigator::ProcessKeyEvent(const KeyEvent& key_event) {
return kNoop;
TextOrientation text_orientation =
ctx->get_option("_vertical") ? Vertical : Horizontal;
return KeyBindingProcessor::ProcessKeyEvent(key_event, ctx, text_orientation);
return KeyBindingProcessor::ProcessKeyEvent(
key_event, ctx, text_orientation, FallbackOptions::All);
}

bool Navigator::LeftBySyllable(Context* ctx) {
Expand Down
5 changes: 4 additions & 1 deletion src/rime/gear/selector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ ProcessResult Selector::ProcessKeyEvent(const KeyEvent& key_event) {
CandidateListLayout candidate_list_layout =
is_linear_layout(ctx) ? Linear : Stacked;
auto result = KeyBindingProcessor::ProcessKeyEvent(
key_event, ctx, text_orientation | candidate_list_layout);
key_event,
ctx,
text_orientation | candidate_list_layout,
FallbackOptions::None);
if (result != kNoop) {
return result;
}
Expand Down

0 comments on commit 21adf97

Please sign in to comment.