From b73c24b01a411843a98d4ccab7a39341d927e7fd Mon Sep 17 00:00:00 2001 From: liyuqian Date: Fri, 3 Jun 2016 08:47:23 -0700 Subject: [PATCH] Add Softkey UIState to Viewer We can use this to simulate any key/command on Android UI. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2035923002 Review-Url: https://codereview.chromium.org/2035923002 --- tools/viewer/Viewer.cpp | 25 ++++++++++++++++++++++--- tools/viewer/sk_app/CommandSet.cpp | 18 ++++++++++++++++++ tools/viewer/sk_app/CommandSet.h | 8 ++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp index c73b1ba9ce..7e345c5bbc 100644 --- a/tools/viewer/Viewer.cpp +++ b/tools/viewer/Viewer.cpp @@ -65,6 +65,9 @@ const char* kValue = "value"; const char* kOptions = "options"; const char* kSlideStateName = "Slide"; const char* kBackendStateName = "Backend"; +const char* kSoftkeyStateName = "Softkey"; +const char* kSoftkeyHint = "Please select a softkey"; + Viewer::Viewer(int argc, char** argv, void* platformData) : fCurrentMeasurement(0) @@ -411,6 +414,7 @@ void Viewer::onIdle(double ms) { } void Viewer::updateUIState() { + // Slide state Json::Value slideState(Json::objectValue); slideState[kName] = kSlideStateName; slideState[kValue] = fSlides[fCurrentSlide]->getName().c_str(); @@ -420,19 +424,29 @@ void Viewer::updateUIState() { } slideState[kOptions] = allSlideNames; - // This state is currently a demo for the one without options. - // We will be able to change the backend too. + // Backend state Json::Value backendState(Json::objectValue); backendState[kName] = kBackendStateName; backendState[kValue] = kBackendTypeStrings[fBackendType]; backendState[kOptions] = Json::Value(Json::arrayValue); - for(auto str : kBackendTypeStrings) { + for (auto str : kBackendTypeStrings) { backendState[kOptions].append(Json::Value(str)); } + // Softkey state + Json::Value softkeyState(Json::objectValue); + softkeyState[kName] = kSoftkeyStateName; + softkeyState[kValue] = kSoftkeyHint; + softkeyState[kOptions] = Json::Value(Json::arrayValue); + softkeyState[kOptions].append(kSoftkeyHint); + for (const auto& softkey : fCommands.getCommandsAsSoftkeys()) { + softkeyState[kOptions].append(Json::Value(softkey.c_str())); + } + Json::Value state(Json::arrayValue); state.append(slideState); state.append(backendState); + state.append(softkeyState); fWindow->setUIState(state); } @@ -470,6 +484,11 @@ void Viewer::onUIStateChanged(const SkString& stateName, const SkString& stateVa break; } } + } else if (stateName.equals(kSoftkeyStateName)) { + if (!stateValue.equals(kSoftkeyHint)) { + fCommands.onSoftkey(stateValue); + updateUIState(); // This is still needed to reset the value to kSoftkeyHint + } } else { SkDebugf("Unknown stateName: %s", stateName.c_str()); } diff --git a/tools/viewer/sk_app/CommandSet.cpp b/tools/viewer/sk_app/CommandSet.cpp index e426eaab21..4805e6aafe 100644 --- a/tools/viewer/sk_app/CommandSet.cpp +++ b/tools/viewer/sk_app/CommandSet.cpp @@ -71,6 +71,16 @@ bool CommandSet::onChar(SkUnichar c, uint32_t modifiers) { return false; } +bool CommandSet::onSoftkey(const SkString& softkey) { + for (const Command& cmd : fCommands) { + if (cmd.getSoftkeyString().equals(softkey)) { + cmd.fFunction(); + return true; + } + } + return false; +} + void CommandSet::addCommand(SkUnichar c, const char* group, const char* description, std::function function) { fCommands.push_back(Command(c, group, description, function)); @@ -154,4 +164,12 @@ void CommandSet::drawHelp(SkCanvas* canvas) { } } +std::vector CommandSet::getCommandsAsSoftkeys() const { + std::vector result; + for(const Command& command : fCommands) { + result.push_back(command.getSoftkeyString()); + } + return result; +} + } // namespace sk_app diff --git a/tools/viewer/sk_app/CommandSet.h b/tools/viewer/sk_app/CommandSet.h index 5e6373c935..4cbb367e01 100644 --- a/tools/viewer/sk_app/CommandSet.h +++ b/tools/viewer/sk_app/CommandSet.h @@ -12,6 +12,7 @@ #include "Window.h" #include +#include class SkCanvas; @@ -40,6 +41,7 @@ public: 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); + bool onSoftkey(const SkString& softkey); void addCommand(SkUnichar c, const char* group, const char* description, std::function function); @@ -48,6 +50,8 @@ public: void drawHelp(SkCanvas* canvas); + std::vector getCommandsAsSoftkeys() const; + private: struct Command { enum CommandType { @@ -86,6 +90,10 @@ private: SkString fGroup; SkString fDescription; std::function fFunction; + + SkString getSoftkeyString() const { + return SkStringPrintf("%s (%s)", fKeyName.c_str(), fDescription.c_str()); + } }; static bool compareCommandKey(const Command& first, const Command& second);