<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict'

const Immutable = require('immutable')
const electron = require('electron')
const Menu = electron.Menu
const dialog = electron.dialog
const app = electron.app
const BrowserWindow = electron.BrowserWindow

// Constants
const appConfig = require('../../js/constants/appConfig')
const appConstants = require('../../js/constants/appConstants')
const windowConstants = require('../../js/constants/windowConstants')
const messages = require('../../js/constants/messages')
const settings = require('../../js/constants/settings')

// State
const {getByTabId} = require('../common/state/tabState')
const tabState = require('../../app/common/state/tabState')
const appStore = require('../../js/stores/appStore')

// Actions
const appActions = require('../../js/actions/appActions')

// Util
const CommonMenu = require('../common/commonMenu')
const {makeImmutable} = require('../common/state/immutableUtil')
const {fileUrl} = require('../../js/lib/appUrlUtil')
const frameStateUtil = require('../../js/state/frameStateUtil')
const menuUtil = require('../common/lib/menuUtil')
const {getSetting} = require('../../js/settings')
const locale = require('../locale')
const platformUtil = require('../common/lib/platformUtil')
const bookmarkUtil = require('../common/lib/bookmarkUtil')
const isDarwin = platformUtil.isDarwin()
const isLinux = platformUtil.isLinux()
const isWindows = platformUtil.isWindows()

let appMenu = null
let closedFrames = new Immutable.OrderedMap()
let lastClosedUrl = null
let currentLocation = null

// Submenu initialization
const createFileSubmenu = () => {
  const submenu = [
    CommonMenu.newTabMenuItem(),
    CommonMenu.newPrivateTabMenuItem(),
    CommonMenu.newPartitionedTabMenuItem(),
    CommonMenu.newWindowMenuItem(),
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('openFile'),
      accelerator: 'CmdOrCtrl+O',
      click: (item, focusedWindow) => {
        dialog.showOpenDialog(focusedWindow, {
          properties: ['openFile', 'multiSelections']
        }, (paths) => {
          if (paths) {
            paths.forEach((path) => {
              appActions.createTabRequested({
                url: fileUrl(path),
                windowId: focusedWindow.id
              })
            })
          }
        })
      }
    }, {
      label: locale.translation('openLocation'),
      accelerator: 'CmdOrCtrl+L',
      click: function (item, focusedWindow) {
        CommonMenu.sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_FOCUS_URL])
      }
    },
    CommonMenu.separatorMenuItem,
    {
      // This should be disabled when no windows are active.
      label: locale.translation('closeTab'),
      accelerator: 'CmdOrCtrl+W',
      click: function () {
        appActions.tabCloseRequested(tabState.TAB_ID_ACTIVE)
      }
    }, {
      // This should be disabled when no windows are active.
      label: locale.translation('closeWindow'),
      accelerator: 'CmdOrCtrl+Shift+W',
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          appActions.closeWindow(focusedWindow.id)
        }
      }
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('savePageAs'),
      accelerator: 'CmdOrCtrl+S',
      click: function (item, focusedWindow) {
        CommonMenu.sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_SAVE])
      }
    }, {
      label: locale.translation('share'),
      submenu: [
        CommonMenu.simpleShareActiveTabMenuItem('emailPageLink', 'email'),
        CommonMenu.separatorMenuItem,
        CommonMenu.simpleShareActiveTabMenuItem('tweetPageLink', 'twitter'),
        CommonMenu.simpleShareActiveTabMenuItem('facebookPageLink', 'facebook'),
        CommonMenu.simpleShareActiveTabMenuItem('pinterestPageLink', 'pinterest'),
        CommonMenu.simpleShareActiveTabMenuItem('googlePlusPageLink', 'googlePlus'),
        CommonMenu.simpleShareActiveTabMenuItem('linkedInPageLink', 'linkedIn'),
        CommonMenu.simpleShareActiveTabMenuItem('bufferPageLink', 'buffer'),
        CommonMenu.simpleShareActiveTabMenuItem('redditPageLink', 'reddit')
      ]
    },
    // Move inside share menu when it's enabled
    CommonMenu.separatorMenuItem,
    CommonMenu.printMenuItem()
  ]

  if (!isDarwin) {
    submenu.push(CommonMenu.separatorMenuItem)
    submenu.push(CommonMenu.quitMenuItem())
  }

  return submenu
}

