File: default_frame_view.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 (638 lines) | stat: -rw-r--r-- 24,414 bytes parent folder | download | duplicates (3)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Copyright 2012 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/window/default_frame_view.h"

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

#include "base/containers/adapters.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/hit_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/resources/grit/views_resources.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/client_view.h"
#include "ui/views/window/frame_background.h"
#include "ui/views/window/window_button_order_provider.h"
#include "ui/views/window/window_resources.h"
#include "ui/views/window/window_shape.h"

#if BUILDFLAG(IS_WIN)
#include "ui/display/win/screen_win.h"
#endif

namespace views {

namespace {

// Various edges of the frame border have a 1 px shadow along their edges;
// in a few cases we shift elements based on this amount for visual appeal.
constexpr int kFrameShadowThickness = 1;
// The frame border is only visible in restored mode and is hardcoded to 4 px on
// each side regardless of the system window border size.
constexpr int kFrameBorderThickness = 4;
// In the window corners, the resize areas don't actually expand bigger, but the
// 16 px at the end of each edge triggers diagonal resizing.
constexpr int kResizeAreaCornerSize = 16;
// The titlebar never shrinks too short to show the caption button plus some
// padding below it.
constexpr int kCaptionButtonHeightWithPadding = 19;
// The titlebar has a 2 px 3D edge along the top and bottom.
constexpr int kTitlebarTopAndBottomEdgeThickness = 2;
// The icon is inset 2 px from the left frame border.
constexpr int kIconLeftSpacing = 2;
// The space between the window icon and the title text.
constexpr int kTitleIconOffsetX = 4;
// The space between the title text and the caption buttons.
constexpr int kTitleCaptionSpacing = 5;
// In restored mode, we draw a 1 px edge around the content area inside the
// frame border.
constexpr int kClientEdgeThickness = 1;

void LayoutButton(ImageButton* button, const gfx::Rect& bounds) {
  button->SetVisible(true);
  button->SetImageVerticalAlignment(ImageButton::ALIGN_BOTTOM);
  button->SetBoundsRect(bounds);
}

}  // namespace

DefaultFrameView::DefaultFrameView(Widget* frame)
    : frame_(frame), frame_background_(new FrameBackground()) {
  close_button_ = InitWindowCaptionButton(
      base::BindRepeating(&Widget::CloseWithReason, base::Unretained(frame_),
                          views::Widget::ClosedReason::kCloseButtonClicked),
      IDS_APP_ACCNAME_CLOSE, IDR_CLOSE, IDR_CLOSE_H, IDR_CLOSE_P);
  minimize_button_ = InitWindowCaptionButton(
      base::BindRepeating(&Widget::Minimize, base::Unretained(frame_)),
      IDS_APP_ACCNAME_MINIMIZE, IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
  maximize_button_ = InitWindowCaptionButton(
      base::BindRepeating(&Widget::Maximize, base::Unretained(frame_)),
      IDS_APP_ACCNAME_MAXIMIZE, IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
  restore_button_ = InitWindowCaptionButton(
      base::BindRepeating(&Widget::Restore, base::Unretained(frame_)),
      IDS_APP_ACCNAME_RESTORE, IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);

  if (frame_->widget_delegate()->ShouldShowWindowIcon()) {
    window_icon_ =
        AddChildView(std::make_unique<ImageButton>(Button::PressedCallback()));
    // `window_icon_` does not need to be focusable as it is not used here as a
    // button and is not interactive.
    window_icon_->SetFocusBehavior(FocusBehavior::NEVER);
  }
}

DefaultFrameView::~DefaultFrameView() = default;

gfx::Rect DefaultFrameView::GetBoundsForClientView() const {
  return client_view_bounds_;
}

gfx::Rect DefaultFrameView::GetWindowBoundsForClientBounds(
    const gfx::Rect& client_bounds) const {
  int top_height = NonClientTopBorderHeight();
  int border_thickness = NonClientBorderThickness();
  return gfx::Rect(client_bounds.x() - border_thickness,
                   client_bounds.y() - top_height,
                   client_bounds.width() + (2 * border_thickness),
                   client_bounds.height() + top_height + border_thickness);
}

int DefaultFrameView::NonClientHitTest(const gfx::Point& point) {
  // Sanity check.
  if (!bounds().Contains(point)) {
    return HTNOWHERE;
  }

  int frame_component = frame_->client_view()->NonClientHitTest(point);

  // See if we're in the sysmenu region.  (We check the ClientView first to be
  // consistent with OpaqueBrowserFrameView; it's not really necessary here.)
  gfx::Rect sysmenu_rect(IconBounds());
  // In maximized mode we extend the rect to the screen corner to take advantage
  // of Fitts' Law.
  if (frame_->IsMaximized()) {
    sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
  }
  sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
  if (sysmenu_rect.Contains(point)) {
    return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
  }

  if (frame_component != HTNOWHERE) {
    return frame_component;
  }

  // Then see if the point is within any of the window controls.
  if (close_button_->GetMirroredBounds().Contains(point)) {
    return HTCLOSE;
  }
  if (restore_button_->GetMirroredBounds().Contains(point)) {
    return HTMAXBUTTON;
  }
  if (maximize_button_->GetMirroredBounds().Contains(point)) {
    return HTMAXBUTTON;
  }
  if (minimize_button_->GetMirroredBounds().Contains(point)) {
    return HTMINBUTTON;
  }
  if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point)) {
    return HTSYSMENU;
  }

  gfx::Insets resize_border(NonClientBorderThickness());
  // The top resize border has extra thickness.
  resize_border.set_top(FrameBorderThickness());
  int window_component = GetHTComponentForFrame(
      point, resize_border, kResizeAreaCornerSize, kResizeAreaCornerSize,
      frame_->widget_delegate()->CanResize());
  // Fall back to the caption if no other component matches.
  return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
}

void DefaultFrameView::GetWindowMask(const gfx::Size& size,
                                     SkPath* window_mask) {
  DCHECK(window_mask);
  if (frame_->IsMaximized() || !ShouldShowTitleBarAndBorder()) {
    return;
  }

  GetDefaultWindowMask(size, window_mask);
}

void DefaultFrameView::ResetWindowControls() {
  restore_button_->SetState(Button::STATE_NORMAL);
  minimize_button_->SetState(Button::STATE_NORMAL);
  maximize_button_->SetState(Button::STATE_NORMAL);
  // The close button isn't affected by this constraint.
}

void DefaultFrameView::UpdateWindowIcon() {
  if (window_icon_) {
    window_icon_->SchedulePaint();
  }
}

void DefaultFrameView::UpdateWindowTitle() {
  if (frame_->widget_delegate()->ShouldShowWindowTitle() &&
      // If this is still unset, we haven't laid out window controls yet.
      maximum_title_bar_x_ > -1) {
    LayoutTitleBar();
    SchedulePaintInRect(title_bounds_);
  }
}

void DefaultFrameView::SizeConstraintsChanged() {
  ResetWindowControls();
  LayoutWindowControls();
}

void DefaultFrameView::OnPaint(gfx::Canvas* canvas) {
  if (!ShouldShowTitleBarAndBorder()) {
    return;
  }

  frame_background_->set_frame_color(GetFrameColor());
  frame_background_->set_use_custom_frame(true);
  frame_background_->set_is_active(ShouldPaintAsActive());
  const gfx::ImageSkia frame_image = GetFrameImage();
  frame_background_->set_theme_image(frame_image);
  frame_background_->set_top_area_height(frame_image.height());

  if (frame_->IsMaximized()) {
    PaintMaximizedFrameBorder(canvas);
  } else {
    PaintRestoredFrameBorder(canvas);
  }
  PaintTitleBar(canvas);
  if (ShouldShowClientEdge()) {
    PaintRestoredClientEdge(canvas);
  }
}

void DefaultFrameView::Layout(PassKey) {
  if (ShouldShowTitleBarAndBorder()) {
    LayoutWindowControls();
    LayoutTitleBar();
  }

  LayoutClientView();
  LayoutSuperclass<NonClientFrameView>(this);
}

gfx::Size DefaultFrameView::CalculatePreferredSize(
    const SizeBounds& available_size) const {
  return frame_->non_client_view()
      ->GetWindowBoundsForClientBounds(
          gfx::Rect(frame_->client_view()->GetPreferredSize(available_size)))
      .size();
}

gfx::Size DefaultFrameView::GetMinimumSize() const {
  return frame_->non_client_view()
      ->GetWindowBoundsForClientBounds(
          gfx::Rect(frame_->client_view()->GetMinimumSize()))
      .size();
}

gfx::Size DefaultFrameView::GetMaximumSize() const {
  gfx::Size max_size = frame_->client_view()->GetMaximumSize();
  gfx::Size converted_size =
      frame_->non_client_view()
          ->GetWindowBoundsForClientBounds(gfx::Rect(max_size))
          .size();
  return gfx::Size(max_size.width() == 0 ? 0 : converted_size.width(),
                   max_size.height() == 0 ? 0 : converted_size.height());
}

int DefaultFrameView::FrameBorderThickness() const {
  return frame_->IsMaximized() ? 0 : kFrameBorderThickness;
}

int DefaultFrameView::NonClientBorderThickness() const {
  // In maximized mode, we don't show a client edge.
  return FrameBorderThickness() +
         (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
}

int DefaultFrameView::NonClientTopBorderHeight() const {
  return std::max(FrameBorderThickness() + IconSize(),
                  CaptionButtonY() + kCaptionButtonHeightWithPadding) +
         TitlebarBottomThickness();
}

int DefaultFrameView::CaptionButtonY() const {
  // Maximized buttons start at window top so that even if their images aren't
  // drawn flush with the screen edge, they still obey Fitts' Law.
#if BUILDFLAG(IS_LINUX)
  return FrameBorderThickness();
#else
  return frame_->IsMaximized() ? FrameBorderThickness() : kFrameShadowThickness;
#endif
}

int DefaultFrameView::TitlebarBottomThickness() const {
  return kTitlebarTopAndBottomEdgeThickness +
         (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
}

int DefaultFrameView::IconSize() const {
#if BUILDFLAG(IS_WIN)
  // This metric scales up if either the titlebar height or the titlebar font
  // size are increased.
  return display::win::GetScreenWin()->GetSystemMetricsInDIP(SM_CYSMICON);
#else
  // The icon never shrinks below 16 px on a side.
  constexpr int kIconMinimumSize = 16;
  return std::max(
      TypographyProvider::Get().GetWindowTitleFontList().GetHeight(),
      kIconMinimumSize);
#endif
}

gfx::Rect DefaultFrameView::IconBounds() const {
  int size = IconSize();
  int frame_thickness = FrameBorderThickness();
  // Our frame border has a different "3D look" than Windows'.  Theirs has a
  // more complex gradient on the top that they push their icon/title below;
  // then the maximized window cuts this off and the icon/title are centered
  // in the remaining space.  Because the apparent shape of our border is
  // simpler, using the same positioning makes things look slightly uncentered
  // with restored windows, so when the window is restored, instead of
  // calculating the remaining space from below the frame border, we calculate
  // from below the 3D edge.
  int unavailable_px_at_top = frame_->IsMaximized()
                                  ? frame_thickness
                                  : kTitlebarTopAndBottomEdgeThickness;
  // When the icon is shorter than the minimum space we reserve for the caption
  // button, we vertically center it.  We want to bias rounding to put extra
  // space above the icon, since the 3D edge (+ client edge, for restored
  // windows) below looks (to the eye) more like additional space than does the
  // 3D edge (or nothing at all, for maximized windows) above; hence the +1.
  int y = unavailable_px_at_top +
          (NonClientTopBorderHeight() - unavailable_px_at_top - size -
           TitlebarBottomThickness() + 1) /
              2;
  return gfx::Rect(frame_thickness + kIconLeftSpacing + minimum_title_bar_x_, y,
                   size, size);
}

bool DefaultFrameView::ShouldShowTitleBarAndBorder() const {
  return !frame_->IsFullscreen() &&
         !ViewsDelegate::GetInstance()->WindowManagerProvidesTitleBar(
             frame_->IsMaximized());
}

bool DefaultFrameView::ShouldShowClientEdge() const {
  return !frame_->IsMaximized() && ShouldShowTitleBarAndBorder();
}

void DefaultFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  frame_background_->SetCornerImages(
      rb.GetImageNamed(IDR_WINDOW_TOP_LEFT_CORNER).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_TOP_RIGHT_CORNER).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER).ToImageSkia());
  frame_background_->SetSideImages(
      rb.GetImageNamed(IDR_WINDOW_LEFT_SIDE).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_TOP_CENTER).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_RIGHT_SIDE).ToImageSkia(),
      rb.GetImageNamed(IDR_WINDOW_BOTTOM_CENTER).ToImageSkia());

  frame_background_->PaintRestored(canvas, this);
}

void DefaultFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
  frame_background_->PaintMaximized(canvas, this);

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  // TODO(jamescook): Migrate this into FrameBackground.
  // The bottom of the titlebar actually comes from the top of the Client Edge
  // graphic, with the actual client edge clipped off the bottom.
  const gfx::ImageSkia* titlebar_bottom =
      rb.GetImageNamed(IDR_APP_TOP_CENTER).ToImageSkia();
  int edge_height = titlebar_bottom->height() -
                    (ShouldShowClientEdge() ? kClientEdgeThickness : 0);
  canvas->TileImageInt(*titlebar_bottom, 0,
                       frame_->client_view()->y() - edge_height, width(),
                       edge_height);
}

void DefaultFrameView::PaintTitleBar(gfx::Canvas* canvas) {
  WidgetDelegate* delegate = frame_->widget_delegate();

  // It seems like in some conditions we can be asked to paint after the window
  // that contains us is WM_DESTROYed. At this point, our delegate is NULL. The
  // correct long term fix may be to shut down the RootView in WM_DESTROY.
  if (!delegate || !delegate->ShouldShowWindowTitle()) {
    return;
  }

  gfx::Rect rect = title_bounds_;
  rect.set_x(GetMirroredXForRect(title_bounds_));
  canvas->DrawStringRect(
      delegate->GetWindowTitle(),
      TypographyProvider::Get().GetWindowTitleFontList(),
      GetColorProvider()->GetColor(ui::kColorCustomFrameCaptionForeground),
      rect);
}

void DefaultFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
  gfx::Rect client_area_bounds = frame_->client_view()->bounds();
  // The shadows have a 1 pixel gap on the inside, so draw them 1 pixel inwards.
  gfx::Rect shadowed_area_bounds = client_area_bounds;
  shadowed_area_bounds.Inset(gfx::Insets(1));
  int shadowed_area_top = shadowed_area_bounds.y();

  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();

  // Top: left, center, right sides.
  const gfx::ImageSkia* top_left = rb.GetImageSkiaNamed(IDR_APP_TOP_LEFT);
  const gfx::ImageSkia* top_center = rb.GetImageSkiaNamed(IDR_APP_TOP_CENTER);
  const gfx::ImageSkia* top_right = rb.GetImageSkiaNamed(IDR_APP_TOP_RIGHT);
  int top_edge_y = shadowed_area_top - top_center->height();
  canvas->DrawImageInt(*top_left, shadowed_area_bounds.x() - top_left->width(),
                       top_edge_y);
  canvas->TileImageInt(*top_center, shadowed_area_bounds.x(), top_edge_y,
                       shadowed_area_bounds.width(), top_center->height());
  canvas->DrawImageInt(*top_right, shadowed_area_bounds.right(), top_edge_y);

  // Right side.
  const gfx::ImageSkia* right = rb.GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
  int shadowed_area_bottom =
      std::max(shadowed_area_top, shadowed_area_bounds.bottom());
  int shadowed_area_height = shadowed_area_bottom - shadowed_area_top;
  canvas->TileImageInt(*right, shadowed_area_bounds.right(), shadowed_area_top,
                       right->width(), shadowed_area_height);

  // Bottom: left, center, right sides.
  const gfx::ImageSkia* bottom_left =
      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
  const gfx::ImageSkia* bottom_center =
      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
  const gfx::ImageSkia* bottom_right =
      rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER);

  canvas->DrawImageInt(*bottom_left,
                       shadowed_area_bounds.x() - bottom_left->width(),
                       shadowed_area_bottom);

  canvas->TileImageInt(*bottom_center, shadowed_area_bounds.x(),
                       shadowed_area_bottom, shadowed_area_bounds.width(),
                       bottom_right->height());

  canvas->DrawImageInt(*bottom_right, shadowed_area_bounds.right(),
                       shadowed_area_bottom);
  // Left side.
  const gfx::ImageSkia* left = rb.GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
  canvas->TileImageInt(*left, shadowed_area_bounds.x() - left->width(),
                       shadowed_area_top, left->width(), shadowed_area_height);
}

