<commit_before>_ = require 'underscore-plus'
{ipcRenderer, remote, shell} = require 'electron'
ipcHelpers = require './ipc-helpers'
{Disposable} = require 'event-kit'
getWindowLoadSettings = require './get-window-load-settings'

module.exports =
class ApplicationDelegate
  getWindowLoadSettings: -> getWindowLoadSettings()

  open: (params) ->
    ipcRenderer.send('open', params)

  pickFolder: (callback) ->
    responseChannel = "atom-pick-folder-response"
    ipcRenderer.on responseChannel, (event, path) ->
      ipcRenderer.removeAllListeners(responseChannel)
      callback(path)
    ipcRenderer.send("pick-folder", responseChannel)

  getCurrentWindow: ->
    remote.getCurrentWindow()

  closeWindow: ->
    ipcHelpers.call('window-method', 'close')

  getTemporaryWindowState: ->
    ipcHelpers.call('get-temporary-window-state').then (stateJSON) -> JSON.parse(stateJSON)

  setTemporaryWindowState: (state) ->
    ipcHelpers.call('set-temporary-window-state', JSON.stringify(state))

  getWindowSize: ->
    [width, height] = remote.getCurrentWindow().getSize()
    {width, height}

  setWindowSize: (width, height) ->
    ipcHelpers.call('set-window-size', width, height)

  getWindowPosition: ->
    [x, y] = remote.getCurrentWindow().getPosition()
    {x, y}

  setWindowPosition: (x, y) ->
    ipcHelpers.call('set-window-position', x, y)

  centerWindow: ->
    ipcHelpers.call('center-window')

  focusWindow: ->
    ipcHelpers.call('focus-window')

  showWindow: ->
    ipcHelpers.call('show-window')

  hideWindow: ->
    ipcHelpers.call('hide-window')

  reloadWindow: ->
    ipcHelpers.call('window-method', 'reload')

  restartApplication: ->
    ipcRenderer.send("restart-application")

  minimizeWindow: ->
    ipcHelpers.call('window-method', 'minimize')

  isWindowMaximized: ->
    remote.getCurrentWindow().isMaximized()

  maximizeWindow: ->
    ipcHelpers.call('window-method', 'maximize')

  unmaximizeWindow: ->
    ipcHelpers.call('window-method', 'unmaximize')

  isWindowFullScreen: ->
    remote.getCurrentWindow().isFullScreen()

  setWindowFullScreen: (fullScreen=false) ->
    ipcHelpers.call('window-method', 'setFullScreen', fullScreen)

  onDidEnterFullScreen: (callback) ->
    ipcHelpers.on(ipcRenderer, 'did-enter-full-screen', callback)

  onDidLeaveFullScreen: (callback) ->
    ipcHelpers.on(ipcRenderer, 'did-leave-full-screen', callback)

  openWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'openDevTools'))

  closeWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'closeDevTools'))

  toggleWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'toggleDevTools'))

  executeJavaScriptInWindowDevTools: (code) ->
    ipcRenderer.send("execute-javascript-in-dev-tools", code)

  setWindowDocumentEdited: (edited) ->
    ipcHelpers.call('window-method', 'setDocumentEdited', edited)

  setRepresentedFilename: (filename) ->
    ipcHelpers.call('window-method', 'setRepresentedFilename', filename)

  addRecentDocument: (filename) ->
    ipcRenderer.send("add-recent-document", filename)

  setRepresentedDirectoryPaths: (paths) ->
    ipcHelpers.call('window-method', 'setRepresentedDirectoryPaths', paths)

  setAutoHideWindowMenuBar: (autoHide) ->
    ipcHelpers.call('window-method', 'setAutoHideMenuBar', autoHide)

  setWindowMenuBarVisibility: (visible) ->
    remote.getCurrentWindow().setMenuBarVisibility(visible)

  getPrimaryDisplayWorkAreaSize: ->
    remote.screen.getPrimaryDisplay().workAreaSize

  getUserDefault: (key, type) ->
    remote.systemPreferences.getUserDefault(key, type)

  confirm: ({message, detailedMessage, buttons}) ->
    buttons ?= {}
    if _.isArray(buttons)
      buttonLabels = buttons
    else
      buttonLabels = Object.keys(buttons)

    chosen = remote.dialog.showMessageBox(remote.getCurrentWindow(), {
      type: 'info'
      message: message
      detail: detailedMessage
      buttons: buttonLabels
    })

    if _.isArray(buttons)
      chosen
    else
      callback = buttons[buttonLabels[chosen]]
      callback?()

  showMessageDialog: (params) ->

  showSaveDialog: (params) ->
    if typeof params is 'string'<commit_msg>Normalize access keys (shortcuts) on buttons in dialogs<commit_after>_ = require 'underscore-plus'
{ipcRenderer, remote, shell} = require 'electron'
ipcHelpers = require './ipc-helpers'
{Disposable} = require 'event-kit'
getWindowLoadSettings = require './get-window-load-settings'

