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

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

Issue 2179643002: Fix infobar painting issues at fractional scales (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comment Created 4 years, 5 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
« no previous file with comments | « chrome/browser/ui/views/frame/browser_view_layout.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
64 gfx::Rect(0, 64 gfx::Rect(0,
65 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight, 65 view->height() - InfoBarContainerDelegate::kSeparatorLineHeight,
66 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight), 66 view->width(), InfoBarContainerDelegate::kSeparatorLineHeight),
67 separator_color); 67 separator_color);
68 } 68 }
69 69
70 void InfoBarBackground::PaintMd(gfx::Canvas* canvas, views::View* view) const { 70 void InfoBarBackground::PaintMd(gfx::Canvas* canvas, views::View* view) const {
71 InfoBarView* infobar = static_cast<InfoBarView*>(view); 71 InfoBarView* infobar = static_cast<InfoBarView*>(view);
72 const infobars::InfoBarContainer::Delegate* delegate = 72 const infobars::InfoBarContainer::Delegate* delegate =
73 infobar->container_delegate(); 73 infobar->container_delegate();
74 SkPath stroke_path, fill_path; 74
75 // The arrow part of the top separator. (The rest was drawn by the toolbar or
76 // the infobar above this one.)
77 SkPath stroke_path;
78 // The fill for the arrow
Peter Kasting 2016/07/25 22:21:21 Nit: This comment doesn't seem to add much. I'd p
Evan Stade 2016/07/25 23:16:48 Done.
79 SkPath fill_path;
75 SkColor separator_color = SK_ColorBLACK; 80 SkColor separator_color = SK_ColorBLACK;
81 gfx::ScopedCanvas scoped(canvas);
82 // Undo the scale factor so we can stroke with a width of 1px (not 1dp).
83 const float dsf = canvas->UndoDeviceScaleFactor();
84 // Always scale to a whole number to make sure strokes are sharp.
Peter Kasting 2016/07/25 22:21:21 Nit: Can you extend this comment to explain why fl
Bret 2016/07/25 22:35:31 Which method to use to snap to pixels is somewhat
Peter Kasting 2016/07/25 22:38:27 To be clear, I'm fine with any of them (whatever w
Evan Stade 2016/07/25 23:16:48 good question, I didn't give it much thought beyon
85 auto scale = [dsf](int dimension) { return std::floor(dimension * dsf); };
86 SkScalar arrow_height = scale(infobar->arrow_height());
76 if (delegate) { 87 if (delegate) {
77 separator_color = delegate->GetInfoBarSeparatorColor(); 88 separator_color = delegate->GetInfoBarSeparatorColor();
78 int arrow_x; 89 int arrow_x;
79 if (delegate->DrawInfoBarArrows(&arrow_x) && infobar->arrow_height() > 0) { 90 if (delegate->DrawInfoBarArrows(&arrow_x) && infobar->arrow_height() > 0) {
80 SkScalar arrow_fill_height = SkIntToScalar(infobar->arrow_height()); 91 SkScalar arrow_half_width =
81 SkScalar arrow_fill_half_width = 92 scale(infobar->arrow_half_width());
82 SkIntToScalar(infobar->arrow_half_width()); 93 stroke_path.moveTo(scale(arrow_x) - scale(infobar->arrow_half_width()),
Bret 2016/07/25 22:35:31 reuse arrow_half_width
Evan Stade 2016/07/25 23:23:25 Done.
83 stroke_path.moveTo(SkIntToScalar(arrow_x) - arrow_fill_half_width, 94 arrow_height);
84 SkIntToScalar(infobar->arrow_height())); 95 stroke_path.rLineTo(arrow_half_width, -arrow_height);
85 stroke_path.rLineTo(arrow_fill_half_width, -arrow_fill_height); 96 // Make the tip of the arrow sharp.
86 stroke_path.rLineTo(arrow_fill_half_width, arrow_fill_height); 97 stroke_path.rLineTo(-1, 0);
Peter Kasting 2016/07/25 22:21:21 I don't understand how this line works. Doesn't t
Evan Stade 2016/07/25 23:16:48 Your ASCII art is accurate, except the way it's re
98 stroke_path.rLineTo(arrow_half_width, arrow_height);
87 99
88 fill_path = stroke_path; 100 fill_path = stroke_path;
89 fill_path.close(); 101 fill_path.close();
90 } 102 }
91 } 103 }
92 fill_path.addRect(0, SkIntToScalar(infobar->arrow_height()), 104 fill_path.addRect(0, arrow_height, scale(infobar->width()),
93 SkIntToScalar(infobar->width()), 105 scale(infobar->height()));
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; 106 SkPaint fill;
103 fill.setStyle(SkPaint::kFill_Style); 107 fill.setStyle(SkPaint::kFill_Style);
104 fill.setColor(top_color_); 108 fill.setColor(top_color_);
105 109 canvas->DrawPath(fill_path, fill);
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 110
119 SkPaint stroke; 111 SkPaint stroke;
120 stroke.setStyle(SkPaint::kStroke_Style); 112 stroke.setStyle(SkPaint::kStroke_Style);
121 const int kSeparatorThicknessPx = 1; 113 const int kSeparatorThicknessPx = 1;
122 stroke.setStrokeWidth(SkIntToScalar(kSeparatorThicknessPx)); 114 stroke.setStrokeWidth(SkIntToScalar(kSeparatorThicknessPx));
Bret 2016/07/25 22:35:31 I think the default stroke width is 1px already (w
Evan Stade 2016/07/25 23:23:25 the default is 0 (hairline)
123 stroke.setColor(separator_color); 115 stroke.setColor(separator_color);
124 stroke_path.transform(dsf_transform); 116 stroke.setAntiAlias(true);
125 stroke_path.offset(SK_ScalarHalf, SK_ScalarHalf); 117 canvas->DrawPath(stroke_path, stroke);
126 118
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. 119 // Bottom separator.
132 stroke.setAntiAlias(false); 120 stroke.setAntiAlias(false);
121 gfx::SizeF view_size_px = gfx::ScaleSize(gfx::SizeF(view->size()), dsf);
133 SkScalar y = SkIntToScalar(view_size_px.height() - kSeparatorThicknessPx) + 122 SkScalar y = SkIntToScalar(view_size_px.height() - kSeparatorThicknessPx) +
134 SK_ScalarHalf; 123 SK_ScalarHalf;
135 SkScalar w = SkIntToScalar(view_size_px.width()); 124 SkScalar w = SkIntToScalar(view_size_px.width());
136 canvas_skia->drawLine(0, y, w, y, stroke); 125 canvas->sk_canvas()->drawLine(0, y, w, y, stroke);
137 } 126 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/frame/browser_view_layout.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698