mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-06-07 08:07:18 +02:00
Create MouseHoldEvent
This commit is contained in:
parent
b91474c20b
commit
ed2fb13e17
@ -1706,6 +1706,63 @@ function checkForUpdate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class MouseHoldEvent {
|
||||||
|
#isHolding = false;
|
||||||
|
#timeout;
|
||||||
|
|
||||||
|
#$elm;
|
||||||
|
#callback;
|
||||||
|
#duration;
|
||||||
|
|
||||||
|
#onMouseDown = function(e) {
|
||||||
|
const _this = this;
|
||||||
|
this.#isHolding = false;
|
||||||
|
|
||||||
|
this.#timeout && clearTimeout(this.#timeout);
|
||||||
|
this.#timeout = setTimeout(() => {
|
||||||
|
_this.#isHolding = true;
|
||||||
|
_this.#callback();
|
||||||
|
}, this.#duration);
|
||||||
|
};
|
||||||
|
|
||||||
|
#onMouseUp = e => {
|
||||||
|
this.#timeout && clearTimeout(this.#timeout);
|
||||||
|
this.#timeout = null;
|
||||||
|
|
||||||
|
if (this.#isHolding) {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
}
|
||||||
|
this.#isHolding = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
#addEventListeners = () => {
|
||||||
|
this.#$elm.addEventListener('mousedown', this.#onMouseDown.bind(this));
|
||||||
|
this.#$elm.addEventListener('click', this.#onMouseUp.bind(this));
|
||||||
|
|
||||||
|
this.#$elm.addEventListener('touchstart', this.#onMouseDown.bind(this));
|
||||||
|
this.#$elm.addEventListener('touchend', this.#onMouseUp.bind(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
#clearEventLiseners = () => {
|
||||||
|
this.#$elm.removeEventListener('mousedown', this.#onMouseDown);
|
||||||
|
this.#$elm.removeEventListener('click', this.#onMouseUp);
|
||||||
|
|
||||||
|
this.#$elm.removeEventListener('touchstart', this.#onMouseDown);
|
||||||
|
this.#$elm.removeEventListener('touchend', this.#onMouseUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor($elm, callback, duration=1000) {
|
||||||
|
this.#$elm = $elm;
|
||||||
|
this.#callback = callback;
|
||||||
|
this.#duration = duration;
|
||||||
|
|
||||||
|
this.#addEventListeners();
|
||||||
|
$elm.clearMouseHoldEventListeners = this.#clearEventLiseners;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function addCss() {
|
function addCss() {
|
||||||
let css = `
|
let css = `
|
||||||
.better-xcloud-settings-button {
|
.better-xcloud-settings-button {
|
||||||
@ -3184,31 +3241,10 @@ function injectStreamMenuButtons() {
|
|||||||
|
|
||||||
// Get "Quit game" button
|
// Get "Quit game" button
|
||||||
const $btnQuit = $orgButton.parentElement.querySelector('button:last-of-type');
|
const $btnQuit = $orgButton.parentElement.querySelector('button:last-of-type');
|
||||||
|
// Hold "Quit game" button to refresh the stream
|
||||||
let isHolding = false;
|
new MouseHoldEvent($btnQuit, () => {
|
||||||
let holdTimeout;
|
|
||||||
const onMouseDown = e => {
|
|
||||||
isHolding = false;
|
|
||||||
holdTimeout = setTimeout(() => {
|
|
||||||
isHolding = true;
|
|
||||||
confirm('Do you want to refresh the stream?') && window.location.reload();
|
confirm('Do you want to refresh the stream?') && window.location.reload();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
};
|
|
||||||
const onMouseUp = e => {
|
|
||||||
holdTimeout && clearTimeout(holdTimeout);
|
|
||||||
|
|
||||||
if (isHolding) {
|
|
||||||
e.preventDefault();
|
|
||||||
e.stopPropagation();
|
|
||||||
}
|
|
||||||
isHolding = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
$btnQuit.addEventListener('mousedown', onMouseDown);
|
|
||||||
$btnQuit.addEventListener('click', onMouseUp);
|
|
||||||
|
|
||||||
$btnQuit.addEventListener('touchstart', onMouseDown);
|
|
||||||
$btnQuit.addEventListener('touchend', onMouseUp);
|
|
||||||
|
|
||||||
// Render stream badges
|
// Render stream badges
|
||||||
const $menu = document.querySelector('div[class*=StreamMenu-module__menuContainer] > div[class*=Menu-module]');
|
const $menu = document.querySelector('div[class*=StreamMenu-module__menuContainer] > div[class*=Menu-module]');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user