| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _PUNCTUATION_PUNCTUATION_H_ |
| 8 |
#define _PUNCTUATION_PUNCTUATION_H_ |
| 9 |
|
| 10 |
#include "punctuation_public.h" |
| 11 |
#include <cstdint> |
| 12 |
#include <fcitx-config/configuration.h> |
| 13 |
#include <fcitx-config/enum.h> |
| 14 |
#include <fcitx-config/iniparser.h> |
| 15 |
#include <fcitx-config/option.h> |
| 16 |
#include <fcitx-config/rawconfig.h> |
| 17 |
#include <fcitx-utils/handlertable.h> |
| 18 |
#include <fcitx-utils/i18n.h> |
| 19 |
#include <fcitx-utils/key.h> |
| 20 |
#include <fcitx-utils/signals.h> |
| 21 |
#include <fcitx/action.h> |
| 22 |
#include <fcitx/addonfactory.h> |
| 23 |
#include <fcitx/addoninstance.h> |
| 24 |
#include <fcitx/addonmanager.h> |
| 25 |
#include <fcitx/inputcontext.h> |
| 26 |
#include <fcitx/inputcontextproperty.h> |
| 27 |
#include <fcitx/instance.h> |
| 28 |
#include <istream> |
| 29 |
#include <memory> |
| 30 |
#include <string> |
| 31 |
#include <string_view> |
| 32 |
#include <unordered_map> |
| 33 |
#include <utility> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
FCITX_CONFIGURATION( |
| 37 |
PunctuationConfig, |
| 38 |
fcitx::Option<fcitx::KeyList> hotkey{ |
| 39 |
this, "Hotkey", _("Toggle key"), {fcitx::Key("Control+period")}}; |
| 40 |
fcitx::Option<bool> halfWidthPuncAfterLatinOrNumber{ |
| 41 |
this, "HalfWidthPuncAfterLetterOrNumber", |
| 42 |
_("Half width punctuation after latin letter or number"), true}; |
| 43 |
fcitx::Option<bool> typePairedPunctuationTogether{ |
| 44 |
this, "TypePairedPunctuationsTogether", |
| 45 |
_("Type paired punctuations together (e.g. Quote)"), false}; |
| 46 |
fcitx::HiddenOption<bool> enabled{this, "Enabled", "Enabled", true};); |
| 47 |
|
| 48 |
FCITX_CONFIGURATION( |
| 49 |
PunctuationMapEntryConfig, |
| 50 |
fcitx::Option<std::string> key{ |
| 51 |
this, "Key", C_("Key of the punctuation, e.g. comma", "Key")}; |
| 52 |
fcitx::Option<std::string> mapResult1{this, "Mapping", _("Mapping")}; |
| 53 |
fcitx::Option<std::string> mapResult2{this, "AltMapping", |
| 54 |
_("Alternative Mapping")};) |
| 55 |
|
| 56 |
FCITX_CONFIGURATION( |
| 57 |
PunctuationMapConfig, |
| 58 |
fcitx::OptionWithAnnotation<std::vector<PunctuationMapEntryConfig>, |
| 59 |
fcitx::ListDisplayOptionAnnotation> |
| 60 |
entries{this, |
| 61 |
"Entries", |
| 62 |
_("Entries"), |
| 63 |
{}, |
| 64 |
{}, |
| 65 |
{}, |
| 66 |
fcitx::ListDisplayOptionAnnotation("Key")};); |
| 67 |
|
| 68 |
class PunctuationProfile { |
| 69 |
public: |
| 70 |
PunctuationProfile() = default; |
| 71 |
PunctuationProfile(const PunctuationProfile &) = delete; |
| 72 |
|
| 73 |
void loadSystem(std::istream &in); |
| 74 |
void load(std::istream &in); |
| 75 |
void set(const fcitx::RawConfig &config); |
| 76 |
void save(std::string_view name) const; |
| 77 |
void resetDefaultValue(); |
| 78 |
|
| 79 |
const std::pair<std::string, std::string> & |
| 80 |
getPunctuation(uint32_t unicode) const; |
| 81 |
std::vector<std::string> getPunctuations(uint32_t unicode) const; |
| 82 |
PunctuationMapConfig &config() { return punctuationMapConfig_; } |
| 83 |
const PunctuationMapConfig &config() const { return punctuationMapConfig_; } |
| 84 |
|
| 85 |
static constexpr std::string_view profilePrefix = "punc.mb."; |
| 86 |
|
| 87 |
private: |
| 88 |
void addEntry(uint32_t key, const std::string &value, |
| 89 |
const std::string &value2); |
| 90 |
std::unordered_map<uint32_t, |
| 91 |
std::vector<std::pair<std::string, std::string>>> |
| 92 |
puncMap_; |
| 93 |
PunctuationMapConfig punctuationMapConfig_; |
| 94 |
}; |
| 95 |
|
| 96 |
class PunctuationState; |
| 97 |
|
| 98 |
class Punctuation final : public fcitx::AddonInstance { |
| 99 |
class ToggleAction : public fcitx::Action { |
| 100 |
public: |
| 101 |
ToggleAction(Punctuation *parent) : parent_(parent) {} |
| 102 |
|
| 103 |
std::string shortText(fcitx::InputContext *) const override { |
| 104 |
return parent_->enabled() ? _("Full width punctuation") |
| 105 |
: _("Half width punctuation"); |
| 106 |
} |
| 107 |
std::string icon(fcitx::InputContext *) const override { |
| 108 |
return parent_->enabled() ? "fcitx-punc-active" |
| 109 |
: "fcitx-punc-inactive"; |
| 110 |
} |
| 111 |
|
| 112 |
void activate(fcitx::InputContext *ic) override { |
| 113 |
return parent_->setEnabled(!parent_->enabled(), ic); |
| 114 |
} |
| 115 |
|
| 116 |
private: |
| 117 |
Punctuation *parent_; |
| 118 |
}; |
| 119 |
|
| 120 |
public: |
| 121 |
Punctuation(fcitx::Instance *instance); |
| 122 |
~Punctuation(); |
| 123 |
|
| 124 |
const std::pair<std::string, std::string> & |
| 125 |
getPunctuation(const std::string &language, uint32_t unicode); |
| 126 |
std::vector<std::string> getPunctuations(const std::string &language, |
| 127 |
uint32_t unicode); |
| 128 |
const std::string &pushPunctuation(const std::string &language, |
| 129 |
fcitx::InputContext *ic, |
| 130 |
uint32_t unicode); |
| 131 |
std::pair<std::string, std::string> |
| 132 |
pushPunctuationV2(const std::string &language, fcitx::InputContext *ic, |
| 133 |
uint32_t unicode); |
| 134 |
const std::string &cancelLast(const std::string &language, |
| 135 |
fcitx::InputContext *ic); |
| 136 |
std::vector<std::string> |
| 137 |
getPunctuationCandidates(const std::string &language, uint32_t unicode); |
| 138 |
|
| 139 |
void reloadConfig() override; |
| 140 |
void save() override { |
| 141 |
fcitx::safeSaveAsIni(config_, "conf/punctuation.conf"); |
| 142 |
} |
| 143 |
const fcitx::Configuration *getConfig() const override { return &config_; } |
| 144 |
void setConfig(const fcitx::RawConfig &config) override { |
| 145 |
config_.load(config, true); |
| 146 |
fcitx::safeSaveAsIni(config_, "conf/punctuation.conf"); |
| 147 |
toggleAction_.setHotkey(config_.hotkey.value()); |
| 148 |
} |
| 149 |
const fcitx::Configuration * |
| 150 |
getSubConfig(const std::string &path) const override; |
| 151 |
void setSubConfig(const std::string &path, |
| 152 |
const fcitx::RawConfig &config) override; |
| 153 |
|
| 154 |
FCITX_ADDON_EXPORT_FUNCTION(Punctuation, getPunctuation); |
| 155 |
FCITX_ADDON_EXPORT_FUNCTION(Punctuation, pushPunctuation); |
| 156 |
FCITX_ADDON_EXPORT_FUNCTION(Punctuation, pushPunctuationV2); |
| 157 |
FCITX_ADDON_EXPORT_FUNCTION(Punctuation, cancelLast); |
| 158 |
FCITX_ADDON_EXPORT_FUNCTION(Punctuation, getPunctuationCandidates) |
| 159 |
|
| 160 |
bool enabled() const { return *config_.enabled; } |
| 161 |
void setEnabled(bool enabled, fcitx::InputContext *ic) { |
| 162 |
if (enabled != *config_.enabled) { |
| 163 |
config_.enabled.setValue(enabled); |
| 164 |
toggleAction_.update(ic); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
bool inWhiteList(fcitx::InputContext *inputContext) const; |
| 169 |
|
| 170 |
private: |
| 171 |
void loadProfiles(); |
| 172 |
|
| 173 |
FCITX_ADDON_DEPENDENCY_LOADER(notifications, instance_->addonManager()); |
| 174 |
|
| 175 |
fcitx::Instance *instance_; |
| 176 |
fcitx::FactoryFor<PunctuationState> factory_; |
| 177 |
fcitx::ScopedConnection commitConn_, keyEventConn_; |
| 178 |
std::vector<std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>>> |
| 179 |
eventWatchers_; |
| 180 |
std::unordered_map<std::string, PunctuationProfile> profiles_; |
| 181 |
PunctuationConfig config_; |
| 182 |
ToggleAction toggleAction_{this}; |
| 183 |
}; |
| 184 |
|
| 185 |
class PunctuationFactory : public fcitx::AddonFactory { |
| 186 |
public: |
| 187 |
fcitx::AddonInstance *create(fcitx::AddonManager *manager) override { |
| 188 |
return new Punctuation(manager->instance()); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
#endif // _PUNCTUATION_PUNCTUATION_H_ |