-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compute native scrollbar track size correctly even if not displa…
…yed at application startup When reloading the application, `SciViewportComponent` inside a remote application showed a native scrollbar, but only if the view was inactive at page reload. The following issues are fixed: - If the document is not displayed (e.g., if in a hidden iframe), the track of native scrollbars has no dimension. Thus, it is computed once the document is displayed. - If the scrollbar natively sits on top of the content (e.g., in OS X), the native scrollbar is used instead fixes #87
- Loading branch information
1 parent
44c40f4
commit e12718c
Showing
6 changed files
with
177 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
projects/scion/viewport/src/lib/native-scrollbar-track-size-provider.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright (c) 2018 Swiss Federal Railways | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
import { Inject, Injectable, NgZone, OnDestroy, Renderer2, RendererFactory2 } from '@angular/core'; | ||
import { DOCUMENT } from '@angular/common'; | ||
import { BehaviorSubject, fromEvent, Observable, Subject } from 'rxjs'; | ||
import { debounceTime, distinctUntilChanged, map, startWith, takeUntil } from 'rxjs/operators'; | ||
|
||
/** | ||
* Provides the native scrollbar tracksize. | ||
*/ | ||
@Injectable({providedIn: 'root'}) | ||
export class SciNativeScrollbarTrackSizeProvider implements OnDestroy { | ||
|
||
private readonly _trackSize$ = new BehaviorSubject<NativeScrollbarTrackSize | null>(null); | ||
private readonly _renderer: Renderer2; | ||
private readonly _destroy$ = new Subject<void>(); | ||
|
||
constructor(@Inject(DOCUMENT) private _document: any, rendererFactory: RendererFactory2, private _zone: NgZone) { | ||
this._renderer = rendererFactory.createRenderer(null, null); | ||
this.installNativeScrollbarTrackSizeListener(); | ||
} | ||
|
||
/** | ||
* Returns an {Observable} which emits the native scrollbar track size, if any, or else `null`. | ||
* | ||
* Upon subscription, it emits the track size immediately, and then continuously emits when the track size changes. | ||
*/ | ||
public get trackSize$(): Observable<NativeScrollbarTrackSize | null> { | ||
return this._trackSize$; | ||
} | ||
|
||
/** | ||
* Returns the native scrollbar track size, if any, or else `null`. | ||
*/ | ||
public get trackSize(): NativeScrollbarTrackSize { | ||
return this._trackSize$.getValue(); | ||
} | ||
|
||
/** | ||
* Computes the native scrollbar track size. | ||
* | ||
* @returns native track size, or `null` if the native scrollbars sit on top of the content. | ||
*/ | ||
private computeTrackSize(): NativeScrollbarTrackSize { | ||
// create temporary viewport and viewport client with native scrollbars to compute scrolltrack width | ||
const viewportDiv = this._renderer.createElement('div'); | ||
this.setStyle(this._renderer, viewportDiv, { | ||
position: 'absolute', | ||
overflow: 'scroll', | ||
height: '100px', | ||
width: '100px', | ||
border: 0, | ||
visibility: 'hidden', | ||
}); | ||
|
||
const viewportClientDiv = this._renderer.createElement('div'); | ||
this.setStyle(this._renderer, viewportClientDiv, { | ||
height: '100%', | ||
width: '100%', | ||
border: 0, | ||
}); | ||
|
||
this._renderer.appendChild(viewportDiv, viewportClientDiv); | ||
this._renderer.appendChild(this._document.body, viewportDiv); | ||
|
||
const trackSize: NativeScrollbarTrackSize = { | ||
hScrollbarTrackHeight: viewportDiv.offsetHeight - viewportClientDiv.offsetHeight, | ||
vScrollbarTrackWidth: viewportDiv.offsetWidth - viewportClientDiv.offsetWidth, | ||
}; | ||
|
||
// destroy temporary viewport | ||
this._renderer.removeChild(this._document.body, viewportDiv); | ||
if (trackSize.hScrollbarTrackHeight === 0 && trackSize.vScrollbarTrackWidth === 0) { | ||
return null; | ||
} | ||
|
||
return trackSize; | ||
} | ||
|
||
private installNativeScrollbarTrackSizeListener(): void { | ||
// Listen for window resize events to (re-)compute the native scrollbar track size. | ||
// For instance, if the document is not displayed (e.g., in hidden iframes), the track of native scrollbars do not have a dimension. | ||
// However, once displayed, the window sends a resize event. | ||
this._zone.runOutsideAngular(() => { | ||
fromEvent(window, 'resize') | ||
.pipe( | ||
debounceTime(5), | ||
startWith(null), // trigger the initial computation | ||
map((): NativeScrollbarTrackSize => this.computeTrackSize()), | ||
distinctUntilChanged((t1: NativeScrollbarTrackSize, t2: NativeScrollbarTrackSize) => JSON.stringify(t1) === JSON.stringify(t2)), | ||
takeUntil(this._destroy$), | ||
) | ||
.subscribe((trackSize: NativeScrollbarTrackSize) => { | ||
this._zone.run(() => this._trackSize$.next(trackSize)); | ||
}); | ||
}); | ||
} | ||
|
||
private setStyle(renderer: Renderer2, element: Element, style: { [key: string]: any }): void { | ||
Object.keys(style).forEach(key => renderer.setStyle(element, key, style[key])); | ||
} | ||
|
||
public ngOnDestroy(): void { | ||
this._renderer.destroy(); | ||
this._destroy$.next(); | ||
} | ||
} | ||
|
||
/** | ||
* Represents the native scrollbar track size. | ||
*/ | ||
export interface NativeScrollbarTrackSize { | ||
hScrollbarTrackHeight: number; | ||
vScrollbarTrackWidth: number; | ||
} |
58 changes: 0 additions & 58 deletions
58
projects/scion/viewport/src/lib/native-scrollbar-track-size.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters