Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
sk_app::CommandSet Class Reference

#include <CommandSet.h>

Public Member Functions

 CommandSet ()
 
void attach (Window *window)
 
bool onKey (skui::Key key, skui::InputState state, skui::ModifierKey modifiers)
 
bool onChar (SkUnichar, skui::ModifierKey modifiers)
 
bool onSoftkey (const SkString &softkey)
 
void addCommand (SkUnichar c, const char *group, const char *description, std::function< void(void)> function)
 
void addCommand (skui::Key k, const char *keyName, const char *group, const char *description, std::function< void(void)> function)
 
void drawHelp (SkCanvas *canvas)
 
std::vector< SkStringgetCommandsAsSoftkeys () const
 

Detailed Description

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. 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) Register key and char handlers with the Window, and - depending on your state - forward those events to the CommandSet's onKey, onChar, and onSoftKey. 4) 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.

Definition at line 44 of file CommandSet.h.

Constructor & Destructor Documentation

◆ CommandSet()

sk_app::CommandSet::CommandSet ( )

Definition at line 23 of file CommandSet.cpp.

24 : fHelpMode(kNone_HelpMode) {
25 this->addCommand('h', "Overlays", "Show help screen", [this]() {
26 switch (this->fHelpMode) {
27 case kNone_HelpMode:
28 this->fHelpMode = kGrouped_HelpMode;
29 break;
30 case kGrouped_HelpMode:
31 this->fHelpMode = kAlphabetical_HelpMode;
32 break;
33 case kAlphabetical_HelpMode:
34 this->fHelpMode = kNone_HelpMode;
35 break;
36 }
37 fWindow->inval();
38 });
39}
void addCommand(SkUnichar c, const char *group, const char *description, std::function< void(void)> function)

Member Function Documentation

◆ addCommand() [1/2]

void sk_app::CommandSet::addCommand ( skui::Key  k,
const char *  keyName,
const char *  group,
const char *  description,
std::function< void(void)>  function 
)

Definition at line 84 of file CommandSet.cpp.

85 {
86 fCommands.push_back(Command(k, keyName, group, description, function));
87}
Dart_NativeFunction function
Definition fuchsia.cc:51

◆ addCommand() [2/2]

void sk_app::CommandSet::addCommand ( SkUnichar  c,
const char *  group,
const char *  description,
std::function< void(void)>  function 
)

Definition at line 79 of file CommandSet.cpp.

80 {
81 fCommands.push_back(Command(c, group, description, function));
82}

◆ attach()

void sk_app::CommandSet::attach ( Window window)

Definition at line 41 of file CommandSet.cpp.

41 {
42 fWindow = window;
43}
GLFWwindow * window
Definition main.cc:45

◆ drawHelp()

void sk_app::CommandSet::drawHelp ( SkCanvas canvas)

Definition at line 97 of file CommandSet.cpp.

97 {
98 if (kNone_HelpMode == fHelpMode) {
99 return;
100 }
101
102 // Sort commands for current mode:
103 std::stable_sort(fCommands.begin(), fCommands.end(),
104 kAlphabetical_HelpMode == fHelpMode ? compareCommandKey : compareCommandGroup);
105
107 font.setSize(16);
108
109 SkFont groupFont;
110 groupFont.setSize(18);
111
112 SkPaint bgPaint;
113 bgPaint.setColor(0xC0000000);
114 canvas->drawPaint(bgPaint);
115
117 paint.setAntiAlias(true);
118 paint.setColor(0xFFFFFFFF);
119
120 SkPaint groupPaint;
121 groupPaint.setAntiAlias(true);
122 groupPaint.setColor(0xFFFFFFFF);
123
126
127 // Measure all key strings:
128 SkScalar keyWidth = 0;
129 for (Command& cmd : fCommands) {
130 keyWidth = std::max(keyWidth,
131 font.measureText(cmd.fKeyName.c_str(), cmd.fKeyName.size(),
133 }
134 keyWidth += font.measureText(" ", 1, SkTextEncoding::kUTF8);
135
136 // If we're grouping by category, we'll be adding text height on every new group (including the
137 // first), so no need to do that here. Otherwise, skip down so the first line is where we want.
138 if (kGrouped_HelpMode != fHelpMode) {
139 y += font.getSize();
140 }
141
142 // Print everything:
143 SkString lastGroup;
144 for (Command& cmd : fCommands) {
145 if (kGrouped_HelpMode == fHelpMode && lastGroup != cmd.fGroup) {
146 // Group change. Advance and print header:
147 y += font.getSize();
148 canvas->drawSimpleText(cmd.fGroup.c_str(), cmd.fGroup.size(), SkTextEncoding::kUTF8,
149 x, y, groupFont, groupPaint);
150 y += groupFont.getSize() + 2;
151 lastGroup = cmd.fGroup;
152 }
153
154 canvas->drawSimpleText(cmd.fKeyName.c_str(), cmd.fKeyName.size(), SkTextEncoding::kUTF8,
155 x, y, font, paint);
156 SkString text = SkStringPrintf(": %s", cmd.fDescription.c_str());
157 canvas->drawString(text, x + keyWidth, y, font, paint);
158 y += font.getSize() + 2;
159 }
160}
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkIntToScalar(x)
Definition SkScalar.h:57
SK_API SkString static SkString SkStringPrintf()
Definition SkString.h:287
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
void drawPaint(const SkPaint &paint)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
SkScalar getSize() const
Definition SkFont.h:217
void setSize(SkScalar textSize)
Definition SkFont.cpp:129
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
const Paint & paint
float SkScalar
Definition extension.cpp:12
std::u16string text
double y
double x
SkFont DefaultPortableFont()
font
Font Metadata and Metrics.

◆ getCommandsAsSoftkeys()

std::vector< SkString > sk_app::CommandSet::getCommandsAsSoftkeys ( ) const

Definition at line 162 of file CommandSet.cpp.

162 {
163 std::vector<SkString> result;
164 for(const Command& command : fCommands) {
165 result.push_back(command.getSoftkeyString());
166 }
167 return result;
168}
GAsyncResult * result
list command
Definition valgrind.py:24

◆ onChar()

bool sk_app::CommandSet::onChar ( SkUnichar  c,
skui::ModifierKey  modifiers 
)

Definition at line 58 of file CommandSet.cpp.

58 {
59 for (Command& cmd : fCommands) {
60 if (Command::kChar_CommandType == cmd.fType && c == cmd.fChar) {
61 cmd.fFunction();
62 return true;
63 }
64 }
65
66 return false;
67}

◆ onKey()

bool sk_app::CommandSet::onKey ( skui::Key  key,
skui::InputState  state,
skui::ModifierKey  modifiers 
)

Definition at line 45 of file CommandSet.cpp.

45 {
47 for (Command& cmd : fCommands) {
48 if (Command::kKey_CommandType == cmd.fType && key == cmd.fKey) {
49 cmd.fFunction();
50 return true;
51 }
52 }
53 }
54
55 return false;
56}
AtkStateType state

◆ onSoftkey()

bool sk_app::CommandSet::onSoftkey ( const SkString softkey)

Definition at line 69 of file CommandSet.cpp.

69 {
70 for (const Command& cmd : fCommands) {
71 if (cmd.getSoftkeyString().equals(softkey)) {
72 cmd.fFunction();
73 return true;
74 }
75 }
76 return false;
77}

The documentation for this class was generated from the following files: