File: vector_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 (320 lines) | stat: -rw-r--r-- 11,456 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "ui/views/examples/vector_example.h"

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

#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.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 "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/models/simple_combobox_model.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/editable_combobox/editable_combobox.h"
#include "ui/views/controls/image_view.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/examples/grit/views_examples_resources.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/table_layout.h"
#include "ui/views/view.h"

using l10n_util::GetStringUTF16;
using l10n_util::GetStringUTF8;

namespace views::examples {

namespace {

class VectorIconGallery : public View, public TextfieldController {
  METADATA_HEADER(VectorIconGallery, View)

 public:
  VectorIconGallery() {
    size_input_ = AddChildView(std::make_unique<Textfield>());
    color_input_ = AddChildView(std::make_unique<Textfield>());

    image_view_container_ = AddChildView(std::make_unique<views::View>());

    BoxLayout* box = SetLayoutManager(std::make_unique<BoxLayout>(
        BoxLayout::Orientation::kVertical, gfx::Insets(10), 10));
    box->SetFlexForView(image_view_container_, 1);

    base::FilePath test_dir;
    base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &test_dir);
    std::u16string base_path = test_dir.AsUTF16Unsafe();
    std::vector<std::u16string> icon_dir = {
        base::FilePath(test_dir.AppendASCII("ash")
                           .AppendASCII("resources")
                           .AppendASCII("vector_icons"))
            .AsUTF16Unsafe(),
        base::FilePath(
            test_dir.AppendASCII("chrome").AppendASCII("app").AppendASCII(
                "vector_icons"))
            .AsUTF16Unsafe(),
        base::FilePath(test_dir.AppendASCII("chromeos")
                           .AppendASCII("ui")
                           .AppendASCII("vector_icons"))
            .AsUTF16Unsafe(),
        base::FilePath(
            test_dir.AppendASCII("components").AppendASCII("vector_icons"))
            .AsUTF16Unsafe(),
        base::FilePath(test_dir.AppendASCII("components")
                           .AppendASCII("omnibox")
                           .AppendASCII("browser")
                           .AppendASCII("vector_icons"))
            .AsUTF16Unsafe(),
        base::FilePath(
            test_dir.AppendASCII("ui").AppendASCII("views").AppendASCII(
                "vector_icons"))
            .AsUTF16Unsafe(),
    };
    auto editable_combobox = std::make_unique<views::EditableCombobox>();
    editable_combobox->SetModel(std::make_unique<ui::SimpleComboboxModel>(
        std::vector<ui::SimpleComboboxModel::Item>(icon_dir.begin(),
                                                   icon_dir.end())));
    editable_combobox->SetPlaceholderText(
        GetStringUTF16(IDS_VECTOR_FILE_SELECT_LABEL));
    editable_combobox->GetViewAccessibility().SetName(u"Editable Combobox");

    auto file_container = std::make_unique<View>();
    BoxLayout* file_box =
        file_container->SetLayoutManager(std::make_unique<BoxLayout>(
            BoxLayout::Orientation::kHorizontal, gfx::Insets(10), 10));
    file_chooser_combobox_ =
        file_container->AddChildView(std::move(editable_combobox));

    file_go_button_ =
        file_container->AddChildView(std::make_unique<MdTextButton>(
            base::BindRepeating(&VectorIconGallery::FileGoButtonPressed,
                                base::Unretained(this)),
            GetStringUTF16(IDS_VECTOR_RENDER_LABEL)));
    file_box->SetFlexForView(file_chooser_combobox_, 1);
    AddChildView(std::move(file_container));

    size_input_->SetPlaceholderText(
        GetStringUTF16(IDS_VECTOR_DIP_SIZE_DESC_LABEL));
    size_input_->set_controller(this);
    color_input_->SetPlaceholderText(
        GetStringUTF16(IDS_VECTOR_COLOR_DESC_LABEL));
    color_input_->set_controller(this);
  }

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

  ~VectorIconGallery() override = default;

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

      return;
    }

