dialog

显示用于打开和保存文件,alert框等的原生的系统对话框

进程:Main

对话框例子,展示了选择文件和目录:

const {dialog} = require('electron')
console.log(dialog.showOpenDialog({properties: ['openFile', 'openDirectory', 'multiSelections']}))

对话框从 Electron 的主线程打开。如果要从渲染器进程使用对话框 对象,记得使用 remote 访问它:

const {dialog} = require('electron').remote
console.log(dialog)

方法

dialog模块有以下方法:

dialog.showOpenDialog([browserWindow, ]options[, callback])

  • browserWindowBrowserWindow (可选)
  • optionsObject
    • titleString (可选)
    • defaultPathString (可选)
    • buttonLabelString (可选) - Custom label for the confirmation button, when left empty the default label will be used.
    • filtersFileFilter[](optional)
    • propertiesString[] (可选) - Contains which features the dialog should use. The following values are supported:
      • openFile- Allow files to be selected.
      • openDirectory- Allow directories to be selected.
      • multiSelections- Allow multiple paths to be selected.
      • showHiddenFiles- Show hidden files in dialog.
      • createDirectorymacOS- Allow creating new directories from dialog.
      • promptToCreateWindows- Prompt for creation if the file path entered in the dialog does not exist. This does not actually create the file at the path but allows non-existent paths to be returned that should be created by the application.
    • normalizeAccessKeysBoolean (可选) - Normalize the keyboard access keys across platforms. Default isfalse. Enabling this assumes&is used in the button labels for the placement of the keyboard shortcut access key and labels will be converted so they work correctly on each platform,&characters are removed on macOS, converted to_on Linux, and left untouched on Windows. For example, a button label ofVie&wwill be converted toVie_won Linux andViewon macOS and can be selected viaAlt-Won Windows and Linux.
  • callbackFunction (可选)
    • filePathsString[] - An array of file paths chosen by the user

成功使用这个方法的话,就返回一个可供用户选择的文件路径数组,失败返回undefined

browserWindow参数允许对话框将自身附加到父窗口,使其成为模态。

filters当需要限定用户的行为的时候,指定一个文件数组给用户展示或选择。例如:

{
  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'不正确)。展示全部文件的话,使用'*'通配符 (不支持其他通配符)。

如果callback被调用,将异步调用 API ,并且结果将用过callback(filenames)展示。

注意:在 Windows 和 Linux ,一个打开的 dialog 不能既是文件选择框又是目录选择框, 所以如果在这些平台上设置properties的值为['openFile', 'openDirectory'],将展示一个目录选择框。

dialog.showSaveDialog([browserWindow, ]options[, callback])

  • browserWindowBrowserWindow (可选)
  • optionsObject
    • titleString (可选)
    • defaultPathString (可选)
    • buttonLabelString (可选) - Custom label for the confirmation button, when left empty the default label will be used.
    • filtersFileFilter[](optional)
  • callbackFunction (可选)
    • filenameString

成功使用这个方法的话,就返回一个可供用户选择的文件路径数组,失败返回undefined

browserWindow参数允许对话框将自身附加到父窗口,使其成为模态。

filters指定展示一个文件类型数组, 例子dialog.showOpenDialog

如果callback被调用, 将异步调用 API ,并且结果将用过callback(filenames)展示。

dialog.showMessageBox([browserWindow, ]options[, callback])

  • browserWindowBrowserWindow (可选)
  • optionsObject
    • typeString - 可以是"none","info","error","question""warning". 在 Windows, "question" 与 "info" 展示图标相同, 除非你使用 "icon" 参数.
    • buttonsString[]- (可选) - 按钮上文字的数组,在 Windows 系统中,空数组在按钮上会显示 “OK”.
    • defaultIdInteger (可选) - 在 message box 对话框打开的时候,设置默认选中的按钮,值为在 buttons 数组中的索引.
    • titleString (可选) - message box 的标题,一些平台不显示.
    • messageString (可选) - message box 的内容.
    • detailString (可选)- 额外信息.
    • checkboxLabelString (可选) - 如果有该参数,message box 中会显示一个 checkbox 复选框,它的勾选状态可以在callback回调方法中获取。
    • checkboxCheckedBoolean (可选) - checkbox 的初始值,默认为false.
    • iconNativeImage(可选)
    • cancelIdInteger - 当用户不是通过按钮而是使用其他方式关闭对话框时,比如按Esc键,就返回该值.默认值为对应 "cancel" 或 "no" 标签 button 的索引值, 如果没有这种 button,就返回0. 该选项在 Windows 上无效.
    • noLinkBoolean(可选) - 在 Windows 系统中,Electron 将尝试识别哪个button 是普通 button (如 "Cancel" 或 "Yes"), 然后在对话框中以链接命令(command links)方式展现其它的 button . 这能让对话框展示得很炫酷.如果你不喜欢这种效果,你可以设置noLinktrue.
  • callbackFunction (可选)
    • responseNumber - 被点击按钮的索引值。
    • checkboxCheckedBoolean - 如果设置了checkboxLabel,会显示 checkbox 的选中状态,否则显示false

返回Integer,如果提供了回调,它会返回点击的按钮的索引或者 undefined 。

显示 message box 时, 它会阻塞进程,直到 message box 关闭为止.返回点击按钮的索引值。

browserWindow参数允许对话框将自身附加到父窗口,使其成为模态。

如果callback被调用, 将异步调用 API ,并且结果将用过callback(response)展示。

dialog.showErrorBox(title, content)

  • titleString - 错误框中的标题
  • contentString - 错误框中的内容

展示一个传统的包含错误信息的对话框.

app模块触发ready事件之前,这个 api 可以被安全调用,通常它被用来在启动的早期阶段报告错误. 在 Linux 上,如果在app模块触发ready事件之前调用,message 将会被触发显示 stderr ,并且没有实际 GUI 框显示.

Sheets

在 macOS 上,如果你想像 sheets 一样展示对话框,只需要在browserWindow参数中提供一个BrowserWindow的引用对象.,如果没有则为模态窗口。

你可以调用BrowserWindow.getCurrentWindow().setSheetOffset(offset)来改变 sheets 的窗口框架的偏移量。

results matching ""

    No results matching ""