File: rounded_display_gutter.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 5,354 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 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/rounded_display/rounded_display_gutter.h"

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

#include "ash/frame_sink/ui_resource.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkScalar.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"

namespace ash {
namespace {

using RoundedCornerPosition = RoundedDisplayGutter::RoundedCorner::Position;
using RoundedCorner = RoundedDisplayGutter::RoundedCorner;

}  // namespace

RoundedCorner::RoundedCorner(Position position,
                             int radius,
                             const gfx::Point& origin)
    : position_(position),
      radius_(radius),
      bounds_in_pixels_(gfx::Rect(origin, gfx::Size(radius, radius))) {}

RoundedCorner& RoundedCorner::operator=(RoundedCorner&& other) = default;
RoundedCorner::RoundedCorner(RoundedCorner&& other) = default;

RoundedCorner::~RoundedCorner() = default;

bool RoundedDisplayGutter::RoundedCorner::DoesPaint() const {
  return radius_ > 0;
}

void RoundedDisplayGutter::RoundedCorner::Paint(gfx::Canvas* canvas) const {
  if (!DoesPaint()) {
    return;
  }

  PaintCornerHelper(canvas);
}

void RoundedDisplayGutter::RoundedCorner::PaintCornerHelper(
    gfx::Canvas* canvas) const {
  SkPath path;
  SkScalar startAngle = 0.0, sweepAngle = 0.0;
  SkScalar dx = 0.0, dy = 0.0;
  int translate_dx = 0.0, translate_dy = 0.0;

  switch (position_) {
    case RoundedCornerPosition::kUpperLeft:
      startAngle = -90;
      sweepAngle = -90;
      dx = radius_;
      dy = -radius_;
      translate_dx = 0;
      translate_dy = 0;
      break;
    case RoundedCornerPosition::kLowerLeft:
      startAngle = 90;
      sweepAngle = 90;
      dx = radius_;
      dy = radius_;
      translate_dx = 0;
      translate_dy = -radius_;
      break;
    case RoundedCornerPosition::kUpperRight:
      startAngle = 0;
      sweepAngle = -90;
      dx = radius_;
      dy = radius_;
      translate_dx = -radius_;
      translate_dy = 0;
      break;
    case RoundedCornerPosition::kLowerRight:
      startAngle = 0;
      sweepAngle = 90;
      dx = radius_;
      dy = -radius_;
      translate_dx = -radius_;
      translate_dy = -radius_;
      break;
  }

  const SkScalar oval_radius = radius_ * 2;
  SkRect oval{0, 0, oval_radius, oval_radius};

  path.addArc(oval, startAngle, sweepAngle);

  if (position_ == RoundedCornerPosition::kUpperLeft ||
      position_ == RoundedCornerPosition::kLowerLeft) {
    path.rLineTo(0, dy);
    path.rLineTo(dx, 0);
  }

  if (position_ == RoundedCornerPosition::kUpperRight ||
      position_ == RoundedCornerPosition::kLowerRight) {
    path.rLineTo(dx, 0);
    path.rLineTo(0, dy);
  }

  cc::PaintFlags flags;
  flags.setStyle(cc::PaintFlags::Style::kFill_Style);
  flags.setAntiAlias(true);
  flags.setColor(SK_ColorBLACK);

  canvas->Save();
  canvas->Translate({translate_dx, translate_dy});
  canvas->DrawPath(path, flags);
  canvas->Restore();
}

// -----------------------------------------------------------------------------
// RoundedDisplayGutter:

// static
std::unique_ptr<RoundedDisplayGutter> RoundedDisplayGutter::CreateGutter(
    std::vector<RoundedCorner>&& corners,
    bool is_overlay) {
  return std::make_unique<RoundedDisplayGutter>(std::move(corners), is_overlay);
}

RoundedDisplayGutter::RoundedDisplayGutter(std::vector<RoundedCorner>&& corners,
                                           bool is_overlay)
    : corners_(std::move(corners)), is_overlay_(is_overlay) {
  // A gutter must paint at least one rounded corner and at most four corners.
  DCHECK(corners_.size() > 0 && corners_.size() <= 4);

  // Since the corners of the gutter cannot be changed, both gutter bounds and
  // ui_source_id do not change either.
  bounds_in_pixels_ = CalculateGutterBounds();
  ui_source_id_ = CalculateUiSourceId();
  DCHECK(ui_source_id_ != kInvalidUiSourceId);
}

RoundedDisplayGutter::~RoundedDisplayGutter() = default;

UiSourceId RoundedDisplayGutter::ui_source_id() const {
  return ui_source_id_;
}

UiSourceId RoundedDisplayGutter::CalculateUiSourceId() const {
  UiSourceId ui_source_id = kInvalidUiSourceId;
  // Value of the position mask of the gutter will give a unique value for any
  // combination of RoundedDisplayCorners.
  for (const auto& corner : corners_) {
    ui_source_id |= corner.position();
  }

  return ui_source_id;
}

gfx::Rect RoundedDisplayGutter::CalculateGutterBounds() const {
  gfx::Rect gutter_bounds;

  for (const auto& corner : corners_) {
    gutter_bounds.Union(corner.bounds());
  }

  return gutter_bounds;
}

const gfx::Rect& RoundedDisplayGutter::bounds() const {
  return bounds_in_pixels_;
}

void RoundedDisplayGutter::Paint(gfx::Canvas* canvas) const {
  for (const auto& corner : corners_) {
    canvas->Save();
    const gfx::Vector2d offset =
        corner.bounds().OffsetFromOrigin() - bounds().OffsetFromOrigin();
    canvas->Translate(offset);
    corner.Paint(canvas);
    canvas->Restore();
  }
}

}  // namespace ash