Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

954 B
Raw Blame
import { Inject, Injectable, DOCUMENT } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { debugLog } from 'core-app/shared/helpers/debug_output';

@Injectable({ providedIn: 'root' })
export class ActiveWindowService {
  private activeState$ = new BehaviorSubject<boolean>(true);

  constructor(@Inject(DOCUMENT) document:Document) {
    document.addEventListener('visibilitychange', () => {
      if (document.visibilityState) {
        debugLog(`Browser window has visibility state changed to ${document.visibilityState}`);
        this.activeState$.next(document.visibilityState === 'visible');
      }
    });
  }

  /**
   * Returns whether the browser window/tab is active
   */
  public get isActive():boolean {
    return this.activeState$.value;
  }

  /**
   * Observable for notifying when visibility changes
   */
  public get active$():Observable<boolean> {
    return this.activeState$.asObservable();
  }
}