7 #ifndef _FCITX_CONFIG_OPTION_H_ 8 #define _FCITX_CONFIG_OPTION_H_ 10 #include "fcitxconfig_export.h" 14 #include <type_traits> 15 #include <fcitx-config/marshallfunction.h> 16 #include <fcitx-config/option_details.h> 17 #include <fcitx-config/optiontypename.h> 18 #include <fcitx-config/rawconfig.h> 26 std::string description, std::string uri);
28 std::string typeString()
const override;
29 void reset()
override;
30 bool isDefault()
const override;
32 void marshall(
RawConfig &config)
const override;
33 bool unmarshall(
const RawConfig &config,
bool partial)
override;
34 std::unique_ptr<Configuration> subConfigSkeleton()
const override;
36 bool equalTo(
const OptionBase &other)
const override;
37 void copyFrom(
const OptionBase &other)
override;
39 bool skipDescription()
const override;
40 bool skipSave()
const override;
41 void dumpDescription(
RawConfig &config)
const override;
44 std::string externalUri_;
50 using ExternalOption::ExternalOption;
51 void dumpDescription(
RawConfig &config)
const override;
58 bool check(
const T &)
const {
return true; }
59 void dumpDescription(
RawConfig &)
const {}
64 bool skipDescription() {
return false; }
65 bool skipSave() {
return false; }
66 void dumpDescription(
RawConfig &)
const {}
73 bool skipDescription() {
return false; }
74 bool skipSave() {
return false; }
75 void dumpDescription(
RawConfig &config)
const {
76 config.setValueByPath(
"Tooltip", tooltip_);
86 : option_(std::move(option)) {}
88 bool skipDescription() {
return false; }
89 bool skipSave() {
return false; }
90 void dumpDescription(
RawConfig &config)
const {
91 config.setValueByPath(
"ListDisplayOption", option_);
102 bool skipDescription() {
return false; }
103 bool skipSave() {
return false; }
104 void dumpDescription(
RawConfig &config) {
105 config.setValueByPath(
"Font",
"True");
116 bool skipDescription() {
return false; }
117 bool skipSave() {
return false; }
118 void dumpDescription(
RawConfig &config)
const {
119 config.setValueByPath(
"IsEnum",
"True");
131 bool skipDescription() {
return true; }
132 bool skipSave() {
return false; }
133 void dumpDescription(
RawConfig &)
const {}
136 template <
typename Annotation>
138 using Annotation::Annotation;
139 bool skipDescription() {
return true; }
140 using Annotation::dumpDescription;
141 using Annotation::skipSave;
147 template <
typename SubConstrain>
149 ListConstrain(SubConstrain sub = SubConstrain()) : sub_(std::move(sub)) {}
151 using ElementType =
typename SubConstrain::Type;
152 using Type = std::vector<ElementType>;
153 bool check(
const Type &value) {
155 value.begin(), value.end(),
156 [
this](
const ElementType &ele) {
return sub_.check(ele); });
159 void dumpDescription(
RawConfig &config)
const {
160 sub_.dumpDescription(*config.get(
"ListConstrain",
true));
172 int max = std::numeric_limits<int>::max())
173 : min_(min), max_(max) {}
174 bool check(
int value)
const {
return value >= min_ && value <= max_; }
175 void dumpDescription(
RawConfig &config)
const {
176 if (min_ != std::numeric_limits<int>::min()) {
177 marshallOption(config[
"IntMin"], min_);
179 if (max_ != std::numeric_limits<int>::max()) {
180 marshallOption(config[
"IntMax"], max_);
190 enum class KeyConstrainFlag {
192 AllowModifierOnly = (1 << 0),
194 AllowModifierLess = (1 << 1),
205 bool check(
const Key &key)
const {
206 if (!flags_.test(KeyConstrainFlag::AllowModifierLess) &&
211 if (!flags_.test(KeyConstrainFlag::AllowModifierOnly) &&
219 void dumpDescription(
RawConfig &config)
const {
220 if (flags_.test(KeyConstrainFlag::AllowModifierLess)) {
221 config[
"AllowModifierLess"] =
"True";
223 if (flags_.test(KeyConstrainFlag::AllowModifierOnly)) {
224 config[
"AllowModifierOnly"] =
"True";
233 template <
typename T>
237 void marshall(
RawConfig &config,
const T &value)
const {
238 return marshallOption(config, value);
240 bool unmarshall(T &value,
const RawConfig &config,
bool partial)
const {
241 return unmarshallOption(value, config, partial);
246 template <
typename OptionType>
249 using value_type =
typename OptionType::value_type;
252 : option_(option), value_(option ? option->value() : value_type()) {}
256 option_->setValue(std::move(value_));
261 : option_(other.option_), value_(std::move(other.value_)) {
262 other.option_ =
nullptr;
266 option_ = other.option_;
267 value_ = std::move(other.value_);
268 other.option_ =
nullptr;
271 value_type &operator*() {
return value_; }
272 value_type *operator->() {
return &value_; }
283 template <
typename T,
typename Constrain = NoConstrain<T>,
284 typename Marshaller = DefaultMarshaller<T>,
285 typename Annotation = NoAnnotation>
288 using value_type = T;
289 using constrain_type = Constrain;
292 const T &defaultValue = T(), Constrain constrain = Constrain(),
293 Marshaller marshaller = Marshaller(),
294 Annotation annotation = Annotation())
295 :
OptionBaseV3(parent, std::move(path), std::move(description)),
296 defaultValue_(defaultValue), value_(defaultValue),
297 marshaller_(std::move(marshaller)), constrain_(std::move(constrain)),
298 annotation_(std::move(annotation)) {
299 if (!constrain_.check(defaultValue_)) {
300 throw std::invalid_argument(
301 "defaultValue doesn't satisfy constrain");
307 void dumpDescription(
RawConfig &config)
const override {
308 OptionBase::dumpDescription(config);
309 if constexpr (not std::is_base_of_v<Configuration, T>) {
310 marshaller_.marshall(config[
"DefaultValue"], defaultValue_);
312 constrain_.dumpDescription(config);
313 annotation_.dumpDescription(config);
314 using ::fcitx::dumpDescriptionHelper;
315 dumpDescriptionHelper(
316 config,
static_cast<typename RemoveVector<T>::type *
>(
nullptr));
319 std::unique_ptr<Configuration> subConfigSkeleton()
const override {
320 if constexpr (std::is_base_of_v<Configuration, T>) {
321 auto skeleton = std::make_unique<T>(defaultValue_);
322 skeleton->syncDefaultValueToCurrent();
326 typename RemoveVector<T>::type>) {
327 return std::make_unique<typename RemoveVector<T>::type>();
333 bool isDefault()
const override {
return defaultValue_ == value_; }
334 void reset()
override { value_ = defaultValue_; }
336 const T &value()
const {
return value_; }
338 const T &defaultValue()
const {
return defaultValue_; }
340 const T &operator*()
const {
return value(); }
341 const T *operator->()
const {
return &value_; }
343 template <
typename U>
344 bool setValue(U &&value) {
345 if (!constrain_.check(value)) {
348 value_ = std::forward<U>(value);
352 template <
typename Dummy = int,
353 std::enable_if_t<!std::is_same<Constrain, NoConstrain<T>>::value,
359 template <
typename Dummy = int,
360 std::enable_if_t<std::is_same<Constrain, NoConstrain<T>>::value,
366 void marshall(
RawConfig &config)
const override {
367 return marshaller_.marshall(config, value_);
369 bool unmarshall(
const RawConfig &config,
bool partial)
override {
374 if (!marshaller_.unmarshall(tempValue, config, partial)) {
377 return setValue(tempValue);
380 bool equalTo(
const OptionBase &other)
const override {
381 auto otherP =
static_cast<const Option *
>(&other);
382 return value_ == otherP->value_;
385 void copyFrom(
const OptionBase &other)
override {
386 auto otherP =
static_cast<const Option *
>(&other);
387 value_ = otherP->value_;
390 bool skipDescription()
const override {
391 return annotation_.skipDescription();
394 bool skipSave()
const override {
return annotation_.skipSave(); }
396 void syncDefaultValueToCurrent()
override {
397 defaultValue_ = value_;
398 if constexpr (std::is_base_of_v<Configuration, T>) {
399 value_.syncDefaultValueToCurrent();
400 defaultValue_.syncDefaultValueToCurrent();
404 auto &annotation()
const {
return annotation_; }
409 Marshaller marshaller_;
410 Constrain constrain_;
411 mutable Annotation annotation_;
415 template <
typename T,
typename Annotation>
430 template <
typename T,
typename Constrain = NoConstrain<T>,
431 typename Marshaller = DefaultMarshaller<T>,
432 typename Annotation = NoAnnotation>
436 template <
bool h
idden,
typename T>
439 template <
typename T,
typename Constrain,
typename Marshaller,
442 Option<T, Constrain, Marshaller, Annotation>> {
446 template <
typename T,
typename Constrain,
typename Marshaller,
449 Option<T, Constrain, Marshaller, Annotation>> {
453 template <
bool h
idden,
typename T>
454 using ConditionalHidden =
459 #endif // _FCITX_CONFIG_OPTION_H_ A helper class provide writing ability to option value.
Option that will not shown in UI.
Default Constrain with no actual constrain.
Annotation to be used against String type to indicate this is a Font.
List Constrain that applies the constrain to all element.
For a list of sub config, the field that should be used for display.
bool isModifier() const
Check if the key is a modifier press.
An option that launches external tool.
An option that launches external tool.
Annotation to be used against String type, for those type of string that should shown as a combobox...
Default marshaller that write the config RawConfig.
Represent a Configuration option.
Default Annotation with no options.
Integer type constrain with a lower and a upper bound.