summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/frame/glass_browser_frame_view.cc
blob: 04fbcb0afa9ef120d41d37c765e81781507e11cc (plain)
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright (c) 2012 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.

#include "chrome/browser/ui/views/frame/glass_browser_frame_view.h"

#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/app/chrome_dll_resource.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/profiles/avatar_menu_button.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_header_helper.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "grit/theme_resources.h"
#include "skia/ext/image_operations.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/resource/resource_bundle_win.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/win/dpi.h"
#include "ui/resources/grit/ui_resources.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/resources/grit/views_resources.h"
#include "ui/views/win/hwnd_util.h"
#include "ui/views/window/client_view.h"

HICON GlassBrowserFrameView::throbber_icons_[
    GlassBrowserFrameView::kThrobberIconCount];

namespace {
// Size of client edge drawn inside the outer frame borders.
const int kNonClientBorderThicknessPreWin10 = 3;
const int kNonClientBorderThicknessWin10 = 1;
// Besides the frame border, there's empty space atop the window in restored
// mode, to use to drag the window around.
const int kNonClientRestoredExtraThickness = 11;
// In the window corners, the resize areas don't actually expand bigger, but the
// 16 px at the end of the top and bottom edges triggers diagonal resizing.
const int kResizeCornerWidth = 16;
// How far the new avatar button is from the left of the minimize button.
const int kNewAvatarButtonOffset = 5;
// The content edge images have a shadow built into them.
const int kContentEdgeShadowThickness = 2;
// In restored mode, the New Tab button isn't at the same height as the caption
// buttons, but the space will look cluttered if it actually slides under them,
// so we stop it when the gap between the two is down to 5 px.
const int kNewTabCaptionRestoredSpacing = 5;
// In maximized mode, where the New Tab button and the caption buttons are at
// similar vertical coordinates, we need to reserve a larger, 16 px gap to avoid
// looking too cluttered.
const int kNewTabCaptionMaximizedSpacing = 16;

// Converts the |image| to a Windows icon and returns the corresponding HICON
// handle. |image| is resized to desired |width| and |height| if needed.
base::win::ScopedHICON CreateHICONFromSkBitmapSizedTo(
    const gfx::ImageSkia& image,
    int width,
    int height) {
  return IconUtil::CreateHICONFromSkBitmap(
      width == image.width() && height == image.height()
          ? *image.bitmap()
          : skia::ImageOperations::Resize(*image.bitmap(),
                                          skia::ImageOperations::RESIZE_BEST,
                                          width, height));
}

}  // namespace

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, public:

GlassBrowserFrameView::GlassBrowserFrameView(BrowserFrame* frame,
                                             BrowserView* browser_view)
    : BrowserNonClientFrameView(frame, browser_view),
      profile_switcher_(this),
      throbber_running_(false),
      throbber_frame_(0) {
  if (browser_view->ShouldShowWindowIcon())
    InitThrobberIcons();
}

GlassBrowserFrameView::~GlassBrowserFrameView() {
}

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, BrowserNonClientFrameView implementation:

gfx::Rect GlassBrowserFrameView::GetBoundsForTabStrip(
    views::View* tabstrip) const {
  // In maximized RTL windows, don't let the tabstrip overlap the caption area,
  // or the alpha-blending it does will make things like the new avatar button
  // look glitchy.
  const int offset =
    (ui::MaterialDesignController::IsModeMaterial() || !base::i18n::IsRTL() ||
     !frame()->IsMaximized()) ?
        GetLayoutInsets(AVATAR_ICON).right() : 0;
  const int x = incognito_bounds_.right() + offset;
  int end_x = width() - NonClientBorderThickness(false);
  if (!base::i18n::IsRTL()) {
    end_x = std::min(frame()->GetMinimizeButtonOffset(), end_x) -
        (frame()->IsMaximized() ?
            kNewTabCaptionMaximizedSpacing : kNewTabCaptionRestoredSpacing);

    // The new avatar button is optionally displayed to the left of the
    // minimize button.
    if (profile_switcher_.view()) {
      const int old_end_x = end_x;
      end_x -= profile_switcher_.view()->width() + kNewAvatarButtonOffset;

      // In non-maximized mode, allow the new tab button to slide completely
      // under the avatar button.
      if (!frame()->IsMaximized()) {
        end_x = std::min(end_x + GetLayoutSize(NEW_TAB_BUTTON).width() +
                             kNewTabCaptionRestoredSpacing,
                         old_end_x);
      }
    }
  }
  return gfx::Rect(x, NonClientTopBorderHeight(false), std::max(0, end_x - x),
                   tabstrip->GetPreferredSize().height());
}