const createEditSubmenu = () => {
  const submenu = [
    {
      label: locale.translation('undo'),
      accelerator: 'CmdOrCtrl+Z',
      role: 'undo'
    }, {
      label: locale.translation('redo'),
      accelerator: 'Shift+CmdOrCtrl+Z',
      role: 'redo'
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('cut'),
      accelerator: 'CmdOrCtrl+X',
      role: 'cut'
    }, {
      label: locale.translation('copy'),
      accelerator: 'CmdOrCtrl+C',
      role: 'copy'
    }, {
      label: locale.translation('paste'),
      accelerator: 'CmdOrCtrl+V',
      role: 'paste'
    }, {
      label: locale.translation('pasteWithoutFormatting'),
      accelerator: 'Shift+CmdOrCtrl+V',
      click: function (item, focusedWindow) {
        focusedWindow.webContents.pasteAndMatchStyle()
      }
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('delete'),
      accelerator: 'Delete',
      click: function (item, focusedWindow) {
        focusedWindow.webContents.delete()
      }
    }, {
      label: locale.translation('selectAll'),
      accelerator: 'CmdOrCtrl+A',
      role: 'selectall'
    },
    CommonMenu.separatorMenuItem,
    CommonMenu.findOnPageMenuItem(),
    {
      label: locale.translation('findNext'),
      visible: false,
      accelerator: 'CmdOrCtrl+G'
    }, {
      label: locale.translation('findPrevious'),<commit_msg>Do not register accelerator for Delete in Edit parity: see Edit menu in Chrome<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

'use strict'

const Immutable = require('immutable')
const electron = require('electron')
const Menu = electron.Menu
const dialog = electron.dialog
const app = electron.app
const BrowserWindow = electron.BrowserWindow

// Constants
const appConfig = require('../../js/constants/appConfig')
const appConstants = require('../../js/constants/appConstants')
const windowConstants = require('../../js/constants/windowConstants')
const messages = require('../../js/constants/messages')
const settings = require('../../js/constants/settings')

// State
const {getByTabId} = require('../common/state/tabState')
const tabState = require('../../app/common/state/tabState')
const appStore = require('../../js/stores/appStore')

// Actions
const appActions = require('../../js/actions/appActions')

// Util
const CommonMenu = require('../common/commonMenu')
const {makeImmutable} = require('../common/state/immutableUtil')
const {fileUrl} = require('../../js/lib/appUrlUtil')
const frameStateUtil = require('../../js/state/frameStateUtil')
const menuUtil = require('../common/lib/menuUtil')
const {getSetting} = require('../../js/settings')
const locale = require('../locale')
const platformUtil = require('../common/lib/platformUtil')
const bookmarkUtil = require('../common/lib/bookmarkUtil')
const isDarwin = platformUtil.isDarwin()
const isLinux = platformUtil.isLinux()
const isWindows = platformUtil.isWindows()

let appMenu = null
let closedFrames = new Immutable.OrderedMap()
let lastClosedUrl = null
let currentLocation = null

// Submenu initialization
const createFileSubmenu = () => {
  const submenu = [
    CommonMenu.newTabMenuItem(),
    CommonMenu.newPrivateTabMenuItem(),
    CommonMenu.newPartitionedTabMenuItem(),
    CommonMenu.newWindowMenuItem(),
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('openFile'),
      accelerator: 'CmdOrCtrl+O',
      click: (item, focusedWindow) => {
        dialog.showOpenDialog(focusedWindow, {
          properties: ['openFile', 'multiSelections']
        }, (paths) => {
          if (paths) {
            paths.forEach((path) => {
              appActions.createTabRequested({
                url: fileUrl(path),
                windowId: focusedWindow.id
              })
            })
          }
        })
      }
    }, {
      label: locale.translation('openLocation'),
      accelerator: 'CmdOrCtrl+L',
      click: function (item, focusedWindow) {
        CommonMenu.sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_FOCUS_URL])
      }
    },
    CommonMenu.separatorMenuItem,
    {
      // This should be disabled when no windows are active.
      label: locale.translation('closeTab'),
      accelerator: 'CmdOrCtrl+W',
      click: function () {
        appActions.tabCloseRequested(tabState.TAB_ID_ACTIVE)
      }
    }, {
      // This should be disabled when no windows are active.
      label: locale.translation('closeWindow'),
      accelerator: 'CmdOrCtrl+Shift+W',
      click: function (item, focusedWindow) {
        if (focusedWindow) {
          appActions.closeWindow(focusedWindow.id)
        }
      }
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('savePageAs'),
      accelerator: 'CmdOrCtrl+S',
      click: function (item, focusedWindow) {
        CommonMenu.sendToFocusedWindow(focusedWindow, [messages.SHORTCUT_ACTIVE_FRAME_SAVE])
      }
    }, {
      label: locale.translation('share'),
      submenu: [
        CommonMenu.simpleShareActiveTabMenuItem('emailPageLink', 'email'),
        CommonMenu.separatorMenuItem,
        CommonMenu.simpleShareActiveTabMenuItem('tweetPageLink', 'twitter'),
        CommonMenu.simpleShareActiveTabMenuItem('facebookPageLink', 'facebook'),
        CommonMenu.simpleShareActiveTabMenuItem('pinterestPageLink', 'pinterest'),
        CommonMenu.simpleShareActiveTabMenuItem('googlePlusPageLink', 'googlePlus'),
        CommonMenu.simpleShareActiveTabMenuItem('linkedInPageLink', 'linkedIn'),
        CommonMenu.simpleShareActiveTabMenuItem('bufferPageLink', 'buffer'),
        CommonMenu.simpleShareActiveTabMenuItem('redditPageLink', 'reddit')
      ]
    },
    // Move inside share menu when it's enabled
    CommonMenu.separatorMenuItem,
    CommonMenu.printMenuItem()
  ]

  if (!isDarwin) {
    submenu.push(CommonMenu.separatorMenuItem)
    submenu.push(CommonMenu.quitMenuItem())
  }

  return submenu
}

