1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/examples/scroll_view_example.h"
#include <memory>
#include <utility>
#include "base/strings/utf_string_conversions.h"
#include "cc/paint/paint_flags.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/radio_button.h"
#include "ui/views/examples/examples_color_id.h"
#include "ui/views/examples/grit/views_examples_resources.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
using l10n_util::GetStringUTF16;
using l10n_util::GetStringUTF8;
namespace views::examples {
// ScrollView's content, which draws gradient color on background.
// TODO(oshima): add child views as well.
class ScrollViewExample::ScrollableView : public BoxLayoutView {
public:
ScrollableView() {
SetColor(ExamplesColorIds::kColorScrollViewExampleTallFrom,
ExamplesColorIds::kColorScrollViewExampleTallTo);
SetOrientation(views::BoxLayout::Orientation::kVertical);
SetInsideBorderInsets(gfx::Insets());
SetBetweenChildSpacing(0);
const auto add_child = [this](std::unique_ptr<View> view) {
auto* container = AddChildView(std::make_unique<BoxLayoutView>());
container->SetOrientation(views::BoxLayout::Orientation::kVertical);
container->AddChildView(std::move(view));
};
add_child(std::make_unique<LabelButton>(
Button::PressedCallback(),
GetStringUTF16(IDS_SCROLL_VIEW_BUTTON_LABEL)));
add_child(std::make_unique<RadioButton>(
GetStringUTF16(IDS_SCROLL_VIEW_RADIO_BUTTON_LABEL), 0));
SetDefaultFlex(1);
}
ScrollableView(const ScrollableView&) = delete;
ScrollableView& operator=(const ScrollableView&) = delete;
void SetColor(ui::ColorId from_id, ui::ColorId to_id) {
from_color_id_ = from_id;
to_color_id_ = to_id;
}
std::pair<SkColor, SkColor> GetColors() const {
auto* const cp = GetColorProvider();
if (!cp) {
return {gfx::kPlaceholderColor, gfx::kPlaceholderColor};
}
return {cp->GetColor(from_color_id_), cp->GetColor(to_color_id_)};
}
void OnPaintBackground(gfx::Canvas* canvas) override {
cc::PaintFlags flags;
std::pair<SkColor, SkColor> gradient_colors = GetColors();
flags.setShader(gfx::CreateGradientShader(
gfx::Point(), gfx::Point(0, height()), gradient_colors.first,
gradient_colors.second));
flags.setStyle(cc::PaintFlags::kFill_Style);
canvas->DrawRect(GetLocalBounds(), flags);
}
private:
ui::ColorId from_color_id_;
ui::ColorId to_color_id_;
};
ScrollViewExample::ScrollViewExample()
: ExampleBase(GetStringUTF8(IDS_SCROLL_VIEW_SELECT_LABEL).c_str()) {}
ScrollViewExample::~ScrollViewExample() = default;
void ScrollViewExample::CreateExampleView(View* container) {
auto scroll_view = std::make_unique<ScrollView>();
scrollable_ = scroll_view->SetContents(std::make_unique<ScrollableView>());
scrollable_->SetBounds(0, 0, 1000, 100);
scrollable_->SetColor(ExamplesColorIds::kColorScrollViewExampleWideFrom,
ExamplesColorIds::kColorScrollViewExampleWideTo);
container->SetLayoutManager(std::make_unique<FlexLayout>())
->SetOrientation(LayoutOrientation::kVertical);
auto full_flex = FlexSpecification(MinimumFlexSizeRule::kScaleToZero,
MaximumFlexSizeRule::kUnbounded)
.WithWeight(1);
// Add scroll view.
scroll_view_ = container->AddChildView(std::move(scroll_view));
scroll_view_->SetProperty(views::kFlexBehaviorKey, full_flex);
// Add control buttons.
auto* button_panel = container->AddChildView(std::make_unique<View>());
button_panel->SetLayoutManager(std::make_unique<FlexLayout>())
->SetOrientation(LayoutOrientation::kHorizontal);
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&ScrollViewExample::ButtonPressed,
base::Unretained(this), gfx::Rect(0, 0, 1000, 100),
ExamplesColorIds::kColorScrollViewExampleWideFrom,
ExamplesColorIds::kColorScrollViewExampleWideTo),
GetStringUTF16(IDS_SCROLL_VIEW_WIDE_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&ScrollViewExample::ButtonPressed,
base::Unretained(this), gfx::Rect(0, 0, 100, 1000),
ExamplesColorIds::kColorScrollViewExampleTallFrom,
ExamplesColorIds::kColorScrollViewExampleTallTo),
GetStringUTF16(IDS_SCROLL_VIEW_TALL_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(
&ScrollViewExample::ButtonPressed, base::Unretained(this),
gfx::Rect(0, 0, 1000, 1000),
ExamplesColorIds::kColorScrollViewExampleBigSquareFrom,
ExamplesColorIds::kColorScrollViewExampleBigSquareTo),
GetStringUTF16(IDS_SCROLL_VIEW_BIG_SQUARE_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(
&ScrollViewExample::ButtonPressed, base::Unretained(this),
gfx::Rect(0, 0, 100, 100),
ExamplesColorIds::kColorScrollViewExampleSmallSquareFrom,
ExamplesColorIds::kColorScrollViewExampleSmallSquareTo),
GetStringUTF16(IDS_SCROLL_VIEW_SMALL_SQUARE_LABEL)));
button_panel->AddChildView(std::make_unique<LabelButton>(
base::BindRepeating(&View::ScrollRectToVisible,
base::Unretained(scroll_view_->contents()),
gfx::Rect(20, 500, 1000, 500)),
GetStringUTF16(IDS_SCROLL_VIEW_SCROLL_TO_LABEL)));
for (View* child : button_panel->children()) {
child->SetProperty(views::kFlexBehaviorKey, full_flex);
}
}
void ScrollViewExample::ButtonPressed(gfx::Rect bounds,
ui::ColorId from,
ui::ColorId to) {
scrollable_->SetBoundsRect(std::move(bounds));
scrollable_->SetColor(from, to);
}
} // namespace views::examples
|