Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(switcher): superfluously load saved options #842

Merged
merged 3 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/rime/config/config_component.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Config::~Config() {}

Config::Config(an<ConfigData> data) : ConfigItemRef(data.get()), data_(data) {}

bool Config::Save() {
return data_->Save();
}

bool Config::LoadFromStream(std::istream& stream) {
return data_->LoadFromStream(stream);
}
Expand Down
2 changes: 2 additions & 0 deletions src/rime/config/config_component.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Config : public Class<Config, const string&>, public ConfigItemRef {
// in the ConfigComponent
explicit Config(an<ConfigData> 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);
Expand Down
8 changes: 6 additions & 2 deletions src/rime/config/config_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ an<ConfigItem> ConvertFromYaml(const YAML::Node& yaml_node,
void EmitYaml(an<ConfigItem> 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) {
Expand Down
2 changes: 2 additions & 0 deletions src/rime/config/config_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/rime/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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("_");
Expand Down
28 changes: 13 additions & 15 deletions src/rime/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class ConcreteEngine : public Engine {
vector<of<Filter>> filters_;
vector<of<Formatter>> formatters_;
vector<of<Processor>> post_processors_;
// To make sure dumping user.yaml when processors_.clear(),
// switcher is owned by processors_[0]
weak<Switcher> switcher_;
an<Switcher> switcher_;
};

// implementations
Expand Down Expand Up @@ -85,6 +83,11 @@ ConcreteEngine::ConcreteEngine() {
[this](Context* ctx, const string& property) {
OnPropertyUpdate(ctx, property);
});

switcher_ = New<Switcher>(this);
// saved options should be loaded only once per input session
switcher_->RestoreSavedOptions();

InitializeComponents();
InitializeOptions();
}
Expand Down Expand Up @@ -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();
Expand All @@ -303,11 +299,10 @@ void ConcreteEngine::InitializeComponents() {
translators_.clear();
filters_.clear();

if (auto switcher = New<Switcher>(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);
}
}
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 9 additions & 1 deletion src/rime/switcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/rime/switcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class Switcher : public Processor, public Engine {
Config* config,
function<bool(const string& schema_id)> process_entry);

void SetActiveSchema(const string& schema_id);
Schema* CreateSchema();
void SelectNextSchema();
bool IsAutoSave(const string& option) const;
void RestoreSavedOptions();

void RefreshMenu();
void Activate();
Expand All @@ -46,7 +48,7 @@ class Switcher : public Processor, public Engine {
protected:
void InitializeComponents();
void LoadSettings();
void RestoreSavedOptions();

void HighlightNextSchema();
void OnSelect(Context* ctx);

Expand Down