Skip to content

Commit ce77835

Browse files
groverlynnlotem
andcommitted
fix(switch_translator): skip switches with no state labels
Fixes #866 Closes #640 Squashed commit of the following: commit e3addcf Author: groverlynn <groverlynn@gmail.com> Date: Tue Mar 28 01:07:39 2023 +0200 Skip switches with no state labels --------- Co-authored-by: 居戎氏 <chen.sst@gmail.com>
1 parent 527fbcc commit ce77835

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/rime/gear/switch_translator.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ inline static string get_state_label(const SwitchOption& option,
3030
Switches::GetStateLabel(option.the_switch, state_index, abbreviate));
3131
}
3232

33+
inline static bool has_state_label(const SwitchOption& option,
34+
size_t state_index) {
35+
return bool(Switches::GetStateLabel(option.the_switch, state_index, false));
36+
}
37+
3338
class Switch : public SimpleCandidate, public SwitcherCommand {
3439
public:
3540
Switch(const SwitchOption& option, bool current_state, bool auto_save)
@@ -208,6 +213,9 @@ void SwitchTranslation::LoadSwitches(Switcher* switcher) {
208213
switches.FindOption(
209214
[this, switcher, context,
210215
&groups](Switches::SwitchOption option) -> Switches::FindResult {
216+
if (!has_state_label(option, 0)) {
217+
return Switches::kContinue;
218+
}
211219
if (option.type == Switches::kToggleOption) {
212220
bool current_state = context->get_option(option.option_name);
213221
Append(New<Switch>(option, current_state,
@@ -234,9 +242,11 @@ void SwitchTranslation::LoadSwitches(Switcher* switcher) {
234242
Switches::SwitchOption option) -> Switches::FindResult {
235243
bool current_state = context->get_option(option.option_name);
236244
if (option.type == Switches::kToggleOption) {
237-
folded_options->Append(option, current_state);
245+
if (has_state_label(option, current_state)) {
246+
folded_options->Append(option, current_state);
247+
}
238248
} else if (option.type == Switches::kRadioGroup) {
239-
if (current_state) {
249+
if (current_state && has_state_label(option, option.option_index)) {
240250
folded_options->Append(option, option.option_index);
241251
}
242252
}

src/rime/switches.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ struct StringSlice {
1717
operator string() const {
1818
return str && length ? string(str, length) : string();
1919
}
20+
21+
operator bool() const { return bool(str) && bool(length); }
2022
};
2123

2224
class Switches {

0 commit comments

Comments
 (0)