Commit 2fff47ae authored by Allen Bauer's avatar Allen Bauer Committed by Commit Bot

Added support for command-line switch to select individual examples.

Hide the example combo-box selector if there is only one example to show.

Part of Views Documentation effort.

Change-Id: I9019148e54d7162bfb8b95a88c515504f130e7d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2153307
Commit-Queue: Allen Bauer <kylixrd@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760990}
parent 604772b9
......@@ -41,20 +41,19 @@ ComboboxExample::ComboboxExample() : ExampleBase("Combo Box") {}
ComboboxExample::~ComboboxExample() = default;
void ComboboxExample::CreateExampleView(View* container) {
combobox_ = new Combobox(std::make_unique<ComboboxModelExample>());
container->SetLayoutManager(std::make_unique<BoxLayout>(
BoxLayout::Orientation::kVertical, gfx::Insets(10, 0), 5));
combobox_ = container->AddChildView(
std::make_unique<Combobox>(std::make_unique<ComboboxModelExample>()));
combobox_->set_listener(this);
combobox_->SetSelectedIndex(3);
auto* disabled_combobox =
new Combobox(std::make_unique<ComboboxModelExample>());
auto* disabled_combobox = container->AddChildView(
std::make_unique<Combobox>(std::make_unique<ComboboxModelExample>()));
disabled_combobox->set_listener(this);
disabled_combobox->SetSelectedIndex(4);
disabled_combobox->SetEnabled(false);
container->SetLayoutManager(std::make_unique<BoxLayout>(
BoxLayout::Orientation::kVertical, gfx::Insets(10, 0), 5));
container->AddChildView(combobox_);
container->AddChildView(disabled_combobox);
}
void ComboboxExample::OnPerformAction(Combobox* combobox) {
......
......@@ -39,8 +39,8 @@ namespace views {
namespace examples {
// Creates the default set of examples.
ExampleVector CreateExamples() {
ExampleVector examples;
ExampleVector CreateExamples(ExampleVector extra_examples) {
ExampleVector examples = std::move(extra_examples);
examples.push_back(std::make_unique<AxExample>());
examples.push_back(std::make_unique<BoxLayoutExample>());
examples.push_back(std::make_unique<BubbleExample>());
......
......@@ -14,10 +14,9 @@
namespace views {
namespace examples {
using ExampleVector = std::vector<std::unique_ptr<ExampleBase>>;
// Creates the default set of examples.
ExampleVector VIEWS_EXAMPLES_EXPORT CreateExamples();
ExampleVector VIEWS_EXAMPLES_EXPORT
CreateExamples(ExampleVector extra_examples = ExampleVector());
} // namespace examples
} // namespace views
......
......@@ -6,6 +6,7 @@
#define UI_VIEWS_EXAMPLES_EXAMPLE_BASE_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "ui/views/examples/views_examples_export.h"
......@@ -38,6 +39,8 @@ class VIEWS_EXAMPLES_EXPORT ExampleBase {
DISALLOW_COPY_AND_ASSIGN(ExampleBase);
};
using ExampleVector = std::vector<std::unique_ptr<ExampleBase>>;
} // namespace examples
} // namespace views
......
......@@ -10,8 +10,11 @@
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/stl_util.h"
#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/combobox_model.h"
......@@ -29,21 +32,47 @@
namespace views {
namespace examples {
using ExampleVector = std::vector<std::unique_ptr<ExampleBase>>;
namespace {
struct ExampleTitleCompare {
bool operator()(const std::unique_ptr<ExampleBase>& a,
const std::unique_ptr<ExampleBase>& b) {
const char kEnableExamples[] = "enable-examples";
ExampleVector GetExamplesToShow(ExampleVector examples) {
using StringVector = std::vector<std::string>;
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
std::sort(examples.begin(), examples.end(), [](const auto& a, const auto& b) {
return a->example_title() < b->example_title();
});
std::string enable_examples =
command_line->GetSwitchValueASCII(kEnableExamples);
if (!enable_examples.empty()) {
StringVector enabled =
base::SplitString(enable_examples, ";,", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
// Transform list of examples to just the list of names.
StringVector example_names;
std::transform(
examples.begin(), examples.end(), std::back_inserter(example_names),
[](const auto& example) { return example->example_title(); });
std::sort(enabled.begin(), enabled.end());
// Get an intersection of list of titles between the full list and the list
// from the command-line.
StringVector valid_examples =
base::STLSetIntersection<StringVector>(enabled, example_names);
// If there are still example names in the list, only include the examples
// from the list.
if (!valid_examples.empty()) {
base::EraseIf(examples, [valid_examples](auto& example) {
return std::find(valid_examples.begin(), valid_examples.end(),
example->example_title()) == valid_examples.end();
});
}
}
};
ExampleVector GetExamplesToShow(ExampleVector extra) {
ExampleVector examples = CreateExamples();
std::move(extra.begin(), extra.end(), std::back_inserter(examples));
std::sort(examples.begin(), examples.end(), ExampleTitleCompare());
for (auto& example : examples)
example->CreateExampleView(example->example_view());
......@@ -104,7 +133,9 @@ class ExamplesWindowContents : public WidgetDelegateView,
layout->StartRow(0 /* no expand */, 0);
combobox_ = layout->AddView(std::move(combobox));
if (combobox_model_->GetItemCount() > 0) {
auto item_count = combobox_model_->GetItemCount();
if (item_count > 0) {
combobox_->SetVisible(item_count > 1);
layout->StartRow(1, 0);
auto example_shown = std::make_unique<View>();
example_shown->SetLayoutManager(std::make_unique<FillLayout>());
......@@ -176,12 +207,12 @@ class ExamplesWindowContents : public WidgetDelegateView,
ExamplesWindowContents* ExamplesWindowContents::instance_ = nullptr;
void ShowExamplesWindow(base::OnceClosure on_close,
gfx::NativeWindow window_context,
ExampleVector extra_examples) {
ExampleVector examples,
gfx::NativeWindow window_context) {
if (ExamplesWindowContents::instance()) {
ExamplesWindowContents::instance()->GetWidget()->Activate();
} else {
ExampleVector examples = GetExamplesToShow(std::move(extra_examples));
examples = GetExamplesToShow(std::move(examples));
Widget* widget = new Widget;
Widget::InitParams params;
params.delegate =
......
......@@ -11,21 +11,20 @@
#include "base/strings/stringprintf.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/examples/create_examples.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/examples/views_examples_export.h"
namespace views {
namespace examples {
class VIEWS_EXAMPLES_EXPORT ExampleBase;
// Shows a window with the views examples in it. |extra_examples| contains any
// additional examples to add. |window_context| is used to determine where the
// window should be created (see |Widget::InitParams::context| for details).
VIEWS_EXAMPLES_EXPORT void ShowExamplesWindow(
base::OnceClosure on_close,
gfx::NativeWindow window_context = nullptr,
std::vector<std::unique_ptr<ExampleBase>> extra_examples =
std::vector<std::unique_ptr<ExampleBase>>());
ExampleVector examples = CreateExamples(),
gfx::NativeWindow window_context = nullptr);
// Prints |string| in the status area, at the bottom of the window.
VIEWS_EXAMPLES_EXPORT void LogStatus(const std::string& string);
......
......@@ -9,6 +9,8 @@
#include <vector>
#include "content/public/browser/browser_context.h"
#include "ui/views/examples/create_examples.h"
#include "ui/views/examples/example_base.h"
#include "ui/views/examples/webview_example.h"
namespace views {
......@@ -17,10 +19,10 @@ namespace examples {
void ShowExamplesWindowWithContent(base::OnceClosure on_close,
content::BrowserContext* browser_context,
gfx::NativeWindow window_context) {
std::vector<std::unique_ptr<ExampleBase>> extra_examples;
extra_examples.push_back(std::make_unique<WebViewExample>(browser_context));
ShowExamplesWindow(std::move(on_close), window_context,
std::move(extra_examples));
ExampleVector examples;
examples.push_back(std::make_unique<WebViewExample>(browser_context));
ShowExamplesWindow(std::move(on_close), CreateExamples(std::move(examples)),
window_context);
}
} // namespace examples
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment