File: reference_lines.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 (242 lines) | stat: -rw-r--r-- 9,009 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 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/hud_display/reference_lines.h"

#include "ash/hud_display/hud_constants.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkBlendMode.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/text_constants.h"

namespace ash {
namespace hud_display {

namespace {

constexpr SkColor kHUDGraphReferenceLineColor = SkColorSetRGB(162, 162, 220);

std::u16string GenerateLabelText(float value, const std::u16string& dimention) {
  if (value == (int)value) {
    return base::ASCIIToUTF16(base::StringPrintf("%d ", (int)value).c_str()) +
           dimention;
  } else {
    return base::ASCIIToUTF16(base::StringPrintf("%.2f ", value).c_str()) +
           dimention;
  }
}

}  // anonymous namespace

BEGIN_METADATA(ReferenceLines)
END_METADATA

ReferenceLines::ReferenceLines(float left,
                               float top,
                               float right,
                               float bottom,
                               const std::u16string& x_unit,
                               const std::u16string& y_unit,
                               int horizontal_points_number,
                               int horizontal_ticks_interval,
                               float vertical_ticks_interval)
    : color_(kHUDGraphReferenceLineColor),
      left_(left),
      top_(top),
      right_(right),
      bottom_(bottom),
      x_unit_(x_unit),
      y_unit_(y_unit),
      horizontal_points_number_(horizontal_points_number),
      horizontal_ticks_interval_(horizontal_ticks_interval),
      vertical_ticks_interval_(vertical_ticks_interval) {
  // Text is set later.
  right_top_label_ = AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_LABEL));
  right_middle_label_ = AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_LABEL));
  right_bottom_label_ = AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_LABEL));
  left_bottom_label_ = AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_LABEL));

  // Set label text.
  SetTopLabel(top_);
  SetBottomLabel(bottom_);
  SetLeftLabel(left_);

  right_top_label_->SetEnabledColor(color_);
  right_top_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  right_middle_label_->SetEnabledColor(color_);
  right_middle_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  right_bottom_label_->SetEnabledColor(color_);
  right_bottom_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  left_bottom_label_->SetEnabledColor(color_);
  left_bottom_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
}

ReferenceLines::~ReferenceLines() = default;

void ReferenceLines::Layout(PassKey) {
  // Align all the right labels on their left edge.
  gfx::Size right_top_label_size = right_top_label_->GetPreferredSize(
      views::SizeBounds(right_top_label_->width(), {}));
  gfx::Size right_middle_label_size = right_middle_label_->GetPreferredSize(
      views::SizeBounds(right_middle_label_->width(), {}));
  gfx::Size right_bottom_label_size = right_bottom_label_->GetPreferredSize(
      views::SizeBounds(right_bottom_label_->width(), {}));

  const int right_labels_width = std::max(
      right_top_label_size.width(), std::max(right_middle_label_size.width(),
                                             right_bottom_label_size.width()));
  right_top_label_size.set_width(right_labels_width);
  right_middle_label_size.set_width(right_labels_width);
  right_bottom_label_size.set_width(right_labels_width);

  right_top_label_->SetSize(right_top_label_size);
  right_middle_label_->SetSize(right_middle_label_size);
  right_bottom_label_->SetSize(right_bottom_label_size);

  left_bottom_label_->SetSize(left_bottom_label_->GetPreferredSize(
      views::SizeBounds(left_bottom_label_->width(), {})));

  constexpr int label_border = 3;  // Offset to labels from the reference lines.

  const gfx::Point right_top_label_position(
      bounds().width() - right_top_label_size.width() - label_border,
      label_border);
  const gfx::Point right_middle_label_position(
      bounds().width() - right_middle_label_size.width() - label_border,
      bounds().height() / 2 - right_middle_label_size.height() - label_border);
  const gfx::Point right_bottom_label_position(
      bounds().width() - right_bottom_label_size.width() - label_border,
      bounds().height() - right_bottom_label_size.height() - label_border);

  right_top_label_->SetPosition(right_top_label_position);
  right_middle_label_->SetPosition(right_middle_label_position);
  right_bottom_label_->SetPosition(right_bottom_label_position);

  left_bottom_label_->SetPosition(
      {label_border, bounds().height() -
                         left_bottom_label_
                             ->GetPreferredSize(views::SizeBounds(
                                 left_bottom_label_->width(), {}))
                             .height() -
                         label_border});

  LayoutSuperclass<views::View>(this);
}

