]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/misc/screen.service.ts
db481204e5ced7d75e971ae7821c9c62bd8f0ade
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / misc / screen.service.ts
1 import { Injectable } from '@angular/core'
2
3 @Injectable()
4 export class ScreenService {
5 private windowInnerWidth: number
6 private lastFunctionCallTime: number
7 private cacheForMs = 500
8
9 constructor () {
10 this.refreshWindowInnerWidth()
11 }
12
13 isInSmallView () {
14 return this.getWindowInnerWidth() < 800
15 }
16
17 isInMobileView () {
18 return this.getWindowInnerWidth() < 500
19 }
20
21 // Cache window inner width, because it's an expensive call
22 private getWindowInnerWidth () {
23 if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
24
25 return this.windowInnerWidth
26 }
27
28 private refreshWindowInnerWidth () {
29 this.lastFunctionCallTime = new Date().getTime()
30
31 this.windowInnerWidth = window.innerWidth
32 }
33
34 private cacheWindowInnerWidthExpired () {
35 if (!this.lastFunctionCallTime) return true
36
37 return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)
38 }
39 }