Skip to content

Commit

Permalink
feat: new when for key_binder, ascii_mode & !ascii_mode;
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Feb 5, 2024
1 parent d402755 commit 23801db
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/rime/gear/key_binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace rime {

enum KeyBindingCondition {
kNever,
kWhenAscii,
kWhenNotAscii,
kWhenPredicting, // showing prediction candidates
kWhenPaging, // user has changed page
kWhenHasMenu, // at least one candidate
Expand All @@ -33,6 +35,8 @@ static struct KeyBindingConditionDef {
KeyBindingCondition condition;
const char* name;
} condition_definitions[] = {{kWhenPredicting, "predicting"},
{kWhenAscii, "ascii_mode"},
{kWhenNotAscii, "!ascii_mode"},
{kWhenPaging, "paging"},
{kWhenHasMenu, "has_menu"},
{kWhenComposing, "composing"},
Expand Down Expand Up @@ -160,6 +164,15 @@ static void select_schema(Engine* engine, const string& schema) {
}
}

static void ascii_send(Engine* engine, const int ch) {
if (!engine)
return;
Context* ctx = engine->context();
ctx->PushInput(ch);
ctx->ClearNonConfirmedComposition();
ctx->Commit();
}

void KeyBindings::LoadBindings(const an<ConfigList>& bindings) {
if (!bindings)
return;
Expand All @@ -186,7 +199,10 @@ void KeyBindings::LoadBindings(const an<ConfigList>& bindings) {
if (auto target = map->GetValue("send")) {
KeyEvent key;
if (key.Parse(target->str())) {
binding.target.push_back(std::move(key));
if (whence->str() != "ascii_mode")
binding.target.push_back(std::move(key));
else
binding.action = std::bind(&ascii_send, _1, key.keycode());
} else {
LOG(WARNING) << "invalid key binding #" << i << ".";
continue;
Expand Down Expand Up @@ -239,6 +255,11 @@ KeyBindingConditions::KeyBindingConditions(Context* ctx) {
insert(kWhenComposing);
}

if (ctx->get_option("ascii_mode"))
insert(kWhenAscii);
else
insert(kWhenNotAscii);

if (ctx->HasMenu() && !ctx->get_option("ascii_mode")) {
insert(kWhenHasMenu);
}
Expand Down

0 comments on commit 23801db

Please sign in to comment.