-
Notifications
You must be signed in to change notification settings - Fork 85
/
AutoHide.ts
246 lines (200 loc) · 6.78 KB
/
AutoHide.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (c) 2023-present NAVER Corp.
* egjs projects are licensed under the MIT license
*/
import ControlBar from "./ControlBar";
import View360 from "../../View360";
import * as BROWSER from "../../const/browser";
import { FULLSCREEN_CHANGE } from "../../const/browser";
import TextureVideo from "../../texture/TextureVideo";
import { isFullscreen } from "../../utils";
/**
* Options for ControlBar's {@link ControlBarOptions#autoHide}
* @ko ControlBar의 {@link ControlBarOptions#autoHide}용 옵션
* @category Plugin
* @since 4.0.0
*/
export interface AutoHideOptions {
/**
* Initial delay before the control bar hides (ms)
* @ko 컨트롤바가 처음으로 표시되고 사라지기까지 걸리는 시간 (ms)
* @default 3000
* @since 4.0.0
*/
initialDelay: number;
/**
* Delay time before hiding the control bar after mouse leave (ms)
* @ko 마우스가 컨트롤바 영역을 떠난 뒤 컨트롤바가 사라지기까지 걸리는 시간 (ms)
* @default 0
* @since 4.0.0
*/
delay: number;
/**
* Delay time before hiding the control bar becomes active, like touch on mobile device or mouse move in fullscreen mode (ms)
* @ko 모바일이나 풀스크린 환경 등에서 사용자 입력이 없을 때 컨트롤바가 사라지기까지 걸리는 시간 (ms)
* @default 3000
* @since 4.0.0
*/
idleDelay: number;
}
class AutoHide {
private _initialDelay: AutoHideOptions["initialDelay"];
private _delay: AutoHideOptions["delay"];
private _idleDelay: AutoHideOptions["idleDelay"];
private _controlBar: ControlBar;
private _timer: number;
private _isGrabbing: boolean;
private _isCursorInside: boolean;
private _isFullscreen: boolean;
private _targetEl: HTMLElement | null;
private _video: TextureVideo | null;
public get enabled() { return !!this._targetEl; }
public get hidden() { return this._controlBar.containerEl.classList.contains(this._hiddenClass); }
private get _hiddenClass() { return this._controlBar.className.HIDDEN; }
private get _fixedClass() { return this._controlBar.className.FIXED; }
public constructor(controlBar: ControlBar, {
initialDelay = 3000,
delay = 0,
idleDelay: activationDelay = 3000
}: Partial<AutoHideOptions>) {
this._controlBar = controlBar;
this._initialDelay = initialDelay;
this._delay = delay;
this._idleDelay = activationDelay;
this._timer = -1;
this._isCursorInside = false;
this._isGrabbing = false;
this._isFullscreen = false;
this._video = null;
this._targetEl = null;
}
public enable(viewer: View360) {
if (this._targetEl) {
this.disable(viewer);
}
const initialDelay = this._initialDelay;
const root = viewer.rootEl;
this._targetEl = viewer.rootEl;
this._timer = window.setTimeout(() => {
this.hide();
}, initialDelay);
root.addEventListener(BROWSER.EVENTS.MOUSE_DOWN, this._onHold);
root.addEventListener(BROWSER.EVENTS.MOUSE_ENTER, this._onMouseEnter);
root.addEventListener(BROWSER.EVENTS.MOUSE_MOVE, this._onMouseMove);
root.addEventListener(BROWSER.EVENTS.MOUSE_LEAVE, this._onMouseLeave);
this._addFullscreenHandlers();
const video = viewer.mesh?.getTexture();
if (!video || !video.isVideo()) {
return;
}
if (video.isPaused()) {
this._controlBar.containerEl.classList.add(this._fixedClass);
}
video.source.addEventListener(BROWSER.EVENTS.VIDEO_PLAY, this._onVideoPlay);
video.source.addEventListener(BROWSER.EVENTS.VIDEO_PAUSE, this._onVideoPause);
this._video = video;
}
public disable(viewer: View360) {
if (!this._targetEl) return;
const controlBar = this._controlBar;
const root = viewer.rootEl;
const video = this._video;
root.removeEventListener(BROWSER.EVENTS.MOUSE_DOWN, this._onHold);
window.removeEventListener(BROWSER.EVENTS.MOUSE_UP, this._onRelease);
root.removeEventListener(BROWSER.EVENTS.MOUSE_ENTER, this._onMouseEnter);
root.removeEventListener(BROWSER.EVENTS.MOUSE_MOVE, this._onMouseMove);
root.removeEventListener(BROWSER.EVENTS.MOUSE_LEAVE, this._onMouseLeave);
this._removeFullscreenHandlers();
window.clearTimeout(this._timer);
controlBar.containerEl.classList.remove(this._fixedClass);
if (video) {
video.source.removeEventListener(BROWSER.EVENTS.VIDEO_PLAY, this._onVideoPlay);
video.source.removeEventListener(BROWSER.EVENTS.VIDEO_PAUSE, this._onVideoPause);
}
this._isCursorInside = false;
this._isGrabbing = false;
this._video = null;
this._targetEl = null;
}
public show() {
this._clearHideTimer();
this._controlBar.containerEl.classList.remove(this._hiddenClass);
}
public showTemporaliy() {
this.show();
this._hideAfterDelay(this._idleDelay);
}
public hide() {
this._clearHideTimer();
this._controlBar.containerEl.classList.add(this._hiddenClass);
}
private _clearHideTimer() {
if (this._timer) {
window.clearTimeout(this._timer);
this._timer = -1;
}
}
private _hideAfterDelay(delay = this._delay) {
if (this._isGrabbing || (!this._isFullscreen && this._isCursorInside)) return;
this._clearHideTimer();
if (delay <= 0) {
this.hide();
} else {
this._timer = window.setTimeout(() => {
this.hide();
}, delay);
}
}
private _onMouseEnter = () => {
this._isCursorInside = true;
this.show();
};
private _onMouseLeave = () => {
this._isCursorInside = false;
this._hideAfterDelay();
};
private _onMouseMove = () => {
if (!this._isFullscreen) return;
this.showTemporaliy();
}
private _onHold = (evt: PointerEvent) => {
this._isGrabbing = true;
if (evt.pointerType === "mouse") {
this._isCursorInside = true;
}
window.addEventListener(BROWSER.EVENTS.MOUSE_UP, this._onRelease);
this.show();
};
private _onRelease = () => {
this._isGrabbing = false;
window.removeEventListener(BROWSER.EVENTS.MOUSE_UP, this._onRelease);
this._hideAfterDelay();
};
private _onVideoPlay = () => {
const root = this._targetEl;
if (!root) return;
this._controlBar.containerEl.classList.remove(this._fixedClass);
};
private _onVideoPause = () => {
const root = this._targetEl;
if (!root) return;
this._controlBar.containerEl.classList.add(this._fixedClass);
};
private _addFullscreenHandlers() {
FULLSCREEN_CHANGE.forEach(evtName => {
document.addEventListener(evtName, this._onFullscreenChange);
});
}
private _removeFullscreenHandlers() {
FULLSCREEN_CHANGE.forEach(evtName => {
document.removeEventListener(evtName, this._onFullscreenChange);
});
}
private _onFullscreenChange = () => {
this._isFullscreen = isFullscreen();
if (this._isFullscreen) {
this._hideAfterDelay();
}
};
}
export default AutoHide;