From 7892a4f53d1a2f12706401b07cbd366eaf87596c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sat, 9 Mar 2024 23:45:43 +0800 Subject: [PATCH] fix(switcher): superfluously load saved options --- src/rime/engine.cc | 28 +++++++++++++--------------- src/rime/switcher.cc | 10 +++++++++- src/rime/switcher.h | 4 +++- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/rime/engine.cc b/src/rime/engine.cc index 64c0fb68b..44406c070 100644 --- a/src/rime/engine.cc +++ b/src/rime/engine.cc @@ -52,9 +52,7 @@ class ConcreteEngine : public Engine { vector> filters_; vector> formatters_; vector> post_processors_; - // To make sure dumping user.yaml when processors_.clear(), - // switcher is owned by processors_[0] - weak switcher_; + an switcher_; }; // implementations @@ -85,6 +83,11 @@ ConcreteEngine::ConcreteEngine() { [this](Context* ctx, const string& property) { OnPropertyUpdate(ctx, property); }); + + switcher_ = New(this); + // saved options should be loaded only once per input session + switcher_->RestoreSavedOptions(); + InitializeComponents(); InitializeOptions(); } @@ -281,14 +284,7 @@ void ConcreteEngine::OnSelect(Context* ctx) { void ConcreteEngine::ApplySchema(Schema* schema) { if (!schema) return; - if (auto switcher = switcher_.lock()) { - if (Config* user_config = switcher->user_config()) { - user_config->SetString("var/previously_selected_schema", - schema->schema_id()); - user_config->SetInt("var/schema_access_time/" + schema->schema_id(), - time(NULL)); - } - } + switcher_->SetActiveSchema(schema->schema_id()); schema_.reset(schema); context_->Clear(); context_->ClearTransientOptions(); @@ -303,11 +299,10 @@ void ConcreteEngine::InitializeComponents() { translators_.clear(); filters_.clear(); - if (auto switcher = New(this)) { - switcher_ = switcher; - processors_.push_back(switcher); + if (switcher_) { + processors_.push_back(switcher_); if (schema_->schema_id() == ".default") { - if (Schema* schema = switcher->CreateSchema()) { + if (Schema* schema = switcher_->CreateSchema()) { schema_.reset(schema); } } @@ -397,10 +392,13 @@ void ConcreteEngine::InitializeComponents() { } void ConcreteEngine::InitializeOptions() { + LOG(INFO) << "ConcreteEngine::InitializeOptions"; // reset custom switches Config* config = schema_->config(); Switches switches(config); switches.FindOption([this](Switches::SwitchOption option) { + LOG(INFO) << "found switch option: " << option.option_name + << ", reset: " << option.reset_value; if (option.reset_value >= 0) { if (option.type == Switches::kToggleOption) { context_->set_option(option.option_name, (option.reset_value != 0)); diff --git a/src/rime/switcher.cc b/src/rime/switcher.cc index 7f6741883..4abb3f4d6 100644 --- a/src/rime/switcher.cc +++ b/src/rime/switcher.cc @@ -29,7 +29,6 @@ Switcher::Switcher(const Ticket& ticket) : Processor(ticket) { user_config_.reset(Config::Require("user_config")->Create("user")); InitializeComponents(); LoadSettings(); - RestoreSavedOptions(); } Switcher::~Switcher() { @@ -163,6 +162,15 @@ int Switcher::ForEachSchemaListEntry( return num_processed_entries; } +void Switcher::SetActiveSchema(const string& schema_id) { + if (user_config_) { + user_config_->SetString("var/previously_selected_schema", schema_id); + user_config_->SetInt("var/schema_access_time/" + schema_id, time(NULL)); + // persist recently used schema and options that have changed + user_config_->Save(); + } +} + Schema* Switcher::CreateSchema() { Config* config = schema_->config(); if (!config) diff --git a/src/rime/switcher.h b/src/rime/switcher.h index dd7e67493..0c92db79c 100644 --- a/src/rime/switcher.h +++ b/src/rime/switcher.h @@ -31,9 +31,11 @@ class Switcher : public Processor, public Engine { Config* config, function process_entry); + void SetActiveSchema(const string& schema_id); Schema* CreateSchema(); void SelectNextSchema(); bool IsAutoSave(const string& option) const; + void RestoreSavedOptions(); void RefreshMenu(); void Activate(); @@ -46,7 +48,7 @@ class Switcher : public Processor, public Engine { protected: void InitializeComponents(); void LoadSettings(); - void RestoreSavedOptions(); + void HighlightNextSchema(); void OnSelect(Context* ctx);