File: layout_provider.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 (266 lines) | stat: -rw-r--r-- 9,537 bytes parent folder | download | duplicates (4)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/layout/layout_provider.h"

#include <algorithm>

#include "base/containers/fixed_flat_map.h"
#include "base/logging.h"
#include "ui/base/ui_base_features.h"
#include "ui/gfx/font_list.h"
#include "ui/views/controls/focus_ring.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/views_delegate.h"

namespace views {

namespace {

LayoutProvider* g_layout_delegate = nullptr;

}  // namespace

LayoutProvider::LayoutProvider() {
  g_layout_delegate = this;
}

LayoutProvider::~LayoutProvider() {
  if (this == g_layout_delegate) {
    g_layout_delegate = nullptr;
  }
}

// static
LayoutProvider* LayoutProvider::Get() {
  return g_layout_delegate;
}

// static
int LayoutProvider::GetControlHeightForFont(int context,
                                            int style,
                                            const gfx::FontList& font) {
  return std::max(TypographyProvider::Get().GetLineHeight(context, style),
                  font.GetHeight()) +
         Get()->GetDistanceMetric(DISTANCE_CONTROL_VERTICAL_TEXT_PADDING) * 2;
}

gfx::Insets LayoutProvider::GetInsetsMetric(int metric) const {
  DCHECK_GE(metric, VIEWS_INSETS_START);
  DCHECK_LT(metric, VIEWS_INSETS_MAX);
  switch (metric) {
    case InsetsMetric::INSETS_DIALOG:
    case InsetsMetric::INSETS_DIALOG_SUBSECTION:
    case InsetsMetric::INSETS_DIALOG_FOOTNOTE:
      return gfx::Insets(13);
    case InsetsMetric::INSETS_DIALOG_BUTTON_ROW: {
      const gfx::Insets dialog_insets = GetInsetsMetric(INSETS_DIALOG);
      return gfx::Insets::TLBR(0, dialog_insets.left(), dialog_insets.bottom(),
                               dialog_insets.right());
    }
    case InsetsMetric::INSETS_DIALOG_TITLE: {
      const gfx::Insets dialog_insets = GetInsetsMetric(INSETS_DIALOG);
      return gfx::Insets::TLBR(dialog_insets.top(), dialog_insets.left(), 0,
                               dialog_insets.right());
    }
    case InsetsMetric::INSETS_TOOLTIP_BUBBLE:
      return gfx::Insets(8);
    case InsetsMetric::INSETS_CHECKBOX_RADIO_BUTTON:
      return gfx::Insets::VH(5, 6);
    case InsetsMetric::INSETS_VECTOR_IMAGE_BUTTON:
      return gfx::Insets(4);
    case InsetsMetric::INSETS_LABEL_BUTTON:
      return gfx::Insets::VH(5, 6);
    case InsetsMetric::INSETS_ICON_BUTTON:
      return gfx::Insets(2);
  }
  NOTREACHED();
}

int LayoutProvider::GetDistanceMetric(int metric) const {
  DCHECK_GE(metric, VIEWS_DISTANCE_START);
  DCHECK_LT(metric, VIEWS_DISTANCE_END);

  switch (static_cast<DistanceMetric>(metric)) {
    case DISTANCE_BUBBLE_HEADER_VECTOR_ICON_SIZE:
      return 20;
    case DISTANCE_BUBBLE_PREFERRED_WIDTH:
      return kSmallDialogWidth;
    case DISTANCE_BUTTON_HORIZONTAL_PADDING:
      return 16;
    case DISTANCE_BUTTON_MAX_LINKABLE_WIDTH:
      return 112;
    case DISTANCE_CLOSE_BUTTON_MARGIN:
      return 20;
    case DISTANCE_CONTROL_LIST_VERTICAL:
      return 12;
    case DISTANCE_CONTROL_VERTICAL_TEXT_PADDING:
      return 10;
    case DISTANCE_TABLE_VERTICAL_TEXT_PADDING:
      return 6;
    case DISTANCE_DIALOG_BUTTON_MINIMUM_WIDTH:
      // Minimum label size plus padding.
      return 32 + 2 * GetDistanceMetric(DISTANCE_BUTTON_HORIZONTAL_PADDING);
    case DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_CONTROL:
      return 24;
    case DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_TEXT:
      // This is reduced so there is about the same amount of visible
      // whitespace, compensating for the text's internal leading.
      return GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_CONTROL) -
             8;
    case DISTANCE_DIALOG_CONTENT_MARGIN_TOP_CONTROL:
      return 16;
    case DISTANCE_DIALOG_CONTENT_MARGIN_TOP_TEXT:
      // See the comment in DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_TEXT above.
      return GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_TOP_CONTROL) - 8;
    case DISTANCE_DROPDOWN_BUTTON_LABEL_ARROW_SPACING:
      return 8;
    case DISTANCE_DROPDOWN_BUTTON_RIGHT_MARGIN:
      return 12;
    case DISTANCE_DROPDOWN_BUTTON_LEFT_MARGIN:
      return 16;
    case DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH:
      return kMediumDialogWidth;
    case DISTANCE_LARGE_MODAL_DIALOG_PREFERRED_WIDTH:
      return kLargeDialogWidth;
    case DISTANCE_RELATED_BUTTON_HORIZONTAL:
      return 8;
    case DISTANCE_RELATED_CONTROL_HORIZONTAL:
      return 16;
    case DISTANCE_RELATED_CONTROL_VERTICAL:
      return 8;
    case DISTANCE_RELATED_LABEL_HORIZONTAL:
      return 12;
    case DISTANCE_DIALOG_SCROLLABLE_AREA_MAX_HEIGHT:
      return 192;
    case DISTANCE_MODAL_DIALOG_SCROLLABLE_AREA_MAX_HEIGHT:
      return 448;
    case DISTANCE_TABLE_CELL_HORIZONTAL_MARGIN:
      return 12;
    case DISTANCE_TEXTFIELD_HORIZONTAL_TEXT_PADDING:
      return 10;
    case DISTANCE_UNRELATED_CONTROL_HORIZONTAL:
      return 16;
    case DISTANCE_UNRELATED_CONTROL_VERTICAL:
      return 16;
    case DISTANCE_VECTOR_ICON_PADDING:
      return 4;
    case VIEWS_DISTANCE_END:
    case VIEWS_DISTANCE_MAX:
      NOTREACHED();
  }
  NOTREACHED();
}

