<commit_before>import path from 'path';
import NylasStore from 'nylas-store';
import {remote, ipcRenderer} from 'electron';
import {UnreadBadgeStore, CanvasUtils} from 'nylas-exports';
const NativeImage = remote.require('native-image');
const Menu = remote.require('menu');
const {canvasWithSystemTrayIconAndText} = CanvasUtils;

// Must be absolute real system path
// https://github.com/atom/electron/issues/1299
const BASE_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas.png');
const UNREAD_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas-unread.png');

const _menuTemplate = [
  {
    label: 'New Message',
    click: ()=> ipcRenderer.send('command', 'application:new-message'),
  },
  {
    label: 'Preferences',
    click: ()=> ipcRenderer.send('command', 'application:open-preferences'),
  },
  {
    type: 'separator',
  },
  {
    label: 'Quit N1',
    click: ()=> ipcRenderer.send('command', 'application:quit'),
  },
];

if (process.platform !== 'win32') {
  _menuTemplate.unshift({
    label: 'Open Inbox',
    click: ()=> ipcRenderer.send('command', 'application:show-main-window'),
  });
}

const _buildMenu = ()=> {
  return Menu.buildFromTemplate(_menuTemplate);
};

class TrayStore extends NylasStore {

  constructor(platform) {
    super();
    this._platform = platform;

    this._unreadIcon = NativeImage.createFromPath(UNREAD_ICON_PATH);
    this._baseIcon = NativeImage.createFromPath(BASE_ICON_PATH);
    this._menu = _buildMenu();
    this._icon = this._getIconImg();

    this._unreadString = new Number(UnreadBadgeStore.count()).toLocaleString();
    this.listenTo(UnreadBadgeStore, this._onUnreadCountChanged);
  }

  icon() {
    return this._icon;
  }

  tooltip() {
    return `${this._unreadString} unread messages`;
  }

  menu() {
    return this._menu;
  }

  _getIconImg() {
    const imgHandlers = {
      'darwin': ()=> {
        const img = new Image();<commit_msg>fix(tray): Order of operations causing exception<commit_after>import path from 'path';
import NylasStore from 'nylas-store';
import {remote, ipcRenderer} from 'electron';
import {UnreadBadgeStore, CanvasUtils} from 'nylas-exports';
const NativeImage = remote.require('native-image');
const Menu = remote.require('menu');
const {canvasWithSystemTrayIconAndText} = CanvasUtils;

// Must be absolute real system path
// https://github.com/atom/electron/issues/1299
const BASE_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas.png');
const UNREAD_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas-unread.png');

const _menuTemplate = [
  {
    label: 'New Message',
    click: ()=> ipcRenderer.send('command', 'application:new-message'),
  },
  {
    label: 'Preferences',
    click: ()=> ipcRenderer.send('command', 'application:open-preferences'),
  },
  {
    type: 'separator',
  },
  {
    label: 'Quit N1',
    click: ()=> ipcRenderer.send('command', 'application:quit'),
  },
];

if (process.platform !== 'win32') {
  _menuTemplate.unshift({
    label: 'Open Inbox',
    click: ()=> ipcRenderer.send('command', 'application:show-main-window'),
  });
}

const _buildMenu = ()=> {
  return Menu.buildFromTemplate(_menuTemplate);
};

class TrayStore extends NylasStore {

  constructor(platform) {
    super();
    this._platform = platform;

    this._unreadIcon = NativeImage.createFromPath(UNREAD_ICON_PATH);
    this._unreadString = new Number(UnreadBadgeStore.count()).toLocaleString();
    this._baseIcon = NativeImage.createFromPath(BASE_ICON_PATH);
    this._menu = _buildMenu();
    this._icon = this._getIconImg();

    this.listenTo(UnreadBadgeStore, this._onUnreadCountChanged);
  }

  icon() {
    return this._icon;
  }

  tooltip() {
    return `${this._unreadString} unread messages`;
  }

  menu() {
    return this._menu;
  }

  _getIconImg() {
    const imgHandlers = {
      'darwin': ()=> {
        const img = new Image();
{'fraction_non_alphanumeric': 0.1585518102372035, 'fraction_numerical': 0.003495630461922597, 'mean_word_length': 4.216145833333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '6606588', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1321, 'n_words': 246}