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