fox-desktop/electron/hud.js

74 lines
1.6 KiB
JavaScript

const { BrowserWindow, screen } = require('electron');
const path = require('path');
const HUD_URL = process.env.FOX_HUD_URL || 'https://fox.pxo.at/hud';
let hudWindow = null;
let hudLoaded = false;
let pendingShow = false;
function createHud() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
hudWindow = new BrowserWindow({
width,
height,
x: 0,
y: 0,
frame: false,
transparent: true,
alwaysOnTop: true,
show: false,
skipTaskbar: true,
hasShadow: false,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
webSecurity: false,
allowRunningInsecureContent: true,
},
});
hudWindow.setAlwaysOnTop(true, 'screen-saver');
hudWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
hudWindow.webContents.once('did-finish-load', () => {
hudLoaded = true;
if (pendingShow) {
pendingShow = false;
hudWindow.webContents.send('hud-show');
}
});
hudWindow.loadURL(HUD_URL);
hudWindow.on('closed', () => {
hudWindow = null;
hudLoaded = false;
pendingShow = false;
});
return hudWindow;
}
function showHud() {
if (!hudWindow) createHud();
hudWindow.show();
hudWindow.focus();
if (hudLoaded) {
hudWindow.webContents.send('hud-show');
} else {
pendingShow = true;
}
}
function hideHud() {
if (!hudWindow) return;
hudWindow.webContents.send('hud-hide');
setTimeout(() => {
if (hudWindow) hudWindow.hide();
}, 600);
}
module.exports = { createHud, showHud, hideHud };