7 #ifndef _FCITX_CONFIG_OPTION_H_ 8 #define _FCITX_CONFIG_OPTION_H_ 14 #include <type_traits> 17 #include <fcitx-config/fcitxconfig_export.h> 18 #include <fcitx-config/marshallfunction.h> 19 #include <fcitx-config/option_details.h> 20 #include <fcitx-config/optiontypename.h> 21 #include <fcitx-config/rawconfig.h> 31 std::string description, std::string uri);
33 std::string typeString()
const override;
34 void reset()
override;
35 bool isDefault()
const override;
37 void marshall(
RawConfig &config)
const override;
38 bool unmarshall(
const RawConfig &config,
bool partial)
override;
39 std::unique_ptr<Configuration> subConfigSkeleton()
const override;
41 bool equalTo(
const OptionBase &other)
const override;
42 void copyFrom(
const OptionBase &other)
override;
44 bool skipDescription()
const override;
45 bool skipSave()
const override;
46 void dumpDescription(
RawConfig &config)
const override;
49 std::string externalUri_;
55 using ExternalOption::ExternalOption;
56 void dumpDescription(
RawConfig &config)
const override;
63 bool check(
const T & )
const {
return true; }
64 void dumpDescription(
RawConfig & )
const {}
69 bool skipDescription() {
return false; }
70 bool skipSave() {
return false; }
71 void dumpDescription(
RawConfig & )
const {}
78 bool skipDescription() {
return false; }
79 bool skipSave() {
return false; }
80 void dumpDescription(
RawConfig &config)
const {
81 config.setValueByPath(
"Tooltip", tooltip_);
91 : option_(std::move(option)) {}
93 bool skipDescription() {
return false; }
94 bool skipSave() {
return false; }
95 void dumpDescription(
RawConfig &config)
const {
96 config.setValueByPath(
"ListDisplayOption", option_);
107 bool skipDescription() {
return false; }
108 bool skipSave() {
return false; }
109 void dumpDescription(
RawConfig &config) {
110 config.setValueByPath(
"Font",
"True");
121 bool skipDescription() {
return false; }
122 bool skipSave() {
return false; }
123 void dumpDescription(
RawConfig &config)
const {
124 config.setValueByPath(
"IsEnum",
"True");
136 bool skipDescription() {
return true; }
137 bool skipSave() {
return false; }
138 void dumpDescription(
RawConfig & )
const {}
141 template <
typename Annotation>
143 using Annotation::Annotation;
144 bool skipDescription() {
return true; }
145 using Annotation::dumpDescription;
146 using Annotation::skipSave;
152 template <
typename SubConstrain>
154 ListConstrain(SubConstrain sub = SubConstrain()) : sub_(std::move(sub)) {}
156 using ElementType =
typename SubConstrain::Type;
157 using Type = std::vector<ElementType>;
158 bool check(
const Type &value) {
160 value.begin(), value.end(),
161 [
this](
const ElementType &ele) {
return sub_.check(ele); });
164 void dumpDescription(
RawConfig &config)
const {
165 sub_.dumpDescription(*config.get(
"ListConstrain",
true));
177 int max = std::numeric_limits<int>::max())
178 : min_(min), max_(max) {}
179 bool check(
int value)
const {
return value >= min_ && value <= max_; }
180 void dumpDescription(
RawConfig &config)
const {
181 if (min_ != std::numeric_limits<int>::min()) {
182 marshallOption(config[
"IntMin"], min_);
184 if (max_ != std::numeric_limits<int>::max()) {
185 marshallOption(config[
"IntMax"], max_);
195 enum class KeyConstrainFlag {
197 AllowModifierOnly = (1 << 0),
199 AllowModifierLess = (1 << 1),
210 bool check(
const Key &key)
const {
211 if (!flags_.test(KeyConstrainFlag::AllowModifierLess) &&
216 if (!flags_.test(KeyConstrainFlag::AllowModifierOnly) &&
224 void dumpDescription(
RawConfig &config)
const {
225 if (flags_.test(KeyConstrainFlag::AllowModifierLess)) {
226 config[
"AllowModifierLess"] =
"True";
228 if (flags_.test(KeyConstrainFlag::AllowModifierOnly)) {
229 config[
"AllowModifierOnly"] =
"True";
238 template <
typename T>
242 void marshall(
RawConfig &config,
const T &value)
const {
243 return marshallOption(config, value);
245 bool unmarshall(T &value,
const RawConfig &config,
bool partial)
const {
246 return unmarshallOption(value, config, partial);
251 template <
typename OptionType>
254 using value_type =
typename OptionType::value_type;
257 : option_(option), value_(option ? option->value() : value_type()) {}
261 option_->setValue(std::move(value_));
266 : option_(other.option_), value_(std::move(other.value_)) {
267 other.option_ =
nullptr;
271 option_ = other.option_;
272 value_ = std::move(other.value_);
273 other.option_ =
nullptr;
276 value_type &operator*() {
return value_; }
277 value_type *operator->() {
return &value_; }
284 template <
typename Constrain,
typename Marshaller,
typename Annotation,
289 std::string description;
291 Constrain constrain{};
292 Marshaller marshaller{};
293 Annotation annotation{};
300 template <
typename T,
typename Constrain = NoConstrain<T>,
301 typename Marshaller = DefaultMarshaller<T>,
302 typename Annotation = NoAnnotation>
305 using value_type = T;
306 using constrain_type = Constrain;
311 const T &defaultValue = T(), Constrain constrain = Constrain(),
312 Marshaller marshaller = Marshaller(),
313 Annotation annotation = Annotation())
314 :
OptionBaseV3(parent, std::move(path), std::move(description)),
315 defaultValue_(defaultValue), value_(defaultValue),
316 marshaller_(std::move(marshaller)), constrain_(std::move(constrain)),
317 annotation_(std::move(annotation)) {
318 if (!constrain_.check(defaultValue_)) {
319 throw std::invalid_argument(
320 "defaultValue doesn't satisfy constrain");
325 :
Option(params.parent, std::move(params.path),
326 std::move(params.description), std::move(params.defaultValue),
327 std::move(params.constrain), std::move(params.marshaller),
328 std::move(params.annotation)) {}
332 void dumpDescription(
RawConfig &config)
const override {
333 OptionBase::dumpDescription(config);
334 if constexpr (not std::is_base_of_v<Configuration, T>) {
335 marshaller_.marshall(config[
"DefaultValue"], defaultValue_);
337 constrain_.dumpDescription(config);
338 annotation_.dumpDescription(config);
339 using ::fcitx::dumpDescriptionHelper;
340 dumpDescriptionHelper(
341 config,
static_cast<typename RemoveVector<T>::type *
>(
nullptr));
344 std::unique_ptr<Configuration> subConfigSkeleton()
const override {
345 if constexpr (std::is_base_of_v<Configuration, T>) {
346 auto skeleton = std::make_unique<T>(defaultValue_);
347 skeleton->syncDefaultValueToCurrent();
351 typename RemoveVector<T>::type>) {
352 return std::make_unique<typename RemoveVector<T>::type>();
358 bool isDefault()
const override {
return defaultValue_ == value_; }
359 void reset()
override { value_ = defaultValue_; }
361 const T &value()
const {
return value_; }
363 const T &defaultValue()
const {
return defaultValue_; }
365 const T &operator*()
const {
return value(); }
366 const T *operator->()
const {
return &value_; }
368 template <
typename U>
369 bool setValue(U &&value) {
370 if (!constrain_.check(value)) {
373 value_ = std::forward<U>(value);
377 template <
typename Dummy = int,
378 std::enable_if_t<!std::is_same<Constrain, NoConstrain<T>>::value,
384 template <
typename Dummy = int,
385 std::enable_if_t<std::is_same<Constrain, NoConstrain<T>>::value,
391 void marshall(
RawConfig &config)
const override {
392 return marshaller_.marshall(config, value_);
394 bool unmarshall(
const RawConfig &config,
bool partial)
override {
399 if (!marshaller_.unmarshall(tempValue, config, partial)) {
402 return setValue(tempValue);
405 bool equalTo(
const OptionBase &other)
const override {
406 auto otherP =
static_cast<const Option *
>(&other);
407 return value_ == otherP->value_;
410 void copyFrom(
const OptionBase &other)
override {
411 auto otherP =
static_cast<const Option *
>(&other);
412 value_ = otherP->value_;
415 bool skipDescription()
const override {
416 return annotation_.skipDescription();
419 bool skipSave()
const override {
return annotation_.skipSave(); }
421 void syncDefaultValueToCurrent()
override {
422 defaultValue_ = value_;
423 if constexpr (std::is_base_of_v<Configuration, T>) {
424 value_.syncDefaultValueToCurrent();
425 defaultValue_.syncDefaultValueToCurrent();
429 auto &annotation()
const {
return annotation_; }
434 Marshaller marshaller_;
435 Constrain constrain_;
436 mutable Annotation annotation_;
440 template <
typename T,
typename Annotation>
445 template <
typename Annotation>
460 template <
typename T,
typename Constrain = NoConstrain<T>,
461 typename Marshaller = DefaultMarshaller<T>,
462 typename Annotation = NoAnnotation>
466 template <
bool h
idden,
typename T>
469 template <
typename T,
typename Constrain,
typename Marshaller,
472 Option<T, Constrain, Marshaller, Annotation>> {
476 template <
typename T,
typename Constrain,
typename Marshaller,
479 Option<T, Constrain, Marshaller, Annotation>> {
492 using SubConfigOption::SubConfigOption;
493 bool skipDescription()
const override {
return true; }
495 using OptionType = HiddenSubConfigOption;
498 template <
bool h
idden,
typename T>
499 using ConditionalHidden =
504 #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...
Helper template class to make easier to use type safe enum flags.
Default marshaller that write the config RawConfig.
Class to represent a key.
Represent a Configuration option.
Default Annotation with no options.
Integer type constrain with a lower and a upper bound.