Skip to content

Commit

Permalink
fix(switcher): superfluously load saved options
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Mar 10, 2024
1 parent 12f460f commit 7892a4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
28 changes: 13 additions & 15 deletions src/rime/engine.cc
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
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
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

0 comments on commit 7892a4f

Please sign in to comment.