SkColor DefaultFrameView::GetFrameColor() const {
  return GetColorProvider()->GetColor(
      frame_->IsActive() ? ui::kColorFrameActive : ui::kColorFrameInactive);
}

gfx::ImageSkia DefaultFrameView::GetFrameImage() const {
  return *ui::ResourceBundle::GetSharedInstance()
              .GetImageNamed(frame_->IsActive() ? IDR_FRAME
                                                : IDR_FRAME_INACTIVE)
              .ToImageSkia();
}

void DefaultFrameView::LayoutWindowControls() {
  minimum_title_bar_x_ = 0;
  maximum_title_bar_x_ = width();

  if (bounds().IsEmpty()) {
    return;
  }

  int caption_y = CaptionButtonY();
  bool is_maximized = frame_->IsMaximized();
  // There should always be the same number of non-shadow pixels visible to the
  // side of the caption buttons.  In maximized mode we extend the edge button
  // to the screen corner to obey Fitts' Law.
  int extra_width =
      is_maximized ? (kFrameBorderThickness - kFrameShadowThickness) : 0;
  int next_button_x = FrameBorderThickness();

  bool is_restored = !is_maximized && !frame_->IsMinimized();
  ImageButton* invisible_button =
      is_restored ? restore_button_.get() : maximize_button_.get();
  invisible_button->SetVisible(false);

  WindowButtonOrderProvider* button_order =
      WindowButtonOrderProvider::GetInstance();
  const std::vector<views::FrameButton>& leading_buttons =
      button_order->leading_buttons();
  const std::vector<views::FrameButton>& trailing_buttons =
      button_order->trailing_buttons();

  ImageButton* button = nullptr;
  for (auto frame_button : leading_buttons) {
    button = GetImageButton(frame_button);
    if (!button) {
      continue;
    }
    gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
                            button->GetPreferredSize({}));
    if (frame_button == leading_buttons.front()) {
      target_bounds.set_width(target_bounds.width() + extra_width);
    }
    LayoutButton(button, target_bounds);
    next_button_x += button->width();
    minimum_title_bar_x_ = std::min(width(), next_button_x);
  }

  // Trailing buttions are laid out in a RTL fashion
  next_button_x = width() - FrameBorderThickness();
  for (auto frame_button : base::Reversed(trailing_buttons)) {
    button = GetImageButton(frame_button);
    if (!button) {
      continue;
    }
    gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
                            button->GetPreferredSize({}));
    if (frame_button == trailing_buttons.back()) {
      target_bounds.set_width(target_bounds.width() + extra_width);
    }
    target_bounds.Offset(-target_bounds.width(), 0);
    LayoutButton(button, target_bounds);
    next_button_x = button->x();
    maximum_title_bar_x_ = std::max(minimum_title_bar_x_, next_button_x);
  }
}

