<commit_before>// This helper remembers the size and position of your windows (and restores
// them in that place after app relaunch).
// Can be used for more than one window, just construct many
// instances of it and give each different name.

import { app, BrowserWindow, screen } from 'electron';
import jetpack from 'fs-jetpack';

export default function (name, options) {

  var userDataDir = jetpack.cwd(app.getPath('userData'));
  var stateStoreFile = 'window-state-' + name +'.json';
  var defaultSize = {
    width: options.width,
    height: options.height
  };
  var state = {};
  var win;

  var restore = function () {
    var restoredState = {};
    try {
      restoredState = userDataDir.read(stateStoreFile, 'json');
    } catch (err) {
      // For some reason json can't be read (might be corrupted).
      // No worries, we have defaults.
    }
    return Object.assign({}, defaultSize, restoredState);
  };

  var getCurrentPosition = function () {
    var position = win.getPosition();
    var size = win.getSize();
    return {
      x: position[0],
      y: position[1],
      width: size[0],
      height: size[1]
    };
  };

  var windowWithinBounds = function (windowState, bounds) {
    return windowState.x >= bounds.x &&
            windowState.y >= bounds.y &&
            windowState.x + windowState.width <= bounds.x + bounds.width &&
            windowState.y + windowState.height <= bounds.y + bounds.height;
  };

  var resetToDefaults = function () {
    var bounds = screen.getPrimaryDisplay().bounds;
    return Object.assign({}, defaultSize, {
      x: (bounds.width - defaultSize.width) / 2,
      y: (bounds.height - defaultSize.height) / 2
    });
  };

  var ensureVisibleOnSomeDisplay = function (windowState) {
    var visible = screen.getAllDisplays().some(function (display) {
      return windowWithinBounds(windowState, display.bounds);
    });
    if (!visible) {
      // Window is partially or fully not visible now.
      // Reset it to safe defaults.
      return resetToDefaults(windowState);
    }
    return windowState;
  };

  var saveState = function () {
    if (!win.isMinimized()) {
      Object.assign(state, getCurrentPosition());
    }
    userDataDir.write(stateStoreFile, state, { atomic: true });
  };

  state = ensureVisibleOnSomeDisplay(restore());

  win = new BrowserWindow(Object.assign({}, options, state));

  win.on('close', saveState);

  return win;
}
<commit_msg>style(main/window): update to ES6 style<commit_after>// This helper remembers the size and position of your windows (and restores
// them in that place after app relaunch).
// Can be used for more than one window, just construct many
// instances of it and give each different name.

import { app, BrowserWindow, screen } from 'electron';
import jetpack from 'fs-jetpack';

export default function (name, options) {
  const userDataDir = jetpack.cwd(app.getPath('userData'));
  const stateStoreFile = 'window-state-' + name +'.json';
  var defaultSize = {
    width: options.width,
    height: options.height
  };
  let state = {};
  let win;

  var restore = function () {
    let restoredState = {};
    try {
      restoredState = userDataDir.read(stateStoreFile, 'json');
    } catch (err) {
      // For some reason json can't be read (might be corrupted).
      // No worries, we have defaults.
    }
    return Object.assign({}, defaultSize, restoredState);
  };

  const getCurrentPosition = function () {
    const [x, y] = win.getPosition();
    const [width, height] = win.getSize();

    return {
      x,
      y,
      width,
      height,
    };
  };

  const windowWithinBounds = function (windowState, bounds) {
    return windowState.x >= bounds.x &&
            windowState.y >= bounds.y &&
            windowState.x + windowState.width <= bounds.x + bounds.width &&
            windowState.y + windowState.height <= bounds.y + bounds.height;
  };

  var resetToDefaults = function () {
    const { bounds } = screen.getPrimaryDisplay();
    return Object.assign({}, defaultSize, {
      x: (bounds.width - defaultSize.width) / 2,
      y: (bounds.height - defaultSize.height) / 2
    });
  };

  const ensureVisibleOnSomeDisplay = function (windowState) {
    const visible = screen.getAllDisplays().some(function (display) {
      return windowWithinBounds(windowState, display.bounds);
    });
    if (!visible) {
      // Window is partially or fully not visible now.
      // Reset it to safe defaults.
      return resetToDefaults(windowState);
    }
    return windowState;
  };

  const saveState = function () {
    if (!win.isMinimized()) {
      Object.assign(state, getCurrentPosition());
    }
    userDataDir.write(stateStoreFile, state, { atomic: true });
  };

  state = ensureVisibleOnSomeDisplay(restore());

  win = new BrowserWindow(Object.assign({}, options, state));

  win.on('close', saveState);

  return win;
}

{'fraction_non_alphanumeric': 0.12686110544564552, 'fraction_numerical': 0.001835610850499694, 'mean_word_length': 2.926341072858287, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5256336', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1398, 'n_words': 416}