const TypographyProvider& LayoutProvider::GetTypographyProvider() const {
  return typography_provider_;
}

int LayoutProvider::GetSnappedDialogWidth(int min_width) const {
  // TODO(pbos): Move snapping logic from ChromeLayoutProvider and update
  // unittests to pass with snapping points (instead of exact preferred width).

  // This is an arbitrary value, but it's a good arbitrary value. Some dialogs
  // have very small widths for their contents views, which causes ugly
  // title-wrapping where a two-word title is split across multiple lines or
  // similar. To prevent that, forbid any snappable dialog from being narrower
  // than this value. In principle it's possible to factor in the title width
  // here, but it is not really worth the complexity.
  return std::max(min_width, 320);
}

gfx::Insets LayoutProvider::GetDialogInsetsForContentType(
    DialogContentType leading,
    DialogContentType trailing) const {
  const int top_margin =
      leading == DialogContentType::kControl
          ? GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_TOP_CONTROL)
          : GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_TOP_TEXT);
  const int bottom_margin =
      trailing == DialogContentType::kControl
          ? GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_CONTROL)
          : GetDistanceMetric(DISTANCE_DIALOG_CONTENT_MARGIN_BOTTOM_TEXT);
  const gfx::Insets dialog_insets = GetInsetsMetric(INSETS_DIALOG);
  return gfx::Insets::TLBR(top_margin, dialog_insets.left(), bottom_margin,
                           dialog_insets.right());
}

int LayoutProvider::GetCornerRadiusMetric(Emphasis emphasis,
                                          const gfx::Size& size) const {
  switch (emphasis) {
    case Emphasis::kNone:
      return 0;
    case Emphasis::kLow:
    case Emphasis::kMedium:
      return 4;
    case Emphasis::kHigh:
      return 8;
    case Emphasis::kMaximum:
      return std::min(size.width(), size.height()) / 2;
  }
}

ShapeSysTokens GetShapeSysToken(ShapeContextTokens id) {
  static constexpr auto shape_token_map =
      base::MakeFixedFlatMap<ShapeContextTokens, ShapeSysTokens>({
          {ShapeContextTokens::kBadgeRadius, ShapeSysTokens::kXSmall},
          {ShapeContextTokens::kButtonRadius, ShapeSysTokens::kFull},
          {ShapeContextTokens::kComboboxRadius, ShapeSysTokens::kSmall},
          {ShapeContextTokens::kDialogRadius, ShapeSysTokens::kMediumSmall},
          {ShapeContextTokens::kExtensionsMenuButtonRadius,
           ShapeSysTokens::kXSmall},
          {ShapeContextTokens::kFindBarViewRadius, ShapeSysTokens::kSmall},
          {ShapeContextTokens::kMenuRadius, ShapeSysTokens::kMediumSmall},
          {ShapeContextTokens::kMenuAuxRadius, ShapeSysTokens::kMediumSmall},
          {ShapeContextTokens::kMenuTouchRadius, ShapeSysTokens::kMediumSmall},
          {ShapeContextTokens::kOmniboxExpandedRadius, ShapeSysTokens::kMedium},
          {ShapeContextTokens::kTextfieldRadius, ShapeSysTokens::kSmall},
          {ShapeContextTokens::kSidePanelContentRadius,
           ShapeSysTokens::kMedium},
          {ShapeContextTokens::kSidePanelPageContentRadius,
           ShapeSysTokens::kSmall},
      });
  const auto it = shape_token_map.find(id);
  return it == shape_token_map.end() ? ShapeSysTokens::kDefault : it->second;
}

int LayoutProvider::GetCornerRadiusMetric(ShapeContextTokens id,
                                          const gfx::Size& size) const {
  ShapeSysTokens token = GetShapeSysToken(id);
  DCHECK_NE(token, ShapeSysTokens::kDefault)
      << "kDefault token means there is a missing mapping between shape tokens";
  switch (token) {
    case ShapeSysTokens::kXSmall:
      return 4;
    case ShapeSysTokens::kSmall:
      return 8;
    case ShapeSysTokens::kMediumSmall:
      return 12;
    case ShapeSysTokens::kMedium:
      return 16;
    case ShapeSysTokens::kLarge:
      return 24;
    case ShapeSysTokens::kFull:
      return std::min(size.width(), size.height()) / 2;
    default:
      return 0;
  }
}

int LayoutProvider::GetShadowElevationMetric(Emphasis emphasis) const {
  switch (emphasis) {
    case Emphasis::kNone:
      return 0;
    case Emphasis::kLow:
      return 1;
    case Emphasis::kMedium:
      return 2;
    case Emphasis::kHigh:
      return 3;
    case Emphasis::kMaximum:
      return 16;
  }
}

}  // namespace views