diff --git a/src/rime/gear/schema_list_translator.cc b/src/rime/gear/schema_list_translator.cc index e31dfaf1d..166fdcd29 100644 --- a/src/rime/gear/schema_list_translator.cc +++ b/src/rime/gear/schema_list_translator.cc @@ -25,12 +25,13 @@ class SchemaSelection : public SimpleCandidate, public SwitcherCommand { }; void SchemaSelection::Apply(Switcher* switcher) { - switcher->Deactivate(); - if (Engine* engine = switcher->attached_engine()) { - if (keyword_ != engine->schema()->schema_id()) { - engine->ApplySchema(new Schema(keyword_)); + switcher->DeactivateAndApply([this, switcher] { + if (Engine* engine = switcher->attached_engine()) { + if (keyword_ != engine->schema()->schema_id()) { + engine->ApplySchema(new Schema(keyword_)); + } } - } + }); } class SchemaAction : public ShadowCandidate, public SwitcherCommand { diff --git a/src/rime/gear/switch_translator.cc b/src/rime/gear/switch_translator.cc index 831a3cd45..f327a64df 100644 --- a/src/rime/gear/switch_translator.cc +++ b/src/rime/gear/switch_translator.cc @@ -55,15 +55,16 @@ class Switch : public SimpleCandidate, public SwitcherCommand { }; void Switch::Apply(Switcher* switcher) { - if (Engine* engine = switcher->attached_engine()) { - engine->context()->set_option(keyword_, target_state_); - } - if (auto_save_) { - if (Config* user_config = switcher->user_config()) { - user_config->SetBool("var/option/" + keyword_, target_state_); + switcher->DeactivateAndApply([this, switcher] { + if (Engine* engine = switcher->attached_engine()) { + engine->context()->set_option(keyword_, target_state_); } - } - switcher->Deactivate(); + if (auto_save_) { + if (Config* user_config = switcher->user_config()) { + user_config->SetBool("var/option/" + keyword_, target_state_); + } + } + }); } class RadioOption; @@ -100,8 +101,7 @@ class RadioOption : public SimpleCandidate, public SwitcherCommand { }; void RadioOption::Apply(Switcher* switcher) { - group_->SelectOption(this); - switcher->Deactivate(); + switcher->DeactivateAndApply([this] { group_->SelectOption(this); }); } void RadioOption::UpdateState(bool selected) { diff --git a/src/rime/switcher.cc b/src/rime/switcher.cc index 4abb3f4d6..d283a92b8 100644 --- a/src/rime/switcher.cc +++ b/src/rime/switcher.cc @@ -249,9 +249,16 @@ void Switcher::Activate() { } void Switcher::Deactivate() { - context_->Clear(); + active_ = false; engine_->set_active_engine(); + context_->Clear(); +} + +void Switcher::DeactivateAndApply(function apply) { active_ = false; + engine_->set_active_engine(); + apply(); + context_->Clear(); } void Switcher::LoadSettings() { diff --git a/src/rime/switcher.h b/src/rime/switcher.h index 0c92db79c..7ad7676e7 100644 --- a/src/rime/switcher.h +++ b/src/rime/switcher.h @@ -40,6 +40,7 @@ class Switcher : public Processor, public Engine { void RefreshMenu(); void Activate(); void Deactivate(); + void DeactivateAndApply(function apply); Engine* attached_engine() const { return engine_; } Config* user_config() const { return user_config_.get(); }