    DCHECK_EQ(color_input_, sender);
    if (new_contents.size() != 8u) {
      return;
    }
    unsigned new_color =
        strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16);
    if (new_color <= 0xffffffff) {
      color_ = new_color;
      Update();
    }
  }

 private:
  void FileGoButtonPressed() {
#if BUILDFLAG(IS_WIN)
    base::FilePath path(base::UTF16ToWide(file_chooser_combobox_->GetText()));
#else
    base::FilePath path(base::UTF16ToUTF8(file_chooser_combobox_->GetText()));
#endif

    // If there is an extension, then it would not be a folder.
    if (path.Extension().size() == 0) {
      GenerateAllIconInFolder(path);
    } else {
      GenerateSingleIcon(path);
    }
  }

  void GenerateSingleIcon(const base::FilePath& path) {
    image_view_container_->RemoveAllChildViews();
    image_view_ =
        image_view_container_->AddChildView(std::make_unique<ImageView>());
    image_view_->SetBorder(
        CreateSolidBorder(1, ExamplesColorIds::kColorVectorExampleImageBorder));

    auto image_layout =
        std::make_unique<BoxLayout>(BoxLayout::Orientation::kHorizontal);
    image_layout->set_cross_axis_alignment(
        BoxLayout::CrossAxisAlignment::kCenter);
    image_layout->set_main_axis_alignment(
        BoxLayout::MainAxisAlignment::kCenter);
    image_view_container_->SetLayoutManager(std::move(image_layout));

    base::ScopedAllowBlockingForTesting allow_blocking;
    base::ReadFileToString(path, &contents_);
    contents_ = CleanUpContents(contents_);
    Update();
  }

  void GenerateAllIconInFolder(const base::FilePath& path) {
    image_view_container_->RemoveAllChildViews();

    int nCols = image_view_container_->width() / size_;
    int kColumnWidth = size_;
    views::TableLayout* layout = image_view_container_->SetLayoutManager(
        std::make_unique<views::TableLayout>());
    for (int i = 0; i < nCols; ++i) {
      layout->AddColumn(LayoutAlignment::kStretch, LayoutAlignment::kStretch,
                        TableLayout::kFixedSize,
                        TableLayout::ColumnSize::kFixed, kColumnWidth,
                        kColumnWidth);
    }

    int nRows = image_view_container_->height() / size_;
    for (int i = 0; i < nRows; ++i) {
      layout->AddRows(1, TableLayout::kFixedSize);
    }
    size_t max = nCols * nRows;
    size_t count = 0;

    {
      base::ScopedAllowBlockingForTesting allow_blocking;
      base::FileEnumerator file_iter(path, false, base::FileEnumerator::FILES,
                                     FILE_PATH_LITERAL("*.icon"));
      std::vector<base::FilePath> files;
      for (base::FilePath input_file = file_iter.Next(); !input_file.empty();
           input_file = file_iter.Next()) {
        files.push_back(input_file);
      }
      std::sort(files.begin(), files.end());

      for (base::FilePath file : files) {
        if (count++ >= max) {
          break;
        }
        std::string file_content;
        base::ReadFileToString(file, &file_content);

        ImageView* icon_view =
            image_view_container_->AddChildView(std::make_unique<ImageView>());
        icon_view->SetImage(
            ui::ImageModel::FromImageSkia(gfx::CreateVectorIconFromSource(
                CleanUpContents(file_content), size_, color_)));
        icon_view->SetTooltipText(file.BaseName().AsUTF16Unsafe());
        file = file_iter.Next();
      }
    }
    InvalidateLayout();
  }

  void Update() {
    if (!contents_.empty() && image_view_ != nullptr) {
      image_view_->SetImage(ui::ImageModel::FromImageSkia(
          gfx::CreateVectorIconFromSource(contents_, size_, color_)));
    }
    InvalidateLayout();
  }

  std::string CleanUpContents(const std::string& file_content) {
    // Skip over comments.
    // This handles very basic cases of // and /*. More complicated edge
    // cases such as /* /* */ */ are not handled.
    std::string output = file_content;
    for (size_t slashes = output.find("//"); slashes != std::string::npos;
         slashes = output.find("//")) {
      size_t eol = output.find("\n", slashes);
      // Add 1 to erase the \n token at the end of the line.
      output.erase(slashes, eol - slashes + 1);
    }

    for (size_t slashes = output.find("/*"); slashes != std::string::npos;
         slashes = output.find("/*")) {
      size_t eol = output.find("*/", slashes);
      output.erase(slashes, eol - slashes + 2);
    }

    // CreateVectorIconFromSource does not work well if there are multiple icon
    // sizes in the same file. Fetch the first icon source in the file.
    std::string result = output;
    size_t start = 0;
    std::string token = "\n\n";
    size_t end = output.find(token);
    while (end != std::string::npos) {
      std::string section = output.substr(start, end - start);
      if (!section.empty() &&
          section.find_first_not_of("\t\n\v\f\r") != std::string::npos) {
        result = section;
        break;
      }
      start = end;
      end = output.find(token, end + token.length());
    }
    return result;
  }

  // 36dp is one of the natural sizes for MD icons, and corresponds roughly to a
  // 32dp usable area.
  int size_ = 36;
  SkColor color_ = gfx::kPlaceholderColor;

  raw_ptr<ImageView> image_view_;
  raw_ptr<View> image_view_container_;
  raw_ptr<Textfield> size_input_;
  raw_ptr<Textfield> color_input_;
  raw_ptr<EditableCombobox> file_chooser_combobox_;
  raw_ptr<Button> file_go_button_;
  std::string contents_;
};

BEGIN_METADATA(VectorIconGallery)
END_METADATA

}  // namespace

VectorExample::VectorExample()
    : ExampleBase(GetStringUTF8(IDS_VECTOR_SELECT_LABEL).c_str()) {}

VectorExample::~VectorExample() = default;

void VectorExample::CreateExampleView(View* container) {
  container->SetLayoutManager(std::make_unique<FillLayout>());
  container->AddChildView(std::make_unique<VectorIconGallery>());
}

}  // namespace views::examples