Skip to content

fix(cdk/virtual-scroll): fix subpixel rounding errors on hdpi screens #16269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cdk/scrolling/virtual-scroll-viewport.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
Spacer used to force the scrolling container to the correct size for the *total* number of items
so that the scrollbar captures the size of the entire data set.
-->
<div class="cdk-virtual-scroll-spacer" [style.transform]="_totalContentSizeTransform"></div>
<div class="cdk-virtual-scroll-spacer"
[style.width]="_totalContentWidth" [style.height]="_totalContentHeight"></div>
35 changes: 26 additions & 9 deletions src/cdk/scrolling/virtual-scroll-viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,17 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
private _renderedRangeSubject = new Subject<ListRange>();

/** The direction the viewport scrolls. */
@Input() orientation: 'horizontal' | 'vertical' = 'vertical';
@Input()
get orientation() {
return this._orientation;
}
set orientation(orientation: 'horizontal' | 'vertical') {
if (this._orientation !== orientation) {
this._orientation = orientation;
this._calculateSpacerSize();
}
}
private _orientation: 'horizontal' | 'vertical' = 'vertical';

// Note: we don't use the typical EventEmitter here because we need to subscribe to the scroll
// strategy lazily (i.e. only if the user is actually listening to the events). We do this because
Expand All @@ -89,17 +99,17 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
/** A stream that emits whenever the rendered range changes. */
renderedRangeStream: Observable<ListRange> = this._renderedRangeSubject.asObservable();

/**
* The transform used to scale the spacer to the same size as all content, including content that
* is not currently rendered.
*/
_totalContentSizeTransform = '';

/**
* The total size of all content (in pixels), including content that is not currently rendered.
*/
private _totalContentSize = 0;

/** A string representing the `style.width` property value to be used for the spacer element. */
_totalContentWidth = '';

/** A string representing the `style.height` property value to be used for the spacer element. */
_totalContentHeight = '';

/**
* The CSS transform applied to the rendered subset of items so that they appear within the bounds
* of the visible viewport.
Expand Down Expand Up @@ -238,8 +248,7 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
setTotalContentSize(size: number) {
if (this._totalContentSize !== size) {
this._totalContentSize = size;
const axis = this.orientation == 'horizontal' ? 'X' : 'Y';
this._totalContentSizeTransform = `scale${axis}(${this._totalContentSize})`;
this._calculateSpacerSize();
this._markChangeDetectionNeeded();
}
}
Expand Down Expand Up @@ -398,4 +407,12 @@ export class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, O
fn();
}
}

/** Calculates the `style.width` and `style.height` for the spacer element. */
private _calculateSpacerSize() {
this._totalContentHeight =
this.orientation === 'horizontal' ? '' : `${this._totalContentSize}px`;
this._totalContentWidth =
this.orientation === 'horizontal' ? `${this._totalContentSize}px` : '';
}
}
3 changes: 2 additions & 1 deletion tools/public_api_guard/cdk/scrolling.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export declare type CdkVirtualForOfContext<T> = {

export declare class CdkVirtualScrollViewport extends CdkScrollable implements OnInit, OnDestroy {
_contentWrapper: ElementRef<HTMLElement>;
_totalContentSizeTransform: string;
_totalContentHeight: string;
_totalContentWidth: string;
elementRef: ElementRef<HTMLElement>;
orientation: 'horizontal' | 'vertical';
renderedRangeStream: Observable<ListRange>;
Expand Down