<commit_before>path = require 'path'
_ = require 'underscore'
{Disposable} = require 'event-kit'
{shell, ipcRenderer, remote} = require 'electron'
fs = require 'fs-plus'
url = require 'url'

# Handles low-level events related to the window.
module.exports =
class WindowEventHandler
  constructor: ->
    @unloadCallbacks = []

    _.defer =>
      @showDevModeMessages()

    ipcRenderer.on 'update-available', (event, detail) ->
      NylasEnv.updateAvailable(detail)

    ipcRenderer.on 'browser-window-focus', ->
      document.body.classList.remove('is-blurred')
      window.dispatchEvent(new Event('browser-window-focus'))

    ipcRenderer.on 'browser-window-blur', ->
      document.body.classList.add('is-blurred')
      window.dispatchEvent(new Event('browser-window-blur'))

    ipcRenderer.on 'command', (event, command, args...) ->
      NylasEnv.commands.dispatch(command, args[0])

    ipcRenderer.on 'scroll-touch-begin', ->
      window.dispatchEvent(new Event('scroll-touch-begin'))

    ipcRenderer.on 'scroll-touch-end', ->
      window.dispatchEvent(new Event('scroll-touch-end'))

    window.onbeforeunload = =>
      # Don't hide the window here if we don't want the renderer process to be
      # throttled in case more work needs to be done before closing

      # In Electron, returning any value other than undefined cancels the close.
      canCloseWindow = @runUnloadCallbacks()
      return undefined if canCloseWindow
      return false

    window.onunload = =>
      NylasEnv.storeWindowDimensions()
      NylasEnv.saveStateAndUnloadWindow()

    NylasEnv.commands.add document.body, 'window:toggle-full-screen', ->
      NylasEnv.toggleFullScreen()

    NylasEnv.commands.add document.body, 'window:close', ->
      NylasEnv.close()
<commit_msg>fix(window-state): Per electron#7278, save state in beforeunload<commit_after>path = require 'path'
_ = require 'underscore'
{Disposable} = require 'event-kit'
{shell, ipcRenderer, remote} = require 'electron'
fs = require 'fs-plus'
url = require 'url'

# Handles low-level events related to the window.
module.exports =
class WindowEventHandler
  constructor: ->
    @unloadCallbacks = []

    _.defer =>
      @showDevModeMessages()

    ipcRenderer.on 'update-available', (event, detail) ->
      NylasEnv.updateAvailable(detail)

    ipcRenderer.on 'browser-window-focus', ->
      document.body.classList.remove('is-blurred')
      window.dispatchEvent(new Event('browser-window-focus'))

    ipcRenderer.on 'browser-window-blur', ->
      document.body.classList.add('is-blurred')
      window.dispatchEvent(new Event('browser-window-blur'))

    ipcRenderer.on 'command', (event, command, args...) ->
      NylasEnv.commands.dispatch(command, args[0])

    ipcRenderer.on 'scroll-touch-begin', ->
      window.dispatchEvent(new Event('scroll-touch-begin'))

    ipcRenderer.on 'scroll-touch-end', ->
      window.dispatchEvent(new Event('scroll-touch-end'))

    window.onbeforeunload = =>
      # Don't hide the window here if we don't want the renderer process to be
      # throttled in case more work needs to be done before closing

      # In Electron, returning any value other than undefined cancels the close.
      if @runUnloadCallbacks()
        # Good to go! Window will be closing...
        NylasEnv.storeWindowDimensions()
        NylasEnv.saveStateAndUnloadWindow()
        return undefined
      return false

    NylasEnv.commands.add document.body, 'window:toggle-full-screen', ->
      NylasEnv.toggleFullScreen()

    NylasEnv.commands.add document.body, 'window:close', ->
      NylasEnv.close()

{'fraction_non_alphanumeric': 0.12205270457697642, 'fraction_numerical': 0.001664355062413315, 'mean_word_length': 3.4354243542435423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '7126969', 'n_tokens_mistral': 1105, 'n_tokens_neox': 1061, 'n_words': 248}