Fcitx
tempmode.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2026-2026 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #ifndef _FCITX_TEMPMODE_H_
9 #define _FCITX_TEMPMODE_H_
10 
11 #include <memory>
12 #include <string_view>
13 #include <fcitx-utils/key.h>
14 #include <fcitx-utils/macros.h>
15 #include <fcitx/event.h>
16 #include <fcitx/fcitxcore_export.h>
18 
19 /// \addtogroup FcitxCore
20 /// \{
21 /// \file
22 /// \brief Temporary input modes for Fcitx.
23 
24 namespace fcitx {
25 
26 class TempModePrivate;
27 
28 template <typename PropertyBaseType>
30  public PropertyBaseType {
31 public:
32  bool isActive() const { return active_; }
33  void setActive(bool active) { active_ = active; }
34 
35 private:
36  bool active_ = false;
37 };
38 
39 /**
40  * A temporary mode that can take over key handling for a short period.
41  *
42  * A temp mode is typically activated by a hotkey, handles subsequent key
43  * events while active, and then returns control to the regular input method.
44  */
45 class FCITXCORE_EXPORT TempMode {
46 public:
47  friend class TempModeManager;
48  friend class TempModePrivate;
49 
50  TempMode();
51  virtual ~TempMode();
52 
53  /**
54  * Return whether this temp mode is registered with a manager.
55  */
56  bool isRegistered() const;
57 
58  /**
59  * Unregister this temp mode from its manager.
60  */
61  void unregister();
62 
63  /**
64  * Return whether this temp mode is currently active.
65  */
66  virtual bool isActive(InputContext *inputContext) const = 0;
67 
68  /**
69  * Handle a potential trigger key event.
70  *
71  * The default implementation checks triggerKeys() against the key event and
72  * activates the mode on a matching key press.
73  *
74  * @param keyEvent key event
75  * @return whether the key event activates the temp mode
76  */
77  virtual bool triggerTempMode(const KeyEvent &keyEvent) = 0;
78 
79  /**
80  * Handle a key event while the temp mode is active.
81  *
82  * @param keyEvent key event
83  * @return whether the key event should be accepted
84  */
85  virtual bool keyEvent(const KeyEvent &keyEvent) = 0;
86 
87  /**
88  * Handle an action invoked while the temp mode is active.
89  *
90  * @param event action event
91  * @return whether the action event should be accepted
92  */
93  virtual bool invokeAction(InvokeActionEvent &event);
94 
95  /**
96  * Reset the temp mode state for an input context.
97  *
98  * @param inputContext input context
99  */
100  virtual void reset(InputContext *inputContext) = 0;
101 
102  /**
103  * Return the input context property name
104  */
105  virtual std::string_view name() const = 0;
106 
107 protected:
108  /**
109  * Create the property object stored for each input context.
110  */
111  virtual InputContextProperty *
112  createProperty(InputContext &inputContext) = 0;
113 
114  /**
115  * Return the registered temp mode state for an input context.
116  */
117  InputContextProperty *genericProperty(InputContext *inputContext) const;
118 
119 private:
120  std::unique_ptr<TempModePrivate> d_ptr;
121  FCITX_DECLARE_PRIVATE(TempMode);
122 };
123 
124 template <typename PropertyBaseType>
125 class SimpleTempMode : public TempMode {
126 public:
128 
129  PropertyType *property(InputContext *inputContext) const {
130  return static_cast<PropertyType *>(genericProperty(inputContext));
131  }
132 
133 protected:
134  bool isActive(InputContext *inputContext) const override {
135  if (auto *prop = property(inputContext)) {
136  return prop->isActive();
137  }
138  return false;
139  }
140 
141  void reset(InputContext *inputContext) override {
142  if (auto *prop = property(inputContext)) {
143  prop->setActive(false);
144  }
145  }
146 
148  FCITX_UNUSED(inputContext);
149  return new PropertyType;
150  }
151 };
152 
153 } // namespace fcitx
154 
155 /// \}
156 
157 #endif // _FCITX_TEMPMODE_H_
Manage registered TempMode objects for an Instance.
void reset(InputContext *inputContext) override
Reset the temp mode state for an input context.
Definition: tempmode.h:141
Definition: action.cpp:17
A temporary mode that can take over key handling for a short period.
Definition: tempmode.h:45
bool isActive(InputContext *inputContext) const override
Return whether this temp mode is currently active.
Definition: tempmode.h:134
InputContextProperty * createProperty(InputContext &inputContext) override
Create the property object stored for each input context.
Definition: tempmode.h:147
Input Method event for Fcitx.
Input Context Property for Fcitx.
An input context represents a client of Fcitx.
Definition: inputcontext.h:50
Class to represent a key.
This is a class that designed to store state that is specific to certain input context.