diff --git a/src/rimeaction.cpp b/src/rimeaction.cpp index 9b74d74..c19b05a 100644 --- a/src/rimeaction.cpp +++ b/src/rimeaction.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,24 @@ namespace fcitx { +namespace { + +std::optional optionValue(RimeEngine *engine, InputContext *ic, + bool requestSession, + const std::string &option) { + auto *state = engine->state(ic); + auto *api = engine->api(); + if (!state) { + return std::nullopt; + } + auto session = state->session(requestSession); + if (!session) { + return std::nullopt; + } + return bool(api->get_option(session, option.c_str())); +} +} // namespace + ToggleAction::ToggleAction(RimeEngine *engine, std::string_view schema, std::string_view option, std::string disabledText, std::string enabledText) @@ -42,38 +61,36 @@ void ToggleAction::activate(InputContext *ic) { } std::string ToggleAction::shortText(InputContext *ic) const { - auto *state = engine_->state(ic); - auto *api = engine_->api(); - if (!state) { + auto value = optionValue(engine_, ic, /*requestSession=*/true, option_); + if (!value.has_value()) { return ""; } - auto session = state->session(); - if (api->get_option(session, option_.c_str())) { + if (*value) { return stringutils::concat(enabledText_, " → ", disabledText_); } return stringutils::concat(disabledText_, " → ", enabledText_); } std::optional ToggleAction::snapshotOption(InputContext *ic) { - auto *state = engine_->state(ic); - auto *api = engine_->api(); - if (!state) { + auto value = optionValue(engine_, ic, /*requestSession=*/false, option_); + if (!value.has_value()) { return std::nullopt; } - auto session = state->session(false); - if (!session) { - return std::nullopt; - } - if (!api->get_option(session, option_.c_str())) { - return stringutils::concat("!", option_); - } - return option_; + return *value ? option_ : stringutils::concat("!", option_); } bool ToggleAction::checkOptionName(std::string_view name) const { return name == option_; } +std::string ToggleAction::optionLabel(InputContext *ic) { + auto value = optionValue(engine_, ic, /*requestSession=*/true, option_); + if (!value.has_value()) { + return ""; + } + return *value ? enabledText_ : disabledText_; +} + SelectAction::SelectAction(RimeEngine *engine, std::string_view schema, std::vector options, std::vector texts) @@ -140,4 +157,8 @@ std::optional SelectAction::snapshotOption(InputContext *ic) { bool SelectAction::checkOptionName(std::string_view name) const { return std::find(options_.begin(), options_.end(), name) != options_.end(); } + +std::string SelectAction::optionLabel(InputContext *ic) { + return shortText(ic); +} } // namespace fcitx diff --git a/src/rimeaction.h b/src/rimeaction.h index b9a5204..ea495b1 100644 --- a/src/rimeaction.h +++ b/src/rimeaction.h @@ -6,14 +6,13 @@ #ifndef _FCITX_RIMEACTION_H_ #define _FCITX_RIMEACTION_H_ -#include #include +#include #include #include #include #include #include -#include #include namespace fcitx { @@ -24,6 +23,8 @@ class RimeOptionAction : public Action { public: // This is used to save the option when we need to release the session. virtual std::optional snapshotOption(InputContext *ic) = 0; + // Return the label of current option. + virtual std::string optionLabel(InputContext* ic) = 0; // Check whether a option name belongs to the action. virtual bool checkOptionName(std::string_view name) const = 0; }; @@ -44,6 +45,8 @@ class ToggleAction : public RimeOptionAction { const std::string &option() const { return option_; } + std::string optionLabel(InputContext *ic) override; + bool checkOptionName(std::string_view name) const override; private: @@ -67,6 +70,8 @@ class SelectAction : public RimeOptionAction { const std::vector &options() const { return options_; } + std::string optionLabel(InputContext *ic) override; + bool checkOptionName(std::string_view name) const override; private: diff --git a/src/rimeengine.cpp b/src/rimeengine.cpp index e01d51f..94cd656 100644 --- a/src/rimeengine.cpp +++ b/src/rimeengine.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include #include diff --git a/src/rimestate.cpp b/src/rimestate.cpp index 6e9ef2d..dcb509d 100644 --- a/src/rimestate.cpp +++ b/src/rimestate.cpp @@ -584,18 +584,14 @@ void RimeState::showChangedOptions() { for (auto *action : actionList) { // Snapshot again, so SelectAction will return the current active value. - auto snapshot = action->snapshotOption(&ic_); - if (!snapshot) { + auto label = action->optionLabel(&ic_); + if (label.empty()) { continue; } - std::string_view option = *snapshot; - const bool state = extractOptionName(option); - auto label = engine_->api()->get_state_label_abbreviated( - session(), option.data(), state, true); - if (!label.str || label.length <= 0) { - continue; + if (!labels.empty()) { + labels.append("|"); } - labels.append(label.str, label.length); + labels.append(label); } if (!labels.empty()) { engine_->instance()->showCustomInputMethodInformation(&ic_, labels);