int GlassBrowserFrameView::GetTopInset(bool restored) const {
  return GetClientAreaInsets(restored).top();
}

int GlassBrowserFrameView::GetThemeBackgroundXInset() const {
  return 0;
}

void GlassBrowserFrameView::UpdateThrobber(bool running) {
  if (throbber_running_) {
    if (running) {
      DisplayNextThrobberFrame();
    } else {
      StopThrobber();
    }
  } else if (running) {
    StartThrobber();
  }
}

gfx::Size GlassBrowserFrameView::GetMinimumSize() const {
  gfx::Size min_size(browser_view()->GetMinimumSize());

  // Account for the client area insets.
  gfx::Insets insets = GetClientAreaInsets(false);
  min_size.Enlarge(insets.width(), insets.height());
  // Client area insets do not include the shadow thickness.
  min_size.Enlarge(2 * kContentEdgeShadowThickness, 0);

  // Ensure that the minimum width is enough to hold a tab strip with minimum
  // width at its usual insets.
  if (browser_view()->IsTabStripVisible()) {
    TabStrip* tabstrip = browser_view()->tabstrip();
    int min_tabstrip_width = tabstrip->GetMinimumSize().width();
    int min_tabstrip_area_width =
        width() - GetBoundsForTabStrip(tabstrip).width() + min_tabstrip_width;
    min_size.set_width(std::max(min_tabstrip_area_width, min_size.width()));
  }

  return min_size;
}

views::View* GlassBrowserFrameView::GetProfileSwitcherView() const {
  return profile_switcher_.view();
}

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, views::NonClientFrameView implementation:

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

gfx::Rect GlassBrowserFrameView::GetWindowBoundsForClientBounds(
    const gfx::Rect& client_bounds) const {
  HWND hwnd = views::HWNDForWidget(frame());
  if (!browser_view()->IsTabStripVisible() && hwnd) {
    // If we don't have a tabstrip, we're either a popup or an app window, in
    // which case we have a standard size non-client area and can just use
    // AdjustWindowRectEx to obtain it. We check for a non-null window handle in
    // case this gets called before the window is actually created.
    RECT rect = client_bounds.ToRECT();
    AdjustWindowRectEx(&rect, GetWindowLong(hwnd, GWL_STYLE), FALSE,
                       GetWindowLong(hwnd, GWL_EXSTYLE));
    return gfx::Rect(rect);
  }

  gfx::Insets insets = GetClientAreaInsets(false);
  return gfx::Rect(std::max(0, client_bounds.x() - insets.left()),
                   std::max(0, client_bounds.y() - insets.top()),
                   client_bounds.width() + insets.width(),
                   client_bounds.height() + insets.height());
}

