File: insets_outsets_f_base.h

package info (click to toggle)
qt6-webengine 6.4.2-final%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,090,536 kB
  • sloc: cpp: 17,808,031; ansic: 5,245,139; javascript: 3,178,881; python: 1,361,176; asm: 648,577; xml: 571,140; java: 196,952; sh: 96,408; objc: 88,289; perl: 70,982; cs: 39,145; fortran: 24,137; makefile: 20,242; pascal: 12,634; sql: 10,875; yacc: 9,671; tcl: 8,385; php: 6,188; lisp: 2,848; lex: 2,263; ada: 727; ruby: 623; awk: 339; sed: 37
file content (133 lines) | stat: -rw-r--r-- 4,351 bytes parent folder | download
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
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_
#define UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_

#include <string>

#include "base/strings/stringprintf.h"

namespace gfx {

// This is the base template class of InsetsF and OutsetsF.
template <typename T>
class InsetsOutsetsFBase {
 public:
  constexpr InsetsOutsetsFBase() = default;
  constexpr explicit InsetsOutsetsFBase(float all)
      : top_(all), left_(all), bottom_(all), right_(all) {}

  constexpr float top() const { return top_; }
  constexpr float left() const { return left_; }
  constexpr float bottom() const { return bottom_; }
  constexpr float right() const { return right_; }

  // Returns the total width taken up by the insets/outsets, which is the
  // sum of the left and right insets/outsets.
  constexpr float width() const { return left_ + right_; }

  // Returns the total height taken up by the insets/outsets, which is the
  // sum of the top and bottom insets/outsets.
  constexpr float height() const { return top_ + bottom_; }

  // Returns true if the insets/outsets are empty.
  bool IsEmpty() const { return width() == 0.f && height() == 0.f; }

  // These setters can be used together with the default constructor and the
  // single-parameter constructor to construct InsetsF instances, for example:
  //                                                    // T, L, B, R
  //   InsetsF a = InsetsF().set_top(2);                // 2, 0, 0, 0
  //   InsetsF b = InsetsF().set_left(2).set_bottom(3); // 0, 2, 3, 0
  //   InsetsF c = InsetsF(1).set_top(5);               // 5, 1, 1, 1
  constexpr T& set_top(float top) {
    top_ = top;
    return *static_cast<T*>(this);
  }
  constexpr T& set_left(float left) {
    left_ = left;
    return *static_cast<T*>(this);
  }
  constexpr T& set_bottom(float bottom) {
    bottom_ = bottom;
    return *static_cast<T*>(this);
  }
  constexpr T& set_right(float right) {
    right_ = right;
    return *static_cast<T*>(this);
  }

  // In addition to the above, we can also use the following methods to
  // construct InsetsF/OutsetsF.
  // TLBR() is for Chomium UI code. We should not use it in blink code because
  // the order of parameters is different from the normal orders used in blink.
  // Blink code can use the above setters and VH().
  static constexpr inline T TLBR(float top,
                                 float left,
                                 float bottom,
                                 float right) {
    return T().set_top(top).set_left(left).set_bottom(bottom).set_right(right);
  }
  static constexpr inline T VH(float vertical, float horizontal) {
    return TLBR(vertical, horizontal, vertical, horizontal);
  }

  // Sets each side to the maximum of the side and the corresponding side of
  // |other|.
  void SetToMax(const T& other) {
    top_ = std::max(top_, other.top_);
    left_ = std::max(left_, other.left_);
    bottom_ = std::max(bottom_, other.bottom_);
    right_ = std::max(right_, other.right_);
  }

  void Scale(float x_scale, float y_scale) {
    top_ *= y_scale;
    left_ *= x_scale;
    bottom_ *= y_scale;
    right_ *= x_scale;
  }
  void Scale(float scale) { Scale(scale, scale); }

  bool operator==(const T& other) const {
    return top_ == other.top_ && left_ == other.left_ &&
           bottom_ == other.bottom_ && right_ == other.right_;
  }

  bool operator!=(const T& other) const { return !(*this == other); }

  void operator+=(const T& other) {
    top_ += other.top_;
    left_ += other.left_;
    bottom_ += other.bottom_;
    right_ += other.right_;
  }

  void operator-=(const T& other) {
    top_ -= other.top_;
    left_ -= other.left_;
    bottom_ -= other.bottom_;
    right_ -= other.right_;
  }

  T operator-() const {
    return T().set_left(-left_).set_right(-right_).set_top(-top_).set_bottom(
        -bottom_);
  }

  // Returns a string representation of the insets/outsets.
  std::string ToString() const {
    return base::StringPrintf("x:%g,%g y:%g,%g", left_, right_, top_, bottom_);
  }

 private:
  float top_ = 0.f;
  float left_ = 0.f;
  float bottom_ = 0.f;
  float right_ = 0.f;
};

}  // namespace gfx

#endif  // UI_GFX_GEOMETRY_INSETS_OUTSETS_F_BASE_H_