skypager-project-types-electron-app
Version:
skypager electron app project type
145 lines (114 loc) • 4.43 kB
JavaScript
var path = require('path')
var events = require('events')
var fs = require('fs')
var extend = require('lodash/defaultsDeep')
var Positioner = require('electron-positioner')
export default function create (opts) {
var electron = require('electron')
var app = electron.app
var Tray = electron.Tray
var BrowserWindow = electron.BrowserWindow
if (typeof opts === 'undefined') opts = {dir: app.getAppPath()}
if (typeof opts === 'string') opts = {dir: opts}
if (!opts.dir) opts.dir = app.getAppPath()
if (!(path.isAbsolute(opts.dir))) opts.dir = path.resolve(opts.dir)
if (!opts.index) opts.index = 'file://' + path.join(opts.dir, 'index.html')
if (!opts['window-position']) opts['window-position'] = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter'
if (typeof opts['show-dock-icon'] === 'undefined') opts['show-dock-icon'] = false
// set width/height on opts to be usable before the window is created
opts.width = opts.width || 400
opts.height = opts.height || 400
opts.tooltip = opts.tooltip || ''
var menubar = new events.EventEmitter()
menubar.app = app
// Set / get options
menubar.setOption = function (opt, val) {
opts[opt] = val
}
menubar.getOption = function (opt) {
return opts[opt]
}
return menubar
function appReady () {
if (app.dock && !opts['show-dock-icon']) app.dock.hide()
var iconPath = opts.icon || path.join(opts.dir, 'IconTemplate.png')
if (!fs.existsSync(iconPath)) iconPath = path.join(__dirname, 'example', 'IconTemplate.png') // default cat icon
var cachedBounds // cachedBounds are needed for double-clicked event
var defaultClickEvent = opts['show-on-right-click'] ? 'right-click' : 'click'
menubar.tray = opts.tray || new Tray(iconPath)
menubar.tray.on(defaultClickEvent, clicked)
menubar.tray.on('double-click', clicked)
menubar.tray.setToolTip(opts.tooltip)
if (opts.preloadWindow || opts['preload-window']) {
createWindow()
}
menubar.showWindow = showWindow
menubar.hideWindow = hideWindow
menubar.emit('ready')
function clicked (e, bounds) {
if (e.altKey || e.shiftKey || e.ctrlKey || e.metaKey) return hideWindow()
if (menubar.window && menubar.window.isVisible()) return hideWindow()
cachedBounds = bounds || cachedBounds
showWindow(cachedBounds)
}
function createWindow () {
menubar.emit('create-window')
var defaults = {
show: false,
frame: false
}
var winOpts = extend(opts, defaults)
menubar.window = new BrowserWindow(winOpts)
menubar.positioner = new Positioner(menubar.window)
if (!opts['always-on-top']) {
menubar.window.on('blur', hideWindow)
} else {
menubar.window.on('blur', emitBlur)
}
if (opts['show-on-all-workspaces'] !== false) {
menubar.window.setVisibleOnAllWorkspaces(true)
}
menubar.window.on('close', windowClear)
menubar.window.loadURL(opts.index)
menubar.emit('after-create-window')
}
function showWindow (trayPos) {
if (!menubar.window) {
createWindow()
}
menubar.emit('show')
if (trayPos && trayPos.x !== 0) {
// Cache the bounds
cachedBounds = trayPos
} else if (cachedBounds) {
// Cached value will be used if showWindow is called without bounds data
trayPos = cachedBounds
}
// Default the window to the right if `trayPos` bounds are undefined or null.
var noBoundsPosition = null
if ((trayPos === undefined || trayPos.x === 0) && opts['window-position'].substr(0, 4) === 'tray') {
noBoundsPosition = (process.platform === 'win32') ? 'bottomRight' : 'topRight'
}
var position = menubar.positioner.calculate(noBoundsPosition || opts['window-position'], trayPos)
var x = (opts.x !== undefined) ? opts.x : position.x
var y = (opts.y !== undefined) ? opts.y : position.y
menubar.window.setPosition(x, y)
menubar.window.show()
menubar.emit('after-show')
return
}
function hideWindow () {
if (!menubar.window) return
menubar.emit('hide')
menubar.window.hide()
menubar.emit('after-hide')
}
function windowClear () {
delete menubar.window
menubar.emit('after-close')
}
function emitBlur () {
menubar.emit('focus-lost')
}
}
}