1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-22 14:33:51 +00:00

electron: fixed binary encoding, reload, args

This commit is contained in:
Steven Hugg 2020-08-02 20:22:20 -05:00
parent 40af6b2602
commit 7712119bca
2 changed files with 52 additions and 32 deletions

View File

@ -3,27 +3,27 @@ const { ipcRenderer } = require('electron');
const fs = require('fs'); const fs = require('fs');
const modpath = require('path'); const modpath = require('path');
// workspace root path
// TODO: reloading clears this
var wsroot = null;
// from browser: read workspace file synchronously
window.getWorkspaceFile = function(path, filetype) {
if (wsroot == null) throw Error("no workspace root set");
try {
// TODO: detect text or binary, or decide based on incbin vs. source file
var fullpath = modpath.join(wsroot, modpath.normalize(path));
var data = fs.readFileSync(fullpath, {encoding:filetype=='text'?'utf8':'binary'});
console.log("getWorkspaceFile", path, filetype);
// TODO: add to watched files
return data;
} catch (e) {
console.log(e);
return null;
}
}
process.once('loaded', () => { process.once('loaded', () => {
// workspace root path
// reload() clears this, so we have to set it every time
var wsroot;
// from browser: read workspace file synchronously
window.getWorkspaceFile = function(path, filetype) {
if (wsroot == null) throw Error("no workspace root set");
try {
// TODO: detect text or binary, or decide based on incbin vs. source file
var fullpath = modpath.join(wsroot, modpath.normalize(path));
var encoding = filetype == 'text' ? 'utf8' : null;
var data = fs.readFileSync(fullpath, {encoding:encoding});
if (encoding == null) data = new Uint8Array(data);
console.log("getWorkspaceFile", path, filetype, data.length);
// TODO: add to watched files
return data;
} catch (e) {
console.log(e);
return null;
}
}
// from electron.js: set workspace root directory // from electron.js: set workspace root directory
ipcRenderer.on('setWorkspaceRoot', (event, data) => { ipcRenderer.on('setWorkspaceRoot', (event, data) => {
wsroot = data.root; wsroot = data.root;

View File

@ -38,7 +38,7 @@ class Workspace {
} }
} }
function openFile(path) { function openFile(path, wnd) {
if (!fs.existsSync(path)) { if (!fs.existsSync(path)) {
dialog.showMessageBox({ dialog.showMessageBox({
type: "error", type: "error",
@ -49,24 +49,38 @@ function openFile(path) {
} }
var dirname = modpath.dirname(path); var dirname = modpath.dirname(path);
var filename = modpath.basename(path); var filename = modpath.basename(path);
var wnd = BrowserWindow.getFocusedWindow(); if (!wnd) wnd = BrowserWindow.getFocusedWindow();
if (wnd.workspace) { wnd.workspace.close(); }
var ws = new Workspace(dirname, filename, wnd); var ws = new Workspace(dirname, filename, wnd);
if (wnd.workspace) { wnd.workspace.close(); }
wnd.workspace = ws; wnd.workspace = ws;
wnd.on('closed', () => { wnd.on('closed', () => {
ws.close(); ws.close();
}); });
openWorkspace(wnd, ws);
app.addRecentDocument(path);
store.set(KEY_lastWorkspaceFilePath, path);
}
function openWorkspace(wnd, ws) {
var qs = new URLSearchParams(); var qs = new URLSearchParams();
qs.set('electron_ws', 1); qs.set('electron_ws', 1);
qs.set('repo', dirname); qs.set('repo', ws.directory);
qs.set('file', filename); qs.set('file', ws.mainfile);
wnd.loadURL(`file://${__dirname}/electron.html?${qs}`).then(() => { wnd.loadURL(`file://${__dirname}/electron.html?${qs}`).then(() => {
wnd.webContents.send('setWorkspaceRoot', {root:dirname}); wnd.webContents.send('setWorkspaceRoot', {root:ws.directory});
app.addRecentDocument(path);
store.set(KEY_lastWorkspaceFilePath, path);
}); });
} }
// 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();
}
}
function openFileDialog() { function openFileDialog() {
dialog.showOpenDialog({ dialog.showOpenDialog({
title: "Open File", title: "Open File",
@ -80,8 +94,11 @@ function openFileDialog() {
} }
function openDefaultFile() { function openDefaultFile() {
createWindow(); var wnd = createWindow();
/* if (process.argv.length >= 3) {
openFile(process.argv[2], wnd);
}
/* TODO
var lastfile = store.get(KEY_lastWorkspaceFilePath); var lastfile = store.get(KEY_lastWorkspaceFilePath);
if (lastfile != null) { if (lastfile != null) {
openFile(lastfile); openFile(lastfile);
@ -182,8 +199,10 @@ function buildMenu() {
{ {
label: 'View', label: 'View',
submenu: [ submenu: [
{ role: 'reload' }, {
{ role: 'forcereload' }, label: 'Reload Window',
click: reloadCurrentWindow
},
{ role: 'toggledevtools' }, { role: 'toggledevtools' },
{ type: 'separator' }, { type: 'separator' },
{ role: 'resetzoom' }, { role: 'resetzoom' },
@ -300,3 +319,4 @@ ipcMain.on('hello', (event, message) => {
console.log(event); console.log(event);
}); });
*/ */