2020-08-02 04:06:18 +00:00
|
|
|
const { app, dialog, ipcMain, ipcRenderer, Menu, BrowserWindow } = require('electron')
|
2020-07-30 19:43:55 +00:00
|
|
|
const modpath = require('path')
|
|
|
|
const fs = require('fs')
|
|
|
|
const {URLSearchParams} = require('url')
|
|
|
|
const isMac = process.platform === 'darwin'
|
|
|
|
const chokidar = require('chokidar')
|
2020-08-02 04:06:18 +00:00
|
|
|
const Store = require('electron-store');
|
|
|
|
const store = new Store();
|
|
|
|
const KEY_lastWorkspaceFilePath = "lastWorkspaceFilePath";
|
2020-07-30 19:43:55 +00:00
|
|
|
|
|
|
|
// file watcher
|
2020-08-02 04:06:18 +00:00
|
|
|
// TODO: add workspace metadata for platform, ROM output, README, etc.
|
2020-07-30 19:43:55 +00:00
|
|
|
class Workspace {
|
|
|
|
constructor(directory, mainfile, wnd) {
|
|
|
|
this.directory = directory;
|
|
|
|
this.mainfile = mainfile;
|
2020-08-02 04:06:18 +00:00
|
|
|
wnd.webContents.send('setWorkspaceRoot', {root:this.directory});
|
2020-07-30 19:43:55 +00:00
|
|
|
this.watcher = chokidar.watch(modpath.join(directory, mainfile));
|
|
|
|
this.watcher.on('all', (event, path) => {
|
|
|
|
console.log(event, path);
|
|
|
|
switch (event) {
|
|
|
|
case 'add':
|
2020-08-02 04:06:18 +00:00
|
|
|
case 'change':
|
|
|
|
wnd.webContents.send('fileChanged', {
|
|
|
|
path: modpath.relative(this.directory, path),
|
2020-07-30 19:43:55 +00:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2020-08-02 04:06:18 +00:00
|
|
|
console.log("workspace opened", this.directory, this.mainfile);
|
|
|
|
}
|
|
|
|
close() {
|
|
|
|
if (this.watcher) {
|
|
|
|
this.watcher.close();
|
|
|
|
this.watcher = null;
|
|
|
|
console.log("workspace closed", this.directory, this.mainfile);
|
|
|
|
}
|
2020-07-30 19:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 01:22:20 +00:00
|
|
|
function openFile(path, wnd) {
|
2020-08-02 04:06:18 +00:00
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
dialog.showMessageBox({
|
|
|
|
type: "error",
|
|
|
|
message: "File not found.",
|
|
|
|
detail: path
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-07-30 19:43:55 +00:00
|
|
|
var dirname = modpath.dirname(path);
|
|
|
|
var filename = modpath.basename(path);
|
2020-08-03 01:22:20 +00:00
|
|
|
if (!wnd) wnd = BrowserWindow.getFocusedWindow();
|
2020-07-30 19:43:55 +00:00
|
|
|
var ws = new Workspace(dirname, filename, wnd);
|
2020-08-03 01:22:20 +00:00
|
|
|
if (wnd.workspace) { wnd.workspace.close(); }
|
2020-07-30 19:43:55 +00:00
|
|
|
wnd.workspace = ws;
|
2020-08-02 04:06:18 +00:00
|
|
|
wnd.on('closed', () => {
|
|
|
|
ws.close();
|
|
|
|
});
|
2020-08-03 01:22:20 +00:00
|
|
|
openWorkspace(wnd, ws);
|
|
|
|
app.addRecentDocument(path);
|
|
|
|
store.set(KEY_lastWorkspaceFilePath, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function openWorkspace(wnd, ws) {
|
2020-07-30 19:43:55 +00:00
|
|
|
var qs = new URLSearchParams();
|
2020-08-02 04:06:18 +00:00
|
|
|
qs.set('electron_ws', 1);
|
2020-08-03 01:22:20 +00:00
|
|
|
qs.set('repo', ws.directory);
|
|
|
|
qs.set('file', ws.mainfile);
|
2020-08-02 04:06:18 +00:00
|
|
|
wnd.loadURL(`file://${__dirname}/electron.html?${qs}`).then(() => {
|
2020-08-03 01:22:20 +00:00
|
|
|
wnd.webContents.send('setWorkspaceRoot', {root:ws.directory});
|
2020-08-02 04:06:18 +00:00
|
|
|
});
|
2020-07-30 19:43:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 01:22:20 +00:00
|
|
|
// TODO: doesn't work if browser window reloads itself
|
|
|
|
function reloadCurrentWindow() {
|
|
|
|
var wnd = BrowserWindow.getFocusedWindow();
|
|
|
|
if (wnd.workspace) {
|
|
|
|
openWorkspace(wnd, wnd.workspace);
|
|
|
|
} else {
|
|
|
|
wnd.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 19:43:55 +00:00
|
|
|
function openFileDialog() {
|
|
|
|
dialog.showOpenDialog({
|
|
|
|
title: "Open File",
|
|
|
|
properties: ['openFile','promptToCreate'],
|
|
|
|
}).then((rtn) => {
|
|
|
|
if (!rtn.canceled && rtn.filePaths && rtn.filePaths.length > 0) {
|
|
|
|
var path = rtn.filePaths[0];
|
|
|
|
openFile(path);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-02 04:06:18 +00:00
|
|
|
function openDefaultFile() {
|
2020-08-03 01:22:20 +00:00
|
|
|
var wnd = createWindow();
|
|
|
|
if (process.argv.length >= 3) {
|
|
|
|
openFile(process.argv[2], wnd);
|
|
|
|
}
|
|
|
|
/* TODO
|
2020-08-02 04:06:18 +00:00
|
|
|
var lastfile = store.get(KEY_lastWorkspaceFilePath);
|
|
|
|
if (lastfile != null) {
|
|
|
|
openFile(lastfile);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-30 19:43:55 +00:00
|
|
|
function openWorkspace() {
|
|
|
|
const { dialog } = require('electron')
|
|
|
|
console.log(dialog.showOpenDialog({
|
|
|
|
title: "Open Workspace",
|
|
|
|
properties: ['openDirectory','createDirectory','promptToCreate'],
|
|
|
|
message: "Choose a directory that holds your source files.",
|
|
|
|
}))
|
|
|
|
}
|
2020-08-02 04:06:18 +00:00
|
|
|
*/
|
2020-07-30 19:43:55 +00:00
|
|
|
|
|
|
|
function openURL(url) {
|
|
|
|
return async () => {
|
|
|
|
const { shell } = require('electron')
|
|
|
|
await shell.openExternal(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildMenu() {
|
|
|
|
|
|
|
|
const template = [
|
|
|
|
// { role: 'appMenu' }
|
|
|
|
...(isMac ? [{
|
|
|
|
label: app.name,
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
|
|
|
{ role: 'hideothers' },
|
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'quit' }
|
|
|
|
]
|
|
|
|
}] : []),
|
|
|
|
// { role: 'fileMenu' }
|
|
|
|
{
|
|
|
|
label: 'File',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'Open File...',
|
|
|
|
click: openFileDialog,
|
|
|
|
accelerator: 'CmdOrCtrl+O',
|
|
|
|
},
|
2020-08-02 04:06:18 +00:00
|
|
|
// When a file is requested from the recent documents menu, the open-file event of app module will be emitted for it.
|
|
|
|
{
|
|
|
|
"label":"Open Recent",
|
|
|
|
"role":"recentdocuments",
|
|
|
|
"submenu":[
|
|
|
|
{
|
|
|
|
"label":"Clear Recent",
|
|
|
|
"role":"clearrecentdocuments"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2020-07-30 19:43:55 +00:00
|
|
|
{ type: 'separator' },
|
|
|
|
isMac ? { role: 'close' } : { role: 'quit' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
// { role: 'editMenu' }
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'undo' },
|
|
|
|
{ role: 'redo' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'cut' },
|
|
|
|
{ role: 'copy' },
|
|
|
|
{ role: 'paste' },
|
|
|
|
...(isMac ? [
|
|
|
|
{ role: 'pasteAndMatchStyle' },
|
|
|
|
{ role: 'delete' },
|
|
|
|
{ role: 'selectAll' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{
|
|
|
|
label: 'Speech',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'startspeaking' },
|
|
|
|
{ role: 'stopspeaking' }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
] : [
|
|
|
|
{ role: 'delete' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'selectAll' }
|
|
|
|
])
|
|
|
|
]
|
|
|
|
},
|
|
|
|
// { role: 'viewMenu' }
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [
|
2020-08-03 01:22:20 +00:00
|
|
|
{
|
|
|
|
label: 'Reload Window',
|
|
|
|
click: reloadCurrentWindow
|
|
|
|
},
|
2020-07-30 19:43:55 +00:00
|
|
|
{ role: 'toggledevtools' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'resetzoom' },
|
|
|
|
{ role: 'zoomin' },
|
|
|
|
{ role: 'zoomout' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'togglefullscreen' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
// { role: 'windowMenu' }
|
|
|
|
{
|
|
|
|
label: 'Window',
|
|
|
|
submenu: [
|
|
|
|
{ role: 'minimize' },
|
|
|
|
{ role: 'zoom' },
|
|
|
|
...(isMac ? [
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'front' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'window' }
|
|
|
|
] : [
|
|
|
|
{ role: 'close' }
|
|
|
|
])
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'help',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
label: 'IDE Help',
|
|
|
|
click: openURL('https://8bitworkshop.com/blog/docs/ide.md.html')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Latest News',
|
|
|
|
click: openURL('https://8bitworkshop.com/blog/')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Follow @8bitworkshop on Twitter',
|
|
|
|
click: openURL('https://twitter.com/8bitworkshop')
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Report an Issue',
|
|
|
|
click: openURL('https://github.com/sehugg/8bitworkshop/issues/new')
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWindow () {
|
|
|
|
// Create the browser window.
|
|
|
|
const win = new BrowserWindow({
|
|
|
|
width: 1024,
|
|
|
|
height: 768,
|
|
|
|
backgroundColor: '#fff',
|
|
|
|
webPreferences: {
|
|
|
|
preload: modpath.join(__dirname, './electron-preload.js'),
|
|
|
|
nodeIntegration: false,
|
|
|
|
enableRemoteModule: false,
|
2020-08-02 04:06:18 +00:00
|
|
|
contextIsolation: false,
|
|
|
|
sandbox: false,
|
2020-07-30 19:43:55 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// and load the index.html of the app.
|
2020-08-02 04:06:18 +00:00
|
|
|
win.loadFile('electron.html', {
|
|
|
|
search: 'repo=/'
|
|
|
|
})
|
2020-07-30 19:43:55 +00:00
|
|
|
|
|
|
|
// Open the DevTools.
|
|
|
|
//win.webContents.openDevTools()
|
|
|
|
|
|
|
|
// Maximize
|
|
|
|
win.maximize();
|
|
|
|
return win;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method will be called when Electron has finished
|
|
|
|
// initialization and is ready to create browser windows.
|
|
|
|
// Some APIs can only be used after this event occurs.
|
2020-08-02 04:06:18 +00:00
|
|
|
app.whenReady().then(buildMenu).then(openDefaultFile)
|
2020-07-30 19:43:55 +00:00
|
|
|
|
|
|
|
// Quit when all windows are closed, except on macOS. There, it's common
|
|
|
|
// for applications and their menu bar to stay active until the user quits
|
|
|
|
// explicitly with Cmd + Q.
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
// On macOS it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-02 04:06:18 +00:00
|
|
|
app.on('open-file', (event, path) => {
|
|
|
|
openFile(path);
|
|
|
|
})
|
|
|
|
|
2020-07-30 19:43:55 +00:00
|
|
|
// In this file you can include the rest of your app's specific main process
|
|
|
|
// code. You can also put them in separate files and require them here.
|
|
|
|
|
|
|
|
// Register IPC messages
|
|
|
|
// https://stackoverflow.com/questions/52236641/electron-ipc-and-nodeintegration
|
|
|
|
/*
|
|
|
|
ipcMain.on('hello', (event, message) => {
|
|
|
|
console.log(event);
|
|
|
|
});
|
|
|
|
*/
|
2020-08-03 01:22:20 +00:00
|
|
|
|