<commit_before>'use strict'

let AppActions = require('../actions/app-actions')
let AppDispatcher = require('../dispatcher/app-dispatcher')
let AppConstants = require('../constants/app-constants')
let Store = require('./store')

let app = require('electron').app
let BrowserWindow = require('electron').BrowserWindow

let window
let quitting = false

let WindowStore = Object.assign(new Store('window-store'), {
  with: (f) => {
    if (!window) return
    f(window)
  }
})

function show () {
  if (window) {
    window.show()
    return
  }

  let width = 1200
  let height = 720

  window = new BrowserWindow({
    title: 'itch',
    icon: './static/images/itchio-tray-x4.png',
    width, height,
    center: true,
    show: false,
    'title-bar-style': 'hidden'
  })

  window.on('close', (e) => {
    console.log(`window event: close ${JSON.stringify(e)}`)
    if (quitting) return
    e.preventDefault()
    window.hide()
  })
  window.webContents.on('dom-ready', (e) => {
    console.log(`dom-ready o/`)
    AppActions.window_ready()
    window.show()
  })
  window.loadURL(`file://${__dirname}/../index.html`)

  if (process.env.DEVTOOLS === '1') {
    window.openDevTools()
  }
}

function hide () {
  WindowStore.with(w => w.hide())
}

function _eval (action) {
  if (!window) return
  let web = window.webContents
  if (!web) return
  web.executeJavaScript(action.code)
}

AppDispatcher.register('window-store', Store.action_listeners(on => {
  on(AppConstants.BOOT, show)
  on(AppConstants.FOCUS_WINDOW, show)
  on(AppConstants.HIDE_WINDOW, hide)
  on(AppConstants.EVAL, _eval)
  on(AppConstants.QUIT, () => {
    quitting = true
    app.quit()
  })
  on(AppConstants.CHANGE_USER, () => {
    if (!window) return
    let web = window.webContents
    if (!web) return
    web.executeJavaScript(`
      var yes = window.confirm('Are you sure you want to log out?')
      if (yes) {
        require('./actions/app-actions').logout()
      }
    `)
  })
  console.log(`in window-store done setting up action listeners`)
}))

app.on('before-quit', e => {
  if (!quitting && process.platform === 'darwin') {
    // Hopefully fixes #85
    AppActions.quit()
  }
})

module.exports = WindowStore<commit_msg>Actually closes #109 (proper before-quit handling)<commit_after>'use strict'

let AppActions = require('../actions/app-actions')
let AppDispatcher = require('../dispatcher/app-dispatcher')
let AppConstants = require('../constants/app-constants')
let Store = require('./store')

let app = require('electron').app
let BrowserWindow = require('electron').BrowserWindow

let window
let quitting = false

let WindowStore = Object.assign(new Store('window-store'), {
  with: (f) => {
    if (!window) return
    f(window)
  }
})

function show () {
  if (window) {
    window.show()
    return
  }

  let width = 1200
  let height = 720

  window = new BrowserWindow({
    title: 'itch',
    icon: './static/images/itchio-tray-x4.png',
    width, height,
    center: true,
    show: false,
    'title-bar-style': 'hidden'
  })

  window.on('close', (e) => {
    console.log(`window event: close ${JSON.stringify(e)}`)
    if (quitting) return
    e.preventDefault()
    window.hide()
  })
  window.webContents.on('dom-ready', (e) => {
    console.log(`dom-ready o/`)
    AppActions.window_ready()
    window.show()
  })
  window.loadURL(`file://${__dirname}/../index.html`)

  if (process.env.DEVTOOLS === '1') {
    window.openDevTools()
  }
}

function hide () {
  WindowStore.with(w => w.hide())
}

function _eval (action) {
  if (!window) return
  let web = window.webContents
  if (!web) return
  web.executeJavaScript(action.code)
}

AppDispatcher.register('window-store', Store.action_listeners(on => {
  on(AppConstants.BOOT, show)
  on(AppConstants.FOCUS_WINDOW, show)
  on(AppConstants.HIDE_WINDOW, hide)
  on(AppConstants.EVAL, _eval)
  on(AppConstants.QUIT, () => {
    quitting = true
    app.quit()
  })
  on(AppConstants.CHANGE_USER, () => {
    if (!window) return
    let web = window.webContents
    if (!web) return
    web.executeJavaScript(`
      var yes = window.confirm('Are you sure you want to log out?')
      if (yes) {
        require('./actions/app-actions').logout()
      }
    `)
  })
  console.log(`in window-store done setting up action listeners`)
}))

app.on('before-quit', e => {
  quitting = true
})

module.exports = WindowStore
{'fraction_non_alphanumeric': 0.1655235482395976, 'fraction_numerical': 0.005258344764517604, 'mean_word_length': 3.29342492639843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '1521623', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1452, 'n_words': 314}