Skip to content

Commit

Permalink
fix(switch_translator): skip switches with no state labels
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
groverlynn and lotem committed Apr 27, 2024
1 parent 527fbcc commit ce77835
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/rime/gear/switch_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ inline static string get_state_label(const SwitchOption& option,
Switches::GetStateLabel(option.the_switch, state_index, abbreviate));
}

inline static bool has_state_label(const SwitchOption& option,
size_t state_index) {
return bool(Switches::GetStateLabel(option.the_switch, state_index, false));
}

class Switch : public SimpleCandidate, public SwitcherCommand {
public:
Switch(const SwitchOption& option, bool current_state, bool auto_save)
Expand Down Expand Up @@ -208,6 +213,9 @@ void SwitchTranslation::LoadSwitches(Switcher* switcher) {
switches.FindOption(
[this, switcher, context,
&groups](Switches::SwitchOption option) -> Switches::FindResult {
if (!has_state_label(option, 0)) {
return Switches::kContinue;
}
if (option.type == Switches::kToggleOption) {
bool current_state = context->get_option(option.option_name);
Append(New<Switch>(option, current_state,
Expand All @@ -234,9 +242,11 @@ void SwitchTranslation::LoadSwitches(Switcher* switcher) {
Switches::SwitchOption option) -> Switches::FindResult {
bool current_state = context->get_option(option.option_name);
if (option.type == Switches::kToggleOption) {
folded_options->Append(option, current_state);
if (has_state_label(option, current_state)) {
folded_options->Append(option, current_state);
}
} else if (option.type == Switches::kRadioGroup) {
if (current_state) {
if (current_state && has_state_label(option, option.option_index)) {
folded_options->Append(option, option.option_index);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/rime/switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ struct StringSlice {
operator string() const {
return str && length ? string(str, length) : string();
}

operator bool() const { return bool(str) && bool(length); }
};

class Switches {
Expand Down

1 comment on commit ce77835

@ijliym
Copy link

@ijliym ijliym commented on ce77835 Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Please sign in to comment.