blob: 4cbb367e01cf99269817f76c34ead05fc2672f56 [file] [log] [blame]
brianosman622c8d52016-05-10 06:50:49 -07001/*
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
11#include "SkString.h"
12#include "Window.h"
13
14#include <functional>
liyuqianb73c24b2016-06-03 08:47:23 -070015#include <vector>
brianosman622c8d52016-05-10 06:50:49 -070016
17class SkCanvas;
18
19namespace sk_app {
20
21/**
22 * Helper class used by applications that want to hook keypresses to trigger events.
23 *
24 * An app can simply store an instance of CommandSet and then use it as follows:
25 * 1) Attach to the Window at initialization time. This registers key handlers on the window.
26 * 2) Register commands to be executed for characters or keys. Each command needs a Group and a
27 * description (both just strings). Commands attached to Keys (rather than characters) also need
28 * a displayable name for the Key. Finally, a function to execute when the key or character is
29 * pressed must be supplied. The easiest option to is pass in a lambda that captures [this]
30 * (your application object), and performs whatever action is desired.
31 * 3) At the end of your onPaint, call drawHelp, and pass in the application's canvas.
32
33 * The CommandSet always binds 'h' to cycle through two different help screens. The first shows
34 * all commands, organized by Group (with headings for each Group). The second shows all commands
35 * alphabetically by key/character.
36 */
37class CommandSet {
38public:
39 CommandSet();
40
41 void attach(Window* window);
42 bool onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers);
43 bool onChar(SkUnichar, uint32_t modifiers);
liyuqianb73c24b2016-06-03 08:47:23 -070044 bool onSoftkey(const SkString& softkey);
brianosman622c8d52016-05-10 06:50:49 -070045
46 void addCommand(SkUnichar c, const char* group, const char* description,
47 std::function<void(void)> function);
48 void addCommand(Window::Key k, const char* keyName, const char* group, const char* description,
49 std::function<void(void)> function);
50
51 void drawHelp(SkCanvas* canvas);
52
liyuqianb73c24b2016-06-03 08:47:23 -070053 std::vector<SkString> getCommandsAsSoftkeys() const;
54
brianosman622c8d52016-05-10 06:50:49 -070055private:
56 struct Command {
57 enum CommandType {
58 kChar_CommandType,
59 kKey_CommandType,
60 };
61
62 Command(SkUnichar c, const char* group, const char* description,
63 std::function<void(void)> function)
64 : fType(kChar_CommandType)
65 , fChar(c)
66 , fKeyName(SkStringPrintf("%c", c))
67 , fGroup(group)
68 , fDescription(description)
69 , fFunction(function) {}
70
71 Command(Window::Key k, const char* keyName, const char* group, const char* description,
72 std::function<void(void)> function)
73 : fType(kKey_CommandType)
74 , fKey(k)
75 , fKeyName(keyName)
76 , fGroup(group)
77 , fDescription(description)
78 , fFunction(function) {}
79
80 CommandType fType;
81
82 // For kChar_CommandType
83 SkUnichar fChar;
84
85 // For kKey_CommandType
86 Window::Key fKey;
87
88 // Common to all command types
89 SkString fKeyName;
90 SkString fGroup;
91 SkString fDescription;
92 std::function<void(void)> fFunction;
liyuqianb73c24b2016-06-03 08:47:23 -070093
94 SkString getSoftkeyString() const {
95 return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_str());
96 }
brianosman622c8d52016-05-10 06:50:49 -070097 };
98
99 static bool compareCommandKey(const Command& first, const Command& second);
100 static bool compareCommandGroup(const Command& first, const Command& second);
101
102 enum HelpMode {
103 kNone_HelpMode,
104 kGrouped_HelpMode,
105 kAlphabetical_HelpMode,
106 };
107
108 Window* fWindow;
109 SkTArray<Command> fCommands;
110 HelpMode fHelpMode;
111};
112
113} // namespace sk_app
114
115#endif