Skip to content

Commit 303dd69

Browse files
jelbournkara
authored andcommitted
fix(overlay): prevent blurry connected overlays (#1784)
1 parent c2597b6 commit 303dd69

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

e2e/components/menu/menu-page.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ export class MenuPage {
5353

5454
expectMenuLocation(el: ElementFinder, {x,y}: {x: number, y: number}) {
5555
el.getLocation().then((loc) => {
56-
expect(loc.x).toEqual(x);
57-
expect(loc.y).toEqual(y);
56+
// Round the values because we expect the menu overlay to also have been rounded
57+
// to avoid blurriness due to subpixel rendering.
58+
expect(loc.x).toEqual(Math.round(x));
59+
expect(loc.y).toEqual(Math.round(y));
5860
});
5961
}
6062

src/lib/core/overlay/position/connected-position-strategy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ export class ConnectedPositionStrategy implements PositionStrategy {
227227
* @param overlayPoint
228228
*/
229229
private _setElementPosition(element: HTMLElement, overlayPoint: Point) {
230-
let x = overlayPoint.x;
231-
let y = overlayPoint.y;
230+
// Round the values to prevent blurry overlays due to subpixel rendering.
231+
let x = Math.round(overlayPoint.x);
232+
let y = Math.round(overlayPoint.y);
232233

233234
// TODO(jelbourn): we don't want to always overwrite the transform property here,
234235
// because it will need to be used for animations.

src/lib/menu/menu.spec.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,13 @@ describe('MdMenu', () => {
155155
// In "before" position, the right sides of the overlay and the origin are aligned.
156156
// To find the overlay left, subtract the menu width from the origin's right side.
157157
const expectedLeft = triggerRect.right - overlayRect.width;
158-
expect(overlayRect.left.toFixed(2))
159-
.toEqual(expectedLeft.toFixed(2),
158+
expect(overlayRect.left)
159+
.toBe(Math.round(expectedLeft),
160160
`Expected menu to open in "before" position if "after" position wouldn't fit.`);
161161

162162
// The y-position of the overlay should be unaffected, as it can already fit vertically
163-
expect(overlayRect.top.toFixed(2))
164-
.toEqual(triggerRect.top.toFixed(2),
163+
expect(overlayRect.top)
164+
.toBe(Math.round(triggerRect.top),
165165
`Expected menu top position to be unchanged if it can fit in the viewport.`);
166166
});
167167

@@ -184,13 +184,13 @@ describe('MdMenu', () => {
184184
// In "above" position, the bottom edges of the overlay and the origin are aligned.
185185
// To find the overlay top, subtract the menu height from the origin's bottom edge.
186186
const expectedTop = triggerRect.bottom - overlayRect.height;
187-
expect(overlayRect.top.toFixed(2))
188-
.toEqual(expectedTop.toFixed(2),
187+
expect(overlayRect.top)
188+
.toBe(Math.round(expectedTop),
189189
`Expected menu to open in "above" position if "below" position wouldn't fit.`);
190190

191191
// The x-position of the overlay should be unaffected, as it can already fit horizontally
192-
expect(overlayRect.left.toFixed(2))
193-
.toEqual(triggerRect.left.toFixed(2),
192+
expect(overlayRect.left)
193+
.toBe(Math.round(triggerRect.left),
194194
`Expected menu x position to be unchanged if it can fit in the viewport.`);
195195
});
196196

@@ -214,12 +214,12 @@ describe('MdMenu', () => {
214214
const expectedLeft = triggerRect.right - overlayRect.width;
215215
const expectedTop = triggerRect.bottom - overlayRect.height;
216216

217-
expect(overlayRect.left.toFixed(2))
218-
.toEqual(expectedLeft.toFixed(2),
217+
expect(overlayRect.left)
218+
.toBe(Math.round(expectedLeft),
219219
`Expected menu to open in "before" position if "after" position wouldn't fit.`);
220220

221-
expect(overlayRect.top.toFixed(2))
222-
.toEqual(expectedTop.toFixed(2),
221+
expect(overlayRect.top)
222+
.toBe(Math.round(expectedTop),
223223
`Expected menu to open in "above" position if "below" position wouldn't fit.`);
224224
});
225225

@@ -236,14 +236,14 @@ describe('MdMenu', () => {
236236

237237
// As designated "before" position won't fit on screen, the menu should fall back
238238
// to "after" mode, where the left sides of the overlay and trigger are aligned.
239-
expect(overlayRect.left.toFixed(2))
240-
.toEqual(triggerRect.left.toFixed(2),
239+
expect(overlayRect.left)
240+
.toBe(Math.round(triggerRect.left),
241241
`Expected menu to open in "after" position if "before" position wouldn't fit.`);
242242

243243
// As designated "above" position won't fit on screen, the menu should fall back
244244
// to "below" mode, where the top edges of the overlay and trigger are aligned.
245-
expect(overlayRect.top.toFixed(2))
246-
.toEqual(triggerRect.top.toFixed(2),
245+
expect(overlayRect.top)
246+
.toBe(Math.round(triggerRect.top),
247247
`Expected menu to open in "below" position if "above" position wouldn't fit.`);
248248
});
249249

0 commit comments

Comments
 (0)