From 0ffa322ecd313badaa006d950d64d2efad6185f9 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Thu, 5 Apr 2018 14:34:45 -0400 Subject: [PATCH] [viewer] Consolidate external slide loading Handle all external slides (.skp, .jpg, .json, .svg) consistently: * support both single-file/directory flags * add "directory" slides for all types Change-Id: I4881c51f597ed4cb2a5b4c61706a168c754a5a8f Reviewed-on: https://skia-review.googlesource.com/119020 Reviewed-by: Brian Osman Commit-Queue: Florin Malita --- tools/viewer/Viewer.cpp | 122 +++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 71 deletions(-) diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp index fb659555e2..9b6a89adf1 100644 --- a/tools/viewer/Viewer.cpp +++ b/tools/viewer/Viewer.cpp @@ -511,86 +511,66 @@ void Viewer::initSlides() { reg = reg->next(); } - // SKPs - for (int i = 0; i < FLAGS_skps.count(); i++) { - if (SkStrEndsWith(FLAGS_skps[i], ".skp")) { - if (SkCommandLineFlags::ShouldSkip(FLAGS_match, FLAGS_skps[i])) { - continue; - } + using SlideFactory = sk_sp(*)(const SkString& name, const SkString& path); + static const struct { + const char* fExtension; + const char* fDirName; + const SkCommandLineFlags::StringArray& fFlags; + const SlideFactory fFactory; + } gExternalSlidesInfo[] = { + { ".skp", "skp-dir", FLAGS_skps, + [](const SkString& name, const SkString& path) -> sk_sp { + return sk_make_sp(name, path);} + }, + { ".jpg", "jpg-dir", FLAGS_jpgs, + [](const SkString& name, const SkString& path) -> sk_sp { + return sk_make_sp(name, path);} + }, + { ".json", "skottie-dir", FLAGS_jsons, + [](const SkString& name, const SkString& path) -> sk_sp { + return sk_make_sp(name, path);} + }, + { ".svg", "svg-dir", FLAGS_svgs, + [](const SkString& name, const SkString& path) -> sk_sp { + return sk_make_sp(name, path);} + }, + }; - SkString path(FLAGS_skps[i]); - sk_sp slide(new SKPSlide(SkOSPath::Basename(path.c_str()), path)); - if (slide) { - fSlides.push_back(slide); - } - } else { - SkOSFile::Iter it(FLAGS_skps[i], ".skp"); - SkString skpName; - while (it.next(&skpName)) { - if (SkCommandLineFlags::ShouldSkip(FLAGS_match, skpName.c_str())) { - continue; - } + SkTArray, true> dirSlides; - SkString path = SkOSPath::Join(FLAGS_skps[i], skpName.c_str()); - sk_sp slide(new SKPSlide(skpName, path)); - if (slide) { - fSlides.push_back(slide); - } - } + const auto addSlide = [&](const SkString& name, + const SkString& path, + const SlideFactory& fact) { + if (SkCommandLineFlags::ShouldSkip(FLAGS_match, name.c_str())) { + return; } - } - // JPGs - for (int i = 0; i < FLAGS_jpgs.count(); i++) { - SkOSFile::Iter it(FLAGS_jpgs[i], ".jpg"); - SkString jpgName; - while (it.next(&jpgName)) { - if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jpgName.c_str())) { - continue; - } - - SkString path = SkOSPath::Join(FLAGS_jpgs[i], jpgName.c_str()); - sk_sp slide(new ImageSlide(jpgName, path)); - if (slide) { - fSlides.push_back(slide); - } - } - } - - // JSONs - for (const auto& json : FLAGS_jsons) { - SkTArray, true> dirSlides; - - SkOSFile::Iter it(json.c_str(), ".json"); - SkString jsonName; - while (it.next(&jsonName)) { - if (SkCommandLineFlags::ShouldSkip(FLAGS_match, jsonName.c_str())) { - continue; - } - auto slide = sk_make_sp(jsonName, SkOSPath::Join(json.c_str(), - jsonName.c_str())); + if (auto slide = fact(name, path)) { dirSlides.push_back(slide); fSlides.push_back(std::move(slide)); } + }; - if (!dirSlides.empty()) { - fSlides.push_back(sk_make_sp(SkStringPrintf("skottie-dir[%s]", json.c_str()), - std::move(dirSlides))); - } - } - - // SVGs - for (const auto& svg : FLAGS_svgs) { - SkOSFile::Iter it(svg.c_str(), ".svg"); - - SkString svgName; - while (it.next(&svgName)) { - if (SkCommandLineFlags::ShouldSkip(FLAGS_match, svgName.c_str())) { - continue; + for (const auto& info : gExternalSlidesInfo) { + for (const auto& flag : info.fFlags) { + if (SkStrEndsWith(flag.c_str(), info.fExtension)) { + // single file + addSlide(SkOSPath::Basename(flag.c_str()), flag, info.fFactory); + } else { + // directory + SkOSFile::Iter it(flag.c_str(), info.fExtension); + SkString name; + while (it.next(&name)) { + addSlide(name, SkOSPath::Join(flag.c_str(), name.c_str()), info.fFactory); + } + } + + if (!dirSlides.empty()) { + fSlides.push_back( + sk_make_sp(SkStringPrintf("%s[%s]", info.fDirName, flag.c_str()), + std::move(dirSlides))); + dirSlides.reset(); } - auto slide = sk_make_sp(svgName, SkOSPath::Join(svg.c_str(), - svgName.c_str())); - fSlides.push_back(std::move(slide)); } } }