-
Notifications
You must be signed in to change notification settings - Fork 870
/
Copy pathdialog.js
50 lines (42 loc) · 1.18 KB
/
dialog.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
const { dialog } = require('electron')
const i18n = require('i18next')
const { IS_MAC } = require('../common/consts')
const dock = require('../utils/dock')
// NOTE: always send the buttons in the order [OK, Cancel, ...Actions].
// See this post for more interesting information about the topic:
// https://medium.muz.li/ok-key-and-cancel-key-which-one-should-be-set-up-on-the-left-4780e86c16eb
module.exports = function ({
title, message, type = 'info', showDock = true, buttons = [
i18n.t('ok'),
i18n.t('cancel')
], ...opts
}) {
const options = {
type: type,
buttons: buttons,
noLink: true,
...opts
}
if (IS_MAC) {
options.message = title
options.detail = message
} else {
options.title = title
options.message = message
}
const isInverse = !IS_MAC
if (isInverse) {
options.buttons.reverse()
}
if (buttons.length > 1) {
options.defaultId = isInverse ? buttons.length - 1 : 0
options.cancelId = isInverse ? buttons.length - 2 : 1
}
if (showDock) dock.show()
const selected = dialog.showMessageBoxSync(options)
if (showDock) dock.hide()
if (!isInverse) {
return selected
}
return buttons.length - selected - 1
}