From 68e3c31d97cb500095f991f18d1ed6b319391fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sat, 20 Jul 2024 10:37:51 +0800 Subject: [PATCH] fix(switcher): deactivate before setting option Fixes #918 --- src/rime/gear/schema_list_translator.cc | 11 ++++++----- src/rime/gear/switch_translator.cc | 22 ++++++++++++---------- src/rime/switcher.cc | 9 ++++++++- src/rime/switcher.h | 1 + 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/rime/gear/schema_list_translator.cc b/src/rime/gear/schema_list_translator.cc index e31dfaf1d8..166fdcd294 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 831a3cd454..5d8f095d48 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,9 @@ 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 4abb3f4d6a..aab885bb3f 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 0c92db79cc..db85af03b3 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(); }