Skip to content
Snippets Groups Projects
menu.js 5.65 KiB
Newer Older
  • Learn to ignore specific revisions
  • James Long's avatar
    James Long committed
    const { Menu, ipcMain, app, shell } = require('electron');
    
    function getMenu(isDev, createWindow) {
      const template = [
        {
          label: 'File',
          submenu: [
            {
              label: 'Load Backup...',
              enabled: false,
              click(item, focusedWindow) {
                if (focusedWindow) {
                  if (focusedWindow.webContents.getTitle() === 'Actual') {
                    focusedWindow.webContents.executeJavaScript(
    
                      "__actionsForMenu.replaceModal('load-backup')",
    
    James Long's avatar
    James Long committed
                    );
                  }
                }
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
              label: 'Manage files...',
              accelerator: 'CmdOrCtrl+O',
              click(item, focusedWindow) {
                if (focusedWindow) {
                  if (focusedWindow.webContents.getTitle() === 'Actual') {
                    focusedWindow.webContents.executeJavaScript(
    
                      '__actionsForMenu.closeBudget()',
    
    James Long's avatar
    James Long committed
                    );
                  } else {
                    focusedWindow.close();
                  }
                } else {
                  // The default page is the budget list
                  createWindow();
                }
    
    James Long's avatar
    James Long committed
        },
        {
          label: 'Edit',
          submenu: [
            {
              label: 'Undo',
              enabled: false,
              accelerator: 'CmdOrCtrl+Z',
    
              click: function (menuItem, focusedWin) {
    
    James Long's avatar
    James Long committed
                // Undo
                focusedWin.webContents.executeJavaScript('__actionsForMenu.undo()');
    
    James Long's avatar
    James Long committed
            },
            {
              label: 'Redo',
              enabled: false,
              accelerator: 'Shift+CmdOrCtrl+Z',
    
              click: function (menuItem, focusedWin) {
    
    James Long's avatar
    James Long committed
                // Redo
                focusedWin.webContents.executeJavaScript('__actionsForMenu.redo()');
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'cut',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'copy',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'paste',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'pasteandmatchstyle',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'delete',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'selectall',
            },
          ],
    
    James Long's avatar
    James Long committed
        },
        {
          label: 'View',
          submenu: [
            isDev && {
              label: 'Reload',
              accelerator: 'CmdOrCtrl+R',
              click(item, focusedWindow) {
                if (focusedWindow) focusedWindow.reload();
    
    James Long's avatar
    James Long committed
            },
            {
              label: 'Toggle Developer Tools',
              accelerator:
                process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
              click(item, focusedWindow) {
                if (focusedWindow) focusedWindow.webContents.toggleDevTools();
    
    James Long's avatar
    James Long committed
            },
            isDev && {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'resetzoom',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'zoomin',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'zoomout',
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'togglefullscreen',
            },
          ].filter(x => x),
    
    James Long's avatar
    James Long committed
        },
        {
          label: 'Tools',
          submenu: [
            {
              label: 'Find schedules',
              enabled: false,
    
              click: function (menuItem, focusedWin) {
    
    James Long's avatar
    James Long committed
                focusedWin.webContents.executeJavaScript(
    
                  'window.__actionsForMenu && window.__actionsForMenu.pushModal("schedules-discover")',
    
    James Long's avatar
    James Long committed
                );
    
    James Long's avatar
    James Long committed
            },
    
    James Long's avatar
    James Long committed
        },
        {
          role: 'window',
          submenu: [
            {
    
              role: 'minimize',
            },
          ],
    
    James Long's avatar
    James Long committed
        },
        {
          role: 'help',
          submenu: [
            {
              label: 'Learn More',
              click() {
    
                shell.openExternal('https://actualbudget.org/docs/');
    
    James Long's avatar
    James Long committed
      ];
    
      if (process.platform === 'win32') {
        // Add about to help menu on Windows
        template[template.length - 1].submenu.unshift({
          label: 'About Actual',
          click() {
            ipcMain.emit('show-about');
    
    James Long's avatar
    James Long committed
        });
      } else if (process.platform === 'darwin') {
        const name = app.getName();
        template.unshift({
          label: name,
          submenu: [
            {
              label: 'About Actual',
              click() {
                ipcMain.emit('show-about');
    
    James Long's avatar
    James Long committed
            },
            isDev && {
              label: 'Screenshot',
              click() {
                ipcMain.emit('screenshot');
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
              role: 'services',
    
              submenu: [],
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'hide',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'hideothers',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'unhide',
    
    James Long's avatar
    James Long committed
            },
            {
    
              type: 'separator',
    
    James Long's avatar
    James Long committed
            },
            {
    
              role: 'quit',
            },
          ].filter(x => x),
    
    James Long's avatar
    James Long committed
        });
        // Edit menu.
        let editIdx = template.findIndex(t => t.label === 'Edit');
        template[editIdx].submenu.push(
          {
    
            type: 'separator',
    
    James Long's avatar
    James Long committed
          },
          {
            label: 'Speech',
            submenu: [
              {
    
                role: 'startspeaking',
    
    James Long's avatar
    James Long committed
              },
              {
    
                role: 'stopspeaking',
              },
            ],
          },
    
    James Long's avatar
    James Long committed
        );
        // Window menu.
        let windowIdx = template.findIndex(t => t.role === 'window');
        template[windowIdx].submenu = [
          {
            label: 'Close',
            accelerator: 'CmdOrCtrl+W',
    
            role: 'close',
    
    James Long's avatar
    James Long committed
          },
          {
            label: 'Minimize',
            accelerator: 'CmdOrCtrl+M',
    
            role: 'minimize',
    
    James Long's avatar
    James Long committed
          },
          {
            label: 'Zoom',
    
            role: 'zoom',
    
    James Long's avatar
    James Long committed
          },
          {
    
            type: 'separator',
    
    James Long's avatar
    James Long committed
          },
          {
            label: 'Bring All to Front',
    
            role: 'front',
          },
    
    James Long's avatar
    James Long committed
        ];
      }
    
      return Menu.buildFromTemplate(template);
    }
    
    module.exports = getMenu;