<commit_before>"use strict";

/* eslint no-undef:0 */
const { app, BrowserWindow, nativeImage, ipcMain } = require("electron");
const path = require("path");
const url = require("url");
const fs = require("fs");

const setupEvents = require("./installer/setup-events");
if (setupEvents.handleSquirrelEvent()) {
  // squirrel event handled and app will exit in 1000ms, so don't do anything else
  return;
}

// Setup file handlers
var osxFile;
app.on("open-file", (event, filePath) => {
  if (win && win.webContents && !win.webContents.isLoading()) {
    win.webContents.send("file-found", filePath);
  } else {
    osxFile = filePath;
  }
});

let win;

let image = createNativeImage("./img/icon.png");
if (app.dock) {
  app.dock.setIcon(image);
}

function createNativeImage(relativePath) {
  return nativeImage.createFromPath(path.join(__dirname, relativePath));
}
function setThumbarState(state) {
  if (!win) {
    return;
  }

  let buttonFlags = state == "ended" ? ["disabled"] : ["enabled"];

  let buttons = [];
  buttons.push({
    tooltip: "Previous song",
    icon: createNativeImage("./img/electron/previous-song.png"),
    click() {
      win.webContents.send("request-video-action", "previous-song");
    },
    flags: buttonFlags,
  });

  if (state == "play") {
    buttons.push({
      tooltip: "Pause",
      icon: createNativeImage("./img/electron/pause.png"),
      click() {
        win.webContents.send("request-video-action", "pause");
      }
    });
  } else {
    buttons.push({
      tooltip: "Play",
      icon: createNativeImage("./img/electron/play.png"),
      click() {
        win.webContents.send("request-video-action", "play");
      },
      flags: buttonFlags,
    });
  }

  buttons.push({
    tooltip: "Next song",
    icon: createNativeImage("./img/electron/next-song.png"),
    click() {
      win.webContents.send("request-video-action", "previous-song");
    },
    flags: buttonFlags,
  });

  win.setThumbarButtons(buttons);
}

function onMediaStateChange(event, state) {
  if (app.dock) {
    let badge;
    switch (state) {
      case "pause":
        badge = "❚ ❚";
        break;
      case "play":
        badge = "▶";
        break;
      case "ended":
        badge = "";
        break;
    }
    app.dock.setBadge(badge);
  }
  if (win.setThumbarButtons) {
    setThumbarState(state);
  }
}

function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    minWidth: 290,
    minHeight: 290,
    icon: __dirname + "/img/icon.png",
    title: "Media Player",
    hasShadow: true,
    frame: false,
    center: true,
    titleBarStyle: "hidden-inset"
  });
  win.setMenu(null);
  win.loadURL(url.format({
    pathname: path.join(__dirname, "./index.html"),
    protocol: "file:",
    slashes: true,
  }));

  win.webContents.once("did-stop-loading", () => {
    if (process.platform == "win32") {
      let fileDesc = fs.lstatSync(process.argv[process.argv.length - 1]);
      if (fileDesc.isFile()) {
        var openedFilePath = process.argv[process.argv.length - 1];
        win.webContents.send("file-found", openedFilePath);
      }
    } else if (osxFile) {
      win.webContents.send("file-found", osxFile);
    }
    ipcMain.on("add-recent-file", (_, mediaPath) => app.addRecentDocument(mediaPath));
    ipcMain.on("media-state-change", onMediaStateChange);
  });

  // Close handler
  win.on("closed", () => {
    win = null;
    app.dock.setBadge("");
    ipcMain.removeAllListeners();
  });
}

app.on("ready", createWindow);

app.on("window-all-closed", () => app.quit());

app.on("activate", () => (win === null) ? createWindow() : 0);
<commit_msg>fix a close handler bug<commit_after>"use strict";

/* eslint no-undef:0 */
const { app, BrowserWindow, nativeImage, ipcMain } = require("electron");
const path = require("path");
const url = require("url");
const fs = require("fs");

