auto-hide-controller.js [plain text]
const AutoHideDelayMS = 4000;
class AutoHideController
{
constructor(mediaControls)
{
this._mediaControls = mediaControls;
if (GestureRecognizer.SupportsTouches) {
this._touchIdentifiersPreventingAutoHide = new Set;
this._tapGestureRecognizer = new TapGestureRecognizer(this._mediaControls.element, this);
}
this.autoHideDelay = AutoHideDelayMS;
}
get fadesWhileIdle()
{
return this._fadesWhileIdle;
}
set fadesWhileIdle(flag)
{
if (this._fadesWhileIdle == flag)
return;
this._fadesWhileIdle = flag;
if (GestureRecognizer.SupportsTouches) {
if (flag) {
this._mediaControls.element.addEventListener("touchstart", this);
this._mediaControls.element.addEventListener("touchend", this);
} else {
this._mediaControls.element.removeEventListener("touchstart", this);
this._mediaControls.element.removeEventListener("touchend", this);
}
this._tapGestureRecognizer.enabled = flag;
} else {
if (flag) {
this._mediaControls.element.addEventListener("mousemove", this);
this._mediaControls.element.addEventListener("mouseleave", this);
} else {
this._mediaControls.element.removeEventListener("mousemove", this);
this._mediaControls.element.removeEventListener("mouseleave", this);
}
}
if (flag && !this._mediaControls.faded)
this._resetAutoHideTimer(false);
else if (!flag)
this._mediaControls.faded = false;
}
handleEvent(event)
{
if (event.currentTarget !== this._mediaControls.element)
return;
if (event.type === "mousemove") {
this._mediaControls.faded = false;
this._resetAutoHideTimer(true);
if (this._mediaControls.isPointInControls(new DOMPoint(event.clientX, event.clientY))) {
this._disableAutoHiding = true;
this._cancelNonEnforcedAutoHideTimer();
} else {
delete this._disableAutoHiding;
this._resetAutoHideTimer(true);
}
} else if (event.type === "mouseleave" && this._fadesWhileIdle && !this.hasSecondaryUIAttached && !this._enforceAutoHideTimer)
this._mediaControls.faded = !this._mediaControls.isPointInControls(new DOMPoint(event.clientX, event.clientY));
if (event.type === "touchstart") {
this._nextTapCanFadeControls = !this._mediaControls.faded;
for (var touch in event.changedTouches)
this._touchIdentifiersPreventingAutoHide.add(touch.identifier);
this._mediaControls.faded = false;
this._disableAutoHiding = true;
this._cancelNonEnforcedAutoHideTimer();
} else if (event.type === "touchend") {
for (var touch in event.changedTouches)
this._touchIdentifiersPreventingAutoHide.delete(touch.identifier);
if (!this._touchIdentifiersPreventingAutoHide.size) {
delete this._disableAutoHiding;
this._resetAutoHideTimer(true);
}
}
}
gestureRecognizerStateDidChange(recognizer)
{
if (this._tapGestureRecognizer !== recognizer || recognizer.state !== GestureRecognizer.States.Recognized)
return;
this._mediaControls.faded = this._nextTapCanFadeControls && !this._mediaControls.isPointInControls(recognizer.locationInClient());
delete this._nextTapCanFadeControls;
}
mediaControlsFadedStateDidChange()
{
if (this._mediaControls.faded)
delete this._enforceAutoHideTimer;
else
this._resetAutoHideTimer(true);
}
mediaControlsBecameInvisible()
{
this._cancelNonEnforcedAutoHideTimer();
}
_cancelNonEnforcedAutoHideTimer()
{
if (!this._enforceAutoHideTimer)
this._cancelAutoHideTimer();
}
_cancelAutoHideTimer()
{
window.clearTimeout(this._autoHideTimer);
delete this._autoHideTimer;
}
_resetAutoHideTimer(cancelable)
{
if (cancelable && this._enforceAutoHideTimer)
return;
this._cancelAutoHideTimer();
if (cancelable)
delete this._enforceAutoHideTimer;
else
this._enforceAutoHideTimer = true;
this._autoHideTimer = window.setTimeout(this._autoHideTimerFired.bind(this), this.autoHideDelay);
}
_autoHideTimerFired()
{
delete this._enforceAutoHideTimer;
if (this._disableAutoHiding)
return;
this._cancelAutoHideTimer();
this._mediaControls.faded = this._fadesWhileIdle && !this.hasSecondaryUIAttached;
}
}