int GlassBrowserFrameView::NonClientHitTest(const gfx::Point& point) {
  // If the browser isn't in normal mode, we haven't customized the frame, so
  // Windows can figure this out.  If the point isn't within our bounds, then
  // it's in the native portion of the frame, so again Windows can figure it
  // out.
  if (!browser_view()->IsBrowserTypeNormal() || !bounds().Contains(point))
    return HTNOWHERE;

  // See if the point is within the incognito icon or the new avatar menu.
  if ((avatar_button() &&
       avatar_button()->GetMirroredBounds().Contains(point)) ||
      (profile_switcher_.view() &&
       profile_switcher_.view()->GetMirroredBounds().Contains(point)))
    return HTCLIENT;

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

  // See if we're in the sysmenu region.  We still have to check the tabstrip
  // first so that clicks in a tab don't get treated as sysmenu clicks.
  int nonclient_border_thickness = NonClientBorderThickness(false);
  if (gfx::Rect(nonclient_border_thickness,
                gfx::win::GetSystemMetricsInDIP(SM_CYSIZEFRAME),
                gfx::win::GetSystemMetricsInDIP(SM_CXSMICON),
                gfx::win::GetSystemMetricsInDIP(SM_CYSMICON)).Contains(point))
    return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;

  if (frame_component != HTNOWHERE)
    return frame_component;

  int frame_top_border_height = FrameTopBorderHeight(false);
  // We want the resize corner behavior to apply to the kResizeCornerWidth
  // pixels at each end of the top and bottom edges.  Because |point|'s x
  // coordinate is based on the DWM-inset portion of the window (so, it's 0 at
  // the first pixel inside the left DWM margin), we need to subtract the DWM
  // margin thickness, which we calculate as the total frame border thickness
  // minus the nonclient border thickness.
  const int dwm_margin = FrameBorderThickness() - nonclient_border_thickness;
  int window_component = GetHTComponentForFrame(point, frame_top_border_height,
      nonclient_border_thickness, frame_top_border_height,
      kResizeCornerWidth - dwm_margin, frame()->widget_delegate()->CanResize());
  // Fall back to the caption if no other component matches.
  return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
}

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, views::View overrides:

void GlassBrowserFrameView::OnPaint(gfx::Canvas* canvas) {
  if (!browser_view()->IsTabStripVisible())
    return;
  if (IsToolbarVisible())
    PaintToolbarBackground(canvas);
  if (!frame()->IsMaximized())
    PaintClientEdge(canvas);
}

void GlassBrowserFrameView::Layout() {
  if (browser_view()->IsRegularOrGuestSession())
    LayoutNewStyleAvatar();
  LayoutIncognitoIcon();
  LayoutClientView();
}

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, protected:

// BrowserNonClientFrameView:
void GlassBrowserFrameView::UpdateAvatar() {
  if (browser_view()->IsRegularOrGuestSession())
    profile_switcher_.Update(AvatarButtonStyle::NATIVE);
  else
    UpdateOldAvatarButton();
}

///////////////////////////////////////////////////////////////////////////////
// GlassBrowserFrameView, private:

// views::NonClientFrameView:
bool GlassBrowserFrameView::DoesIntersectRect(const views::View* target,
                                              const gfx::Rect& rect) const {
  CHECK_EQ(target, this);
  bool hit_incognito_icon = avatar_button() &&
      avatar_button()->GetMirroredBounds().Intersects(rect);
  bool hit_new_avatar_button =
      profile_switcher_.view() &&
      profile_switcher_.view()->GetMirroredBounds().Intersects(rect);
  return hit_incognito_icon || hit_new_avatar_button ||
         !frame()->client_view()->bounds().Intersects(rect);
}

int GlassBrowserFrameView::FrameBorderThickness() const {
  return (frame()->IsMaximized() || frame()->IsFullscreen()) ?
      0 : gfx::win::GetSystemMetricsInDIP(SM_CXSIZEFRAME);
}

int GlassBrowserFrameView::FrameTopBorderHeight(bool restored) const {
  // We'd like to use FrameBorderThickness() here, but the maximized Aero glass
  // frame has a 0 frame border around most edges and a CYSIZEFRAME-thick border
  // at the top (see AeroGlassFrame::OnGetMinMaxInfo()).
  return (frame()->IsFullscreen() && !restored) ?
      0 : gfx::win::GetSystemMetricsInDIP(SM_CYSIZEFRAME);
}

