Skip to content

Commit

Permalink
Implemented brs file list on recent menu #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo Lv Cabral authored and Marcelo Lv Cabral committed Oct 17, 2019
1 parent 3d5a5dc commit 7e4acba
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 89 deletions.
24 changes: 12 additions & 12 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +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" ;
let overscanMode = window.localStorage.getItem("overscanMode") || "disabled";
// Setup Menu
setupMenuSwitches()
setupMenuSwitches();
// Load Registry
const storage = window.localStorage;
for (let index = 0; index < storage.length; index++) {
Expand Down Expand Up @@ -419,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 @@ -610,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 Down Expand Up @@ -652,5 +652,5 @@ function setupMenuSwitches() {
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");
}
appMenu.getMenuItemById("status-bar").checked = status.style.visibility === "visible";
}
146 changes: 94 additions & 52 deletions src/menu/fileMenuTemplate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as dialog from "../helpers/dialog";
import { getRecentPackage, clearRecentFiles, addRecentPackage } from "./menuService"
import { getRecentPackage, clearRecentFiles, addRecentPackage, addRecentSource, getRecentSource } from "./menuService";

export const fileMenuTemplate = {
label: "&File",
Expand All @@ -22,81 +22,60 @@ export const fileMenuTemplate = {
id: "file-open-recent",
label: "Open Recent",
submenu: [
{
id: "zip-0",
label:"",
{
id: "zip-0",
label: "",
visible: false,
click (event, window) {
window.webContents.send("fileSelected", [getRecentPackage(0)]);
addRecentPackage(getRecentPackage(0));
window.blur();
window.focus();
click: (event, window) => {
packageClick(window, 0);
}
},
{
{
id: "zip-1",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(1)]);
addRecentPackage(getRecentPackage(1));
window.blur();
window.focus();
packageClick(window, 1);
}
},
{
{
id: "zip-2",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(2)]);
addRecentPackage(getRecentPackage(2));
window.blur();
window.focus();
packageClick(window, 2);
}
},
{
{
id: "zip-3",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(3)]);
addRecentPackage(getRecentPackage(3));
window.blur();
window.focus();
packageClick(window, 3);
}
},
{
{
id: "zip-4",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(4)]);
addRecentPackage(getRecentPackage(4));
window.blur();
window.focus();
packageClick(window, 4);
}
},
{
{
id: "zip-5",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(5)]);
addRecentPackage(getRecentPackage(5));
window.blur();
window.focus();
packageClick(window, 5);
}
},
{
{
id: "zip-6",
label:"",
label: "",
visible: false,
click: (event, window) => {
window.webContents.send("fileSelected", [getRecentPackage(6)]);
addRecentPackage(getRecentPackage(6));
window.blur();
window.focus();
packageClick(window, 6);
}
},
{
Expand All @@ -105,13 +84,62 @@ export const fileMenuTemplate = {
enabled: false
},
{ type: "separator" },
{ id: "brs-0", label:"", visible: false},
{ id: "brs-1", label:"", visible: false},
{ id: "brs-2", label:"", visible: false},
{ id: "brs-3", label:"", visible: false},
{ id: "brs-4", label:"", visible: false},
{ id: "brs-5", label:"", visible: false},
{ id: "brs-6", label:"", visible: false},
{
id: "brs-0",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 0);
}
},
{
id: "brs-1",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 1);
}
},
{
id: "brs-2",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 2);
}
},
{
id: "brs-3",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 3);
}
},
{
id: "brs-4",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 4);
}
},
{
id: "brs-5",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 5);
}
},
{
id: "brs-6",
label: "",
visible: false,
click: (event, window) => {
sourceClick(window, 6);
}
},
{
id: "brs-empty",
label: "No Source .brs File Recently Opened",
Expand Down Expand Up @@ -154,3 +182,17 @@ export const fileMenuTemplate = {
}
]
};

function packageClick(window, id) {
window.webContents.send("fileSelected", [ getRecentPackage(id) ]);
addRecentPackage(getRecentPackage(id));
window.blur();
window.focus();
}

function sourceClick(window, id) {
window.webContents.send("fileSelected", [ getRecentSource(id) ]);
addRecentSource(getRecentSource(id));
window.blur();
window.focus();
}
63 changes: 38 additions & 25 deletions src/menu/menuService.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ const recentFilesJson = "recent-files.json";
let fileMenuIndex = 0;
let recentMenuIndex = 2;
let recentFiles;
let recentMenu;
let menuTemplate;

