Skip to content

Commit

Permalink
fix(switcher): deactivate before setting option
Browse files Browse the repository at this point in the history
Fixes rime#918
  • Loading branch information
lotem committed Jul 20, 2024
1 parent f6dd300 commit f81c971
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
11 changes: 6 additions & 5 deletions src/rime/gear/schema_list_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions src/rime/gear/switch_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion src/rime/switcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,16 @@ void Switcher::Activate() {
}

void Switcher::Deactivate() {
context_->Clear();
active_ = false;
engine_->set_active_engine();
context_->Clear();
}

void Switcher::DeactivateAndApply(function<void()> apply) {
active_ = false;
engine_->set_active_engine();
apply();
context_->Clear();
}

void Switcher::LoadSettings() {
Expand Down
1 change: 1 addition & 0 deletions src/rime/switcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Switcher : public Processor, public Engine {
void RefreshMenu();
void Activate();
void Deactivate();
void DeactivateAndApply(function<void()> apply);

Engine* attached_engine() const { return engine_; }
Config* user_config() const { return user_config_.get(); }
Expand Down

0 comments on commit f81c971

Please sign in to comment.