int GlassBrowserFrameView::NonClientBorderThickness(bool restored) const {
  if ((frame()->IsMaximized() || frame()->IsFullscreen()) && !restored)
    return 0;

  return (base::win::GetVersion() < base::win::VERSION_WIN10)
             ? kNonClientBorderThicknessPreWin10
             : kNonClientBorderThicknessWin10;
}

int GlassBrowserFrameView::NonClientTopBorderHeight(bool restored) const {
  if (frame()->IsFullscreen() && !restored)
    return 0;

  const int top = FrameTopBorderHeight(restored);
  // The tab top inset is equal to the height of any shadow region above the
  // tabs, plus a 1 px top stroke.  In maximized mode, we want to push the
  // shadow region off the top of the screen but leave the top stroke.
  // Annoyingly, the pre-MD layout uses different heights for the hit-test
  // exclusion region (which we want here, since we're trying to size the border
  // so that the region above the tab's hit-test zone matches) versus the shadow
  // thickness.
  const int exclusion = GetLayoutConstant(TAB_TOP_EXCLUSION_HEIGHT);
  return (frame()->IsMaximized() && !restored) ?
      (top - GetLayoutInsets(TAB).top() + 1) :
      (top + kNonClientRestoredExtraThickness - exclusion);
}

bool GlassBrowserFrameView::IsToolbarVisible() const {
  return browser_view()->IsToolbarVisible() &&
      !browser_view()->toolbar()->GetPreferredSize().IsEmpty();
}

void GlassBrowserFrameView::PaintToolbarBackground(gfx::Canvas* canvas) const {
  gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds());
  if (toolbar_bounds.IsEmpty())
    return;
  gfx::Point toolbar_origin(toolbar_bounds.origin());
  ConvertPointToTarget(browser_view(), this, &toolbar_origin);
  toolbar_bounds.set_origin(toolbar_origin);

  const ui::ThemeProvider* tp = GetThemeProvider();
  const gfx::ImageSkia* const bg = tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR);
  int x = toolbar_bounds.x();
  const int y = toolbar_bounds.y();
  const int bg_y = GetTopInset(false) + Tab::GetYInsetForActiveTabBackground();
  int w = toolbar_bounds.width();
  const int h = toolbar_bounds.height();
  const SkColor separator_color =
      tp->GetColor(ThemeProperties::COLOR_TOOLBAR_BOTTOM_SEPARATOR);
  if (ui::MaterialDesignController::IsModeMaterial()) {
    // Background.  The top stroke is drawn above the toolbar bounds, so unlike
    // in the non-Material Design code below, we don't need to exclude any
    // region from having the background image drawn over it.
    if (tp->HasCustomImage(IDR_THEME_TOOLBAR)) {
      canvas->TileImageInt(*bg, x + GetThemeBackgroundXInset(), y - bg_y, x, y,
                           w, h);
    } else {
      canvas->FillRect(toolbar_bounds,
                       tp->GetColor(ThemeProperties::COLOR_TOOLBAR));
    }

    // Material Design has no corners to mask out.

    // Top stroke.  For Material Design, the toolbar has no side strokes.
    gfx::Rect separator_rect(x, y, w, 0);
    gfx::ScopedCanvas scoped_canvas(canvas);
    gfx::Rect tabstrip_bounds(GetBoundsForTabStrip(browser_view()->tabstrip()));
    tabstrip_bounds.set_x(GetMirroredXForRect(tabstrip_bounds));
    canvas->sk_canvas()->clipRect(gfx::RectToSkRect(tabstrip_bounds),
                                  SkRegion::kDifference_Op);
    separator_rect.set_y(tabstrip_bounds.bottom());
    BrowserView::Paint1pxHorizontalLine(canvas, GetToolbarTopSeparatorColor(),
                                        separator_rect, true);

    // Toolbar/content separator.
    toolbar_bounds.Inset(kClientEdgeThickness, 0);
    BrowserView::Paint1pxHorizontalLine(canvas, separator_color, toolbar_bounds,
                                        true);
  } else {
    // Background.  The top stroke is drawn using the IDR_CONTENT_TOP_XXX
    // images, which overlay the toolbar.  The top 2 px of these images is the
    // actual top stroke + shadow, and is partly transparent, so the toolbar
    // background shouldn't be drawn over it.
    const int bg_dest_y = y + kContentEdgeShadowThickness;
    canvas->TileImageInt(*bg, x + GetThemeBackgroundXInset(), bg_dest_y - bg_y,
                         x, bg_dest_y, w, h - kContentEdgeShadowThickness);

    // On Windows 10+ where we don't draw our own window border but rather go
    // right to the system border, the toolbar has no corners or side strokes.
    if (base::win::GetVersion() < base::win::VERSION_WIN10) {
      // Mask out the corners.
      const gfx::ImageSkia* const left =
          tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER);
      const int img_w = left->width();
      x -= kContentEdgeShadowThickness;
      SkPaint paint;
      paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
      canvas->DrawImageInt(
          *tp->GetImageSkiaNamed(IDR_CONTENT_TOP_LEFT_CORNER_MASK), 0, 0, img_w,
          h, x, y, img_w, h, false, paint);
      const int right_x =
          toolbar_bounds.right() + kContentEdgeShadowThickness - img_w;
      canvas->DrawImageInt(
          *tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER_MASK), 0, 0,
          img_w, h, right_x, y, img_w, h, false, paint);

      // Corner and side strokes.
      canvas->DrawImageInt(*left, 0, 0, img_w, h, x, y, img_w, h, false);
      canvas->DrawImageInt(
          *tp->GetImageSkiaNamed(IDR_CONTENT_TOP_RIGHT_CORNER), 0, 0, img_w, h,
          right_x, y, img_w, h, false);

      x += img_w;
      w = right_x - x;
    }

    // Top stroke.
    canvas->TileImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_TOP_CENTER), x, y,
                         w, kContentEdgeShadowThickness);

    // Toolbar/content separator.
    toolbar_bounds.Inset(kClientEdgeThickness, h - kClientEdgeThickness,
                         kClientEdgeThickness, 0);
    canvas->FillRect(toolbar_bounds, separator_color);
  }
}

