mcopy/app/lib/display/index.js

198 lines
5.9 KiB
JavaScript
Raw Normal View History

'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module display
* Provides features for displaying a full screen display of images for the digital module.
**/
2019-06-15 15:06:57 +00:00
const path_1 = require("path");
const url_1 = require("url");
const delay_1 = require("delay");
const log_1 = require("log");
const { BrowserWindow } = require('electron');
class WebView {
constructor(platform, display) {
this.opened = false;
this.showing = false;
this.loadWait = {};
const prefs = {
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: false,
enableRemoteModule: true,
contextIsolation: false
},
width: 800,
height: 600,
minWidth: 800,
minHeight: 600 //,
//icon: path.join(__dirname, '../../assets/icons/icon.png')
};
2024-04-06 23:09:50 +00:00
const pagePath = (0, path_1.normalize)((0, path_1.join)(__dirname, '../../display.html'));
const pageUrl = (0, url_1.format)({
pathname: pagePath,
protocol: 'file:'
});
this.init();
if (!display.primary) {
prefs.x = display.x + 50;
prefs.y = display.y + 50;
}
this.digitalWindow = new BrowserWindow(prefs);
2023-02-25 17:33:18 +00:00
require('@electron/remote/main').enable(this.digitalWindow.webContents);
this.digitalWindow.loadURL(pageUrl);
if (process.argv.indexOf('-d') !== -1 || process.argv.indexOf('--dev') !== -1) {
this.digitalWindow.webContents.openDevTools();
}
this.digitalWindow.on('closed', () => {
this.digitalWindow = null;
this.close();
});
//this.digitalWindow.hide();
this.platform = platform;
this.display = display;
this.ipc = require('electron').ipcMain;
this.ipc.on('display_load', this.onLoad.bind(this));
}
async init() {
this.log = await (0, log_1.Log)({ label: 'devices' });
}
async open() {
this.digitalWindow.show();
this.showing = true;
this.opened = true;
await this.digitalWindow.setFullScreen(true);
2024-04-06 23:09:50 +00:00
await (0, delay_1.delay)(300);
if (this.platform === 'osx') {
2024-04-06 23:09:50 +00:00
await (0, delay_1.delay)(300); //give macs an extra 300ms to open fullscreen
}
}
2019-06-15 15:06:57 +00:00
async show(src) {
2024-04-06 23:09:50 +00:00
const normalSrc = (0, path_1.normalize)((0, path_1.join)(src));
if (!this.digitalWindow) {
this.log.warn(`Cannot show "${src}" because window does not exist`);
return false;
}
try {
this.digitalWindow.webContents.send('display', { src: normalSrc });
}
catch (err) {
this.log.error(err);
}
2019-06-15 15:06:57 +00:00
this.showing = true;
return new Promise(function (resolve) {
this.loadWait[src] = resolve;
}.bind(this));
}
onLoad(evt, arg) {
if (this.loadWait[arg.src]) {
this.loadWait[arg.src]();
delete this.loadWait[arg.src];
}
}
async focus() {
if (!this.digitalWindow) {
this.log.warn(`Cannot show focus screen because window does not exist`);
return false;
}
2024-04-06 23:09:50 +00:00
await (0, delay_1.delay)(500);
try {
this.digitalWindow.webContents.send('focus', { focus: true });
}
catch (err) {
this.log.error(err);
}
}
async field(ratio) {
if (!this.digitalWindow) {
this.log.warn(`Cannot show field guide because window does not exist`);
return false;
}
2024-04-06 23:09:50 +00:00
await (0, delay_1.delay)(500);
try {
this.digitalWindow.webContents.send('field', { field: true, ratio });
}
catch (err) {
this.log.error(err);
}
}
async meter() {
if (!this.digitalWindow) {
this.log.warn(`Cannot show meter screen because window does not exist`);
return false;
}
2024-04-06 23:09:50 +00:00
await (0, delay_1.delay)(500);
try {
this.digitalWindow.webContents.send('meter', { meter: true });
}
catch (err) {
this.log.error(err);
}
}
hide() {
if (this.digitalWindow) {
this.digitalWindow.hide();
}
this.showing = false;
return true;
}
close() {
2019-06-15 15:06:57 +00:00
this.hide();
if (this.digitalWindow) {
this.digitalWindow.close();
this.digitalWindow = null;
}
this.opened = false;
this.showing = false;
return true;
}
}
2019-06-15 15:06:57 +00:00
class Display {
constructor(sys) {
this.platform = sys.platform;
this.displays = sys.displays;
2024-04-06 23:09:50 +00:00
this.tmpdir = (0, path_1.join)(sys.tmp, 'mcopy_digital');
this.display = this.displays.find((display) => {
if (display.primary)
return true;
});
2019-06-15 15:06:57 +00:00
}
async open() {
if (this.wv && this.wv.display && this.wv.display.id !== this.display.id) {
this.wv.close();
}
if (!this.wv || !this.wv.opened) {
this.wv = new WebView(this.platform, this.display);
await this.wv.open();
}
}
async show(src) {
await this.wv.show(src);
}
async showPath(pathStr) {
return await this.wv.show(pathStr);
}
2019-06-15 15:06:57 +00:00
hide() {
}
async close() {
return await this.wv.close();
}
async focus() {
return await this.wv.focus();
}
async field(ratio) {
return await this.wv.field(ratio);
}
async meter() {
return await this.wv.meter();
}
change(id) {
this.display = this.displays.find((display) => {
if (display.id == id)
return true;
});
}
}
module.exports = function (sys) {
2019-06-15 15:06:57 +00:00
return new Display(sys);
};
//# sourceMappingURL=index.js.map