Files
JiboSDK/node_modules/jibo-tools/electron/main.js
2026-03-22 03:21:45 +02:00

227 lines
6.9 KiB
JavaScript

var app = require('app'),
globalShortcut = require('global-shortcut'),
BrowserWindow = require('browser-window'),
Menu = require('menu'),
mainWindow = null,
path = require('path'),
ipc = require('ipc'),
program = require('commander'),
settings = require('../lib/simulator/settings'),
devTools = require('../lib/simulator/dev-tools');
program
.option('-p, --path <path>', 'The path to the skill')
.option('-r, --remote <address>', 'Run this as a remote simulation')
.option('-t, --token <token>', 'Run this as a remote simulation')
.option('-f, --frameless', 'Run with frameless window (no menu bar)')
.parse(process.argv);
var inRemoteMode = typeof program.remote !== "undefined" && program.remote.length > 0;
ipc.on('is-remote-mode', function (event) {
event.returnValue = inRemoteMode.toString();
});
if (!program.path) {
console.error("You must specify a --path");
process.exit(1);
}
// Figure out which short cut style we should do based on the current platform
var toggleDevToolsShortcutStem = 'Alt+Command+';
if (process.platform != 'darwin'){
toggleDevToolsShortcutStem = "Shift+Control+"
}
settings.init();
var windowing = require('../lib/simulator/windowing');
function shutdown(windowThatClosed){
if(mainWindow){
if( mainWindow !== windowThatClosed ){
mainWindow.close();
}
mainWindow = null;
}
windowing.shutdown();
devTools.shutdown();
}
/**
* This is called by the visualizer when running in the simulator. It uses
* this to load in the entire simulator module. This way the simulator module doesn't
* bloat up the size of the jibo module.
*/
ipc.on('get-skill-path', function (event) {
event.returnValue = program.path;
});
var template = [
{
label: 'Electron',
submenu: [
{
label: 'Quit',
accelerator: 'CommandOrControl+Q',
click: function () {
app.quit();
}
}
]
},
{
label: 'Edit',
submenu: [
{label: 'Undo', accelerator: 'Command+Z', selector: 'undo:'},
{label: 'Redo', accelerator: 'Command+Shift+Z', selector: 'redo:'},
{type: 'separator'},
{label: 'Cut', accelerator: 'Command+X', selector: 'cut:'},
{label: 'Copy', accelerator: 'Command+C', selector: 'copy:'},
{label: 'Paste', accelerator: 'Command+V', selector: 'paste:'},
{label: 'Select All', accelerator: 'Command+A', selector: 'selectAll:'},
]
},
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CommandOrControl+R',
click: function () {
if (mainWindow) {
mainWindow.webContents.send('reload-skill');
}
}
},
{
type: 'separator'
}
]
// Defer to the window windowing module to set up it's related menu items
.concat(windowing.getMenuItems())
.concat([
{
label: 'Developer',
submenu: [
{
label: 'Developer Tools',
accelerator: toggleDevToolsShortcutStem + 'I',
click: function () {
if(mainWindow){
settings.update({
isDevToolsOpened: !settings.get('isDevToolsOpened')
});
}
}
}
]
}
])
}
];
app.on('window-all-closed', function () {
if (process.platform != 'darwin'){
app.quit();
}
});
function loadSkill() {
var registryHost = inRemoteMode ? program.remote : undefined;
//
if(!inRemoteMode){
// The simulator window tell us where the registry end point is
ipc.on('registry-init', function (event, _registryHost) {
registryHost = _registryHost;
});
}
// Set the window size taking into consideration retina display type
// monitors so that our simulation window is pixel perfect
var screen = require('screen'), display;
// Try to guess which screen the window will end up so that we can
// factor in the display's scaleFactor
try{
display = screen.getDisplayMatching({
x: settings.get('windowX'),
y: settings.get('windowY'),
width: settings.get('contentWidth'),
height: settings.get('contentHeight')
});
} catch(error){
display = screen.getDisplayMatching({
x: 100, //settings.get('windowX'),
y: 100, //settings.get('windowY'),
width: 1000, //settings.get('contentWidth'),
height: 1000 // settings.get('contentHeight')
});
};
// Force 2d mode if running remote mode
if(inRemoteMode){
settings.update({
viewMode: '2d'
});
}
var options = {
//"zoom-factor": windowing.getInitialZoomFactor()
};
if( settings.get('fullscreen') ){
options.fullscreen = true;
} else {
// This ensures the width/height of the body are actually what we set
// and unaffected by os specific window framing elements
options["use-content-size"] = true;
options.x = settings.get('windowX');
options.y = settings.get('windowY');
options.width = settings.get('contentWidth') / display.scaleFactor;
options.height = settings.get('contentHeight') / display.scaleFactor;
}
mainWindow = new BrowserWindow(options);
devTools.init(mainWindow);
// Add an unlisted shortcut for opening/closing the dev tools of the visualizer itself
globalShortcut.register(toggleDevToolsShortcutStem + 'J', function(){
if(mainWindow){
mainWindow.webContents.toggleDevTools();
}
});
windowing.setWindow(mainWindow);
settings.setWindow(mainWindow);
ipc.on('get-registry-host', function (event, arg) {
event.sender.send('set-registry-host', {
registryHost: registryHost,
token: program.token
});
});
var indexPath = program.path;
// Make sure the working directory is the root directory of the skill
process.chdir(path.dirname(indexPath));
// Load the visualizer web app which will itself load the skill's index.html
mainWindow.loadUrl('file://' + path.resolve(__dirname, "index.html") );
mainWindow.on('closed', function () {
shutdown(mainWindow);
});
}
app.on('ready', function () {
// Set the context menu
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
loadSkill();
});