void ReferenceLines::OnPaint(gfx::Canvas* canvas) {
  SkPath dotted_path;
  SkPath solid_path;

  // Draw dashed line at 50%.
  dotted_path.moveTo({0, bounds().height() / 2.0f});
  dotted_path.lineTo(
      {static_cast<SkScalar>(bounds().width()), bounds().height() / 2.0f});

  // Draw border and ticks.
  solid_path.addRect(SkRect::MakeXYWH(bounds().x(), bounds().y(),
                                      bounds().width(), bounds().height()));

  const SkScalar tick_length = 3;

  // Vertical interval ticks (drawn horizontally).
  if (vertical_ticks_interval_ > 0) {
    float tick_bottom_offset = vertical_ticks_interval_;
    while (tick_bottom_offset <= 1) {
      // Skip 50%.
      if (fabs(tick_bottom_offset - .5) > 0.01) {
        const SkScalar line_y = (1 - tick_bottom_offset) * bounds().height();
        solid_path.moveTo({0, line_y});
        solid_path.lineTo({tick_length, line_y});

        solid_path.moveTo({bounds().width() - tick_length, line_y});
        solid_path.lineTo({static_cast<SkScalar>(bounds().width()), line_y});
      }
      tick_bottom_offset += vertical_ticks_interval_;
    }
  }

  // Horizontal interval ticks (drawn vertically).
  if (horizontal_points_number_ > 0 && horizontal_ticks_interval_ > 0) {
    // Add one more tick if graph width is not a multiple of tick width.
    const int h_ticks =
        horizontal_points_number_ / horizontal_ticks_interval_ +
        (horizontal_points_number_ % horizontal_ticks_interval_ ? 1 : 0);
    // Interval between ticks in pixels.
    const SkScalar tick_per_pixels = bounds().width() /
                                     (float)horizontal_points_number_ *
                                     horizontal_ticks_interval_;
    for (int i = 1; i < h_ticks; ++i) {
      const SkScalar line_x = bounds().width() - tick_per_pixels * i;
      solid_path.moveTo({line_x, 0});
      solid_path.lineTo({line_x, tick_length});

      solid_path.moveTo({line_x, bounds().height() - tick_length});
      solid_path.lineTo({line_x, static_cast<SkScalar>(bounds().height())});
    }
  }

  cc::PaintFlags flags;
  flags.setAntiAlias(true);
  flags.setBlendMode(SkBlendMode::kSrc);
  flags.setStyle(cc::PaintFlags::kStroke_Style);
  flags.setStrokeWidth(kHUDGraphReferenceLineWidth);
  flags.setColor(color_);
  canvas->DrawPath(solid_path, flags);

  const SkScalar intervals[] = {5, 3};
  flags.setPathEffect(cc::PathEffect::MakeDash(
      intervals, sizeof(intervals) / sizeof(intervals[0]), /*phase=*/0));
  canvas->DrawPath(dotted_path, flags);
}

void ReferenceLines::SetTopLabel(float top) {
  top_ = top;
  right_top_label_->SetText(GenerateLabelText(top_, y_unit_));
  right_middle_label_->SetText(
      GenerateLabelText((top_ - bottom_) / 2, y_unit_));

  // This might trigger label resize.
  InvalidateLayout();
}

void ReferenceLines::SetBottomLabel(float bottom) {
  bottom_ = bottom;
  right_bottom_label_->SetText(GenerateLabelText(bottom_, y_unit_));
  right_middle_label_->SetText(
      GenerateLabelText((top_ - bottom_) / 2, y_unit_));

  // This might trigger label resize.
  InvalidateLayout();
}

void ReferenceLines::SetLeftLabel(float left) {
  left_ = left;
  left_bottom_label_->SetText(GenerateLabelText(left_, x_unit_));

  // This might trigger label resize.
  InvalidateLayout();
}

void ReferenceLines::SetVerticalTicksInterval(float interval) {
  interval = std::abs(interval) >= 1 ? 0 : std::abs(interval);
  if (interval == vertical_ticks_interval_)
    return;

  vertical_ticks_interval_ = interval;
}

}  // namespace hud_display
}  // namespace ash