Fcitx
inputcontextproperty.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #ifndef _FCITX_INPUTCONTEXTPROPERTY_H_
8 #define _FCITX_INPUTCONTEXTPROPERTY_H_
9 
10 #include <memory>
11 #include <fcitx-utils/macros.h>
13 #include "fcitxcore_export.h"
14 
15 /// \addtogroup FcitxCore
16 /// \{
17 /// \file
18 /// \brief Input Context Property for Fcitx.
19 
20 namespace fcitx {
21 
22 /**
23  * This is a class that designed to store state that is specific to certain
24  * input context.
25  *
26  * The state may be copied to other input context, depending on the value
27  * GlobalConfig::shareInputState. Also in reality, you don't need to copy
28  * everything. For example, sub input mode can be a good candidate to copy over
29  * the value, like Japanese input method is under hiragana mode or katakana
30  * mode. The current input buffer usually need not to be shared between
31  * different input context. In Fcitx 5, there can be more than one input context
32  * has active focus (Rare for one-seat setup, but it is possible), so it is
33  * important to consider that and not to use a global state in the input method
34  * engine.
35  *
36  */
37 class FCITXCORE_EXPORT InputContextProperty {
38 public:
39  virtual ~InputContextProperty() {}
40  /**
41  * copy state to another property.
42  *
43  * Default implemenation is empty.
44  * This is triggered by InputContext::updateProperty.
45  *
46  * @see InputContext::updateProperty
47  */
48  virtual void copyTo(InputContextProperty *) {}
49  /// Quick check if there's need to copy over the state.
50  virtual bool needCopy() const { return false; }
51 };
52 
53 class InputContext;
54 class InputContextPropertyFactoryPrivate;
55 
56 /**
57  * Factory class for input context property.
58  *
59  * Factory can be only registered with one InputContextManager.
60  * The factory will automatically unregister itself upon destruction.
61  * The factory need to unregister before the destruction of InputContextManager.
62  *
63  * @see InputContextManager::registerProperty
64  */
65 class FCITXCORE_EXPORT InputContextPropertyFactory
66  : public fcitx::TrackableObject<InputContextPropertyFactory> {
67  friend class InputContextManager;
68 
69 public:
71  virtual ~InputContextPropertyFactory();
72  virtual InputContextProperty *create(InputContext &) = 0;
73 
74  /// Return whether the factory is already registered with an
75  /// InputContextManager.
76  bool registered() const;
77  /// Unregister the factory from current InputContextManager.
78  void unregister();
79 
80 private:
81  std::unique_ptr<InputContextPropertyFactoryPrivate> d_ptr;
82  FCITX_DECLARE_PRIVATE(InputContextPropertyFactory);
83 };
84 
85 template <typename T>
87 public:
88  typedef T PropertyType;
89  InputContextProperty *create(InputContext &) override { return new T; }
90 };
91 
92 template <typename Ret>
94 public:
95  typedef Ret PropertyType;
96  LambdaInputContextPropertyFactory(std::function<Ret *(InputContext &)> f)
97  : func_(std::move(f)) {}
98 
99  InputContextProperty *create(InputContext &ic) override {
100  return func_(ic);
101  }
102 
103 private:
104  std::function<Ret *(InputContext &)> func_;
105 };
106 
107 /**
108  * Convinient short type alias for creating a LambdaInputContextPropertyFactory.
109  *
110  * Example usage:
111  * Define it as a field of AddonInstance.
112  * @code
113  * FactoryFor<MyProperty> factory_;
114  * @endcode
115  *
116  * Register the property to InputContextManager in constructor of AddonInstance.
117  * @code
118  * instance_->inputContextManager().registerProperty("propertyName",
119  * &factory_);
120  * @endcode
121  * The name of property need to be unique.
122  *
123  * Get the property from input context within the addon.
124  * @code
125  * auto *state = inputContext->propertyFor(&factory_);
126  * @endcode
127  * The returned type will be casted automatically. Get property with factory is
128  * faster than get property with name.
129  *
130  * Accessing the property outside the addon.
131  * @code
132  * InputContextProperty *state = inputContext->property("propertyName");
133  * @endcode
134  * You may need to cast the type to access any private data within it.
135  */
136 template <typename T>
138 } // namespace fcitx
139 
140 #endif // _FCITX_INPUTCONTEXTPROPERTY_H_
Utitliy classes for statically tracking the life of a object.
Definition: action.cpp:12
virtual void copyTo(InputContextProperty *)
copy state to another property.
Helper class to be used with TrackableObjectReference.
Factory class for input context property.
virtual bool needCopy() const
Quick check if there&#39;s need to copy over the state.
An input context represents a client of Fcitx.
Definition: inputcontext.h:45
This is a class that designed to store state that is specific to certain input context.