electron-multi-monitor
Version:
Create multi monitor applications using web development
82 lines (81 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiMonitor = void 0;
const electron_1 = require("electron");
const MultiMonitorFactory_1 = require("./MultiMonitorFactory");
class MultiMonitor {
constructor(monitorFactory) {
this.monitorFactory = monitorFactory;
this.onNewWindow = (event, url, frameName, disposition, options, additionalFeatures, referrer) => {
console.debug(`on mainMonitor 'new-window' - url: ${url} - frameName: ${frameName}`);
if (frameName.substr(0, 8) !== "MM-other") {
console.debug(`we're not interested in frame ${frameName} since it doesn't start with 'MM-other'`);
return;
}
const { monitorFactory, _mainMonitor } = this;
monitorFactory.updateOptions(options);
// @ts-ignore TODO: check to solve this typing issue
options.webContents.once('dom-ready', (event) => {
// @ts-ignore TODO: check to solve this typing issue
const browserWindow = electron_1.BrowserWindow.fromWebContents(event.sender);
if (!browserWindow) {
console.warn(`Couldn't create a BrowserWindow from the event.sender`);
return;
}
this.addOtherMonitor(browserWindow);
// browserWindow.webContents.openDevTools();
//TODO set bounds etc.
browserWindow.setSize(1280, 1024);
browserWindow.setPosition(1280, 0);
});
let domLoaded = false;
// @ts-ignore TODO: check to solve this typing issue
options.webContents.on('dom-ready', (event) => {
domLoaded = true;
});
// @ts-ignore TODO: check to solve this typing issue
options.webContents.on('will-navigate', (e, url) => {
if (!_mainMonitor)
return;
if (_mainMonitor.webContents.getURL() === url && !domLoaded)
return; //only pass the same url to the mainMonitor when the dom was already loaded
console.info('Other window received url navigation which it will send to the main window:', url);
e.preventDefault();
domLoaded = false;
_mainMonitor.loadURL(url);
});
};
this._otherMonitors = [];
this._mainMonitor = null;
}
get monitors() {
if (!this._mainMonitor)
return this._otherMonitors;
return [this._mainMonitor, ...this._otherMonitors];
}
async openUrl(url, numberOfMonitors) {
if (numberOfMonitors < 1)
throw new Error("numberOfMonitors should be at least 1");
this.destroyAllMonitors();
const { monitorFactory } = this;
this._mainMonitor = monitorFactory.createMain(numberOfMonitors - 1);
this.monitors.push(this._mainMonitor);
// this._mainMonitor.webContents.openDevTools();
//TODO: refactor to use webContents.setWindowOpenHandler
this._mainMonitor.webContents.on('new-window', this.onNewWindow);
await this._mainMonitor.loadURL(url);
}
destroyAllMonitors() {
const { monitors } = this;
for (const monitor of monitors) {
monitor.destroy();
}
this._mainMonitor = null;
this._otherMonitors = [];
}
addOtherMonitor(monitor) {
this._otherMonitors.push(monitor);
}
}
exports.MultiMonitor = MultiMonitor;
MultiMonitor.instance = new MultiMonitorFactory_1.MultiMonitorFactory().create();