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

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: move path calc 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
34 InfoBarView* infobar = static_cast<InfoBarView*>(view);
Peter Kasting 2016/03/30 00:20:26 Nit: Google style guide suggests declaring these "
Evan Stade 2016/04/02 02:21:41 Done.
35 const infobars::InfoBarContainer::Delegate* delegate =
36 infobar->container_delegate();
37 SkColor separator_color =
38 delegate ? delegate->GetInfoBarSeparatorColor() : SK_ColorBLACK;
Peter Kasting 2016/03/30 00:20:26 When will the delegate be null during painting? I
Evan Stade 2016/04/02 02:21:41 I don't know why it would be null. Removed.
39
28 SkPoint gradient_points[2]; 40 SkPoint gradient_points[2];
29 gradient_points[0].iset(0, 0); 41 gradient_points[0].iset(0, 0);
30 gradient_points[1].iset(0, view->height()); 42 gradient_points[1].iset(0, view->height());
31 SkColor gradient_colors[2] = { top_color_, bottom_color_ }; 43 SkColor gradient_colors[2] = {top_color_, bottom_color_};
32 SkPaint paint; 44 SkPaint paint;
33 paint.setStrokeWidth( 45 paint.setStrokeWidth(
34 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight)); 46 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight));
35 paint.setStyle(SkPaint::kFill_Style); 47 paint.setStyle(SkPaint::kFill_Style);
36 paint.setStrokeCap(SkPaint::kRound_Cap); 48 paint.setStrokeCap(SkPaint::kRound_Cap);
37 paint.setShader(SkGradientShader::MakeLinear( 49 paint.setShader(SkGradientShader::MakeLinear(
38 gradient_points, gradient_colors, NULL, 2, SkShader::kClamp_TileMode)); 50 gradient_points, gradient_colors, NULL, 2, SkShader::kClamp_TileMode));
39 51
40 InfoBarView* infobar = static_cast<InfoBarView*>(view);
41 SkCanvas* canvas_skia = canvas->sk_canvas(); 52 SkCanvas* canvas_skia = canvas->sk_canvas();
42 canvas_skia->drawPath(infobar->fill_path(), paint); 53 canvas_skia->drawPath(infobar->fill_path(), paint);
43 54
44 paint.setShader(NULL); 55 paint.setShader(NULL);
45 paint.setColor(SkColorSetA(separator_color_, 56 paint.setColor(SkColorSetA(separator_color, SkColorGetA(gradient_colors[0])));
46 SkColorGetA(gradient_colors[0])));
47 paint.setStyle(SkPaint::kStroke_Style); 57 paint.setStyle(SkPaint::kStroke_Style);
48 // Anti-alias the path so it doesn't look goofy when the edges are not at 45 58 // 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, 59 // degree angles, but don't anti-alias anything else, especially not the fill,
50 // lest we get weird color bleeding problems. 60 // lest we get weird color bleeding problems.
51 paint.setAntiAlias(true); 61 paint.setAntiAlias(true);
52 canvas_skia->drawPath(infobar->stroke_path(), paint); 62 canvas_skia->drawPath(infobar->stroke_path(), paint);
53 paint.setAntiAlias(false); 63 paint.setAntiAlias(false);
54 64
55 // Now draw the separator at the bottom. 65 // Now draw the separator at the bottom.
56 canvas->FillRect( 66 canvas->FillRect(
57 gfx::Rect(0, 67 gfx::Rect(0,
58 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight, 68 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight,
59 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight), 69 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight),
60 separator_color_); 70 separator_color);
61 } 71 }
72
73 void InfoBarBackground::PaintMd(gfx::Canvas* canvas, views::View* view) const {
74 InfoBarView* infobar = static_cast<InfoBarView*>(view);
75 const infobars::InfoBarContainer::Delegate* delegate =
76 infobar->container_delegate();
77 SkPath stroke_path, fill_path;
78 SkColor separator_color = SK_ColorBLACK;
79 if (delegate) {
80 separator_color = delegate->GetInfoBarSeparatorColor();
81 int arrow_x;
82 SkScalar arrow_fill_height =
83 SkIntToScalar(std::max(infobar->arrow_height(), 0));
Peter Kasting 2016/03/30 00:20:26 Can arrow_height() actually return a negative valu
Evan Stade 2016/04/02 02:21:41 I looked into this and can't find anywhere that se
84 if (delegate->DrawInfoBarArrows(&arrow_x) && arrow_fill_height) {
85 SkScalar arrow_fill_half_width =
86 SkIntToScalar(infobar->arrow_half_width());
87 stroke_path.moveTo(SkIntToScalar(arrow_x) - arrow_fill_half_width,
88 SkIntToScalar(infobar->arrow_height()));
89 stroke_path.rLineTo(arrow_fill_half_width, -arrow_fill_height);
90 stroke_path.rLineTo(arrow_fill_half_width, arrow_fill_height);
91
92 fill_path = stroke_path;
93 fill_path.close();
94 }
95 }
96 fill_path.addRect(
97 0.0, SkIntToScalar(infobar->arrow_height()),
Peter Kasting 2016/03/30 00:20:26 Nit: Unless it fails to compile, I'd use 0 instead
Evan Stade 2016/04/02 02:21:41 Done.
98 SkIntToScalar(infobar->width()),
99 SkIntToScalar(infobar->height() -
100 InfoBarContainerDelegate::kSeparatorLineHeight));
Peter Kasting 2016/03/30 00:20:26 Two issues: (1) In general, I don't think we want
Evan Stade 2016/04/02 02:21:41 ok, good point. Removed usage of this constant in
101
102 gfx::ScopedCanvas scoped(canvas);
103 // Undo the scale factor so we can stroke with a width of 1px (not 1dp).
104 const float scale = canvas->UndoDeviceScaleFactor();
105 // View bounds are in dp. Manually scale for px.
106 gfx::SizeF view_size_px = gfx::ScaleSize(gfx::SizeF(view->size()), scale);
107
108 SkPaint fill;
109 fill.setStyle(SkPaint::kFill_Style);
110 fill.setColor(top_color_);
111
112 SkCanvas* canvas_skia = canvas->sk_canvas();
113 // The paths provided by |infobar| are in dp. Manually scale for px.
114 SkMatrix dsf_transform;
115 dsf_transform.setScale(SkFloatToScalar(scale), SkFloatToScalar(scale));
116 fill_path.transform(dsf_transform);
117 fill_path.offset(SK_ScalarHalf, SK_ScalarHalf);
Peter Kasting 2016/03/30 00:20:26 Nit: This deserves an explanation
Evan Stade 2016/04/02 02:21:41 Done.
118 canvas_skia->drawPath(fill_path, fill);
119
120 SkPaint stroke;
121 stroke.setStyle(SkPaint::kStroke_Style);
122 stroke.setStrokeWidth(
123 SkIntToScalar(InfoBarContainerDelegate::kSeparatorLineHeight));
124 stroke.setColor(separator_color);
125 stroke_path.transform(dsf_transform);
126 stroke_path.offset(SK_ScalarHalf, SK_ScalarHalf);
127
128 // Top separator (with arrow shape).
Peter Kasting 2016/03/30 00:20:26 Doesn't this only draw the arrow shape, i.e. there
Evan Stade 2016/04/02 02:21:41 reworded
129 stroke.setAntiAlias(true);
130 canvas_skia->drawPath(stroke_path, stroke);
131 // Bottom separator.
132 stroke.setAntiAlias(false);
Peter Kasting 2016/03/30 00:20:26 Nit: I think this whole bottom section can just be
Evan Stade 2016/04/02 02:21:41 I don't think this is a huge win. We go from 4 lin
133 SkScalar y = SkIntToScalar(view_size_px.height() -
134 InfoBarContainerDelegate::kSeparatorLineHeight) +
135 SK_ScalarHalf;
136 SkScalar w = SkIntToScalar(view_size_px.width());
137 canvas_skia->drawLine(0, y, w, y, stroke);
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698