dialog
显示用于打开和保存文件、警报等的原生系统对话框。
¥Display native system dialogs for opening and saving files, alerting, etc.
进程:主进程
¥Process: Main
显示选择多个文件的对话框的示例:
¥An example of showing a dialog to select multiple files:
const { dialog } = require('electron')
console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] }))
方法
¥Methods
dialog
模块有以下方法:
¥The dialog
module has the following methods:
dialog.showOpenDialogSync([window, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 string[] | undefined
,即用户选择的文件路径;如果对话框被取消,则返回 undefined
。
¥Returns string[] | undefined
, the file paths chosen by the user; if the dialog is cancelled it returns undefined
.
window
参数允许对话框将自身附加到父窗口,使其成为模式。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
filters
指定当你想要将用户限制为特定类型时可以显示或选择的文件类型数组。例如:
¥The filters
specifies an array of file types that can be displayed or
selected when you want to limit the user to a specific type. For example:
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] },
{ name: 'All Files', extensions: ['*'] }
]
}
extensions
数组应包含不带通配符或点的扩展名(例如 'png'
较好,但 '.png'
和 '*.png'
较差)。要显示所有文件,请使用 '*'
通配符(不支持其他通配符)。
¥The extensions
array should contain extensions without wildcards or dots (e.g.
'png'
is good but '.png'
and '*.png'
are bad). To show all files, use the
'*'
wildcard (no other wildcard is supported).
注意:在 Windows 和 Linux 上,打开的对话框不能同时是文件选择器和目录选择器,因此如果在这些平台上将 properties
设置为 ['openFile', 'openDirectory']
,将显示目录选择器。
¥Note: On Windows and Linux an open dialog can not be both a file selector
and a directory selector, so if you set properties
to
['openFile', 'openDirectory']
on these platforms, a directory selector will be
shown.
dialog.showOpenDialogSync(mainWindow, {
properties: ['openFile', 'openDirectory']
})
dialog.showOpenDialog([window, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 Promise<Object>
- 使用包含以下内容的对象进行解析:
¥Returns Promise<Object>
- Resolve with an object containing the following:
-
canceled
布尔值 - 对话框是否被取消。¥
canceled
boolean - whether or not the dialog was canceled. -
filePaths
字符串[] - 用户选择的文件路径数组。如果取消对话框,这将是一个空数组。¥
filePaths
string[] - An array of file paths chosen by the user. If the dialog is cancelled this will be an empty array. -
bookmarks
string[] (可选) macOS mas - 与 Base64 编码字符串的filePaths
数组匹配的数组,其中包含安全范围的书签数据。必须启用securityScopedBookmarks
才能填充此内容。(有关返回值,请参阅 此处的表格。)¥
bookmarks
string[] (optional) macOS MAS - An array matching thefilePaths
array of base64 encoded strings which contains security scoped bookmark data.securityScopedBookmarks
must be enabled for this to be populated. (For return values, see table here.)
window
参数允许对话框将自身附加到父窗口,使其成为模式。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
filters
指定当你想要将用户限制为特定类型时可以显示或选择的文件类型数组。例如:
¥The filters
specifies an array of file types that can be displayed or
selected when you want to limit the user to a specific type. For example:
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] },
{ name: 'All Files', extensions: ['*'] }
]
}
extensions
数组应包含不带通配符或点的扩展名(例如 'png'
较好,但 '.png'
和 '*.png'
较差)。要显示所有文件,请使用 '*'
通配符(不支持其他通配符)。
¥The extensions
array should contain extensions without wildcards or dots (e.g.
'png'
is good but '.png'
and '*.png'
are bad). To show all files, use the
'*'
wildcard (no other wildcard is supported).
注意:在 Windows 和 Linux 上,打开的对话框不能同时是文件选择器和目录选择器,因此如果在这些平台上将 properties
设置为 ['openFile', 'openDirectory']
,将显示目录选择器。
¥Note: On Windows and Linux an open dialog can not be both a file selector
and a directory selector, so if you set properties
to
['openFile', 'openDirectory']
on these platforms, a directory selector will be
shown.
dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'openDirectory']
}).then(result => {
console.log(result.canceled)
console.log(result.filePaths)
}).catch(err => {
console.log(err)
})
dialog.showSaveDialogSync([window, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 string
,即用户选择的文件的路径;如果对话框被取消,它将返回一个空字符串。
¥Returns string
, the path of the file chosen by the user; if the dialog is cancelled it returns an empty string.
window
参数允许对话框将自身附加到父窗口,使其成为模式。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
filters
指定可以显示的文件类型数组,请参阅 dialog.showOpenDialog
的示例。
¥The filters
specifies an array of file types that can be displayed, see
dialog.showOpenDialog
for an example.
dialog.showSaveDialog([window, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 Promise<Object>
- 使用包含以下内容的对象进行解析:
¥Returns Promise<Object>
- Resolve with an object containing the following:
-
canceled
布尔值 - 对话框是否被取消。¥
canceled
boolean - whether or not the dialog was canceled. -
filePath
字符串 - 如果对话框被取消,这将是一个空字符串。¥
filePath
string - If the dialog is canceled, this will be an empty string. -
bookmark
字符串(可选)macOS mas - Base64 编码字符串,其中包含已保存文件的安全范围书签数据。必须启用securityScopedBookmarks
才能出现此情况。(有关返回值,请参阅 此处的表格。)¥
bookmark
string (optional) macOS MAS - Base64 encoded string which contains the security scoped bookmark data for the saved file.securityScopedBookmarks
must be enabled for this to be present. (For return values, see table here.)
window
参数允许对话框将自身附加到父窗口,使其成为模式。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
filters
指定可以显示的文件类型数组,请参阅 dialog.showOpenDialog
的示例。
¥The filters
specifies an array of file types that can be displayed, see
dialog.showOpenDialog
for an example.
注意:在 macOS 上,建议使用异步版本以避免展开和折叠对话框时出现问题。
¥Note: On macOS, using the asynchronous version is recommended to avoid issues when expanding and collapsing the dialog.
dialog.showMessageBoxSync([wndow, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 Integer
- 单击的按钮的索引。
¥Returns Integer
- the index of the clicked button.
显示一个消息框,它将阻塞进程,直到消息框关闭。它返回被单击按钮的索引。
¥Shows a message box, it will block the process until the message box is closed. It returns the index of the clicked button.
window
参数允许对话框将自身附加到父窗口,使其成为模式。如果未显示 window
,则不会附加对话框。在这种情况下,它将显示为独立窗口。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
If window
is not shown dialog will not be attached to it. In such case it will be displayed as an independent window.
dialog.showMessageBox([window, ]options)
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 Promise<Object>
- 解析为包含以下属性的 promise:
¥Returns Promise<Object>
- resolves with a promise containing the following properties:
-
response
数字 - 单击的按钮的索引。¥
response
number - The index of the clicked button. -
checkboxChecked
布尔值 - 如果设置了checkboxLabel
,则复选框的选中状态。否则false
。¥
checkboxChecked
boolean - The checked state of the checkbox ifcheckboxLabel
was set. Otherwisefalse
.
显示消息框。
¥Shows a message box.
window
参数允许对话框将自身附加到父窗口,使其成为模式。
¥The window
argument allows the dialog to attach itself to a parent window, making it modal.
dialog.showErrorBox(title, content)
-
title
字符串 - 错误框中显示的标题。¥
title
string - The title to display in the error box. -
content
字符串 - 错误框中显示的文本内容。¥
content
string - The text content to display in the error box.
显示一个显示错误消息的模式对话框。
¥Displays a modal dialog that shows an error message.
该 API 可以在 app
模块发出 ready
事件之前安全地调用,通常用于在启动初期报告错误。如果在 Linux 上的应用 ready
event 之前调用,该消息将发送到 stderr,并且不会出现 GUI 对话框。
¥This API can be called safely before the ready
event the app
module emits,
it is usually used to report errors in early stage of startup. If called
before the app ready
event on Linux, the message will be emitted to stderr,
and no GUI dialog will appear.
dialog.showCertificateTrustDialog([window, ]options)
macOS Windows
-
window
BaseWindow(可选)¥
window
BaseWindow (optional)
返回 Promise<void>
- 当显示证书信任对话框时解决。
¥Returns Promise<void>
- resolves when the certificate trust dialog is shown.
在 macOS 上,这会显示一个模式对话框,其中显示消息和证书信息,并为用户提供信任/导入证书的选项。如果你提供 window
参数,对话框将附加到父窗口,使其成为模式。
¥On macOS, this displays a modal dialog that shows a message and certificate
information, and gives the user the option of trusting/importing the
certificate. If you provide a window
argument the dialog will be
attached to the parent window, making it modal.
在 Windows 上,由于使用了 Win32 API,选项更加有限:
¥On Windows the options are more limited, due to the Win32 APIs used:
-
不使用
message
参数,因为操作系统提供了自己的确认对话框。¥The
message
argument is not used, as the OS provides its own confirmation dialog. -
window
参数被忽略,因为不可能使此确认对话框成为模态。¥The
window
argument is ignored since it is not possible to make this confirmation dialog modal.
书签数组
¥Bookmarks array
showOpenDialog
、showOpenDialogSync
、showSaveDialog
和 showSaveDialogSync
将返回 bookmarks
数组。
¥showOpenDialog
, showOpenDialogSync
, showSaveDialog
, and showSaveDialogSync
will return a bookmarks
array.
构建类型 | securityScopedBookmarks 布尔值 | 返回类型 | 返回值 |
---|---|---|---|
macOS 系统 | 真的 | 成功 | ['LONGBOOKMARKSTRING'] |
macOS 系统 | 真的 | 错误 | [''] (空字符串数组) |
macOS 系统 | 错误的 | 不适用 | [] (空数组) |
非马斯 | any | 不适用 | [] (空数组) |
床单
¥Sheets
在 macOS 上,如果你在 window
参数中提供 BaseWindow
引用,则对话框将显示为附加到窗口的工作表;如果未提供窗口,则对话框将显示为模态框。
¥On macOS, dialogs are presented as sheets attached to a window if you provide
a BaseWindow
reference in the window
parameter, or modals if no
window is provided.
你可以调用 BaseWindow.getCurrentWindow().setSheetOffset(offset)
来更改距粘贴纸张的窗框的偏移量。
¥You can call BaseWindow.getCurrentWindow().setSheetOffset(offset)
to change
the offset from the window frame where sheets are attached.