Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add config #6

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/hallelujah-addon.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version=@PROJECT_VERSION@
Library=libhallelujah
Type=SharedLibrary
OnDemand=True
Configurable=True

[Addon/Dependencies]
0=spell
1 change: 1 addition & 0 deletions src/hallelujah.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Icon=fcitx-hallelujah
Label=h
LangCode=en
Addon=hallelujah
Configurable=True
31 changes: 25 additions & 6 deletions src/hallelujah.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ class HallelujahCandidateWord : public CandidateWord {
}

void select(InputContext *inputContext) const override {
inputContext->commitString(text().toString());
auto config = static_cast<const HallelujahEngineConfig *>(
state_->engine()->getConfig());
auto word = text().toString();
if (*config->commitWithSpace) {
word += " ";
}
inputContext->commitString(word);
state_->reset(inputContext);
}

Expand Down Expand Up @@ -205,12 +211,17 @@ void HallelujahState::keyEvent(KeyEvent &event) {
std::copy(userInput.begin(), userInput.end(), word.begin());
}
if (iter != words_->end()) {
auto ipa = iter->second.ipa_;
if (!ipa.empty()) {
ipa = fmt::format("[{}] ", ipa);
auto config = static_cast<const HallelujahEngineConfig *>(
engine_->getConfig());
std::string comment = *config->showIPA ? iter->second.ipa_ : "";
if (!comment.empty()) {
comment = fmt::format("[{}] ", comment);
}
if (*config->showTranslation) {
comment += fmt::format(
"{}", fmt::join(iter->second.translation_, " "));
}
comments.emplace_back(fmt::format(
"{}{}", ipa, fmt::join(iter->second.translation_, " ")));
comments.emplace_back(std::move(comment));
} else {
comments.emplace_back("");
}
Expand Down Expand Up @@ -326,6 +337,14 @@ void HallelujahEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
auto *state = ic->propertyFor(&factory_);
state->keyEvent(keyEvent);
}

void HallelujahEngine::reloadConfig() { readAsIni(config_, ConfPath); }

void HallelujahEngine::setConfig(const RawConfig &config) {
config_.load(config, true);
safeSaveAsIni(config_, ConfPath);
reloadConfig();
}
} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::HallelujahFactory);
15 changes: 15 additions & 0 deletions src/hallelujah.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef _FCITX5_HALLELUJAH_HALLELUJAH_H_
#define _FCITX5_HALLELUJAH_HALLELUJAH_H_

#include <fcitx-config/iniparser.h>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/inputbuffer.h>
#include <fcitx/addonfactory.h>
Expand All @@ -13,6 +14,14 @@
#include <unordered_map>

namespace fcitx {
FCITX_CONFIGURATION(HallelujahEngineConfig,
Option<bool> showIPA{this, "ShowIPA", _("Show IPA"), true};
Option<bool> showTranslation{this, "ShowTranslation",
_("Show translation"), true};
Option<bool> commitWithSpace{this, "CommitWithSpace",
_("Commit with space"),
false};);

struct HallelujahWord {
HallelujahWord(const std::vector<std::string> &translation,
const std::string &ipa, int frequency)
Expand All @@ -36,6 +45,7 @@ class HallelujahState : public InputContextProperty {
void updateUI(InputContext *ic, const std::vector<std::string> &words,
const std::vector<std::string> &candidates);
void reset(InputContext *ic);
HallelujahEngine *engine() { return engine_; }

private:
HallelujahEngine *engine_;
Expand All @@ -54,6 +64,9 @@ class HallelujahEngine final : public InputMethodEngine {

void keyEvent(const InputMethodEntry &entry, KeyEvent &keyEvent) override;
void reset(const InputMethodEntry &, InputContextEvent &event) override;
const Configuration *getConfig() const override { return &config_; }
void setConfig(const RawConfig &config) override;
void reloadConfig() override;
FCITX_ADDON_DEPENDENCY_LOADER(spell, instance_->addonManager());

private:
Expand All @@ -62,10 +75,12 @@ class HallelujahEngine final : public InputMethodEngine {
void loadPinyin();

Instance *instance_;
HallelujahEngineConfig config_;
FactoryFor<HallelujahState> factory_;
marisa::Trie trie_;
std::unordered_map<std::string, HallelujahWord> words_;
std::unordered_map<std::string, std::vector<std::string>> pinyin_;
static const inline std::string ConfPath = "conf/hallelujah.conf";
};

class HallelujahFactory : public AddonFactory {
Expand Down