@@ -108,35 +108,88 @@ void Switcher::HighlightNextSchema() {
108
108
return ;
109
109
}
110
110
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
+
111
171
Schema* Switcher::CreateSchema () {
112
172
Config* config = schema_->config ();
113
173
if (!config)
114
- return NULL ;
115
- auto schema_list = config->GetList (" schema_list" );
116
- if (!schema_list)
117
- return NULL ;
174
+ return nullptr ;
118
175
string previous;
119
- if (user_config_) {
176
+ if (user_config_ && !fix_schema_list_order_ ) {
120
177
user_config_->GetString (" var/previously_selected_schema" , &previous);
121
178
}
122
179
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
+ });
138
191
if (recent.empty ())
139
- return NULL ;
192
+ return nullptr ;
140
193
else
141
194
return new Schema (recent);
142
195
}
@@ -225,6 +278,7 @@ void Switcher::LoadSettings() {
225
278
}
226
279
}
227
280
config->GetBool (" switcher/fold_options" , &fold_options_);
281
+ config->GetBool (" switcher/fix_schema_list_order" , &fix_schema_list_order_);
228
282
}
229
283
230
284
void Switcher::InitializeComponents () {
0 commit comments