Skip to content

Commit

Permalink
refactor: move createMenu to shared
Browse files Browse the repository at this point in the history
so that renderer and main use the same logic
  • Loading branch information
Xiphe committed Apr 23, 2020
1 parent 68f4a13 commit 7ff3c1d
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 76 deletions.
48 changes: 22 additions & 26 deletions main/defaultMenu.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { Menu } from 'electron';
import { Menu, app } from 'electron';
import {
createMenu,
createFileMenu,
createOpenRecent,
} from '../src/shared/createMenu';
import { Settings } from '../src/shared/settings';

export function createDefaultMenu(
createWindow: (file?: string) => void,
openFile: () => void,
settings: Settings,
) {
const defaultMenu = Menu.buildFromTemplate([
{ role: 'appMenu' },
{
label: 'File',
submenu: [
{
label: 'New Budget',
accelerator: 'CommandOrControl+N',
click() {
createWindow();
},
},
{ type: 'separator' },
{
label: 'Open...',
accelerator: 'CommandOrControl+O',
click: openFile,
},
{ type: 'separator' },
process.platform === 'darwin' ? { role: 'close' } : { role: 'quit' },
],
},
]);

return {
activate() {
Menu.setApplicationMenu(defaultMenu);
Menu.setApplicationMenu(
Menu.buildFromTemplate(
createMenu(app.name, [
createFileMenu({
openRecent: createOpenRecent(
settings.getRecentFiles(),
createWindow,
),
fileNew: () => createWindow(),
fileOpen: openFile,
}),
]),
),
);
},
};
}
16 changes: 9 additions & 7 deletions main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@ import disableUnused from './disableUnused';
import { createDefaultMenu } from './defaultMenu';
import registerSave from './registerSave';
import moneymoneyHandlers from './moneymoney/handlers';
import getSettings from '../src/shared/settings';

export default function main() {
/* ref https://github.com/Xiphe/budgetbudget/issues/12 */
app.allowRendererProcessReuse = true;

disableUnused(app);

const windowManager = createWindowManager(app, ipcMain);
const settings = getSettings();
const windowManager = createWindowManager(app, ipcMain, settings);
windowManager.init();
const openFile = createOpenFile(ipcMain, windowManager.createWindow);
const defaultMenu = createDefaultMenu(windowManager.createWindow, openFile);
const defaultMenu = createDefaultMenu(
windowManager.createWindow,
openFile,
settings,
);
registerSave(ipcMain, windowManager);
moneymoneyHandlers(ipcMain);

Expand All @@ -33,11 +39,7 @@ export default function main() {
() => windowManager.broadcast('UPDATE_COLOR_PREFERENCES'),
);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
} else {
defaultMenu.activate();
}
defaultMenu.activate();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
Expand Down
9 changes: 6 additions & 3 deletions main/windowManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { BrowserWindow, IpcMain, WebContents, App } from 'electron';
import { join } from 'path';
import getSettings from '../src/shared/settings';
import { Settings } from '../src/shared/settings';

