forked from max-mapper/menubar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (124 loc) · 4.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var path = require('path')
var events = require('events')
var fs = require('fs')
var electron = require('electron')
var app = electron.app
var Tray = electron.Tray
var BrowserWindow = electron.BrowserWindow
var extend = require('extend')
var Positioner = require('electron-positioner')
module.exports = function create (opts) {
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.windowPosition) opts.windowPosition = (process.platform === 'win32') ? 'trayBottomCenter' : 'trayCenter'
if (typeof opts.showDockIcon === 'undefined') opts.showDockIcon = 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
if (app.isReady()) appReady()
else app.on('ready', appReady)
// 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.showDockIcon) 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.showOnRightClick ? '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)
var supportsTrayHighlightState = false
try {
menubar.tray.setHighlightMode('never')
supportsTrayHighlightState = true
} catch (e) {}
if (opts.preloadWindow) {
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(defaults, opts)
menubar.window = new BrowserWindow(winOpts)
menubar.positioner = new Positioner(menubar.window)
menubar.window.on('blur', function () {
opts.alwaysOnTop ? emitBlur() : hideWindow()
})
if (opts.showOnAllWorkspaces !== false) {
menubar.window.setVisibleOnAllWorkspaces(true)
}
menubar.window.on('close', windowClear)
menubar.window.loadURL(opts.index)
menubar.emit('after-create-window')
}
function showWindow (trayPos) {
if (supportsTrayHighlightState) menubar.tray.setHighlightMode('always')
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
} else if (menubar.tray.getBounds) {
// Get the current tray bounds
trayPos = menubar.tray.getBounds()
}
// Default the window to the right if `trayPos` bounds are undefined or null.
var noBoundsPosition = null
if ((trayPos === undefined || trayPos.x === 0) && opts.windowPosition.substr(0, 4) === 'tray') {
noBoundsPosition = (process.platform === 'win32') ? 'bottomRight' : 'topRight'
}
var position = menubar.positioner.calculate(noBoundsPosition || opts.windowPosition, 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 (supportsTrayHighlightState) menubar.tray.setHighlightMode('never')
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')
}
}
}