const setupEvents = require("./installer/setup-events");
if (setupEvents.handleSquirrelEvent()) {
  // squirrel event handled and app will exit in 1000ms, so don't do anything else
  return;
}

// Setup file handlers
var osxFile;
app.on("open-file", (event, filePath) => {
  if (win && win.webContents && !win.webContents.isLoading()) {
    win.webContents.send("file-found", filePath);
  } else {
    osxFile = filePath;
  }
});

let win;

let image = createNativeImage("./img/icon.png");
if (app.dock) {
  app.dock.setIcon(image);
}

function createNativeImage(relativePath) {
  return nativeImage.createFromPath(path.join(__dirname, relativePath));
}
function setThumbarState(state) {
  if (!win) {
    return;
  }

  let buttonFlags = state == "ended" ? ["disabled"] : ["enabled"];

  let buttons = [];
  buttons.push({
    tooltip: "Previous song",
    icon: createNativeImage("./img/electron/previous-song.png"),
    click() {
      win.webContents.send("request-video-action", "previous-song");
    },
    flags: buttonFlags,
  });

  if (state == "play") {
    buttons.push({
      tooltip: "Pause",
      icon: createNativeImage("./img/electron/pause.png"),
      click() {
        win.webContents.send("request-video-action", "pause");
      }
    });
  } else {
    buttons.push({
      tooltip: "Play",
      icon: createNativeImage("./img/electron/play.png"),
      click() {
        win.webContents.send("request-video-action", "play");
      },
      flags: buttonFlags,
    });
  }

  buttons.push({
    tooltip: "Next song",
    icon: createNativeImage("./img/electron/next-song.png"),
    click() {
      win.webContents.send("request-video-action", "previous-song");
    },
    flags: buttonFlags,
  });

  win.setThumbarButtons(buttons);
}

function onMediaStateChange(event, state) {
  if (app.dock) {
    let badge;
    switch (state) {
      case "pause":
        badge = "❚ ❚";
        break;
      case "play":
        badge = "▶";
        break;
      case "ended":
        badge = "";
        break;
    }
    app.dock.setBadge(badge);
  }
  if (win.setThumbarButtons) {
    setThumbarState(state);
  }
}

function createWindow() {
  win = new BrowserWindow({
    width: 800,
    height: 600,
    minWidth: 290,
    minHeight: 290,
    icon: __dirname + "/img/icon.png",
    title: "Media Player",
    hasShadow: true,
    frame: false,
    center: true,
    titleBarStyle: "hidden-inset"
  });
  win.setMenu(null);
  win.loadURL(url.format({
    pathname: path.join(__dirname, "./index.html"),
    protocol: "file:",
    slashes: true,
  }));

  win.webContents.once("did-stop-loading", () => {
    if (process.platform == "win32") {
      let fileDesc = fs.lstatSync(process.argv[process.argv.length - 1]);
      if (fileDesc.isFile()) {
        var openedFilePath = process.argv[process.argv.length - 1];
        win.webContents.send("file-found", openedFilePath);
      }
    } else if (osxFile) {
      win.webContents.send("file-found", osxFile);
    }
    ipcMain.on("add-recent-file", (_, mediaPath) => app.addRecentDocument(mediaPath));
    ipcMain.on("media-state-change", onMediaStateChange);
  });

  // Close handler
  win.on("closed", () => {
    win = null;
    if (app.dock) {
      app.dock.setBadge("");
      ipcMain.removeAllListeners();
    }
  });
}

app.on("ready", createWindow);

app.on("window-all-closed", () => app.quit());

app.on("activate", () => (win === null) ? createWindow() : 0);

{'fraction_non_alphanumeric': 0.17618917441224713, 'fraction_numerical': 0.006014215418261345, 'mean_word_length': 2.9766304347826087, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 15, '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': '6209624', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2432, 'n_words': 472}