void GlassBrowserFrameView::PaintClientEdge(gfx::Canvas* canvas) const {
  // Pre-Material Design, the client edge images start below the toolbar.  In MD
  // the client edge images start at the top of the toolbar.
  gfx::Rect client_bounds = CalculateClientAreaBounds();
  const int x = client_bounds.x();
  const bool md = ui::MaterialDesignController::IsModeMaterial();
  const gfx::Rect toolbar_bounds(browser_view()->GetToolbarBounds());
  const int y =
      client_bounds.y() + (md ? toolbar_bounds.y() : toolbar_bounds.bottom());
  const int w = client_bounds.width();
  const int right = client_bounds.right();
  const int bottom = std::max(y, height() - NonClientBorderThickness(false));
  const int height = bottom - y;

  // Draw the client edge images.  For non-MD, we fill the toolbar color
  // underneath these images so they will lighten/darken it appropriately to
  // create a "3D shaded" effect.  For MD, where we want a flatter appearance,
  // we do the filling afterwards so the user sees the unmodified toolbar color.
  const ui::ThemeProvider* tp = GetThemeProvider();
  const SkColor toolbar_color = tp->GetColor(ThemeProperties::COLOR_TOOLBAR);
  if (!md)
    FillClientEdgeRects(x, y, right, bottom, toolbar_color, canvas);
  const gfx::ImageSkia* const right_image =
      tp->GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
  const int img_w = right_image->width();
  canvas->TileImageInt(*right_image, right, y, img_w, height);
  canvas->DrawImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER),
                       right, bottom);
  const gfx::ImageSkia* const bottom_image =
      tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
  canvas->TileImageInt(*bottom_image, x, bottom, w, bottom_image->height());
  canvas->DrawImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER),
                       x - img_w, bottom);
  canvas->TileImageInt(*tp->GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE), x - img_w,
                       y, img_w, height);
  if (md)
    FillClientEdgeRects(x, y, right, bottom, toolbar_color, canvas);
}

