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

ash / style / style_viewer / cutout_instances_grid_view_factory.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/style/rounded_rect_cutout_path_builder.h"
#include "ash/style/style_viewer/system_ui_components_grid_view.h"
#include "ash/style/style_viewer/system_ui_components_grid_view_factories.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/views/background.h"
#include "ui/views/view.h"

namespace ash {

namespace {

constexpr size_t kGridViewRowNum = 5;
constexpr size_t kGridViewColNum = 4;
constexpr size_t kGridViewRowGroupSize = 1;
constexpr size_t kGridViewColGroupSize = 1;

// Create multiple cutouts with the same size.
struct CutoutsSpec {
  gfx::Size cutout_size;
  std::vector<RoundedRectCutoutPathBuilder::Corner> corners;
};

// Specify a size and location for a cutout.
struct CutoutSpec {
  gfx::Size size;
  RoundedRectCutoutPathBuilder::Corner corner;
};

struct CutoutEntry {
  std::u16string name;
  SkColor color;
  absl::variant<std::vector<CutoutSpec>, CutoutsSpec> cutouts;
  std::optional<int> radius;
  std::optional<int> outer_radius;
  std::optional<int> inner_radius;
};

class CutoutsGridView : public SystemUIComponentsGridView {
 public:
  CutoutsGridView()
      : SystemUIComponentsGridView(kGridViewRowNum,
                                   kGridViewColNum,
                                   kGridViewRowGroupSize,
                                   kGridViewColGroupSize) {}
  CutoutsGridView(const CutoutsGridView&) = delete;
  CutoutsGridView& operator=(const CutoutsGridView&) = delete;

  ~CutoutsGridView() override = default;

