aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/viewer/sk_app/CommandSet.h
diff options
context:
space:
mode:
authorGravatar brianosman <brianosman@google.com>2016-05-10 06:50:49 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-05-10 06:50:49 -0700
commit622c8d5de12f264e496e8d4664a2eea9333922d0 (patch)
tree6c548356f43ad6675d944c85d729200687e73261 /tools/viewer/sk_app/CommandSet.h
parent3622a17291ccddcb073275e50a8711bf61b7bf27 (diff)
Add flexible keybinding/command system to sk_app.
Viewer demonstrates use: Just create an instance of CommandSet, register with the window, and add commands. Hopefully, we can keep all commands in one place, and get some nice side-benefits. With this framework, if you want to add a new command, you are only required to add code in ONE place. And you get added to the help screen, for free. CommandSet automatically binds 'h' to cycle through the help modes. (Functional grouping is most useful for general use, but the other mode is nice to know what a key does, or to find an unused key for a new feature). Grouped by function: https://screenshot.googleplex.com/G5h3f52wFKu.png Alphabetical by key: https://screenshot.googleplex.com/nZiopabLKJ6.png BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1955293002 Review-Url: https://codereview.chromium.org/1955293002
Diffstat (limited to 'tools/viewer/sk_app/CommandSet.h')
-rw-r--r--tools/viewer/sk_app/CommandSet.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/tools/viewer/sk_app/CommandSet.h b/tools/viewer/sk_app/CommandSet.h
new file mode 100644
index 0000000000..5e6373c935
--- /dev/null
+++ b/tools/viewer/sk_app/CommandSet.h
@@ -0,0 +1,107 @@
+/*
+* Copyright 2016 Google Inc.
+*
+* Use of this source code is governed by a BSD-style license that can be
+* found in the LICENSE file.
+*/
+
+#ifndef CommandSet_DEFINED
+#define CommandSet_DEFINED
+
+#include "SkString.h"
+#include "Window.h"
+
+#include <functional>
+
+class SkCanvas;
+
+namespace sk_app {
+
+/**
+ * Helper class used by applications that want to hook keypresses to trigger events.
+ *
+ * An app can simply store an instance of CommandSet and then use it as follows:
+ * 1) Attach to the Window at initialization time. This registers key handlers on the window.
+ * 2) Register commands to be executed for characters or keys. Each command needs a Group and a
+ * description (both just strings). Commands attached to Keys (rather than characters) also need
+ * a displayable name for the Key. Finally, a function to execute when the key or character is
+ * pressed must be supplied. The easiest option to is pass in a lambda that captures [this]
+ * (your application object), and performs whatever action is desired.
+ * 3) At the end of your onPaint, call drawHelp, and pass in the application's canvas.
+
+ * The CommandSet always binds 'h' to cycle through two different help screens. The first shows
+ * all commands, organized by Group (with headings for each Group). The second shows all commands
+ * alphabetically by key/character.
+ */
+class CommandSet {
+public:
+ CommandSet();
+
+ void attach(Window* window);
+ bool onKey(sk_app::Window::Key key, sk_app::Window::InputState state, uint32_t modifiers);
+ bool onChar(SkUnichar, uint32_t modifiers);
+
+ void addCommand(SkUnichar c, const char* group, const char* description,
+ std::function<void(void)> function);
+ void addCommand(Window::Key k, const char* keyName, const char* group, const char* description,
+ std::function<void(void)> function);
+
+ void drawHelp(SkCanvas* canvas);
+
+private:
+ struct Command {
+ enum CommandType {
+ kChar_CommandType,
+ kKey_CommandType,
+ };
+
+ Command(SkUnichar c, const char* group, const char* description,
+ std::function<void(void)> function)
+ : fType(kChar_CommandType)
+ , fChar(c)
+ , fKeyName(SkStringPrintf("%c", c))
+ , fGroup(group)
+ , fDescription(description)
+ , fFunction(function) {}
+
+ Command(Window::Key k, const char* keyName, const char* group, const char* description,
+ std::function<void(void)> function)
+ : fType(kKey_CommandType)
+ , fKey(k)
+ , fKeyName(keyName)
+ , fGroup(group)
+ , fDescription(description)
+ , fFunction(function) {}
+
+ CommandType fType;
+
+ // For kChar_CommandType
+ SkUnichar fChar;
+
+ // For kKey_CommandType
+ Window::Key fKey;
+
+ // Common to all command types
+ SkString fKeyName;
+ SkString fGroup;
+ SkString fDescription;
+ std::function<void(void)> fFunction;
+ };
+
+ static bool compareCommandKey(const Command& first, const Command& second);
+ static bool compareCommandGroup(const Command& first, const Command& second);
+
+ enum HelpMode {
+ kNone_HelpMode,
+ kGrouped_HelpMode,
+ kAlphabetical_HelpMode,
+ };
+
+ Window* fWindow;
+ SkTArray<Command> fCommands;
+ HelpMode fHelpMode;
+};
+
+} // namespace sk_app
+
+#endif