From 16923fda36a0e1f87683af297bd965a603e5fcfe Mon Sep 17 00:00:00 2001 From: LEOYoon-Tsaw Date: Sat, 20 Feb 2021 17:09:38 -0500 Subject: [PATCH 1/2] Better Option Update Display (cherry picked from commit f42b68a6fe10c0ab6663d4a41efc588979f853e9) --- src/rime/context.cc | 23 ++++++++++++++++++++++- src/rime/context.h | 3 +++ src/rime/engine.cc | 4 ++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/rime/context.cc b/src/rime/context.cc index 4d69bd122..902ee5606 100644 --- a/src/rime/context.cc +++ b/src/rime/context.cc @@ -247,9 +247,30 @@ void Context::set_input(const string& value) { update_notifier_(this); } +void Context::set_option_state(const string& name, int order, const string& value) { + option_states_[name][order] = value; +} + +string Context::get_option_state(const string& name, int order) { + if (option_states_.count(name) > 0) { + auto states = option_states_[name]; + if (states.size() == 1) { + return states[1]; + } else if (states.size() == 2) { + if ((order <= 1) && (order >= 0)) { + return states[order]; + } + } + } + return ""; +} + void Context::set_option(const string& name, bool value) { options_[name] = value; - option_update_notifier_(this, name); + const string& state = get_option_state(name, int(value)); + if (state != "") { + option_update_notifier_(this, state); + } } bool Context::get_option(const string& name) const { diff --git a/src/rime/context.h b/src/rime/context.h index d8bc285c8..657d72775 100644 --- a/src/rime/context.h +++ b/src/rime/context.h @@ -69,6 +69,8 @@ class Context { const CommitHistory& commit_history() const { return commit_history_; } void set_option(const string& name, bool value); + void set_option_state(const string& name, int order, const string& value); + string get_option_state(const string& name, int order); bool get_option(const string& name) const; void set_property(const string& name, const string& value); string get_property(const string& name) const; @@ -98,6 +100,7 @@ class Context { Composition composition_; CommitHistory commit_history_; map options_; + map> option_states_; map properties_; Notifier commit_notifier_; diff --git a/src/rime/engine.cc b/src/rime/engine.cc index 093dbf8b3..1a1069a9d 100644 --- a/src/rime/engine.cc +++ b/src/rime/engine.cc @@ -395,14 +395,18 @@ void ConcreteEngine::InitializeOptions() { continue; int value = 0; reset_value->GetInt(&value); + auto states = As(item->Get("states")); if (auto option_name = item->GetValue("name")) { // toggle + context_->set_option_state(option_name->str(), 0, states->GetValueAt(0)->str()); + context_->set_option_state(option_name->str(), 1, states->GetValueAt(1)->str()); context_->set_option(option_name->str(), (value != 0)); } else if (auto options = As(item->Get("options"))) { // radio for (size_t i = 0; i < options->size(); ++i) { if (auto option_name = options->GetValueAt(i)) { + context_->set_option_state(option_name->str(), 1, states->GetValueAt(i)->str()); context_->set_option(option_name->str(), static_cast(i) == value); } From f12abbf0f967fd4cc4937f998984af194c56a3d2 Mon Sep 17 00:00:00 2001 From: LEO Yoon-Tsaw Date: Thu, 7 Apr 2022 13:53:44 -0400 Subject: [PATCH 2/2] Fix bug in option update --- src/rime/context.cc | 6 +++++- src/rime/engine.cc | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/rime/context.cc b/src/rime/context.cc index 902ee5606..9bfcf86e7 100644 --- a/src/rime/context.cc +++ b/src/rime/context.cc @@ -255,7 +255,11 @@ string Context::get_option_state(const string& name, int order) { if (option_states_.count(name) > 0) { auto states = option_states_[name]; if (states.size() == 1) { - return states[1]; + if (order == 1) { + return states[0]; + } else { + return ""; + } } else if (states.size() == 2) { if ((order <= 1) && (order >= 0)) { return states[order]; diff --git a/src/rime/engine.cc b/src/rime/engine.cc index 1a1069a9d..86dfbedf8 100644 --- a/src/rime/engine.cc +++ b/src/rime/engine.cc @@ -406,7 +406,7 @@ void ConcreteEngine::InitializeOptions() { // radio for (size_t i = 0; i < options->size(); ++i) { if (auto option_name = options->GetValueAt(i)) { - context_->set_option_state(option_name->str(), 1, states->GetValueAt(i)->str()); + context_->set_option_state(option_name->str(), 0, states->GetValueAt(i)->str()); context_->set_option(option_name->str(), static_cast(i) == value); }