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

144 lines
3.8 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;
this.lastProbeResponseTs = null;
this.uiURI = browser.runtime.getURL('/csui/csui.html');
this.extensionBase = browser.runtime.getURL('').replace(/\/$/, "");
2020-12-03 01:35:22 +01:00
2022-03-20 20:40:11 +01:00
console.log('init init');
2020-12-03 01:35:22 +01:00
this.init();
}
async 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;
// in onMouseMove, we currently can't access this because we didn't
// do things the most properly
const uiURI = this.uiURI;
const iframe = document.createElement('iframe');
iframe.setAttribute('src', uiURI);
iframe.style.width = "100%";
iframe.style.height = "100%";
iframe.style.position = "absolute";
iframe.style.zIndex = "1000";
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() {
function onMouseMove(event) {
let coords;
if (event.currentTarget === document) {
coords = {
x: event.pageX - iframe.offsetLeft,
y: event.pageY - iframe.offsetTop
2022-03-20 20:40:11 +01:00
}
} else {
coords = {
x: event.clientX,
y: event.clientY
2022-03-20 20:40:11 +01:00
}
}
console.warn('coords:', coords, 'uiURI:', 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
);
// iframe.style.pointerEvents = isClickable ? 'auto' : 'none';
2022-03-20 20:40:11 +01:00
}
document.addEventListener('mousemove', onMouseMove, true);
2020-12-04 02:02:25 +01:00
}
rootDiv.appendChild(iframe);
console.log('ui created')
// subscribe to events coming back to us
window.addEventListener('message', (event) => this.handleMessage(event));
// set uiIframe for handleMessage
this.uiIframe = iframe;
2022-03-20 20:40:11 +01:00
}
/**
* Handles events received from the iframe.
* @param {*} event
*/
handleMessage(event) {
console.log('[main] received event:', event.origin, this.uiURI, this.extensionBase, event)
if (event.origin === this.extensionBase) {
console.log('this is the event we want', this.uiIframe );
if (event.data.cmd === 'uwui-clickable') {
if (event.data.ts < this.lastProbeResponseTs) {
console.log('event too early')
return;
}
this.lastProbeResponseTs = event.data.ts;
console.log('---- event is clickable?', event.data.clickable)
this.uiIframe.style.pointerEvents = event.data.clickable ? 'auto' : 'none';
}
}
}
/**
* 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;