Skip to content
51 changes: 50 additions & 1 deletion app/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"use strict";

const electron = require("electron");
const storage = require("electron-json-storage");
const capture = require("./capture");
const CustomMenu = require("./menu");
const CustomWindowManager = require("./window").CustomWindowManager;
Expand All @@ -28,6 +29,10 @@ const menu = new CustomMenu();

// Global reference, which keeps windows from being garbage collected.
global.windowManager = new CustomWindowManager();
// default settings
global.settings = {
printFrontAndBack: false
};

function showOpenDialog(next) {
electron.dialog.showOpenDialog({
Expand Down Expand Up @@ -68,6 +73,21 @@ menu.on("printhelp", (/* browserWindow */) => {
});
});

menu.on("printfrontback", (browserWindow, checked) => {
let window = global.windowManager.getByBrowserWindow(browserWindow);
window.printFrontAndBack(checked);
global.settings.printFrontAndBack = checked;
storage.set("card-creatr-studio-settings", global.settings, function(error) {
if (error) {
electron.dialog.showMessageBox({
type: "warning",
message: "Unable to store card creatr settings.",
detail: error.toString()
});
}
});
});

menu.on("print", (browserWindow) => {
let window = global.windowManager.getByBrowserWindow(browserWindow);
window.print();
Expand Down Expand Up @@ -152,6 +172,11 @@ menu.on("addcard", (browserWindow) => {
window.addCard();
});

menu.on("copycard", (browserWindow) => {
let window = global.windowManager.getByBrowserWindow(browserWindow);
window.copyCard();
});

menu.on("movecardup", (browserWindow) => {
let window = global.windowManager.getByBrowserWindow(browserWindow);
window.moveCardUp();
Expand Down Expand Up @@ -255,7 +280,23 @@ electron.app.on("open-file", (event, filePath) => {

// Populate filesToOpen on non-Mac OS
if (process.platform !== "darwin") {
filesToOpen.push(process.argv[1]);
for (let arg of process.argv) {
if (arg.endsWith(".ccsb") || arg.endsWith(".ccst")) {
filesToOpen.push(arg);
}
}
}

electron.app.on("browser-window-focus", (event, browserWindow) => {
updateMenuEntries(browserWindow);
});

function updateMenuEntries(browserWindow) {
const window = global.windowManager.getByBrowserWindow(browserWindow);
if (window) {
window.printFrontAndBack(global.settings.printFrontAndBack);
electron.Menu.getApplicationMenu().getMenuItemById("printFrontBack").checked = global.settings.printFrontAndBack;
}
}

// This method will be called when Electron has finished
Expand All @@ -267,6 +308,14 @@ electron.app.on("ready", () => {
global.openFile(filePath);
}
electron.Menu.setApplicationMenu(menu.getMenuInstance());
storage.get("card-creatr-studio-settings", (error, settings) => {
if (error) {
console.error(error);
settings = {};
}
global.settings = settings;
updateMenuEntries(electron.BrowserWindow.getFocusedWindow());
});
if (global.windowManager.count() === 0) {
showOpenDialog(global.openFile);
}
Expand Down
18 changes: 18 additions & 0 deletions app/electron/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,24 @@ class CustomMenu extends EventEmitter {
{
type: "separator"
},
{
id: "printFrontBack",
label: "Print front and back in one document",
type: "checkbox",
click: (item, focusedWindow) => {
this.emit("printfrontback", focusedWindow, item.checked);
}
},
{
label: "Export to PDF…",
accelerator: "CmdOrCtrl+Alt+E",
click: (item, focusedWindow) => {
this.emit("print5", focusedWindow);
}
},
{
type: "separator"
},
{
label: "Export Card Images…",
accelerator: "CmdOrCtrl+Shift+E",
Expand Down Expand Up @@ -308,6 +319,13 @@ class CustomMenu extends EventEmitter {
this.emit("addcard", focusedWindow);
}
},
{
label: "Copy Card",
accelerator: "CmdOrCtrl+Shift+C",
click: (item, focusedWindow) => {
this.emit("copycard", focusedWindow);
}
},
{
label: "Move Card Up",
accelerator: "CmdOrCtrl+Alt+Up",
Expand Down
8 changes: 8 additions & 0 deletions app/electron/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class CustomWindow extends EventEmitter {
}
}

printFrontAndBack(checked) {
this._sendMessage("printfrontback", checked);
}

print(options) {
this._sendMessage("print", options);
}
Expand Down Expand Up @@ -96,6 +100,10 @@ class CustomWindow extends EventEmitter {
this._sendMessage("addcard", {});
}

copyCard() {
this._sendMessage("copycard", {});
}

moveCardUp() {
this._sendMessage("movecardup", {});
}
Expand Down
Loading