export function createMenu() {
menuTemplate = [ fileMenuTemplate, editMenuTemplate, deviceMenuTemplate, viewMenuTemplate, helpMenuTemplate ];
if(isMacOS) {
if (isMacOS) {
const fileMenu = menuTemplate[0].submenu;
fileMenu.splice(fileMenu.length-2, 2);
menuTemplate.unshift({role: "appMenu"});
fileMenu.splice(fileMenu.length - 2, 2);
menuTemplate.unshift({ role: "appMenu" });
fileMenuIndex = 1;
}
restoreRecentFiles();
Expand All @@ -42,6 +41,10 @@ export function addRecentPackage(filePath) {
rebuildMenu();
}

export function getRecentSource(index) {
return recentFiles.brs[index];
}

export function addRecentSource(filePath) {
let idx = recentFiles.brs.indexOf(filePath);
if (idx >= 0) {
Expand All @@ -53,13 +56,13 @@ export function addRecentSource(filePath) {
}

export function clearRecentFiles() {
recentFiles = {zip:[], brs:[]};
recentFiles = { zip: [], brs: [] };
saveRecentFiles();
rebuildMenu();
}

function restoreRecentFiles() {
let recentFilesDefault = {zip:[], brs:[]};
let recentFilesDefault = { zip: [], brs: [] };
try {
recentFiles = userDataDir.read(recentFilesJson, "json");
} catch (err) {
Expand All @@ -78,47 +81,57 @@ function saveRecentFiles() {

function rebuildMenu(template = false) {
if (isMacOS || template) {
recentMenu = menuTemplate[fileMenuIndex].submenu[recentMenuIndex].submenu;
const recentMenu = menuTemplate[fileMenuIndex].submenu[recentMenuIndex].submenu;
for (let index = 0; index < maxFiles; index++) {
let fileMenu = recentMenu[index];
if (index < recentFiles.zip.length) {
fileMenu.label = recentFiles.zip[index];
fileMenu.visible = true;
} else {
fileMenu.visible = false;
}
}
}
if (recentFiles.zip.length > 0) {
recentMenu[recentMenu.length-1].enabled = true;
recentMenu[maxFiles].visible = false;
} else {
recentMenu[recentMenu.length-1].enabled = false;
recentMenu[maxFiles].visible = true;
for (let index = 0; index < maxFiles; index++) {
let fileMenu = recentMenu[index + maxFiles + 2];
if (index < recentFiles.brs.length) {
fileMenu.label = recentFiles.brs[index];
fileMenu.visible = true;
} else {
fileMenu.visible = false;
}
}
const brsEnd = maxFiles * 2 + 2;
recentMenu[maxFiles].visible = recentFiles.zip.length === 0;
recentMenu[brsEnd].visible = recentFiles.brs.length === 0;
recentMenu[recentMenu.length - 1].enabled = recentFiles.zip.length + recentFiles.brs.length > 0;
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
let window = BrowserWindow.fromId(1);
if (isMacOS && window) {
window.webContents.send("updateMenu");
}
}
else {
} else {
const appMenu = app.getApplicationMenu();
const recentMenu = appMenu.getMenuItemById("file-open-recent");
const recentMenu = appMenu.getMenuItemById("file-open-recent").submenu;
for (let index = 0; index < maxFiles; index++) {
let fileMenu = recentMenu.submenu.getMenuItemById(`zip-${index}`);
let fileMenu = recentMenu.getMenuItemById(`zip-${index}`);
if (index < recentFiles.zip.length) {
fileMenu.label = recentFiles.zip[index];
fileMenu.visible = true;
} else {
fileMenu.visible = false;
}
}
}
if (recentFiles.zip.length > 0) {
recentMenu.submenu.getMenuItemById("file-clear").enabled = true;
recentMenu.submenu.getMenuItemById("zip-empty").visible = false;
} else {
recentMenu.submenu.getMenuItemById("file-clear").enabled = false;
recentMenu.submenu.getMenuItemById("zip-empty").visible = true;
for (let index = 0; index < maxFiles; index++) {
let fileMenu = recentMenu.getMenuItemById(`brs-${index}`);
if (index < recentFiles.brs.length) {
fileMenu.label = recentFiles.brs[index];
fileMenu.visible = true;
} else {
fileMenu.visible = false;
}
}
recentMenu.getMenuItemById("zip-empty").visible = recentFiles.zip.length === 0;
recentMenu.getMenuItemById("brs-empty").visible = recentFiles.brs.length === 0;
recentMenu.getMenuItemById("file-clear").enabled = recentFiles.zip.length + recentFiles.brs.length > 0;
}
}

0 comments on commit 7e4acba

Please sign in to comment.