跳至主要内容
版本: v2.9.0

对话框

运行时的这部分提供了对原生对话框的访问,例如文件选择器和消息框。

JavaScript

对话框当前在 JS 运行时中不受支持。

OpenDirectoryDialog

打开一个对话框,提示用户选择一个目录。可以使用 OpenDialogOptions 进行自定义。

Go: OpenDirectoryDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)

返回: 选定的目录(如果用户取消则为空)或错误

OpenFileDialog

打开一个对话框,提示用户选择一个文件。可以使用 OpenDialogOptions 进行自定义。

Go: OpenFileDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)

返回: 选定的文件(如果用户取消则为空)或错误

OpenMultipleFilesDialog

打开一个对话框,提示用户选择多个文件。可以使用 OpenDialogOptions 进行自定义。

Go: OpenMultipleFilesDialog(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error)

返回: 选定的文件(如果用户取消则为空)或错误

SaveFileDialog

打开一个对话框,提示用户选择一个文件名以便保存。可以使用 SaveDialogOptions 进行自定义。

Go: SaveFileDialog(ctx context.Context, dialogOptions SaveDialogOptions) (string, error)

返回: 选定的文件(如果用户取消则为空)或错误

MessageDialog

使用消息对话框显示一条消息。可以使用 MessageDialogOptions 进行自定义。

Go: MessageDialog(ctx context.Context, dialogOptions MessageDialogOptions) (string, error)

返回: 选定按钮的文本或错误

选项

OpenDialogOptions

type OpenDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
ResolvesAliases bool
TreatPackagesAsDirectories bool
}
字段描述WinMacLin
DefaultDirectory对话框打开时将显示的目录
DefaultFilename默认文件名
Title对话框的标题
Filters文件过滤器的列表
ShowHiddenFiles显示系统隐藏的文件
CanCreateDirectories允许用户创建目录
ResolvesAliases如果为真,则返回文件而不是别名
TreatPackagesAsDirectories允许导航到包中

SaveDialogOptions

type SaveDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
TreatPackagesAsDirectories bool
}
字段描述WinMacLin
DefaultDirectory对话框打开时将显示的目录
DefaultFilename默认文件名
Title对话框的标题
Filters文件过滤器的列表
ShowHiddenFiles显示系统隐藏的文件
CanCreateDirectories允许用户创建目录
TreatPackagesAsDirectories允许导航到包中

MessageDialogOptions

type MessageDialogOptions struct {
Type DialogType
Title string
Message string
Buttons []string
DefaultButton string
CancelButton string
}
字段描述WinMacLin
类型消息对话框的类型,例如 question、info…
Title对话框的标题
消息要向用户显示的消息
按钮按钮标题的列表
DefaultButton具有此文本的按钮应被视为默认按钮。绑定到 return*
CancelButton具有此文本的按钮应被视为取消按钮。绑定到 escape

Windows

Windows 具有标准的对话框类型,其中按钮不可自定义。返回的值将是以下之一: "Ok"、"Cancel"、"Abort"、"Retry"、"Ignore"、"Yes"、"No"、"Try Again" 或 "Continue"。

对于 Question 对话框,默认按钮是 "Yes",取消按钮是 "No"。可以通过将 DefaultButton 值设置为 "No" 来更改此设置。

示例

    result, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Question",
Message: "Do you want to continue?",
DefaultButton: "No",
})

Linux

Linux 具有标准的对话框类型,其中按钮不可自定义。返回的值将是以下之一: "Ok"、"Cancel"、"Yes"、"No"

Mac

Mac 上的消息对话框可以指定最多 4 个按钮。如果没有给出 DefaultButtonCancelButton,则第一个按钮被视为默认按钮,并绑定到 return 键。

对于以下代码

selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
})

第一个按钮显示为默认按钮


如果我们将 DefaultButton 指定为 "two"

selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
DefaultButton: "two",
})

第二个按钮显示为默认按钮。按下 return 时,将返回值 "two"。


如果我们现在将 CancelButton 指定为 "three"

selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
DefaultButton: "two",
CancelButton: "three",
})

带有 "three" 的按钮将显示在对话框的底部。按下 escape 时,将返回值 "three"




DialogType

const (
InfoDialog DialogType = "info"
WarningDialog DialogType = "warning"
ErrorDialog DialogType = "error"
QuestionDialog DialogType = "question"
)

FileFilter

type FileFilter struct {
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
Pattern string // semi-colon separated list of extensions, EG: "*.jpg;*.png"
}

Windows

Windows 允许您在对话框中使用多个文件过滤器。每个 FileFilter 将在对话框中显示为一个单独的条目




Linux

Linux 允许您在对话框中使用多个文件过滤器。每个 FileFilter 将在对话框中显示为一个单独的条目




Mac

Mac 对话框只支持单个模式集来过滤文件。如果提供了多个 FileFilters,Wails 将使用所有定义的模式。

示例

    selection, err := runtime.OpenFileDialog(b.ctx, runtime.OpenDialogOptions{
Title: "Select File",
Filters: []runtime.FileFilter{
{
DisplayName: "Images (*.png;*.jpg)",
Pattern: "*.png;*.jpg",
}, {
DisplayName: "Videos (*.mov;*.mp4)",
Pattern: "*.mov;*.mp4",
},
},
})

这将导致打开文件对话框使用 *.png,*.jpg,*.mov,*.mp4 作为过滤器。