Skip to content

Commit

Permalink
Cleanup menu
Browse files Browse the repository at this point in the history
  • Loading branch information
hokaccha committed May 3, 2018
1 parent 101a801 commit db7c139
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 160 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"classnames": "2.2.5",
"codemirror": "5.33.0",
"csv-stringify": "2.0.1",
"electron-is-dev": "0.3.0",
"electron-log": "2.2.14",
"electron-updater": "2.21.4",
"font-awesome": "4.7.0",
Expand Down
4 changes: 2 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import electron from "electron";
import { updater } from "./updater";
import menu from "./menu";
import { initMenu } from "./menu";

const app = electron.app;
let mainWindow;
Expand All @@ -27,7 +27,7 @@ app.on("window-all-closed", () => {
app.on("ready", () => {
createWindow();
updater.watch();
electron.Menu.setApplicationMenu(electron.Menu.buildFromTemplate(menu));
initMenu();
});

app.on("activate", () => {
Expand Down
200 changes: 80 additions & 120 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
@@ -1,128 +1,88 @@
import { app } from "electron";
import zoom from "./zoom";
import { app, Menu, dialog } from "electron";
import { updater, UpdateState } from "./updater";
import isDev from "electron-is-dev";

const menu: Electron.MenuItemConstructorOptions[] = [
{
label: "Application",
submenu: [
{
label: "About Bdash",
role: "about"
},
{
type: "separator"
},
{
label: "Hide Bdash",
accelerator: "CmdOrCtrl+H",
role: "hide"
},
{
label: "Hide Others",
accelerator: "Option+CmdOrCtrl+H",
role: "hideothers"
},
{
label: "Show All",
role: "unhide"
},
{
type: "separator"
},
{
label: "Quit",
accelerator: "CmdOrCtrl+Q",
click: () => {
app.quit();
}
const editMenu: Electron.MenuItemConstructorOptions = {
label: "Edit",
submenu: [{ role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectall" }]
};

const viewMenu: Electron.MenuItemConstructorOptions = {
label: "View",
submenu: [
{ role: "toggledevtools" },
{ type: "separator" },
{ role: "resetzoom" },
{ role: "zoomin" },
{ role: "zoomout" }
]
};

if (isDev && Array.isArray(viewMenu.submenu)) {
viewMenu.submenu.unshift({ role: "reload" });
}

const windowMenu: Electron.MenuItemConstructorOptions = {
role: "window",
submenu: [{ role: "minimize" }, { role: "close" }]
};

const helpMenu: Electron.MenuItemConstructorOptions = {
role: "help",
submenu: [
{
label: "Report Issue",
click() {
require("electron").shell.openExternal("https://github.com/bdash-app/bdash/issues/new");
}
]
},
{
label: "Edit",
submenu: [
{
label: "Undo",
accelerator: "CmdOrCtrl+Z",
role: "undo"
},
{
label: "Redo",
accelerator: "Shift+CmdOrCtrl+Z",
role: "redo"
},
{
type: "separator"
},
{
label: "Cut",
accelerator: "CmdOrCtrl+X",
role: "cut"
},
{
label: "Copy",
accelerator: "CmdOrCtrl+C",
role: "copy"
},
{
label: "Paste",
accelerator: "CmdOrCtrl+V",
role: "paste"
},
{
label: "Select All",
accelerator: "CmdOrCtrl+A",
role: "selectall"
}
]
};

const checkForUpdateItem = {
label: "Check for Updates...",
click() {
switch (updater.state) {
case UpdateState.UpdateNotAvailable: {
const message = "There are currently no updates available.";
dialog.showMessageBox({ message });
return;
}
]
},
{
label: "View",
submenu: [
{
label: "Zoom In",
accelerator: "CmdOrCtrl+Plus",
click: () => zoom.zoomIn()
},
{
label: "Zoom Out",
accelerator: "CmdOrCtrl+-",
click: () => zoom.zoomOut()
},
{
label: "Zoom Reset",
accelerator: "CmdOrCtrl+0",
click: () => zoom.reset()
},
{ type: "separator" },
{
label: "Reload",
accelerator: "CmdOrCtrl+Alt+R",
click: (_, focusedWindow) => {
if (focusedWindow) {
focusedWindow.reload();
}
}
},
{
label: "Toggle Developer Tools",
accelerator: process.platform === "darwin" ? "Alt+Command+I" : "Ctrl+Shift+I",
click: (_, focusedWindow: any) => {
if (focusedWindow) {
focusedWindow.toggleDevTools();
case UpdateState.UpdateDownloaded: {
const message = "There is an available update. Restart app to apply the latest update.";
const buttons = ["Update Now", "Later"];
dialog.showMessageBox({ message, buttons }, buttonIndex => {
if (buttonIndex === 0) {
updater.quit();
}
}
});
return;
}
]
},
{
label: "Window",
role: "window",
}
}
};

const template: Electron.MenuItemConstructorOptions[] = [editMenu, viewMenu, windowMenu, helpMenu];

if (process.platform === "darwin") {
template.unshift({
label: app.getName(),
submenu: [
{ label: "Minimize", accelerator: "CmdOrCtrl+M", role: "minimize" },
{ label: "Bring All to Front", role: "front" }
{ role: "about" },
checkForUpdateItem,
{ type: "separator" },
{ role: "hide" },
{ role: "hideothers" },
{ role: "unhide" },
{ type: "separator" },
{ role: "quit" }
]
}
];
});
} else if (Array.isArray(helpMenu.submenu)) {
helpMenu.submenu.push(checkForUpdateItem);
}

export default menu;
export function initMenu() {
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
2 changes: 2 additions & 0 deletions src/main/updater.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { autoUpdater, AppUpdater } from "electron-updater";
import isDev from "electron-is-dev";
import logger from "./logger";

autoUpdater.logger = logger;
Expand All @@ -22,6 +23,7 @@ export class Updater {
}

check() {
if (isDev) return;
this.autoUpdater.checkForUpdates();
}

Expand Down
37 changes: 0 additions & 37 deletions src/main/zoom.ts

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ electron-download@^4.1.0:
semver "^5.3.0"
sumchecker "^2.0.1"

electron-is-dev@^0.3.0:
electron-is-dev@0.3.0, electron-is-dev@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/electron-is-dev/-/electron-is-dev-0.3.0.tgz#14e6fda5c68e9e4ecbeff9ccf037cbd7c05c5afe"

Expand Down

0 comments on commit db7c139

Please sign in to comment.