void DefaultFrameView::LayoutTitleBar() {
  DCHECK_GE(maximum_title_bar_x_, 0);
  // The window title position is calculated based on the icon position, even
  // when there is no icon.
  gfx::Rect icon_bounds(IconBounds());
  bool show_window_icon = window_icon_ != nullptr;
  if (show_window_icon) {
    window_icon_->SetBoundsRect(icon_bounds);
  }

  if (!frame_->widget_delegate()->ShouldShowWindowTitle()) {
    return;
  }

  // The offset between the window left edge and the title text.
  int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
                                 : icon_bounds.x();
  int title_height =
      TypographyProvider::Get().GetWindowTitleFontList().GetHeight();
  // We bias the title position so that when the difference between the icon and
  // title heights is odd, the extra pixel of the title is above the vertical
  // midline rather than below.  This compensates for how the icon is already
  // biased downwards (see IconBounds()) and helps prevent descenders on the
  // title from overlapping the 3D edge at the bottom of the titlebar.
  title_bounds_.SetRect(
      title_x,
      icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
      std::max(0, maximum_title_bar_x_ - kTitleCaptionSpacing - title_x),
      title_height);
}

void DefaultFrameView::LayoutClientView() {
  if (!ShouldShowTitleBarAndBorder()) {
    client_view_bounds_ = bounds();
    return;
  }

  int top_height = NonClientTopBorderHeight();
  int border_thickness = NonClientBorderThickness();
  client_view_bounds_.SetRect(
      border_thickness, top_height,
      std::max(0, width() - (2 * border_thickness)),
      std::max(0, height() - top_height - border_thickness));
}

