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

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: format 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(SkColorSetA(separator_color, SkColorGetA(gradient_colors[0])));
47 paint.setStyle(SkPaint::kStroke_Style); 54 paint.setStyle(SkPaint::kStroke_Style);
48 // Anti-alias the path so it doesn't look goofy when the edges are not at 45 55 // 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, 56 // degree angles, but don't anti-alias anything else, especially not the fill,
50 // lest we get weird color bleeding problems. 57 // lest we get weird color bleeding problems.
51 paint.setAntiAlias(true); 58 paint.setAntiAlias(true);
52 canvas_skia->drawPath(infobar->stroke_path(), paint); 59 canvas_skia->drawPath(infobar->stroke_path(), paint);
53 paint.setAntiAlias(false); 60 paint.setAntiAlias(false);
54 61
55 // Now draw the separator at the bottom. 62 // Now draw the separator at the bottom.
56 canvas->FillRect( 63 canvas->FillRect(
57 gfx::Rect(0, 64 gfx::Rect(0,
58 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight, 65 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight,
59 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight), 66 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight),
60 separator_color_); 67 separator_color);
61 } 68 }
69
70 void InfoBarBackground::PaintMd(gfx::Canvas* canvas, views::View* view) const {
71 InfoBarView* infobar = static_cast<InfoBarView*>(view);
72 const infobars::InfoBarContainer::Delegate* delegate =
73 infobar->container_delegate();
74 SkPath stroke_path, fill_path;
75 SkColor separator_color = SK_ColorBLACK;
76 if (delegate) {
77 separator_color = delegate->GetInfoBarSeparatorColor();
78 int arrow_x;
79 if (delegate->DrawInfoBarArrows(&arrow_x) && infobar->arrow_height() > 0) {
80 SkScalar arrow_fill_height = SkIntToScalar(infobar->arrow_height());
81 SkScalar arrow_fill_half_width =
82 SkIntToScalar(infobar->arrow_half_width());
83 stroke_path.moveTo(SkIntToScalar(arrow_x) - arrow_fill_half_width,
84 SkIntToScalar(infobar->arrow_height()));
85 stroke_path.rLineTo(arrow_fill_half_width, -arrow_fill_height);
86 stroke_path.rLineTo(arrow_fill_half_width, arrow_fill_height);
87
88 fill_path = stroke_path;
89 fill_path.close();
90 }
91 }
92 fill_path.addRect(0, SkIntToScalar(infobar->arrow_height()),
93 SkIntToScalar(infobar->width()),
94 SkIntToScalar(infobar->height()));
95
96 gfx::ScopedCanvas scoped(canvas);
97 // Undo the scale factor so we can stroke with a width of 1px (not 1dp).
98 const float scale = canvas->UndoDeviceScaleFactor();
99 // View bounds are in dp. Manually scale for px.
100 gfx::SizeF view_size_px = gfx::ScaleSize(gfx::SizeF(view->size()), scale);
101
102 SkPaint fill;
103 fill.setStyle(SkPaint::kFill_Style);
104 fill.setColor(top_color_);
105
106 SkCanvas* canvas_skia = canvas->sk_canvas();
107 // The paths provided by the above calculations are in dp. Manually scale for
108 // px.
109 SkMatrix dsf_transform;
110 dsf_transform.setScale(SkFloatToScalar(scale), SkFloatToScalar(scale));
111 fill_path.transform(dsf_transform);
112 // In order to affect exactly the pixels we want, the fill and stroke paths
113 // need to go through the pixel centers instead of along the pixel
114 // edges/corners. Skia considers integral coordinates to be the edges between
115 // pixels, so offset by 0.5 to get to the centers.
116 fill_path.offset(SK_ScalarHalf, SK_ScalarHalf);
117 canvas_skia->drawPath(fill_path, fill);
118
119 SkPaint stroke;
120 stroke.setStyle(SkPaint::kStroke_Style);
121 const int kSeparatorThicknessPx = 1;
122 stroke.setStrokeWidth(SkIntToScalar(kSeparatorThicknessPx));
123 stroke.setColor(separator_color);
124 stroke_path.transform(dsf_transform);
125 stroke_path.offset(SK_ScalarHalf, SK_ScalarHalf);
126
127 // The arrow part of the top separator. (The rest was drawn by the toolbar or
128 // the infobar above this one.)
129 stroke.setAntiAlias(true);
130 canvas_skia->drawPath(stroke_path, stroke);
131 // Bottom separator.
132 stroke.setAntiAlias(false);
133 SkScalar y = SkIntToScalar(view_size_px.height() - kSeparatorThicknessPx) +
134 SK_ScalarHalf;
135 SkScalar w = SkIntToScalar(view_size_px.width());
136 canvas_skia->drawLine(0, y, w, y, stroke);
137 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/infobars/infobar_background.h ('k') | chrome/browser/ui/views/infobars/infobar_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698