  void AddCutoutSample(const CutoutEntry& entry) {
    auto view = std::make_unique<views::View>();
    view->SetBackground(views::CreateSolidBackground(entry.color));
    view->SetPreferredSize(std::make_optional<gfx::Size>(200, 150));
    auto builder =
        RoundedRectCutoutPathBuilder(gfx::SizeF(view->GetPreferredSize()));
    if (absl::holds_alternative<std::vector<CutoutSpec>>(entry.cutouts)) {
      const auto& cutouts = absl::get<std::vector<CutoutSpec>>(entry.cutouts);
      if (!cutouts.empty()) {
        for (const auto& spec : cutouts) {
          builder.AddCutout(spec.corner, gfx::SizeF(spec.size));
        }
      }
    } else {
      const CutoutsSpec& cutouts = absl::get<CutoutsSpec>(entry.cutouts);
      if (!cutouts.corners.empty()) {
        for (const auto& corner : cutouts.corners) {
          builder.AddCutout(corner, gfx::SizeF(cutouts.cutout_size));
        }
      }
    }

    if (entry.radius.has_value()) {
      builder.CornerRadius(entry.radius.value());
    }

    if (entry.outer_radius.has_value()) {
      builder.CutoutOuterCornerRadius(entry.outer_radius.value());
    }

    if (entry.inner_radius.has_value()) {
      builder.CutoutInnerCornerRadius(entry.inner_radius.value());
    }

    view->SetClipPath(builder.Build());
    AddInstance(entry.name, std::move(view));
  }
};

std::vector<CutoutSpec> MakeSpecs(gfx::Size size,
                                  RoundedRectCutoutPathBuilder::Corner corner) {
  std::vector<CutoutSpec> specs;
  specs.emplace_back(size, corner);
  return specs;
}

}  // namespace

std::unique_ptr<SystemUIComponentsGridView> CreateCutoutsGridView() {
  const std::array<CutoutEntry, kGridViewRowNum * kGridViewColNum> entries = {{
      // Row 1
      {u"TopLeft", SK_ColorRED,
       CutoutsSpec{
           gfx::Size(40, 30),
           {RoundedRectCutoutPathBuilder::Corner::kUpperLeft},
       }},
      {u"TopRight", SK_ColorCYAN,
       CutoutsSpec{
           gfx::Size(30, 50),
           {RoundedRectCutoutPathBuilder::Corner::kUpperRight},
       }},
      {u"BottomLeft", SK_ColorGREEN,
       CutoutsSpec{
           gfx::Size(40, 40),
           {RoundedRectCutoutPathBuilder::Corner::kLowerLeft},
       }},
      {u"BottomRight", SK_ColorMAGENTA,
       CutoutsSpec{gfx::Size(30, 30),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight}}},
      // Row 2
      {u"TopLeft 4px corner", SK_ColorRED,
       CutoutsSpec{gfx::Size(80, 30),
                   {RoundedRectCutoutPathBuilder::Corner::kUpperLeft}},
       4},
      {u"TopRight 8px corner", SK_ColorCYAN,
       CutoutsSpec{gfx::Size(30, 50),
                   {RoundedRectCutoutPathBuilder::Corner::kUpperRight}},
       8},
      {u"BottomLeft 12px corner", SK_ColorGREEN,
       CutoutsSpec{gfx::Size(40, 40),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerLeft}},
       12},
      {u"BottomRight 20px corner", SK_ColorMAGENTA,
       CutoutsSpec{gfx::Size(30, 30),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight}},
       20},
      // Row 3
      {u"No cutout", SK_ColorBLACK, CutoutsSpec{gfx::Size(), {}}},
      {u"4px corner. 6px small, 12px inner", SK_ColorBLUE,
       CutoutsSpec{gfx::Size(40, 40),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight}},
       4, 6, 12},
      {u"60px corner, 12px small, 4px inner", SK_ColorMAGENTA,
       CutoutsSpec{gfx::Size(60, 60),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight}},
       60, 12, 4},
      {u"Everything 6px", SK_ColorRED,
       CutoutsSpec{gfx::Size(20, 20),
                   {RoundedRectCutoutPathBuilder::Corner::kUpperRight}},
       6, 6, 6},
      // Row 4
      {u"Lower Cutouts", SK_ColorBLUE,
       CutoutsSpec{gfx::Size(40, 40),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight,
                    RoundedRectCutoutPathBuilder::Corner::kLowerLeft}},
       8, 16, 12},
      {u"Across", SK_ColorMAGENTA,
       CutoutsSpec{gfx::Size(40, 40),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight,
                    RoundedRectCutoutPathBuilder::Corner::kUpperLeft}},
       8, 16, 12},
      {u"3 cutouts", SK_ColorRED,
       CutoutsSpec{gfx::Size(40, 40),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight,
                    RoundedRectCutoutPathBuilder::Corner::kLowerLeft,
                    RoundedRectCutoutPathBuilder::Corner::kUpperRight}},
       8, 16, 12},
      {u"All 4 Cutouts", SK_ColorGREEN,
       CutoutsSpec{gfx::Size(88, 60),
                   {RoundedRectCutoutPathBuilder::Corner::kLowerRight,
                    RoundedRectCutoutPathBuilder::Corner::kLowerLeft,
                    RoundedRectCutoutPathBuilder::Corner::kUpperLeft,
                    RoundedRectCutoutPathBuilder::Corner::kUpperRight}},
       16, 12, 20},
      // Row 5
      {.name = u"Very large cutout",
       .color = SK_ColorMAGENTA,
       .cutouts = MakeSpecs(gfx::Size(120, 80),
                            RoundedRectCutoutPathBuilder::Corner::kLowerLeft),
       .radius = 12,
       .outer_radius = 12,
       .inner_radius = 12},
      {u"One small one large (across)", SK_ColorBLACK,
       std::vector<CutoutSpec>{
           {gfx::Size(120, 80),
            RoundedRectCutoutPathBuilder::Corner::kLowerRight},
           {gfx::Size(35, 35),
            RoundedRectCutoutPathBuilder::Corner::kUpperLeft}},
       4, 4, 4},
      {u"One small one large (vertical)", SK_ColorBLUE,
       std::vector<CutoutSpec>{
           {gfx::Size(120, 80),
            RoundedRectCutoutPathBuilder::Corner::kUpperRight},
           {gfx::Size(35, 35),
            RoundedRectCutoutPathBuilder::Corner::kLowerRight}},
       12, 12, 12},
      {u"One small one large (horizontal)", SK_ColorGREEN,
       std::vector<CutoutSpec>{
           {gfx::Size(120, 80),
            RoundedRectCutoutPathBuilder::Corner::kUpperLeft},
           {gfx::Size(35, 35),
            RoundedRectCutoutPathBuilder::Corner::kUpperRight}},
       8, 8, 8},
  }};

  auto grid_view = std::make_unique<CutoutsGridView>();

  for (const auto& entry : entries) {
    grid_view->AddCutoutSample(entry);
  }

  return grid_view;
}

}  // namespace ash