import { Inject , Injectable } from '@angular/core' ; import { DOCUMENT } from '@angular/common' ; import { Platform } from '@angular/cdk/platform' ; import { BehaviorSubject , fromEvent , Observable } from 'rxjs' ; import { debounceTime , tap , distinctUntilChanged , map } from 'rxjs/operators' ; import { ScrollbarManager } from '../utils/scrollbar-manager' ; @ Injectable ( { providedIn : 'root' } ) export class NativeScrollbarSizeFactory { private readonly _scrollbarSize = new BehaviorSubject < number > ( this . getNativeScrollbarSize ( ) ) ; scrollbarSize : Observable < number > = this . _scrollbarSize . asObservable ( ) ; constructor ( @ Inject ( DOCUMENT ) private document , private manager , private platform ) { if ( platform . isBrowser ) { fromEvent ( this . document . defaultView , 'resize' , { passive : true } ) . pipe ( debounceTime ( this . manager . globalOptions . windowResizeDebounce ) , map ( ( ) => this . getNativeScrollbarSize ( ) ) , distinctUntilChanged ( ) , tap ( ( size ) => this . _scrollbarSize . next ( size ) ) ) . subscribe ( ) ; } } /**
     * Get native scrollbar size
     */ private getNativeScrollbarSize ( ) { if ( ! this . platform . isBrowser ) { return 0 ; } if ( this . platform . IOS ) { return 6 ; } const box = this . document . createElement ( 'div' ) ; box . className = 'ng-scrollbar-measure' ; box . style . left = '0px' ; box . style . overflow = 'scroll' ; box . style . position = 'fixed' ; box . style . top = '-9999px' ; this . document . body . appendChild ( box ) ; const size = box . getBoundingClientRect ( ) . right ; this . document . body . removeChild ( box ) ; return size ; } }