From ce778352b907ae322f1949f66841ac164c2db634 Mon Sep 17 00:00:00 2001 From: groverlynn Date: Sat, 27 Apr 2024 18:46:49 +0800 Subject: [PATCH] fix(switch_translator): skip switches with no state labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #866 Closes #640 Squashed commit of the following: commit e3addcfed7dc1c0c802fc55fcbb94727513dbab8 Author: groverlynn Date: Tue Mar 28 01:07:39 2023 +0200 Skip switches with no state labels --------- Co-authored-by: 居戎氏 --- src/rime/gear/switch_translator.cc | 14 ++++++++++++-- src/rime/switches.h | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/rime/gear/switch_translator.cc b/src/rime/gear/switch_translator.cc index ffaa3cfb4d..831a3cd454 100644 --- a/src/rime/gear/switch_translator.cc +++ b/src/rime/gear/switch_translator.cc @@ -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) @@ -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(option, current_state, @@ -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); } } diff --git a/src/rime/switches.h b/src/rime/switches.h index cbe424b6fb..b2de5fcfa8 100644 --- a/src/rime/switches.h +++ b/src/rime/switches.h @@ -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 {