ImageButton* DefaultFrameView::InitWindowCaptionButton(
    Button::PressedCallback callback,
    int accessibility_string_id,
    int normal_image_id,
    int hot_image_id,
    int pushed_image_id) {
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  ImageButton* button =
      AddChildView(std::make_unique<ImageButton>(std::move(callback)));
  button->SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
  button->GetViewAccessibility().SetName(
      l10n_util::GetStringUTF16(accessibility_string_id));
  button->SetImageModel(
      Button::STATE_NORMAL,
      ui::ImageModel::FromImage(rb.GetImageNamed(normal_image_id)));
  button->SetImageModel(
      Button::STATE_HOVERED,
      ui::ImageModel::FromImage(rb.GetImageNamed(hot_image_id)));
  button->SetImageModel(
      Button::STATE_PRESSED,
      ui::ImageModel::FromImage(rb.GetImageNamed(pushed_image_id)));
  return button;
}

ImageButton* DefaultFrameView::GetImageButton(views::FrameButton frame_button) {
  ImageButton* button = nullptr;
  switch (frame_button) {
    case views::FrameButton::kMinimize: {
      button = minimize_button_;
      // If we should not show the minimize button, then we return NULL as we
      // don't want this button to become visible and to be laid out.
      bool should_show = frame_->widget_delegate()->CanMinimize();
      button->SetVisible(should_show);
      if (!should_show) {
        return nullptr;
      }

      break;
    }
    case views::FrameButton::kMaximize: {
      bool is_restored = !frame_->IsMaximized() && !frame_->IsMinimized();
      button = is_restored ? maximize_button_.get() : restore_button_.get();
      // If we should not show the maximize/restore button, then we return
      // NULL as we don't want this button to become visible and to be laid
      // out.
      bool should_show = frame_->widget_delegate()->CanMaximize();
      button->SetVisible(should_show);
      if (!should_show) {
        return nullptr;
      }

      break;
    }
    case views::FrameButton::kClose: {
      button = close_button_;
      break;
    }
  }
  return button;
}

}  // namespace views