Flutter Engine
The Flutter Engine
CommandSet.h
Go to the documentation of this file.
1/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#ifndef CommandSet_DEFINED
9#define CommandSet_DEFINED
10
15#include "tools/skui/Key.h"
17
18#include <functional>
19#include <vector>
20
21class SkCanvas;
22
23namespace sk_app {
24class Window;
25
26/**
27 * Helper class used by applications that want to hook keypresses to trigger events.
28 *
29 * An app can simply store an instance of CommandSet and then use it as follows:
30 * 1) Attach to the Window at initialization time.
31 * 2) Register commands to be executed for characters or keys. Each command needs a Group and a
32 * description (both just strings). Commands attached to Keys (rather than characters) also need
33 * a displayable name for the Key. Finally, a function to execute when the key or character is
34 * pressed must be supplied. The easiest option to is pass in a lambda that captures [this]
35 * (your application object), and performs whatever action is desired.
36 * 3) Register key and char handlers with the Window, and - depending on your state - forward those
37 * events to the CommandSet's onKey, onChar, and onSoftKey.
38 * 4) At the end of your onPaint, call drawHelp, and pass in the application's canvas.
39
40 * The CommandSet always binds 'h' to cycle through two different help screens. The first shows
41 * all commands, organized by Group (with headings for each Group). The second shows all commands
42 * alphabetically by key/character.
43 */
45public:
46 CommandSet();
47
48 void attach(Window* window);
50 bool onChar(SkUnichar, skui::ModifierKey modifiers);
51 bool onSoftkey(const SkString& softkey);
52
53 void addCommand(SkUnichar c, const char* group, const char* description,
54 std::function<void(void)> function);
55 void addCommand(skui::Key k, const char* keyName, const char* group, const char* description,
56 std::function<void(void)> function);
57
58 void drawHelp(SkCanvas* canvas);
59
60 std::vector<SkString> getCommandsAsSoftkeys() const;
61
62private:
63 struct Command {
64 enum CommandType {
65 kChar_CommandType,
66 kKey_CommandType,
67 };
68
69 Command(SkUnichar c, const char* group, const char* description,
70 std::function<void(void)> function)
71 : fType(kChar_CommandType)
72 , fChar(c)
73 , fKeyName(' ' == c ? SkString("Space") : SkStringPrintf("%c", c))
74 , fGroup(group)
75 , fDescription(description)
76 , fFunction(function) {}
77
78 Command(skui::Key k, const char* keyName, const char* group, const char* description,
79 std::function<void(void)> function)
80 : fType(kKey_CommandType)
81 , fKey(k)
82 , fKeyName(keyName)
83 , fGroup(group)
84 , fDescription(description)
85 , fFunction(function) {}
86
87 CommandType fType;
88
89 // For kChar_CommandType
90 SkUnichar fChar;
91
92 // For kKey_CommandType
93 skui::Key fKey;
94
95 // Common to all command types
96 SkString fKeyName;
97 SkString fGroup;
98 SkString fDescription;
99 std::function<void(void)> fFunction;
100
101 SkString getSoftkeyString() const {
102 return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_str());
103 }
104 };
105
106 static bool compareCommandKey(const Command& first, const Command& second);
107 static bool compareCommandGroup(const Command& first, const Command& second);
108
109 enum HelpMode {
110 kNone_HelpMode,
111 kGrouped_HelpMode,
112 kAlphabetical_HelpMode,
113 };
114
115 Window* fWindow;
117 HelpMode fHelpMode;
118};
119
120} // namespace sk_app
121
122#endif
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
int32_t SkUnichar
Definition: SkTypes.h:175
const char * c_str() const
Definition: SkString.h:133
void attach(Window *window)
Definition: CommandSet.cpp:41
void drawHelp(SkCanvas *canvas)
Definition: CommandSet.cpp:97
bool onChar(SkUnichar, skui::ModifierKey modifiers)
Definition: CommandSet.cpp:58
bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers)
Definition: CommandSet.cpp:45
std::vector< SkString > getCommandsAsSoftkeys() const
Definition: CommandSet.cpp:162
void addCommand(SkUnichar c, const char *group, const char *description, std::function< void(void)> function)
Definition: CommandSet.cpp:79
bool onSoftkey(const SkString &softkey)
Definition: CommandSet.cpp:69
GLFWwindow * window
Definition: main.cc:45
AtkStateType state
Dart_NativeFunction function
Definition: fuchsia.cc:51
Definition: dart.idl:411
InputState
Definition: InputState.h:6
ModifierKey
Definition: ModifierKey.h:9
Key
Definition: Key.h:6