void GlassBrowserFrameView::FillClientEdgeRects(int x,
                                                int y,
                                                int right,
                                                int bottom,
                                                SkColor color,
                                                gfx::Canvas* canvas) const {
  gfx::Rect side(x - kClientEdgeThickness, y, kClientEdgeThickness,
                 bottom + kClientEdgeThickness - y);
  canvas->FillRect(side, color);
  canvas->FillRect(gfx::Rect(x, bottom, right - x, kClientEdgeThickness),
                   color);
  side.set_x(right);
  canvas->FillRect(side, color);
}

void GlassBrowserFrameView::LayoutNewStyleAvatar() {
  DCHECK(browser_view()->IsRegularOrGuestSession());
  if (!profile_switcher_.view())
    return;

  gfx::Size label_size = profile_switcher_.view()->GetPreferredSize();

  int button_x = frame()->GetMinimizeButtonOffset() -
      kNewAvatarButtonOffset - label_size.width();
  if (base::i18n::IsRTL())
    button_x = width() - frame()->GetMinimizeButtonOffset() +
        kNewAvatarButtonOffset;

  // The caption button position and size is confusing.  In maximized mode, the
  // caption buttons are SM_CYMENUSIZE pixels high and are placed
  // FrameTopBorderHeight() pixels from the top of the window; all those top
  // border pixels are offscreen, so this result in caption buttons flush with
  // the top of the screen.  In restored mode, the caption buttons are first
  // placed just below a 2 px border at the top of the window (which is the
  // first two pixels' worth of FrameTopBorderHeight()), then extended upwards
  // one extra pixel to overlap part of this border.
  //
  // To match both of these, we size the button as if it's always the extra one
  // pixel in height, then we place it at the correct position in restored mode,
  // or one pixel above the top of the screen in maximized mode.
  int button_y = frame()->IsMaximized() ? (FrameTopBorderHeight(false) - 1) : 1;
  profile_switcher_.view()->SetBounds(
      button_x, button_y, label_size.width(),
      gfx::win::GetSystemMetricsInDIP(SM_CYMENUSIZE) + 1);
}

void GlassBrowserFrameView::LayoutIncognitoIcon() {
  const bool md = ui::MaterialDesignController::IsModeMaterial();
  const gfx::Insets insets(GetLayoutInsets(AVATAR_ICON));
  const gfx::Size size(GetOTRAvatarIcon().size());
  int x = NonClientBorderThickness(false);
  // In RTL, the icon needs to start after the caption buttons.
  if (base::i18n::IsRTL()) {
    x = width() - frame()->GetMinimizeButtonOffset() +
        (profile_switcher_.view()
             ? (profile_switcher_.view()->width() + kNewAvatarButtonOffset)
             : 0);
  } else if (!md && !avatar_button() && IsToolbarVisible() &&
             (base::win::GetVersion() < base::win::VERSION_WIN10)) {
    // In non-MD before Win 10, the toolbar has a rounded corner that we don't
    // want the tabstrip to overlap.
    x += browser_view()->GetToolbarBounds().x() - kContentEdgeShadowThickness +
        GetThemeProvider()->GetImageSkiaNamed(
            IDR_CONTENT_TOP_LEFT_CORNER)->width();
  }
  const int bottom = GetTopInset(false) + browser_view()->GetTabStripHeight() -
      insets.bottom();
  const int y = (md || !frame()->IsMaximized()) ?
      (bottom - size.height()) : FrameTopBorderHeight(false);
  incognito_bounds_.SetRect(x + (avatar_button() ? insets.left() : 0), y,
                            avatar_button() ? size.width() : 0, bottom - y);
  if (avatar_button())
    avatar_button()->SetBoundsRect(incognito_bounds_);
}

