Skip to content

Commit 217c72b

Browse files
committed
feat(switcher): enable schema in cases where conditions are met
1 parent 3b5dbf6 commit 217c72b

File tree

3 files changed

+96
-45
lines changed

3 files changed

+96
-45
lines changed

src/rime/gear/schema_list_translator.cc

+17-24
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ void SchemaListTranslation::LoadSchemaList(Switcher* switcher) {
9898
Config* config = switcher->schema()->config();
9999
if (!config)
100100
return;
101-
auto schema_list = config->GetList("schema_list");
102-
if (!schema_list)
103-
return;
104101
// current schema comes first
105102
Schema* current_schema = engine->schema();
106103
if (current_schema) {
@@ -110,27 +107,23 @@ void SchemaListTranslation::LoadSchemaList(Switcher* switcher) {
110107
size_t fixed = candies_.size();
111108
time_t now = time(NULL);
112109
// load the rest schema list
113-
for (size_t i = 0; i < schema_list->size(); ++i) {
114-
auto item = As<ConfigMap>(schema_list->GetAt(i));
115-
if (!item)
116-
continue;
117-
auto schema_property = item->GetValue("schema");
118-
if (!schema_property)
119-
continue;
120-
const string& schema_id(schema_property->str());
121-
if (current_schema && schema_id == current_schema->schema_id())
122-
continue;
123-
Schema schema(schema_id);
124-
auto cand = New<SchemaSelection>(&schema);
125-
int timestamp = 0;
126-
if (user_config &&
127-
user_config->GetInt("var/schema_access_time/" + schema_id,
128-
&timestamp)) {
129-
if (timestamp <= now)
130-
cand->set_quality(timestamp);
131-
}
132-
Append(cand);
133-
}
110+
Switcher::ForEachSchemaListEntry(
111+
config,
112+
[this, current_schema, user_config, now](const string& schema_id) {
113+
if (current_schema && schema_id == current_schema->schema_id())
114+
return /* continue = */true;
115+
Schema schema(schema_id);
116+
auto cand = New<SchemaSelection>(&schema);
117+
int timestamp = 0;
118+
if (user_config &&
119+
user_config->GetInt("var/schema_access_time/" + schema_id,
120+
&timestamp)) {
121+
if (timestamp <= now)
122+
cand->set_quality(timestamp);
123+
}
124+
Append(cand);
125+
return /* continue = */true;
126+
});
134127
DLOG(INFO) << "num schemata: " << candies_.size();
135128
bool fix_order = false;
136129
config->GetBool("switcher/fix_schema_list_order", &fix_order);

src/rime/switcher.cc

+75-21
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,88 @@ void Switcher::HighlightNextSchema() {
108108
return;
109109
}
110110

111+
/*
112+
Example configuration:
113+
114+
```yaml
115+
schema_list:
116+
- case: [mode/wubi, mode/wubi_pinyin]
117+
schema: wubi_pinyin
118+
- case: [mode/wubi]
119+
schema: wubi86
120+
- case: [mode/default]
121+
schema: pinyin
122+
123+
mode:
124+
wubi: false
125+
wubi_pinyin: false
126+
default: true
127+
```
128+
*/
129+
130+
static an<ConfigValue> ParseSchemaListEntry(Config* config,
131+
an<ConfigMap> entry_map) {
132+
if (!entry_map)
133+
return nullptr;
134+
auto schema_property = entry_map->GetValue("schema");
135+
if (!schema_property)
136+
return nullptr;
137+
if (auto case_conditions = As<ConfigList>(entry_map->Get("case"))) {
138+
for (auto iter = case_conditions->begin();
139+
iter != case_conditions->end();
140+
++iter) {
141+
if (auto condition_variable = As<ConfigValue>(*iter)) {
142+
bool condition_met = false;
143+
if (!config->GetBool(condition_variable->str(), &condition_met) ||
144+
!condition_met) {
145+
return nullptr;
146+
}
147+
}
148+
}
149+
}
150+
return schema_property;
151+
}
152+
153+
int Switcher::ForEachSchemaListEntry(
154+
Config* config, function<bool (const string& schema_id)> process_entry) {
155+
auto schema_list = config->GetList("schema_list");
156+
if (!schema_list)
157+
return 0;
158+
int num_processed_entries = 0;
159+
for (auto iter = schema_list->begin(); iter != schema_list->end(); ++iter) {
160+
auto entry = ParseSchemaListEntry(config, As<ConfigMap>(*iter));
161+
if (!entry)
162+
continue;
163+
const string& schema_id = entry->str();
164+
++num_processed_entries;
165+
if (!process_entry(schema_id))
166+
break;
167+
}
168+
return num_processed_entries;
169+
}
170+
111171
Schema* Switcher::CreateSchema() {
112172
Config* config = schema_->config();
113173
if (!config)
114-
return NULL;
115-
auto schema_list = config->GetList("schema_list");
116-
if (!schema_list)
117-
return NULL;
174+
return nullptr;
118175
string previous;
119-
if (user_config_) {
176+
if (user_config_ && !fix_schema_list_order_) {
120177
user_config_->GetString("var/previously_selected_schema", &previous);
121178
}
122179
string recent;
123-
for (size_t i = 0; i < schema_list->size(); ++i) {
124-
auto item = As<ConfigMap>(schema_list->GetAt(i));
125-
if (!item)
126-
continue;
127-
auto schema_property = item->GetValue("schema");
128-
if (!schema_property)
129-
continue;
130-
const string& schema_id(schema_property->str());
131-
if (previous.empty() || previous == schema_id) {
132-
recent = schema_id;
133-
break;
134-
}
135-
if (recent.empty())
136-
recent = schema_id;
137-
}
180+
ForEachSchemaListEntry(
181+
config,
182+
[&previous, &recent](const string& schema_id) {
183+
if (previous.empty() || previous == schema_id) {
184+
recent = schema_id;
185+
return /* continue = */false;
186+
}
187+
if (recent.empty())
188+
recent = schema_id;
189+
return /* continue = */true;
190+
});
138191
if (recent.empty())
139-
return NULL;
192+
return nullptr;
140193
else
141194
return new Schema(recent);
142195
}
@@ -225,6 +278,7 @@ void Switcher::LoadSettings() {
225278
}
226279
}
227280
config->GetBool("switcher/fold_options", &fold_options_);
281+
config->GetBool("switcher/fix_schema_list_order", &fix_schema_list_order_);
228282
}
229283

230284
void Switcher::InitializeComponents() {

src/rime/switcher.h

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class Switcher : public Processor, public Engine {
2727
}
2828
virtual ProcessResult ProcessKeyEvent(const KeyEvent& key_event);
2929

30+
static int ForEachSchemaListEntry(
31+
Config* config, function<bool (const string& schema_id)> process_entry);
32+
3033
Schema* CreateSchema();
3134
void SelectNextSchema();
3235
bool IsAutoSave(const string& option) const;
@@ -51,6 +54,7 @@ class Switcher : public Processor, public Engine {
5154
vector<KeyEvent> hotkeys_;
5255
set<string> save_options_;
5356
bool fold_options_ = false;
57+
bool fix_schema_list_order_ = false;
5458

5559
vector<of<Processor>> processors_;
5660
vector<of<Translator>> translators_;

0 commit comments

Comments
 (0)