ultrawidify/src/ext/lib/uwui/UI.js

147 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-12-03 01:35:22 +01:00
if (process.env.CHANNEL !== 'stable'){
console.info("Loading: UI");
}
class UI {
constructor(
interfaceId,
uiConfig, // {component, parentElement?}
) {
this.interfaceId = interfaceId;
2020-12-03 01:35:22 +01:00
this.uiConfig = uiConfig;
2022-03-20 20:40:11 +01:00
console.log('init init');
2020-12-03 01:35:22 +01:00
this.init();
}
async init() {
2022-03-20 20:40:11 +01:00
try {
console.log('—————————————— init');
const random = Math.round(Math.random() * 69420);
const uwid = `uw-ultrawidify-${this.interfaceId}-root-${random}`
const rootDiv = document.createElement('div');
if (this.uiConfig.additionalStyle) {
rootDiv.setAttribute('style', this.uiConfig.additionalStyle);
}
2020-12-05 03:30:43 +01:00
rootDiv.setAttribute('id', uwid);
rootDiv.classList.add('uw-ultrawidify-container-root');
2020-12-04 02:02:25 +01:00
if (this.uiConfig?.parentElement) {
this.uiConfig.parentElement.appendChild(rootDiv);
} else {
document.body.appendChild(rootDiv);
}
this.element = rootDiv;
try {
2022-03-20 20:40:11 +01:00
const uiURI = browser.runtime.getURL('/csui/csui.html');
const iframe = document.createElement('iframe');
2022-03-20 20:40:11 +01:00
iframe.setAttribute('src', uiURI);
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.position = "absolute";
iframe.style.zIndex = "1000";
2022-03-20 20:40:11 +01:00
iframe.style.border = 0;
iframe.style.pointerEvents = 'none';
// so we have a problem: we want iframe to be clickthrough everywhere except
// on our actual overlay. There's no nice way of doing that, so we need some
// extra javascript to deal with this
iframe.onload = function() {
console.log('[iframe-onload]: we are doing the onload!')
const iframeDocument = iframe.contentDocument;
// console.log('[iframe-onload] iframe:', iframe, 'document:', iframeDocument);
function onMouseMove(event) {
const iframeDocument = iframe.contentDocument;
let coords;
if (event.currentTarget === document) {
coords = {
x: event.pageX - iframe.offsetLeft,
y: event.pageY - iframe.offsetTop
}
} else {
coords = {
x: event.clientX,
y: event.clientY
}
}
console.log('[iframe-mm] mouse coords:', coords.x, coords.y, 'ui URI:', uiURI)
// ask the iframe to check whether there's a clickable element
iframe.contentWindow.postMessage(
{
cmd: 'uwui-probe',
coords,
ts: +new Date() // this should be accurate enough for our purposes
},
uiURI
);
// gentleman's agreement: elements with uw-clickable inside the iframe will
// toggle pointerEvents on the iframe from 'none' to 'auto'
// Children of uw-clickable events should also do that.
let isClickable = false;
let element = iframeDocument.elementFromPoint(coords.x, coords.y);
while (element) {
if (element?.classList.includes('uw-clickable')) {
// we could set 'pointerEvents' here and now & simply use return, but that
// might cause us a problem if we ever try to add more shit to this function
isClickable = true;
break;
}
}
iframe.style.pointerEvents = isClickable ? 'auto' : 'none';
}
document.addEventListener('mousemove', onMouseMove, true);
// iframeDocument.addEventListener('mousemove', onMouseMove, true);
}
rootDiv.appendChild(iframe);
2022-03-20 20:40:11 +01:00
console.log('ui created')
} catch(e) {
2022-03-20 20:40:11 +01:00
console.error('there was an oopsie while creating ui:', e)
2020-12-04 02:02:25 +01:00
}
console.log('——————————————————————————————————————— UI IS BEING CREATED ', rootDiv);
2022-03-20 20:40:11 +01:00
} catch (e) {
console.error('something went VERY wrong while creating ui:', e)
}
}
/**
* Replaces ui config and re-inits the UI
2021-10-22 00:30:36 +02:00
* @param {*} newUiConfig
*/
replace(newUiConfig) {
this.element?.remove();
this.uiConfig = newUiConfig;
this.init();
}
destroy() {
2020-12-04 02:02:25 +01:00
// this.comms?.destroy();
this.element?.remove();
}
}
2020-12-03 01:35:22 +01:00
if (process.env.CHANNEL !== 'stable'){
console.info("UI.js loaded");
}
2020-12-03 01:16:57 +01:00
export default UI;