File: ink_drop_example.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (157 lines) | stat: -rw-r--r-- 6,771 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 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/ink_drop_example.h"

#include <memory>
#include <utility>

#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/events/event.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/animation/flood_fill_ink_drop_ripple.h"
#include "ui/views/animation/ink_drop_host.h"
#include "ui/views/animation/ink_drop_state.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/examples/examples_color_id.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"

namespace views::examples {

class InkDropView : public View {
  METADATA_HEADER(InkDropView, View)

 public:
  InkDropView() = default;
  InkDropView(const InkDropView&) = delete;
  InkDropView& operator=(const InkDropView&) = delete;
  ~InkDropView() override = default;

  // View:
  void OnThemeChanged() override {
    View::OnThemeChanged();
    InkDrop::Get(this)->SetBaseColor(GetColorProvider()->GetColor(
        ExamplesColorIds::kColorInkDropExampleBase));
  }
};

BEGIN_METADATA(InkDropView)
END_METADATA

BEGIN_VIEW_BUILDER(, InkDropView, View)
END_VIEW_BUILDER

}  // namespace views::examples

DEFINE_VIEW_BUILDER(, views::examples::InkDropView)

namespace views::examples {

InkDropExample::InkDropExample() : ExampleBase("FloodFill Ink Drop") {}

InkDropExample::InkDropExample(const char* title) : ExampleBase(title) {}

InkDropExample::~InkDropExample() = default;

void InkDropExample::CreateExampleView(View* container) {
  BoxLayoutView* box_layout_view = nullptr;
  auto get_callback = [this](InkDropState state) {
    return base::BindRepeating(&InkDropExample::SetInkDropState,
                               base::Unretained(this), state);
  };
  Builder<View>(container)
      .SetUseDefaultFillLayout(true)
      .AddChild(
          Builder<BoxLayoutView>()
              .CopyAddressTo(&box_layout_view)
              .SetOrientation(BoxLayout::Orientation::kVertical)
              .SetMainAxisAlignment(BoxLayout::MainAxisAlignment::kEnd)
              .AddChildren(
                  Builder<InkDropView>()
                      .CopyAddressTo(&ink_drop_view_)
                      .SetBorder(CreateRoundedRectBorder(
                          1, 4, ExamplesColorIds::kColorInkDropExampleBorder))
                      .SetProperty(kMarginsKey, gfx::Insets(10)),
                  Builder<BoxLayoutView>()
                      .SetOrientation(BoxLayout::Orientation::kHorizontal)
                      .SetCrossAxisAlignment(
                          BoxLayout::CrossAxisAlignment::kCenter)
                      .SetPreferredSize(gfx::Size(0, 50))
                      .AddChildren(
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(
                                  ToString(InkDropState::HIDDEN)))
                              .SetCallback(get_callback(InkDropState::HIDDEN))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(
                                  ToString(InkDropState::ACTION_PENDING)))
                              .SetCallback(
                                  get_callback(InkDropState::ACTION_PENDING))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(
                                  ToString(InkDropState::ACTION_TRIGGERED)))
                              .SetCallback(
                                  get_callback(InkDropState::ACTION_TRIGGERED))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(ToString(
                                  InkDropState::ALTERNATE_ACTION_PENDING)))
                              .SetCallback(get_callback(
                                  InkDropState::ALTERNATE_ACTION_PENDING))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(ToString(
                                  InkDropState::ALTERNATE_ACTION_TRIGGERED)))
                              .SetCallback(get_callback(
                                  InkDropState::ALTERNATE_ACTION_TRIGGERED))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(
                                  ToString(InkDropState::ACTIVATED)))
                              .SetCallback(
                                  get_callback(InkDropState::ACTIVATED))
                              .SetProperty(kMarginsKey, gfx::Insets::VH(0, 5)),
                          Builder<MdTextButton>()
                              .SetText(base::ASCIIToUTF16(
                                  ToString(InkDropState::DEACTIVATED)))
                              .SetCallback(
                                  get_callback(InkDropState::DEACTIVATED))
                              .SetProperty(kMarginsKey,
                                           gfx::Insets::VH(0, 5)))))
      .BuildChildren();
  box_layout_view->SetFlexForView(ink_drop_view_, 1);
  CreateInkDrop();
}

void InkDropExample::CreateInkDrop() {
  auto ink_drop_host = std::make_unique<InkDropHost>(ink_drop_view_);
  ink_drop_host->SetMode(InkDropHost::InkDropMode::ON);
  InkDrop::Install(ink_drop_view_, std::move(ink_drop_host));
}

void InkDropExample::SetInkDropState(InkDropState state) {
  ui::MouseEvent event(
      ui::EventType::kMousePressed,
      gfx::PointF(ink_drop_view_->GetLocalBounds().CenterPoint()),
      gfx::PointF(ink_drop_view_->origin()), base::TimeTicks(), 0, 0);
  ui::ScopedAnimationDurationScaleMode scale(
      ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
  InkDrop::Get(ink_drop_view_)->AnimateToState(state, &event);
}

}  // namespace views::examples