const createEditSubmenu = () => {
  const submenu = [
    {
      label: locale.translation('undo'),
      accelerator: 'CmdOrCtrl+Z',
      role: 'undo'
    }, {
      label: locale.translation('redo'),
      accelerator: 'Shift+CmdOrCtrl+Z',
      role: 'redo'
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('cut'),
      accelerator: 'CmdOrCtrl+X',
      role: 'cut'
    }, {
      label: locale.translation('copy'),
      accelerator: 'CmdOrCtrl+C',
      role: 'copy'
    }, {
      label: locale.translation('paste'),
      accelerator: 'CmdOrCtrl+V',
      role: 'paste'
    }, {
      label: locale.translation('pasteWithoutFormatting'),
      accelerator: 'Shift+CmdOrCtrl+V',
      click: function (item, focusedWindow) {
        focusedWindow.webContents.pasteAndMatchStyle()
      }
    },
    CommonMenu.separatorMenuItem,
    {
      label: locale.translation('delete'),
      click: function (item, focusedWindow) {
        focusedWindow.webContents.delete()
      }
    }, {
      label: locale.translation('selectAll'),
      accelerator: 'CmdOrCtrl+A',
      role: 'selectall'
    },
    CommonMenu.separatorMenuItem,
    CommonMenu.findOnPageMenuItem(),
    {
      label: locale.translation('findNext'),
      visible: false,
      accelerator: 'CmdOrCtrl+G'
    }, {
      label: locale.translation('findPrevious'),
{'fraction_non_alphanumeric': 0.13487332339791355, 'fraction_numerical': 0.000662361318099023, 'mean_word_length': 3.162301860785665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2837790', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3393, 'n_words': 668}