From cd1fe3e81a9bd5d26a25e046c682dff7bf0bbd89 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:10:45 +0800 Subject: [PATCH 1/3] feat(config): Config::Save saves data if modified --- src/rime/config/config_component.cc | 4 ++++ src/rime/config/config_component.h | 2 ++ src/rime/config/config_data.cc | 8 ++++++-- src/rime/config/config_data.h | 2 ++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/rime/config/config_component.cc b/src/rime/config/config_component.cc index 924bddf63..60c110784 100644 --- a/src/rime/config/config_component.cc +++ b/src/rime/config/config_component.cc @@ -23,6 +23,10 @@ Config::~Config() {} Config::Config(an data) : ConfigItemRef(data.get()), data_(data) {} +bool Config::Save() { + return data_->Save(); +} + bool Config::LoadFromStream(std::istream& stream) { return data_->LoadFromStream(stream); } diff --git a/src/rime/config/config_component.h b/src/rime/config/config_component.h index 3dda808fc..08e9ba14b 100644 --- a/src/rime/config/config_component.h +++ b/src/rime/config/config_component.h @@ -28,6 +28,8 @@ class Config : public Class, public ConfigItemRef { // in the ConfigComponent explicit Config(an data); + // returns whether actually saved to file. + bool Save(); bool LoadFromStream(std::istream& stream); bool SaveToStream(std::ostream& stream); RIME_API bool LoadFromFile(const path& file_path); diff --git a/src/rime/config/config_data.cc b/src/rime/config/config_data.cc index 5859f0de5..06309c4ed 100644 --- a/src/rime/config/config_data.cc +++ b/src/rime/config/config_data.cc @@ -22,8 +22,12 @@ an ConvertFromYaml(const YAML::Node& yaml_node, void EmitYaml(an node, YAML::Emitter* emitter, int depth); ConfigData::~ConfigData() { - if (auto_save_ && modified_ && !file_path_.empty()) - SaveToFile(file_path_); + if (auto_save_) + Save(); +} + +bool ConfigData::Save() { + return modified_ && !file_path_.empty() && SaveToFile(file_path_); } bool ConfigData::LoadFromStream(std::istream& stream) { diff --git a/src/rime/config/config_data.h b/src/rime/config/config_data.h index fb1519be7..daec1b222 100644 --- a/src/rime/config/config_data.h +++ b/src/rime/config/config_data.h @@ -19,6 +19,8 @@ class ConfigData { ConfigData() = default; ~ConfigData(); + // returns whether actually saved to file. + bool Save(); bool LoadFromStream(std::istream& stream); bool SaveToStream(std::ostream& stream); bool LoadFromFile(const path& file_path, ConfigCompiler* compiler); From b095a28cf48e43df7e2ff4c26d4e4a0815913d59 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 2/3] 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); From 579a35106524ee1dd87ac5a8ad7adc17b1fa3881 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:48:06 +0800 Subject: [PATCH 3/3] chore(context): add debug log on option update --- src/rime/context.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rime/context.cc b/src/rime/context.cc index 77bc41bb3..34ca64523 100644 --- a/src/rime/context.cc +++ b/src/rime/context.cc @@ -288,6 +288,7 @@ void Context::set_input(const string& value) { void Context::set_option(const string& name, bool value) { options_[name] = value; + DLOG(INFO) << "Context::set_option " << name << " = " << value; option_update_notifier_(this, name); } @@ -313,8 +314,10 @@ string Context::get_property(const string& name) const { } void Context::ClearTransientOptions() { + DLOG(INFO) << "Context::ClearTransientOptions"; auto opt = options_.lower_bound("_"); while (opt != options_.end() && !opt->first.empty() && opt->first[0] == '_') { + DLOG(INFO) << "cleared opption: " << opt->first; options_.erase(opt++); } auto prop = properties_.lower_bound("_");