File: animated_image_view_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 (155 lines) | stat: -rw-r--r-- 5,428 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
// Copyright 2018 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/animated_image_view_example.h"

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "cc/paint/skottie_wrapper.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/lottie/animation.h"
#include "ui/views/border.h"
#include "ui/views/controls/animated_image_view.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/examples/examples_color_id.h"
#include "ui/views/layout/box_layout_view.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"

namespace views::examples {

namespace {

// This class can load a skottie(and lottie) animation file from disk and play
// it in a view as AnimatedImageView.
// See https://skia.org/user/modules/skottie for more info on skottie.
class AnimationGallery : public BoxLayoutView, public TextfieldController {
  METADATA_HEADER(AnimationGallery, BoxLayoutView)

 public:
  AnimationGallery() {
    View* image_view_container = nullptr;
    BoxLayoutView* file_container = nullptr;
    Builder<BoxLayoutView>(this)
        .SetOrientation(BoxLayout::Orientation::kVertical)
        .SetInsideBorderInsets(gfx::Insets(10))
        .SetBetweenChildSpacing(10)
        .AddChildren(
            Builder<Textfield>()
                .CopyAddressTo(&size_input_)
                .SetPlaceholderText(u"Size in dip (Empty for default)")
                .SetController(this),
            Builder<View>()
                .CopyAddressTo(&image_view_container)
                .SetUseDefaultFillLayout(true)
                .AddChild(
                    Builder<AnimatedImageView>()
                        .CopyAddressTo(&animated_image_view_)
                        .SetBorder(CreateSolidBorder(
                            1, ExamplesColorIds::
                                   kColorAnimatedImageViewExampleBorder))),
            Builder<BoxLayoutView>()
                .CopyAddressTo(&file_container)
                .SetInsideBorderInsets(gfx::Insets(10))
                .SetBetweenChildSpacing(10)
                .AddChildren(
                    Builder<Textfield>()
                        .CopyAddressTo(&file_chooser_)
                        .SetPlaceholderText(u"Enter path to lottie JSON file"),
                    Builder<MdTextButton>()
                        .SetCallback(base::BindRepeating(
                            &AnimationGallery::ButtonPressed,
                            base::Unretained(this)))
                        .SetText(u"Render")))
        .BuildChildren();
    SetFlexForView(image_view_container, 1);
    file_container->SetFlexForView(file_chooser_, 1);
  }

  AnimationGallery(const AnimationGallery&) = delete;
  AnimationGallery& operator=(const AnimationGallery&) = delete;

  ~AnimationGallery() override = default;

  // TextfieldController:
  void ContentsChanged(Textfield* sender,
                       const std::u16string& new_contents) override {
    if (sender == size_input_) {
      if (!base::StringToInt(new_contents, &size_) && (size_ > 0)) {
        size_ = 0;
        size_input_->SetText(std::u16string());
      }
      Update();
    }
  }

  void ButtonPressed(const ui::Event& event) {
    std::string json;
    base::ScopedAllowBlockingForTesting allow_blocking;
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
    base::FilePath path(base::UTF16ToUTF8(file_chooser_->GetText()));
#else
    base::FilePath path(base::UTF16ToWide(file_chooser_->GetText()));
#endif  // BUILDFLAG(IS_POSIX)
    if (base::ReadFileToString(path, &json)) {
      auto skottie = cc::SkottieWrapper::UnsafeCreateSerializable(
          std::vector<uint8_t>(json.begin(), json.end()));
      animated_image_view_->SetAnimatedImage(
          std::make_unique<lottie::Animation>(skottie));
      animated_image_view_->Play();
      Update();
    }
  }

 private:
  void Update() {
    if (size_ > 24) {
      animated_image_view_->SetImageSize(gfx::Size(size_, size_));
    } else {
      animated_image_view_->ResetImageSize();
    }
    InvalidateLayout();
  }

  raw_ptr<AnimatedImageView> animated_image_view_ = nullptr;
  raw_ptr<Textfield> size_input_ = nullptr;
  raw_ptr<Textfield> file_chooser_ = nullptr;

  int size_ = 0;
};

BEGIN_METADATA(AnimationGallery)
END_METADATA

}  // namespace

AnimatedImageViewExample::AnimatedImageViewExample()
    : ExampleBase("Animated Image View") {}

AnimatedImageViewExample::~AnimatedImageViewExample() = default;

void AnimatedImageViewExample::CreateExampleView(View* container) {
  container->SetUseDefaultFillLayout(true);
  container->AddChildView(std::make_unique<AnimationGallery>());
}

}  // namespace views::examples