options all the things

This commit is contained in:
Shelley Vohr 2018-02-20 11:10:53 -05:00
parent 56f06187d5
commit fb7fb4972d
No known key found for this signature in database
GPG key ID: F13993A75599653C
2 changed files with 22 additions and 36 deletions

View file

@ -43,50 +43,32 @@ Menu.prototype._init = function () {
} }
} }
Menu.prototype.popup = function (window, options, callback) { Menu.prototype.popup = function (options) {
let x, y, pos let {window, x, y, positionItem, callback} = options
let [win, opts, cb] = [window, options, callback]
if (!opts && !callback) {
// win.popup({opts})
if (typeof window !== 'function') {
opts = window
// win.popup(callback)
} else {
callback = window
}
}
if (typeof opts === 'object') {
[x, y, pos] = [opts.x, opts.y, opts.newPosition]
}
// win.popup(win, callback)
if (typeof opts === 'function') cb = opts
// no callback passed // no callback passed
if (!cb || typeof cb !== 'function') cb = () => {} if (!callback || typeof callback !== 'function') callback = () => {}
// set defaults // set defaults
if (typeof x !== 'number') x = -1 if (typeof x !== 'number') x = -1
if (typeof y !== 'number') y = -1 if (typeof y !== 'number') y = -1
if (typeof pos !== 'number') pos = -1 if (typeof positionItem !== 'number') positionItem = -1
if (!win || (win && win.constructor !== BrowserWindow)) { if (!window || (window && window.constructor !== BrowserWindow)) {
win = BrowserWindow.getFocusedWindow() window = BrowserWindow.getFocusedWindow()
// No window focused? // No window focused?
if (!win) { if (!window) {
const wins = BrowserWindow.getAllWindows() const wins = BrowserWindow.getAllWindows()
if (wins && wins.length > 0) { if (wins && wins.length > 0) {
win = wins[0] window = wins[0]
} else { } else {
throw new Error(`Cannot open Menu without a BrowserWindow present`) throw new Error(`Cannot open Menu without a BrowserWindow present`)
} }
} }
} }
this.popupAt(win, x, y, pos, cb) this.popupAt(window, x, y, positionItem, callback)
return { browserWindow: win, x, y, position: pos } return { browserWindow: window, x, y, position: positionItem }
} }
Menu.prototype.closePopup = function (window) { Menu.prototype.closePopup = function (window) {

View file

@ -344,7 +344,11 @@ describe('Menu module', () => {
}) })
it('returns immediately', () => { it('returns immediately', () => {
const { browserWindow, x, y } = menu.popup(w, {x: 100, y: 101}) const { browserWindow, x, y } = menu.popup({
window: w,
x: 100,
y: 101
})
assert.equal(browserWindow, w) assert.equal(browserWindow, w)
assert.equal(x, 100) assert.equal(x, 100)
@ -360,7 +364,12 @@ describe('Menu module', () => {
}) })
it('works with a given BrowserWindow, options and callback', (done) => { it('works with a given BrowserWindow, options and callback', (done) => {
const {x, y} = menu.popup(w, {x: 100, y: 101}, () => done()) const {x, y} = menu.popup({
window: w,
x: 100,
y: 101,
callback: () => done()
})
assert.equal(x, 100) assert.equal(x, 100)
assert.equal(y, 101) assert.equal(y, 101)
@ -368,12 +377,7 @@ describe('Menu module', () => {
}) })
it('works with a given BrowserWindow, no options, and a callback', (done) => { it('works with a given BrowserWindow, no options, and a callback', (done) => {
menu.popup(w, () => done()) menu.popup({window: w, callback: () => done()})
menu.closePopup()
})
it('calls the callback', (done) => {
menu.popup({}, () => done())
menu.closePopup() menu.closePopup()
}) })
}) })