module.exports =
class ApplicationDelegate
  getWindowLoadSettings: -> getWindowLoadSettings()

  open: (params) ->
    ipcRenderer.send('open', params)

  pickFolder: (callback) ->
    responseChannel = "atom-pick-folder-response"
    ipcRenderer.on responseChannel, (event, path) ->
      ipcRenderer.removeAllListeners(responseChannel)
      callback(path)
    ipcRenderer.send("pick-folder", responseChannel)

  getCurrentWindow: ->
    remote.getCurrentWindow()

  closeWindow: ->
    ipcHelpers.call('window-method', 'close')

  getTemporaryWindowState: ->
    ipcHelpers.call('get-temporary-window-state').then (stateJSON) -> JSON.parse(stateJSON)

  setTemporaryWindowState: (state) ->
    ipcHelpers.call('set-temporary-window-state', JSON.stringify(state))

  getWindowSize: ->
    [width, height] = remote.getCurrentWindow().getSize()
    {width, height}

  setWindowSize: (width, height) ->
    ipcHelpers.call('set-window-size', width, height)

  getWindowPosition: ->
    [x, y] = remote.getCurrentWindow().getPosition()
    {x, y}

  setWindowPosition: (x, y) ->
    ipcHelpers.call('set-window-position', x, y)

  centerWindow: ->
    ipcHelpers.call('center-window')

  focusWindow: ->
    ipcHelpers.call('focus-window')

  showWindow: ->
    ipcHelpers.call('show-window')

  hideWindow: ->
    ipcHelpers.call('hide-window')

  reloadWindow: ->
    ipcHelpers.call('window-method', 'reload')

  restartApplication: ->
    ipcRenderer.send("restart-application")

  minimizeWindow: ->
    ipcHelpers.call('window-method', 'minimize')

  isWindowMaximized: ->
    remote.getCurrentWindow().isMaximized()

  maximizeWindow: ->
    ipcHelpers.call('window-method', 'maximize')

  unmaximizeWindow: ->
    ipcHelpers.call('window-method', 'unmaximize')

  isWindowFullScreen: ->
    remote.getCurrentWindow().isFullScreen()

  setWindowFullScreen: (fullScreen=false) ->
    ipcHelpers.call('window-method', 'setFullScreen', fullScreen)

  onDidEnterFullScreen: (callback) ->
    ipcHelpers.on(ipcRenderer, 'did-enter-full-screen', callback)

  onDidLeaveFullScreen: (callback) ->
    ipcHelpers.on(ipcRenderer, 'did-leave-full-screen', callback)

  openWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'openDevTools'))

  closeWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'closeDevTools'))

  toggleWindowDevTools: ->
    # Defer DevTools interaction to the next tick, because using them during
    # event handling causes some wrong input events to be triggered on
    # `TextEditorComponent` (Ref.: https://github.com/atom/atom/issues/9697).
    new Promise(process.nextTick).then(-> ipcHelpers.call('window-method', 'toggleDevTools'))

  executeJavaScriptInWindowDevTools: (code) ->
    ipcRenderer.send("execute-javascript-in-dev-tools", code)

  setWindowDocumentEdited: (edited) ->
    ipcHelpers.call('window-method', 'setDocumentEdited', edited)

  setRepresentedFilename: (filename) ->
    ipcHelpers.call('window-method', 'setRepresentedFilename', filename)

  addRecentDocument: (filename) ->
    ipcRenderer.send("add-recent-document", filename)

  setRepresentedDirectoryPaths: (paths) ->
    ipcHelpers.call('window-method', 'setRepresentedDirectoryPaths', paths)

  setAutoHideWindowMenuBar: (autoHide) ->
    ipcHelpers.call('window-method', 'setAutoHideMenuBar', autoHide)

  setWindowMenuBarVisibility: (visible) ->
    remote.getCurrentWindow().setMenuBarVisibility(visible)

  getPrimaryDisplayWorkAreaSize: ->
    remote.screen.getPrimaryDisplay().workAreaSize

  getUserDefault: (key, type) ->
    remote.systemPreferences.getUserDefault(key, type)

  confirm: ({message, detailedMessage, buttons}) ->
    buttons ?= {}
    if _.isArray(buttons)
      buttonLabels = buttons
    else
      buttonLabels = Object.keys(buttons)

    chosen = remote.dialog.showMessageBox(remote.getCurrentWindow(), {
      type: 'info'
      message: message
      detail: detailedMessage
      buttons: buttonLabels
      normalizeAccessKeys: true
    })

    if _.isArray(buttons)
      chosen
    else
      callback = buttons[buttonLabels[chosen]]
      callback?()

  showMessageDialog: (params) ->

  showSaveDialog: (params) ->
    if typeof params is 'string'
{'fraction_non_alphanumeric': 0.13110307414104883, 'fraction_numerical': 0.0024110910186859553, 'mean_word_length': 4.179500520291363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 93, 'https://': 6, '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': '6696106', 'n_tokens_mistral': 3030, 'n_tokens_neox': 2988, 'n_words': 572}