dialog(对话框)

[TOC] 跳出系统对话框功能,包含打开、保存文件、提示、警报等。

下面是一个选择多个文件的对话框示例:

let result = await eagle.dialog.showOpenDialog({
    properties: ['openFile', 'multiSelections']
});

方法

showOpenDialog(options)

显示打开文件对话框。

  • options Object
  • title string (可选) - 对话框窗口的标题
  • defaultPath string (可选) - 对话框的默认展示路径
  • buttonLabel string (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。
  • filters FileFilter[] (可选)
    • name string
    • extensions string[]
  • properties string[] (可选) - 包含对话框相关属性。 支持以下属性值:
    • openFile - 允许选择文件
    • openDirectory - 允许选择文件夹
    • multiSelections- 允许多选。
    • showHiddenFiles- 显示对话框中的隐藏文件。
    • createDirectory macOS - 允许你通过对话框的形式创建新的目录。
    • promptToCreate Windows- 如果输入的文件路径在对话框中不存在, 则提示创建。 这并不是真的在路径上创建一个文件,而是允许返回一些不存在的地址交由应用程序去创
  • message string (可选) macOS - 显示在输入框上方的消息。
  • 返回 Promise<result: Object>
  • resultObject
    • canceled boolean - 对话框是否被取消
    • filePaths string[] - 用户选择的文件路径的数组. 如果对话框被取消,这将是一个空的数组。
{
  filters: [
    { name: 'Images', extensions: ['jpg', 'png', 'gif'] },
    { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
    { name: 'Custom File Type', extensions: ['as'] },
    { name: 'All Files', extensions: ['*'] }
  ]
}
let result = await eagle.dialog.showOpenDialog({
    properties: ['openFile', 'openDirectory']
});

说明:备注:此功能与 Electron API 的 dialog.showOpenDialog 功能类似。


showSaveDialog(options)

显示保存文件对话框。

  • options Object
  • title string (可选) - 对话框窗口的标题
  • defaultPath string (可选) - 对话框的默认展示路径
  • buttonLabel string (可选) - 「确认」按钮的自定义标签, 当为空时, 将使用默认标签。
  • filters FileFilter[] (可选)
    • name string
    • extensions string[]
  • properties string[] (可选) - 包含对话框相关属性。 支持以下属性值:
    • openDirectory - 允许选择文件夹
    • showHiddenFiles- 显示对话框中的隐藏文件。
    • createDirectory macOS - 允许你通过对话框的形式创建新的目录。
  • 返回 Promise<result: Object>
  • resultObject
    • canceled boolean - 对话框是否被取消
    • filePath string - 如果对话框被取消,该值为 undefined。
{
  filters: [
    { name: 'Images', extensions: ['jpg', 'png', 'gif'] },
    { name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
    { name: 'Custom File Type', extensions: ['as'] },
    { name: 'All Files', extensions: ['*'] }
  ]
}
let result = await eagle.dialog.showSaveDialog({
    properties: ['openDirectory']
});

说明:备注:此功能与 Electron API 的 dialog.showSaveDialog 功能类似。


showMessageBox(options)

显示讯息对话框。

  • optionsObject
  • message string - 对话框主要内容
  • title string (可选) - 对话框标题
  • detail string (可选) - 额外信息
  • buttons strings[] (可选) - 按钮文本数组
  • type string (可选) - 可以为 none、 info、 error、question 或者 warning
  • 返回 Promise<result: Object>
  • resultObject
    • response Interger - 点击按钮的索引
let result = await eagle.dialog.showMessageBox({
    title: "Messagebox title",
    message: "Message from the Plugin process",
    detail: "Ultra message here",
    buttons: ["确定", "取消"],
    type: "info"
});

console.log(result);        // {response: 0}

说明:此功能与 Electron API 的 dialog.showSaveDialog 功能类似。


showErrorBox(title, content)

显示错误讯息的对话框。

  • title string - 显示在错误框中的标题
  • content string - 显示在错误框中的文本内容
  • 返回 Promise<void>
await eagle.dialog.showErrorBox("Error box title", "Error message from the Plugin process");

说明:备注:此功能与 Electron API 的 dialog.showSaveDialog 功能类似。