Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/browser/ui/views/infobars/infobar_background.cc

Issue 1826653002: Draw infobar arrow border with width of 1px on any scale factor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: git commit Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/infobars/infobar_background.h" 5 #include "chrome/browser/ui/views/infobars/infobar_background.h"
6 6
7 #include "chrome/browser/ui/infobar_container_delegate.h" 7 #include "chrome/browser/ui/infobar_container_delegate.h"
8 #include "chrome/browser/ui/views/infobars/infobar_view.h" 8 #include "chrome/browser/ui/views/infobars/infobar_view.h"
9 #include "components/infobars/core/infobar.h" 9 #include "components/infobars/core/infobar.h"
10 #include "third_party/skia/include/effects/SkGradientShader.h" 10 #include "third_party/skia/include/effects/SkGradientShader.h"
11 #include "ui/base/material_design/material_design_controller.h"
11 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/color_utils.h" 13 #include "ui/gfx/color_utils.h"
14 #include "ui/gfx/scoped_canvas.h"
13 #include "ui/views/view.h" 15 #include "ui/views/view.h"
14 16
15 InfoBarBackground::InfoBarBackground( 17 InfoBarBackground::InfoBarBackground(
16 infobars::InfoBarDelegate::Type infobar_type) 18 infobars::InfoBarDelegate::Type infobar_type)
17 : separator_color_(SK_ColorBLACK), 19 : top_color_(infobars::InfoBar::GetTopColor(infobar_type)),
18 top_color_(infobars::InfoBar::GetTopColor(infobar_type)),
19 bottom_color_(infobars::InfoBar::GetBottomColor(infobar_type)) { 20 bottom_color_(infobars::InfoBar::GetBottomColor(infobar_type)) {
20 SetNativeControlColor( 21 SetNativeControlColor(
21 color_utils::AlphaBlend(top_color_, bottom_color_, 128)); 22 color_utils::AlphaBlend(top_color_, bottom_color_, 128));
22 } 23 }
23 24
24 InfoBarBackground::~InfoBarBackground() { 25 InfoBarBackground::~InfoBarBackground() {
25 } 26 }
26 27
27 void InfoBarBackground::Paint(gfx::Canvas* canvas, views::View* view) const { 28 void InfoBarBackground::Paint(gfx::Canvas* canvas, views::View* view) const {
29 if (ui::MaterialDesignController::IsModeMaterial()) {
30 PaintMd(canvas, view);
31 return;
32 }
33
28 SkPoint gradient_points[2]; 34 SkPoint gradient_points[2];
29 gradient_points[0].iset(0, 0); 35 gradient_points[0].iset(0, 0);
30 gradient_points[1].iset(0, view->height()); 36 gradient_points[1].iset(0, view->height());
31 SkColor gradient_colors[2] = { top_color_, bottom_color_ }; 37 SkColor gradient_colors[2] = {top_color_, bottom_color_};
32 SkPaint paint; 38 SkPaint paint;
33 paint.setStrokeWidth( 39 paint.setStrokeWidth(
34 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight)); 40 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight));
35 paint.setStyle(SkPaint::kFill_Style); 41 paint.setStyle(SkPaint::kFill_Style);
36 paint.setStrokeCap(SkPaint::kRound_Cap); 42 paint.setStrokeCap(SkPaint::kRound_Cap);
37 paint.setShader(SkGradientShader::MakeLinear( 43 paint.setShader(SkGradientShader::MakeLinear(
38 gradient_points, gradient_colors, NULL, 2, SkShader::kClamp_TileMode)); 44 gradient_points, gradient_colors, nullptr, 2, SkShader::kClamp_TileMode));
39 45
46 SkCanvas* canvas_skia = canvas->sk_canvas();
40 InfoBarView* infobar = static_cast<InfoBarView*>(view); 47 InfoBarView* infobar = static_cast<InfoBarView*>(view);
41 SkCanvas* canvas_skia = canvas->sk_canvas();
42 canvas_skia->drawPath(infobar->fill_path(), paint); 48 canvas_skia->drawPath(infobar->fill_path(), paint);
43 49
44 paint.setShader(NULL); 50 paint.setShader(nullptr);
45 paint.setColor(SkColorSetA(separator_color_, 51 SkColor separator_color =
46 SkColorGetA(gradient_colors[0]))); 52 infobar->container_delegate()->GetInfoBarSeparatorColor();
53 paint.setColor(
54 SkColorSetA(separator_color,
55 SkColorGetA(gradient_colors[0])));
47 paint.setStyle(SkPaint::kStroke_Style); 56 paint.setStyle(SkPaint::kStroke_Style);
48 // Anti-alias the path so it doesn't look goofy when the edges are not at 45 57 // Anti-alias the path so it doesn't look goofy when the edges are not at 45
49 // degree angles, but don't anti-alias anything else, especially not the fill, 58 // degree angles, but don't anti-alias anything else, especially not the fill,
50 // lest we get weird color bleeding problems. 59 // lest we get weird color bleeding problems.
51 paint.setAntiAlias(true); 60 paint.setAntiAlias(true);
52 canvas_skia->drawPath(infobar->stroke_path(), paint); 61 canvas_skia->drawPath(infobar->stroke_path(), paint);
53 paint.setAntiAlias(false); 62 paint.setAntiAlias(false);
54 63
55 // Now draw the separator at the bottom. 64 // Now draw the separator at the bottom.
56 canvas->FillRect( 65 canvas->FillRect(
57 gfx::Rect(0, 66 gfx::Rect(0,
58 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight, 67 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight,
59 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight), 68 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight),
60 separator_color_); 69 separator_color);
61 } 70 }
71
72 void InfoBarBackground::PaintMd(gfx::Canvas* canvas, views::View* view) const {
73 InfoBarView* infobar = static_cast<InfoBarView*>(view);
74 const infobars::InfoBarContainer::Delegate* delegate =
75 infobar->container_delegate();
76 SkPath stroke_path, fill_path;
77 SkColor separator_color = SK_ColorBLACK;
78 if (delegate) {
79 separator_color = delegate->GetInfoBarSeparatorColor();
80 int arrow_x;
81 if (delegate->DrawInfoBarArrows(&arrow_x) && infobar->arrow_height() > 0) {
82 SkScalar arrow_fill_height = SkIntToScalar(infobar->arrow_height());
83 SkScalar arrow_fill_half_width =
84 SkIntToScalar(infobar->arrow_half_width());
85 stroke_path.moveTo(SkIntToScalar(arrow_x) - arrow_fill_half_width,
86 SkIntToScalar(infobar->arrow_height()));
87 stroke_path.rLineTo(arrow_fill_half_width, -arrow_fill_height);
88 stroke_path.rLineTo(arrow_fill_half_width, arrow_fill_height);
89
90 fill_path = stroke_path;
91 fill_path.close();
92 }
93 }
94 fill_path.addRect(0, SkIntToScalar(infobar->arrow_height()),
95 SkIntToScalar(infobar->width()),
96 SkIntToScalar(infobar->height()));
97
98 gfx::ScopedCanvas scoped(canvas);
99 // Undo the scale factor so we can stroke with a width of 1px (not 1dp).
100 const float scale = canvas->UndoDeviceScaleFactor();
101 // View bounds are in dp. Manually scale for px.
102 gfx::SizeF view_size_px = gfx::ScaleSize(gfx::SizeF(view->size()), scale);
103
104 SkPaint fill;
105 fill.setStyle(SkPaint::kFill_Style);
106 fill.setColor(top_color_);
107
108 SkCanvas* canvas_skia = canvas->sk_canvas();
109 // The paths provided by |infobar| are in dp. Manually scale for px.
Peter Kasting 2016/04/02 02:42:11 Nit: I think this comment is slightly out of date,
Evan Stade 2016/04/04 17:24:29 Done.
110 SkMatrix dsf_transform;
111 dsf_transform.setScale(SkFloatToScalar(scale), SkFloatToScalar(scale));
112 fill_path.transform(dsf_transform);
113 // Skia considers the middle of the pixel to be a fractional value (e.g. 0.5).
Peter Kasting 2016/04/02 02:42:11 Nit: This comment is confusing to me since it soun
Evan Stade 2016/04/04 17:24:29 Done.
114 fill_path.offset(SK_ScalarHalf, SK_ScalarHalf);
115 canvas_skia->drawPath(fill_path, fill);
116
117 SkPaint stroke;
118 stroke.setStyle(SkPaint::kStroke_Style);
119 const int kSeparatorThicknessPx = 1;
120 stroke.setStrokeWidth(SkIntToScalar(kSeparatorThicknessPx));
121 stroke.setColor(separator_color);
122 stroke_path.transform(dsf_transform);
123 stroke_path.offset(SK_ScalarHalf, SK_ScalarHalf);
124
125 // Top separator (just the arrow shape).
Peter Kasting 2016/04/02 02:42:11 Nit: Maybe fill this out more: ...(just the arrow
Evan Stade 2016/04/04 17:24:29 Done.
126 stroke.setAntiAlias(true);
127 canvas_skia->drawPath(stroke_path, stroke);
128 // Bottom separator.
129 stroke.setAntiAlias(false);
130 SkScalar y = SkIntToScalar(view_size_px.height() - kSeparatorThicknessPx) +
131 SK_ScalarHalf;
132 SkScalar w = SkIntToScalar(view_size_px.width());
133 canvas_skia->drawLine(0, y, w, y, stroke);
134 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698