export type WindowManager = ReturnType<typeof createWindowManager>;
export default function createWindowManager(app: App, ipcMain: IpcMain) {
const settings = getSettings();
export default function createWindowManager(
app: App,
ipcMain: IpcMain,
settings: Settings,
) {
const windows: { [key: string]: BrowserWindow } = {};
const newWindows: BrowserWindow[] = [];
let appIsQuitting = false;
Expand Down
22 changes: 14 additions & 8 deletions src/budget/useMenu.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { useEffect, useMemo, useState } from 'react';
import { ipcRenderer, remote } from 'electron';
import { getSharedSettings, useSetShowSettings } from '../lib';
import {
createMenu,
createFileMenu,
createEditMenu,
createOpenRecent,
CreateMenuCallbacks,
getSharedSettings,
useSetShowSettings,
} from '../lib';
} from '../shared/createMenu';

export const MENU_ID_SAVE = 'MENU_SAVE';
export const MENU_ID_SAVE_AS = 'MENU_SAVE_AS';

function buildMenu(callbacks: CreateMenuCallbacks) {
return remote.Menu.buildFromTemplate(
createMenu(
remote.app.name,
[
{
label: 'File',
submenu: createFileMenu([
createFileMenu({
fileOpen: ipcRenderer.invoke.bind(null, 'MENU_FILE_OPEN'),
fileNew: ipcRenderer.invoke.bind(null, 'MENU_FILE_NEW'),
openRecent: createOpenRecent(
getSharedSettings().getRecentFiles(),
ipcRenderer.invoke.bind(null, 'MENU_FILE_OPEN_EXISTING'),
),
entries: [
{
label: 'Save',
id: MENU_ID_SAVE,
Expand All @@ -37,8 +43,8 @@ function buildMenu(callbacks: CreateMenuCallbacks) {
ipcRenderer.send('SAVE_AS');
},
},
]),
},
],
}),
createEditMenu(),
],
callbacks,
Expand Down
3 changes: 0 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { NumberFormatter as NumberFormatterT } from './createNumberFormatter';
import { CreateMenuCallbacks as CreateMenuCallbacksT } from './createMenu';
export { default as formatDateKey } from './formatDateKey';
export { default as roundWithFractions } from './roundWithFractions';
export { default as createNumberFormatter } from './createNumberFormatter';
export { default as useInputProps } from './useInputProps';
export { default as useAmountInputProps } from './useAmountInputProps';
export { default as useInit, INIT_EMPTY } from './useInit';
export { default as getSharedSettings } from './getSharedSettings';
export { createMenu, createFileMenu, createEditMenu } from './createMenu';
export {
withShowSettingsProvider,
useSetShowSettings,
Expand All @@ -25,4 +23,3 @@ export {
export * from './guards';
export * from './useIsVisible';
export type NumberFormatter = NumberFormatterT;
export type CreateMenuCallbacks = CreateMenuCallbacksT;
62 changes: 34 additions & 28 deletions src/lib/createMenu.ts → src/shared/createMenu.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MenuItemConstructorOptions } from 'electron';
import { ipcRenderer, remote } from 'electron';
import { basename } from 'path';
import getSharedSettings from './getSharedSettings';
import { RecentFile } from './settings';

type MenuConfig = MenuItemConstructorOptions;
export type CreateMenuCallbacks = {
Expand All @@ -15,6 +14,7 @@ function isMenuConfig(
}

export function createMenu(
appName: string,
entries: MenuConfig[] = [],
{ setShowSettings }: CreateMenuCallbacks = {},
): MenuConfig[] {
Expand All @@ -38,56 +38,62 @@ export function createMenu(

return [
{
label: remote.app.name,
label: appName,
submenu: submenu.filter(isMenuConfig),
},
...entries,
];
}

export function createFileMenu(entries: MenuConfig[] = []): MenuConfig[] {
type FileMenuConfig = {
entries?: MenuConfig[];
openRecent?: MenuConfig | false;
fileNew: () => void;
fileOpen: () => void;
};
export function createFileMenu({
entries = [],
openRecent = false,
fileNew,
fileOpen,
}: FileMenuConfig): MenuConfig {
const config: (MenuConfig | false)[] = [
{
label: 'New Budget',
accelerator: 'CommandOrControl+N',
click() {
ipcRenderer.invoke('MENU_FILE_NEW');
},
click: fileNew,
},
{ type: 'separator' },
{
label: 'Open...',
accelerator: 'CommandOrControl+O',
async click() {
ipcRenderer.invoke('MENU_FILE_OPEN');
},
},
{
label: 'Open Recent',
id: 'open-recent',
submenu: createRecentSubmenu(getSharedSettings().getRecentFiles()),
click: fileOpen,
},
openRecent,
entries.length ? { type: 'separator' } : false,
...entries,
{ type: 'separator' },
process.platform === 'darwin' ? { role: 'close' } : { role: 'quit' },
];

return config.filter(isMenuConfig);
return {
label: 'File',
submenu: config.filter(isMenuConfig),
};
}

export function createRecentSubmenu(
recentFiles: {
name: string;
file: string;
}[],
): MenuConfig['submenu'] {
return recentFiles.map(({ name, file }) => ({
label: `${name} - ${basename(file)}`,
click() {
ipcRenderer.invoke('MENU_FILE_OPEN_EXISTING', file);
},
}));
export function createOpenRecent(
recentFiles: RecentFile[],
openExisting: (file: string) => void,
): MenuConfig {
return {
label: 'Open Recent',
id: 'open-recent',
submenu: recentFiles.map(({ name, file }) => ({
label: `${name} - ${basename(file)}`,
click: openExisting.bind(null, file),
})),
};
}

export function createEditMenu(): MenuConfig {
Expand Down
3 changes: 2 additions & 1 deletion src/shared/settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import settings from 'electron-settings';
import fs from 'fs';

type RecentFile = { name: string; file: string };
export type RecentFile = { name: string; file: string };
export type Settings = ReturnType<typeof getSettings>;

export default function getSettings() {
function getOpenBudgets(checkExists: boolean = true) {
Expand Down

0 comments on commit 7ff3c1d

Please sign in to comment.