Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open recent menu #31

Merged
merged 11 commits into from
Oct 17, 2019
9,489 changes: 9,489 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
"appId": "com.lvcabral.brs-emu.app",
"productName": "BrightScript Emulator",
"artifactName": "brsEmuApp-${version}-${platform}-${arch}.${ext}",
"files": [ "app/**/*", "node_modules/**/*", "package.json" ],
"files": [
"app/**/*",
"node_modules/**/*",
"package.json"
],
"directories": {
"buildResources": "resources",
"output": "dist/${version}"
Expand Down Expand Up @@ -53,7 +57,7 @@
"release": "npm test && webpack --config=build/webpack.app.config.js --env=production && electron-builder"
},
"dependencies": {
"custom-electron-titlebar": "^3.1.0",
"custom-electron-titlebar": "github:lvcabral/custom-electron-titlebar",
"electron-about-window": "^1.13.1",
"fs-jetpack": "^2.1.0",
"jszip": "^3.2.2",
Expand Down
53 changes: 36 additions & 17 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import { remote, ipcRenderer } from "electron";
import JSZip from "jszip";
// App menu and theme configuration
const mainWindow = remote.getCurrentWindow();
const appMenu = remote.Menu.getApplicationMenu();
const userTheme = window.localStorage.getItem("userTheme");
if (userTheme) {
appMenu.getMenuItemById(`theme-${userTheme}`).checked = true;
}
let appMenu = remote.Menu.getApplicationMenu();
let userTheme = window.localStorage.getItem("userTheme") || "purple";
remote.getGlobal("sharedObject").backgroundColor = getComputedStyle(document.documentElement)
.getPropertyValue("--background-color")
.trim();
Expand Down Expand Up @@ -75,7 +72,7 @@ const deviceData = {
const display = document.getElementById("display");
const ctx = display.getContext("2d", { alpha: false });
const screenSize = { width: 1280, height: 720 };
const displayMode = window.localStorage.getItem("displayMode") || "720p";
let displayMode = window.localStorage.getItem("displayMode") || "720p";
if (displayMode === "1080p") {
screenSize.width = 1920;
screenSize.height = 1080;
Expand All @@ -86,7 +83,6 @@ if (displayMode === "1080p") {
let aspectRatio = displayMode === "480p" ? 4 / 3 : 16 / 9;
if (displayMode !== deviceData.displayMode) {
changeDisplayMode(displayMode);
appMenu.getMenuItemById(`device-${displayMode}`).checked = true;
} else {
updateDisplayOnStatus();
}
Expand All @@ -95,8 +91,9 @@ const bufferCanvas = new OffscreenCanvas(screenSize.width, screenSize.height);
const bufferCtx = bufferCanvas.getContext("2d");
let buffer = new ImageData(screenSize.width, screenSize.height);
// Overscan Mode
let overscanMode = window.localStorage.getItem("overscanMode") || "disabled" ;
appMenu.getMenuItemById(`overscan-${overscanMode}`).checked = true;
let overscanMode = window.localStorage.getItem("overscanMode") || "disabled";
// Setup Menu
setupMenuSwitches();
// Load Registry
const storage = window.localStorage;
for (let index = 0; index < storage.length; index++) {
Expand All @@ -106,6 +103,14 @@ for (let index = 0; index < storage.length; index++) {
}
}
// Events from background thread
ipcRenderer.on("closeChannel", function(event) {
if (running) {
closeChannel();
}
});
ipcRenderer.on("updateMenu", function(event) {
setupMenuSwitches();
});
ipcRenderer.on("saveScreenshot", function(event, file) {
const img = display.toDataURL("image/png");
const data = img.replace(/^data:image\/\w+;base64,/, "");
Expand All @@ -115,6 +120,7 @@ ipcRenderer.on("copyScreenshot", function(event) {
copyScreenshot();
});
ipcRenderer.on("setTheme", function(event, theme) {
userTheme = theme;
document.documentElement.setAttribute("data-theme", theme);
remote.getGlobal("sharedObject").backgroundColor = getComputedStyle(document.documentElement)
.getPropertyValue("--background-color")
Expand All @@ -129,6 +135,7 @@ ipcRenderer.on("setTheme", function(event, theme) {
});
ipcRenderer.on("setDisplay", function(event, mode) {
if (mode !== deviceData.displayMode) {
displayMode = mode;
changeDisplayMode(mode);
window.localStorage.setItem("displayMode", mode);
}
Expand Down Expand Up @@ -202,6 +209,7 @@ function loadFile(fileName, fileData) {
running = true;
reader.readAsText(fileData);
}
appMenu.getMenuItemById("close-channel").enabled = running;
display.focus();
}
// Uncompress Zip and execute
Expand Down Expand Up @@ -411,11 +419,11 @@ function runChannel() {
function receiveMessage(event) {
if (event.data instanceof ImageData) {
buffer = event.data;
if (bufferCanvas.width !== buffer.width || bufferCanvas.height !== buffer.height) {
if (bufferCanvas.width !== buffer.width || bufferCanvas.height !== buffer.height) {
statusResolution.innerText = `${buffer.width}x${buffer.height}`;
statusIconRes.innerHTML = "<i class='fa fa-ruler-combined'></i>";
bufferCanvas.width = buffer.width;
bufferCanvas.height = buffer.height;
bufferCanvas.height = buffer.height;
}
bufferCtx.putImageData(buffer, 0, 0);
drawBufferImage();
Expand Down Expand Up @@ -443,7 +451,9 @@ function closeChannel() {
}
brsWorker.terminate();
sharedArray[0] = 0;
bufferCanvas.width = 1;
running = false;
appMenu.getMenuItemById("close-channel").enabled = false;
}
// Remote control emulator
function keyDownHandler(event) {
Expand Down Expand Up @@ -600,21 +610,21 @@ function drawBufferImage() {
if (overscanMode === "enabled") {
let x = Math.round(bufferCanvas.width * overscan);
let y = Math.round(bufferCanvas.height * overscan);
let w = bufferCanvas.width - (x * 2);
let h = bufferCanvas.height - (y * 2);
let w = bufferCanvas.width - x * 2;
let h = bufferCanvas.height - y * 2;
ctx.drawImage(bufferCanvas, x, y, w, h, 0, 0, screenSize.width, screenSize.height);
} else {
ctx.drawImage(bufferCanvas, 0, 0, screenSize.width, screenSize.height);
}
if (overscanMode === "guide-lines") {
let x = Math.round(screenSize.width * overscan);
let y = Math.round(screenSize.height * overscan);
let w = screenSize.width - (x * 2);
let h = screenSize.height - (y * 2);
let w = screenSize.width - x * 2;
let h = screenSize.height - y * 2;
ctx.strokeStyle = "#D0D0D0FF";
ctx.lineWidth = 2;
ctx.setLineDash([1, 2]);
ctx.strokeRect(x, y, w, h);
ctx.setLineDash([ 1, 2 ]);
ctx.strokeRect(x, y, w, h);
}
}
// Change Display Mode
Expand All @@ -635,3 +645,12 @@ function updateDisplayOnStatus() {
statusDisplay.innerText = `${ui} (${deviceData.displayMode})`;
}
}
// Configure Menu Options
function setupMenuSwitches() {
appMenu = remote.Menu.getApplicationMenu();
appMenu.getMenuItemById("close-channel").enabled = running;
appMenu.getMenuItemById(`theme-${userTheme}`).checked = true;
appMenu.getMenuItemById(`device-${displayMode}`).checked = true;
appMenu.getMenuItemById(`overscan-${overscanMode}`).checked = true;
appMenu.getMenuItemById("status-bar").checked = status.style.visibility === "visible";
}
9 changes: 6 additions & 3 deletions src/helpers/dialog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dialog, BrowserWindow } from "electron";
import {addRecentPackage, addRecentSource } from "../menu/menuService";

/*
* Show open dialog to open a .zip or .brs file.
Expand All @@ -9,7 +10,7 @@ export function openChannelPackage() {
filters: [ { name: "Channel Packages", extensions: [ "zip" ] }, { name: "All Files", extensions: [ "*" ] } ],
properties: [ "openFile" ]
};
var window = BrowserWindow.getFocusedWindow();
const window = BrowserWindow.getFocusedWindow();
dialog
.showOpenDialog(window, opts)
.then((result) => {
Expand All @@ -18,6 +19,7 @@ export function openChannelPackage() {
return;
}
window.webContents.send("fileSelected", result.filePaths);
addRecentPackage(result.filePaths[0]);
})
.catch((err) => {
console.log(err);
Expand All @@ -30,7 +32,7 @@ export function openBrightScriptFile() {
filters: [ { name: "BrightScript source files", extensions: [ "brs" ] }, { name: "All Files", extensions: [ "*" ] } ],
properties: [ "openFile" ]
};
var window = BrowserWindow.getFocusedWindow();
const window = BrowserWindow.getFocusedWindow();
dialog
.showOpenDialog(window, opts)
.then((result) => {
Expand All @@ -39,6 +41,7 @@ export function openBrightScriptFile() {
return;
}
window.webContents.send("fileSelected", result.filePaths);
addRecentSource(result.filePaths[0]);
})
.catch((err) => {
console.log(err);
Expand All @@ -50,7 +53,7 @@ export function saveScreenshot() {
title: "Save the Screenshot as",
filters: [ { name: "PNG Image", extensions: [ "png" ] }, { name: "All Files", extensions: [ "*" ] } ]
};
var window = BrowserWindow.getFocusedWindow();
const window = BrowserWindow.getFocusedWindow();
dialog
.showSaveDialog(window, opts)
.then((result) => {
Expand Down
20 changes: 17 additions & 3 deletions src/helpers/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// instances of it and give each different name.

import { app, BrowserWindow, screen, Menu } from "electron";
import path from "path";
import jetpack from "fs-jetpack";

export default (name, options, argv) => {
Expand Down Expand Up @@ -80,7 +81,7 @@ export default (name, options, argv) => {
};

state = ensureVisibleOnSomeDisplay(restore());
var full = {};
let full = {};

if (argv.fullscreen) {
full = { fullscreen: true };
Expand All @@ -98,20 +99,33 @@ export default (name, options, argv) => {
);

win.on("ready-to-show", function() {
let openFile;
if (argv && argv.o) {
win.webContents.send("fileSelected", [ argv.o.trim() ]);
openFile = argv.o.trim();
} else {
try {
let index = argv._.length - 1;
if (index) {
if (jetpack.exists(argv._[index])) {
win.webContents.send("fileSelected", [ argv._[index] ]);
openFile = argv._[index];
}
}
} catch (error) {
console.error("Invalid parameters!", error);
}
}
if (openFile) {
const fileExt = path.parse(openFile).ext.toLowerCase();
if (fileExt === ".zip") {
win.webContents.send("fileSelected", [ openFile ]);
addRecentPackage(openFile);
} else if (fileExt === ".brs") {
win.webContents.send("fileSelected", [ openFile ]);
addRecentSource(openFile);
} else {
console.log("File format not supported: ", fileExt);
}
}
win.show();
win.focus();
});
Expand Down
13 changes: 2 additions & 11 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,21 @@ import env from "env";
import minimist from "minimist";
import { app, screen, Menu } from "electron";
import createWindow from "./helpers/window";
import { fileMenuTemplate } from "./menu/fileMenuTemplate";
import { editMenuTemplate } from "./menu/editMenuTemplate";
import { deviceMenuTemplate } from "./menu/deviceMenuTemplate";
import { viewMenuTemplate } from "./menu/viewMenuTemplate";
import { helpMenuTemplate } from "./menu/helpMenuTemplate";
import { createMenu } from "./menu/menuService"

const argv = minimist(process.argv.slice(1), {
string: [ "o" ],
alias: { f: "fullscreen", d: "devtools" }
});

const setApplicationMenu = () => {
const menus = [ fileMenuTemplate, editMenuTemplate, deviceMenuTemplate, viewMenuTemplate, helpMenuTemplate ];
Menu.setApplicationMenu(Menu.buildFromTemplate(menus));
};

// Save userData in separate folders for each environment.
if (env.name !== "production") {
const userDataPath = app.getPath("userData");
app.setPath("userData", `${userDataPath} (${env.name})`);
}

app.on("ready", () => {
setApplicationMenu();
createMenu();

global.sharedObject = {
backgroundColor: "#251135"
Expand Down
24 changes: 8 additions & 16 deletions src/menu/deviceMenuTemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BrowserWindow, Menu } from "electron";

export const deviceMenuTemplate = {
label: "&Device",
submenu: [
Expand All @@ -8,8 +6,7 @@ export const deviceMenuTemplate = {
label: "Display Mode: SD 480p (4:3)",
type: "radio",
checked: false,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setDisplay", "480p");
}
},
Expand All @@ -18,8 +15,7 @@ export const deviceMenuTemplate = {
label: "Display Mode: HD 720p (16:9)",
type: "radio",
checked: true,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setDisplay", "720p");
}
},
Expand All @@ -28,8 +24,7 @@ export const deviceMenuTemplate = {
label: "Display Mode: FHD 1080p (16:9)",
type: "radio",
checked: false,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setDisplay", "1080p");
}
},
Expand All @@ -39,8 +34,7 @@ export const deviceMenuTemplate = {
label: "TV Overscan: Disabled",
type: "radio",
checked: true,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setOverscan", "disabled");
}
},
Expand All @@ -49,8 +43,7 @@ export const deviceMenuTemplate = {
label: "TV Overscan: Guide Lines",
type: "radio",
checked: false,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setOverscan", "guide-lines");
}
},
Expand All @@ -59,17 +52,16 @@ export const deviceMenuTemplate = {
label: "TV Overscan: Enabled",
type: "radio",
checked: false,
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("setOverscan", "enabled");
}
},
{ type: "separator" },
{
label: "Reset Device",
accelerator: "CmdOrCtrl+Shift+R",
click: () => {
BrowserWindow.getFocusedWindow().webContents.reloadIgnoringCache();
click: (event, window) => {
window.webContents.reloadIgnoringCache();
}
}
]
Expand Down
5 changes: 1 addition & 4 deletions src/menu/editMenuTemplate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { BrowserWindow } from "electron";

export const editMenuTemplate = {
label: "&Edit",
submenu: [
{
label: "Copy Screenshot",
accelerator: "CmdOrCtrl+C",
click: () => {
var window = BrowserWindow.getFocusedWindow();
click: (event, window) => {
window.webContents.send("copyScreenshot");
}
},
Expand Down
Loading