Fcitx
action.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_ACTION_H_
8 #define _FCITX_ACTION_H_
9 
10 #include <memory>
11 #include <string>
13 #include <fcitx-utils/element.h>
14 #include <fcitx-utils/key.h>
15 #include <fcitx-utils/macros.h>
16 #include <fcitx/fcitxcore_export.h>
17 
18 namespace fcitx {
19 class ActionPrivate;
20 class SimpleActionPrivate;
21 class Menu;
22 class UserInterfaceManager;
23 class InputContext;
24 
25 /// \addtogroup FcitxCore
26 /// \{
27 /// \file
28 /// \brief Action class
29 
30 /**
31  * The Action class provides an abstraction for user commands that can be added
32  * to user interfaces.
33  */
34 class FCITXCORE_EXPORT Action : public Element {
35  friend class UserInterfaceManager;
36  friend class UserInterfaceManagerPrivate;
37 
38 public:
39  Action();
40  virtual ~Action();
41 
42  /**
43  * Whether the action is a separator action.
44  */
45  bool isSeparator() const;
46 
47  /**
48  * Set whether this action is a separator.
49  *
50  * How separator is displayed depends on the user interface implementation.
51  * The separator may be not displayed at all.
52  */
53  Action &setSeparator(bool separator);
54 
55  /**
56  * Whether the action is a checkable action.
57  */
58  bool isCheckable() const;
59  /**
60  * Set whether this action is a checkable action.
61  *
62  * This property mainly affect how this action is presented. Usually, it
63  * will be displayed as a radio button.
64  */
65  Action &setCheckable(bool checkable);
66 
67  /**
68  * The action name when this action is registered.
69  */
70  const std::string &name() const;
71 
72  /**
73  * Register an action to UserInterfaceManager.
74  *
75  * All action should have a unique name, but it is suggested that the action
76  * using a human readable string for its name. In order to make addons work
77  * together, other addon may use this name to lookup a certain action.
78  */
79  bool registerAction(const std::string &name,
80  UserInterfaceManager *uiManager);
81 
82  /**
83  * Short description for this action of given input context.
84  */
85  virtual std::string shortText(InputContext *) const = 0;
86 
87  /**
88  * Icon name of this action of given input context.
89  */
90  virtual std::string icon(InputContext *) const = 0;
91 
92  /**
93  * Return if this action is checked.
94  *
95  * It is only meaningful when isCheckable is true.
96  * @see setCheckable
97  */
98  virtual bool isChecked(InputContext * /*unused*/) const { return false; }
99 
100  /**
101  * Return a long description for this action.
102  *
103  * In some UI implementation, this is not displayed at all. In some other
104  * implemented, it is only displayed as tooltip.
105  */
106  virtual std::string longText(InputContext * /*unused*/) const { return {}; }
107 
108  /**
109  * Set the sub menu of this action.
110  *
111  * Some status area implemenation only supports one level of menu, so it is
112  * preferred that only one level of menu is used.
113  */
114  void setMenu(Menu *menu);
115 
116  /**
117  * Return the sub menu of this action.
118  */
119  Menu *menu();
120 
121  /**
122  * Return the unique integer id of action.
123  *
124  * This id is generated when the action is registered. If it is not
125  * registered, the value will be 0.
126  */
127  int id();
128 
129  /**
130  * Activate this action.
131  *
132  * This function may be triggered by user mouse click of the given action.
133  */
134  virtual void activate(InputContext * /*unused*/) {}
135 
136  /**
137  * Notify that this action is required to be updated of given input context.
138  *
139  * @param ic the input context that need to update this action.
140  */
141  void update(InputContext *ic);
142 
143  FCITX_DECLARE_SIGNAL(Action, Update, void(InputContext *));
144 
145  /**
146  * Hotkey bound to the action.
147  * This is only for display purpose when UI implementation supports it,
148  * and it has nothing to do with the key handling logic.
149  *
150  * @return key list.
151  * @since 5.1.11
152  */
153  const KeyList &hotkey() const;
154 
155  /**
156  * Set associated hotkey for display.
157  *
158  * @param hotkey keys that trigger the action.
159  * @since 5.1.11
160  */
161  void setHotkey(const KeyList &hotkey);
162 
163 private:
164  void setName(const std::string &name);
165  void setId(int id);
166 
167  std::unique_ptr<ActionPrivate> d_ptr;
168  FCITX_DECLARE_PRIVATE(Action);
169 };
170 
171 class FCITXCORE_EXPORT SimpleAction : public Action {
172 public:
173  SimpleAction();
174  ~SimpleAction();
175 
176  void setIcon(const std::string &icon);
177  void setChecked(bool checked);
178  void setShortText(const std::string &text);
179  void setLongText(const std::string &text);
180 
181  std::string shortText(InputContext * /*unused*/) const override;
182  std::string icon(InputContext * /*unused*/) const override;
183  bool isChecked(InputContext * /*unused*/) const override;
184  std::string longText(InputContext * /*unused*/) const override;
185 
186  void activate(fcitx::InputContext * /*unused*/) override;
187 
188  FCITX_DECLARE_SIGNAL(SimpleAction, Activated, void(InputContext *));
189 
190 private:
191  std::unique_ptr<SimpleActionPrivate> d_ptr;
192  FCITX_DECLARE_PRIVATE(SimpleAction);
193 };
194 } // namespace fcitx
195 
196 #endif // _FCITX_ACTION_H_
Utility class that provides a hierarchy between multiple objects.
virtual void activate(InputContext *)
Activate this action.
Definition: action.h:134
virtual std::string longText(InputContext *) const
Return a long description for this action.
Definition: action.h:106
#define FCITX_DECLARE_SIGNAL(CLASS_NAME, NAME,...)
Declare signal by type.
Definition: action.cpp:17
The Action class provides an abstraction for user commands that can be added to user interfaces...
Definition: action.h:34
Utilities to enable use object with signal.
virtual bool isChecked(InputContext *) const
Return if this action is checked.
Definition: action.h:98
Menu that contains a list of actions.
Definition: menu.h:31
An input context represents a client of Fcitx.
Definition: inputcontext.h:47
Class to represent a key.
Base class that can be used for UI composition or graph.
Definition: element.h:26