void GlassBrowserFrameView::LayoutClientView() {
  client_view_bounds_ = CalculateClientAreaBounds();
}

gfx::Insets GlassBrowserFrameView::GetClientAreaInsets(bool restored) const {
  if (!browser_view()->IsTabStripVisible())
    return gfx::Insets();

  const int top_height = NonClientTopBorderHeight(restored);
  const int border_thickness = NonClientBorderThickness(restored);
  return gfx::Insets(top_height,
                     border_thickness,
                     border_thickness,
                     border_thickness);
}

gfx::Rect GlassBrowserFrameView::CalculateClientAreaBounds() const {
  gfx::Rect bounds(GetLocalBounds());
  bounds.Inset(GetClientAreaInsets(false));
  return bounds;
}

void GlassBrowserFrameView::StartThrobber() {
  if (!throbber_running_) {
    throbber_running_ = true;
    throbber_frame_ = 0;
    InitThrobberIcons();
    SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
                static_cast<WPARAM>(ICON_SMALL),
                reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
  }
}

void GlassBrowserFrameView::StopThrobber() {
  if (throbber_running_) {
    throbber_running_ = false;

    base::win::ScopedHICON previous_small_icon;
    base::win::ScopedHICON previous_big_icon;
    HICON small_icon = nullptr;
    HICON big_icon = nullptr;

    // Check if hosted BrowserView has a window icon to use.
    if (browser_view()->ShouldShowWindowIcon()) {
      gfx::ImageSkia icon = browser_view()->GetWindowIcon();
      if (!icon.isNull()) {
        // Keep previous icons alive as long as they are referenced by the HWND.
        previous_small_icon = std::move(small_window_icon_);
        previous_big_icon = std::move(big_window_icon_);

        // Take responsibility for eventually destroying the created icons.
        small_window_icon_ = CreateHICONFromSkBitmapSizedTo(
            icon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON));
        big_window_icon_ = CreateHICONFromSkBitmapSizedTo(
            icon, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON));

        small_icon = small_window_icon_.get();
        big_icon = big_window_icon_.get();
      }
    }

    // Fallback to class icon.
    if (!small_icon) {
      small_icon = reinterpret_cast<HICON>(
          GetClassLongPtr(views::HWNDForWidget(frame()), GCLP_HICONSM));
    }
    if (!big_icon) {
      big_icon = reinterpret_cast<HICON>(
          GetClassLongPtr(views::HWNDForWidget(frame()), GCLP_HICON));
    }

    // This will reset the icon which we set in the throbber code.
    // WM_SETICON with null icon restores the icon for title bar but not
    // for taskbar. See http://crbug.com/29996
    SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
                static_cast<WPARAM>(ICON_SMALL),
                reinterpret_cast<LPARAM>(small_icon));

    SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
                static_cast<WPARAM>(ICON_BIG),
                reinterpret_cast<LPARAM>(big_icon));
  }
}

void GlassBrowserFrameView::DisplayNextThrobberFrame() {
  throbber_frame_ = (throbber_frame_ + 1) % kThrobberIconCount;
  SendMessage(views::HWNDForWidget(frame()), WM_SETICON,
              static_cast<WPARAM>(ICON_SMALL),
              reinterpret_cast<LPARAM>(throbber_icons_[throbber_frame_]));
}

// static
void GlassBrowserFrameView::InitThrobberIcons() {
  static bool initialized = false;
  if (!initialized) {
    for (int i = 0; i < kThrobberIconCount; ++i) {
      throbber_icons_[i] =
          ui::LoadThemeIconFromResourcesDataDLL(IDI_THROBBER_01 + i);
      DCHECK(throbber_icons_[i]);
    }
    initialized = true;
  }
}