| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#ifndef _FULLWIDTH_FULLWIDTH_H_ |
| 8 |
#define _FULLWIDTH_FULLWIDTH_H_ |
| 9 |
|
| 10 |
#include <fcitx-config/configuration.h> |
| 11 |
#include <fcitx-config/iniparser.h> |
| 12 |
#include <fcitx-utils/i18n.h> |
| 13 |
#include <fcitx/action.h> |
| 14 |
#include <fcitx/addoninstance.h> |
| 15 |
#include <fcitx/addonmanager.h> |
| 16 |
#include <fcitx/instance.h> |
| 17 |
|
| 18 |
FCITX_CONFIGURATION(FullWidthConfig, fcitx::Option<fcitx::KeyList> hotkey{ |
| 19 |
this, "Hotkey", _("Toggle key")};) |
| 20 |
|
| 21 |
class ToggleAction; |
| 22 |
|
| 23 |
class Fullwidth final : public fcitx::AddonInstance { |
| 24 |
class ToggleAction : public fcitx::Action { |
| 25 |
public: |
| 26 |
ToggleAction(Fullwidth *parent) : parent_(parent) {} |
| 27 |
|
| 28 |
std::string shortText(fcitx::InputContext *) const override { |
| 29 |
return parent_->enabled_ ? _("Full width Character") |
| 30 |
: _("Half width Character"); |
| 31 |
} |
| 32 |
std::string icon(fcitx::InputContext *) const override { |
| 33 |
return parent_->enabled_ ? "fcitx-fullwidth-active" |
| 34 |
: "fcitx-fullwidth-inactive"; |
| 35 |
} |
| 36 |
|
| 37 |
void activate(fcitx::InputContext *ic) override { |
| 38 |
return parent_->setEnabled(!parent_->enabled_, ic); |
| 39 |
} |
| 40 |
|
| 41 |
private: |
| 42 |
Fullwidth *parent_; |
| 43 |
}; |
| 44 |
|
| 45 |
public: |
| 46 |
Fullwidth(fcitx::Instance *instance); |
| 47 |
|
| 48 |
void reloadConfig() override; |
| 49 |
const fcitx::Configuration *getConfig() const override { return &config_; } |
| 50 |
void setConfig(const fcitx::RawConfig &config) override { |
| 51 |
config_.load(config, true); |
| 52 |
fcitx::safeSaveAsIni(config_, "conf/fullwidth.conf"); |
| 53 |
toggleAction_.setHotkey(config_.hotkey.value()); |
| 54 |
} |
| 55 |
|
| 56 |
FCITX_ADDON_DEPENDENCY_LOADER(notifications, instance_->addonManager()); |
| 57 |
|
| 58 |
bool inWhiteList(fcitx::InputContext *inputContext) const; |
| 59 |
|
| 60 |
void setEnabled(bool enabled, fcitx::InputContext *ic) { |
| 61 |
if (enabled != enabled_) { |
| 62 |
enabled_ = enabled; |
| 63 |
toggleAction_.update(ic); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
private: |
| 68 |
bool enabled_ = false; |
| 69 |
fcitx::Instance *instance_; |
| 70 |
FullWidthConfig config_; |
| 71 |
std::vector<std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>>> |
| 72 |
eventHandlers_; |
| 73 |
fcitx::ScopedConnection commitFilterConn_; |
| 74 |
std::unordered_set<std::string> whiteList_; |
| 75 |
ToggleAction toggleAction_{this}; |
| 76 |
}; |
| 77 |
|
| 78 |
#endif // _FULLWIDTH_FULLWIDTH_H_ |