mcopy/src/display/index.ts

222 lines
4.6 KiB
TypeScript
Raw Normal View History

'use strict';
2019-06-14 15:29:52 +00:00
/**
* @module display
* Provides features for displaying a full screen display of images for the digital module.
**/
import spawn = require('spawn');
2019-06-15 15:06:57 +00:00
import { join as pathJoin } from 'path';
import { delay } from 'delay';
import { exec } from 'exec';
const { BrowserWindow } = require('electron');
2019-06-15 15:06:57 +00:00
function padded_frame (i : number) {
let len = (i + '').length;
let str = i + '';
for (let x = 0; x < 5 - len; x++) {
str = '0' + str;
}
return str;
}
class WebView {
private digitalWindow : any;
public opened : boolean = false;
public showing : boolean = false;
constructor() {
this.digitalWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: false
},
width: 800,
height: 600,
minWidth : 800,
minHeight : 600//,
//icon: path.join(__dirname, '../../assets/icons/icon.png')
});
this.digitalWindow.loadURL('file://' + __dirname + '../../../display.html');
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();
}
async open () {
this.digitalWindow.show();
this.showing = true;
this.opened = true;
await this.digitalWindow.setFullScreen(true);
2019-06-15 15:06:57 +00:00
await delay(300);
}
2019-06-15 15:06:57 +00:00
async show (src : string) {
if (!this.digitalWindow) {
console.warn(`Cannot show "${src}" because window does not exist`);
return false;
}
try {
this.digitalWindow.webContents.send('display', { src });
} catch (err) {
console.error(err);
}
2019-06-15 15:06:57 +00:00
this.showing = true;
await delay(100);
return true;
}
async focus () {
if (!this.digitalWindow) {
console.warn(`Cannot show focus screen because window does not exist`);
return false;
}
try {
this.digitalWindow.webContents.send('focus', { focus : true });
} catch (err) {
console.error(err);
}
}
async field () {
if (!this.digitalWindow) {
console.warn(`Cannot show field guide because window does not exist`);
return false;
}
//aspect ratio
try {
this.digitalWindow.webContents.send('field', { field : true });
} catch (err) {
console.error(err);
}
}
async meter () {
if (!this.digitalWindow) {
console.warn(`Cannot show meter screen because window does not exist`);
return false;
}
try {
this.digitalWindow.webContents.send('meter', { meter : true });
} catch (err) {
console.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 EOG {
private cp : any;
constructor () {
2019-06-15 15:06:57 +00:00
}
2019-06-15 15:06:57 +00:00
public open () {
this.hide();
}
2019-06-15 15:06:57 +00:00
public async show (src : string) {
//timeout 3 eog --fullscreen ${src}
this.cp = spawn('eog', ['--fullscreen', src]);
await delay(200)
return true
}
2019-06-15 15:06:57 +00:00
public hide () {
if (this.cp) {
this.cp.kill();
this.cp = null;
}
}
2019-06-15 15:06:57 +00:00
public close () {
this.hide();
}
}
class Display {
private platform : string;
private tmpdir : string;
private wv : WebView;
private eog : EOG;
constructor (sys : any) {
this.platform = sys.platform;
this.tmpdir = pathJoin(sys.tmp, 'mcopy_digital');
}
public async open () {
//if (this.platform !== 'nix') {
2019-06-15 15:06:57 +00:00
if (!this.wv || !this.wv.opened) {
this.wv = new WebView();
await this.wv.open()
}
//} else {
// if (!this.eog) {
// this.eog = new EOG()
// }
//}
2019-06-15 15:06:57 +00:00
}
public async show (frame : number) {
let padded : string = padded_frame(frame);
let ext : string = 'tif';
let tmppath : string;
//if (this.platform !== 'nix') {
2019-06-15 15:06:57 +00:00
ext = 'png';
//}
2019-06-15 15:06:57 +00:00
tmppath = pathJoin(this.tmpdir, `export-${padded}.${ext}`);
//if (this.platform !== 'nix') {
2019-06-15 15:06:57 +00:00
await this.wv.show(tmppath);
//} else {
//await this.eog.show(tmppath);
//}
}
public async showPath (pathStr : string) {
return await this.wv.show(pathStr);
}
2019-06-15 15:06:57 +00:00
public hide () {
//if (this.platform !== 'nix') {
2019-06-15 15:06:57 +00:00
//don't hide between frames
//this.wv.hide();
//} else {
//this.eog.hide();
//}
2019-06-15 15:06:57 +00:00
}
public close () {
//if (this.platform !== 'nix') {
2019-06-15 15:06:57 +00:00
this.wv.close()
//} else {
//this.eog.close()
//}
}
public async focus () {
return await this.wv.focus();
}
public async field () {
return await this.wv.field();
}
public async meter () {
return await this.wv.meter();
}
}
module.exports = function (sys : any) {
2019-06-15 15:06:57 +00:00
return new Display(sys)
}