Skip to content

Commit

Permalink
Use option label from action instead of get_state_label to support ol…
Browse files Browse the repository at this point in the history
…der librime

This also change the label to the full version, which would still look
ok.
  • Loading branch information
wengxt committed Jun 9, 2024
1 parent 5cfb554 commit 2c471c6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
53 changes: 37 additions & 16 deletions src/rimeaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstddef>
#include <fcitx-utils/stringutils.h>
#include <fcitx/action.h>
#include <fcitx/inputcontext.h>
#include <fcitx/userinterfacemanager.h>
#include <optional>
#include <rime_api.h>
Expand All @@ -19,6 +20,24 @@

namespace fcitx {

namespace {

std::optional<bool> 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)
Expand All @@ -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<std::string> 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<std::string> options,
std::vector<std::string> texts)
Expand Down Expand Up @@ -140,4 +157,8 @@ std::optional<std::string> 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
9 changes: 7 additions & 2 deletions src/rimeaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
#ifndef _FCITX_RIMEACTION_H_
#define _FCITX_RIMEACTION_H_

#include <cstddef>
#include <fcitx/action.h>
#include <fcitx/inputcontext.h>
#include <fcitx/menu.h>
#include <list>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace fcitx {
Expand All @@ -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<std::string> 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;
};
Expand All @@ -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:
Expand All @@ -67,6 +70,8 @@ class SelectAction : public RimeOptionAction {

const std::vector<std::string> &options() const { return options_; }

std::string optionLabel(InputContext *ic) override;

bool checkOptionName(std::string_view name) const override;

private:
Expand Down
1 change: 0 additions & 1 deletion src/rimeengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <fcitx/userinterfacemanager.h>
#include <list>
#include <memory>
#include <optional>
#include <rime_api.h>
#include <stdexcept>
#include <string>
Expand Down
14 changes: 5 additions & 9 deletions src/rimestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2c471c6

Please sign in to comment.