]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/wrappers/screen.service.ts
On touchscreens add content overlay for opened menu (#3088)
[github/Chocobozzz/PeerTube.git] / client / src / app / core / wrappers / screen.service.ts
CommitLineData
fc11a44e 1import { Injectable } from '@angular/core'
bbe0f064
C
2
3@Injectable()
4export class ScreenService {
7034b3c9 5 isBroadcastMessageDisplayed = false
6
bbe0f064 7 private windowInnerWidth: number
fc11a44e
C
8 private lastFunctionCallTime: number
9 private cacheForMs = 500
bbe0f064 10
fc11a44e
C
11 constructor () {
12 this.refreshWindowInnerWidth()
bbe0f064
C
13 }
14
8544d8f5
K
15 isInSmallView (marginLeft = 0) {
16 if (marginLeft > 0) {
17 const contentWidth = this.getWindowInnerWidth() - marginLeft
18 return contentWidth < 800
19 }
20
8ff3f883 21 return this.getWindowInnerWidth() < 800
bbe0f064
C
22 }
23
3b20bdd6
RK
24 isInMediumView () {
25 return this.getWindowInnerWidth() < 1100
26 }
27
bbe0f064 28 isInMobileView () {
fc11a44e
C
29 return this.getWindowInnerWidth() < 500
30 }
31
8dfceec4
C
32 isInTouchScreen () {
33 return 'ontouchstart' in window || navigator.msMaxTouchPoints
34 }
35
6eb62c33
C
36 getNumberOfAvailableMiniatures () {
37 const screenWidth = this.getWindowInnerWidth()
38
39 let numberOfVideos = 1
40
cf78883c
C
41 if (screenWidth > 1850) numberOfVideos = 7
42 else if (screenWidth > 1600) numberOfVideos = 6
43 else if (screenWidth > 1370) numberOfVideos = 5
44 else if (screenWidth > 1100) numberOfVideos = 4
45 else if (screenWidth > 850) numberOfVideos = 3
6eb62c33
C
46
47 return numberOfVideos
48 }
49
fc11a44e 50 // Cache window inner width, because it's an expensive call
6eb62c33 51 getWindowInnerWidth () {
fc11a44e
C
52 if (this.cacheWindowInnerWidthExpired()) this.refreshWindowInnerWidth()
53
54 return this.windowInnerWidth
55 }
56
245b9d27
K
57 // https://stackoverflow.com/questions/2264072/detect-a-finger-swipe-through-javascript-on-the-iphone-and-android
58 onFingerSwipe (direction: 'left' | 'right' | 'up' | 'down', action: () => void, removeEventOnEnd = true) {
59 let touchDownClientX: number
60 let touchDownClientY: number
61
62 const onTouchStart = (event: TouchEvent) => {
63 const firstTouch = event.touches[0]
64 touchDownClientX = firstTouch.clientX
65 touchDownClientY = firstTouch.clientY
66 }
67
68 const onTouchMove = (event: TouchEvent) => {
69 if (!touchDownClientX || !touchDownClientY) {
70 return
71 }
72
73 const touchUpClientX = event.touches[0].clientX
74 const touchUpClientY = event.touches[0].clientY
75
76 const touchClientX = Math.abs(touchDownClientX - touchUpClientX)
77 const touchClientY = Math.abs(touchDownClientY - touchUpClientY)
78
79 if (touchClientX > touchClientY) {
80 if (touchClientX > 0) {
81 if (direction === 'left') {
82 if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove)
83 action()
84 }
85 } else {
86 if (direction === 'right') {
87 if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove)
88 action()
89 }
90 }
91 } else {
92 if (touchClientY > 0) {
93 if (direction === 'up') {
94 if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove)
95 action()
96 }
97 } else {
98 if (direction === 'down') {
99 if (removeEventOnEnd) this.removeFingerSwipeEventListener(onTouchStart, onTouchMove)
100 action()
101 }
102 }
103 }
104 }
105
106 document.addEventListener('touchstart', onTouchStart, false)
107 document.addEventListener('touchmove', onTouchMove, false)
108 }
109
110 private removeFingerSwipeEventListener (onTouchStart: (event: TouchEvent) => void, onTouchMove: (event: TouchEvent) => void) {
111 document.removeEventListener('touchstart', onTouchStart)
112 document.removeEventListener('touchmove', onTouchMove)
113 }
114
fc11a44e
C
115 private refreshWindowInnerWidth () {
116 this.lastFunctionCallTime = new Date().getTime()
117
118 this.windowInnerWidth = window.innerWidth
119 }
120
121 private cacheWindowInnerWidthExpired () {
3a0fb65c
C
122 if (!this.lastFunctionCallTime) return true
123
fc11a44e 124 return new Date().getTime() > (this.lastFunctionCallTime + this.cacheForMs)
bbe0f064
C
125 }
126}