<commit_before>const Config = require('electron-config')
const fs = require('fs')
const notify = require('electron-main-notification')
const path = require('path')
const scpClient = require('scp2')
const shorthash = require('shorthash')





let config = new Config
let {
  app,
  clipboard,
  globalShortcut,
  Menu,
  nativeImage,
  systemPreferences,
  Tray,
} = require('electron')
let screenshotFolder
let tray





function displayPreferences () {
  console.log('Display Preferences Pane')
}

function generateShortlink (filename) {
  return `${getURL()}/${filename}`
}

function getURL () {
  return config.get('url')
}

function getSCPConfig () {
  return {
    host: config.get('host'),
//    port: config.get('port'),
    username: config.get('username'),
    password: <PASSWORD>('password'),
    path: config.get('path'),
  }
}

function handleScreenshot (filename) {
  let filepath = path.resolve(screenshotFolder, filename)
  let fileExt = path.extname(filename)
  let hashedFilename = `${shorthash.unique(filename.replace(fileExt, ''))}${fileExt}`
  let hashedFilepath = path.resolve(app.getPath('temp'), hashedFilename)
  let shortlink = generateShortlink(hashedFilename)

  fs.renameSync(filepath, hashedFilepath)

  scpClient.scp(hashedFilepath, getSCPConfig(), (error => {
    if (error) {
      return console.log('Upload error:', error)
    }

    fs.unlinkSync(hashedFilepath)

    clipboard.writeText(shortlink)

    notify('Screenshot uploaded!', {
      body: 'The screenshot URL has been copied to your clipboard.'
    })
  }))
}

function startScreenshotListener () {
  // MacOS
  fs.watch(screenshotFolder, (type, filename) => {
    if (type === 'change' && filename.indexOf('Screen Shot') === 0) {
      handleScreenshot(filename)
    }
  })

  // Windows
//  globalShortcut.register('PrintScreen', () => {
//    console.log('PrintScreen pressed')
//  })
}





app.on('ready', function () {
  let trayIconPath

  screenshotFolder = app.getPath('desktop')

  app.dock.hide()

  if (process.env.NODE_ENV === 'development') {
    trayIconPath = path.resolve(app.getAppPath(), 'assets', 'tray-iconTemplate.png')
  } else {
    trayIconPath = path.resolve(process.resourcesPath, 'tray-iconTemplate.png')
  }

  let trayIcon = nativeImage.createFromPath(path.resolve(trayIconPath))
  trayIcon.setTemplateImage(true)

  tray = new Tray(trayIcon)
  tray.setToolTip('Eidetica')
  tray.setContextMenu(Menu.buildFromTemplate([
    {
      label: 'Preferences...',
      click: displayPreferences
    },
    { type: 'separator' },
    {
      label: 'Quit',
      click: app.quit
    }
  ]))

  startScreenshotListener()
})<commit_msg>Add support for closing the preferences panel via the close button in the titlebar<commit_after>let {
  app,
  BrowserWindow,
  clipboard,
  globalShortcut,
  Menu,
  nativeImage,
  systemPreferences,
  Tray,
} = require('electron')
const Config = require('electron-config')
const notify = require('electron-main-notification')
const fs = require('fs')
const path = require('path')
const scpClient = require('scp2')
const shorthash = require('shorthash')





let config = new Config
let preferencesPane
let screenshotFolder
let shouldQuitApp = false
let tray





function createPreferencesPane () {
  preferencesPane = new BrowserWindow({
    show: false,
    titleBarStyle: 'hidden',
    transparent: true,
    useContentSize: true,
  })

  preferencesPane.loadURL(path.join('file://', __dirname, 'preferences/', 'index.html'))
  preferencesPane.on('blur', preferencesPane.hide)
  preferencesPane.on('close', event => {
    if (shouldQuitApp) {
      return preferencesPane = null
    }

    event.preventDefault()
    preferencesPane.hide()
  })
}

function generateShortlink (filename) {
  return `${getURL()}/${filename}`
}

function getURL () {
  return config.get('url')
}

function getSCPConfig () {
  return {
    host: config.get('host'),
//    port: config.get('port'),
    username: config.get('username'),
    password: <PASSWORD>('password'),
    path: config.get('path'),
  }
}

function handleScreenshot (filename) {
  let filepath = path.resolve(screenshotFolder, filename)
  let fileExt = path.extname(filename)
  let hashedFilename = `${shorthash.unique(filename.replace(fileExt, ''))}${fileExt}`
  let hashedFilepath = path.resolve(app.getPath('temp'), hashedFilename)
  let shortlink = generateShortlink(hashedFilename)

  fs.renameSync(filepath, hashedFilepath)

  scpClient.scp(hashedFilepath, getSCPConfig(), (error => {
    if (error) {
      return console.log('Upload error:', error)
    }

    fs.unlinkSync(hashedFilepath)

    clipboard.writeText(shortlink)

    notify('Screenshot uploaded!', {
      body: 'The screenshot URL has been copied to your clipboard.'
    })
  }))
}

function showPreferencesPane () {
  if (!preferencesPane) {
    createPreferencesPane()
  }

  preferencesPane.show()
}

function startScreenshotListener () {
  // MacOS
  fs.watch(screenshotFolder, (type, filename) => {
    if (type === 'change' && filename.indexOf('Screen Shot') === 0) {
      handleScreenshot(filename)
    }
  })

  // Windows
//  globalShortcut.register('PrintScreen', () => {
//    console.log('PrintScreen pressed')
//  })
}





app.on('ready', function () {
  let trayIconPath

  // Get the screenshot folder
  screenshotFolder = app.getPath('desktop')

  // Prevent the dock icon from being displayed
  app.dock.hide()

  // Preload the preferences pane in a hidden browser window
  createPreferencesPane()

  // Figure out where the tray icon lives
  if (process.env.NODE_ENV === 'development') {
    trayIconPath = path.resolve(app.getAppPath(), 'assets', 'tray-iconTemplate.png')
  } else {
    trayIconPath = path.resolve(process.resourcesPath, 'tray-iconTemplate.png')
  }

  // Generate the tray icon as a native image
  let trayIcon = nativeImage.createFromPath(path.resolve(trayIconPath))

  // setting the tray icon as a template image means macOS will handle changing it from white to black for us
  trayIcon.setTemplateImage(true)

  tray = new Tray(trayIcon)
  tray.setToolTip('Eidetica')
  tray.setContextMenu(Menu.buildFromTemplate([
    {
      label: 'Preferences...',
      click: showPreferencesPane
    },
    { type: 'separator' },
    {
      label: 'Quit',
      click: app.quit
    }
  ]))

  // Start listening for screenshots to be taken
  startScreenshotListener()
})

app.on('before-quit', () => shouldQuitApp = true)
{'fraction_non_alphanumeric': 0.13766314481044126, 'fraction_numerical': 0.0006215040397762585, 'mean_word_length': 3.8876233864844343, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1471870', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2004, 'n_words': 440}