Dialogs
Wails provides a comprehensive dialog system for displaying native system dialogs. These include informational messages, questions, file selection, and more.
Dialog Types
Information Dialog
Display simple informational messages to users:
dialog := application.InfoDialog()dialog.SetTitle("Welcome")dialog.SetMessage("Welcome to our application!")dialog.Show()
Question Dialog
Present users with questions and customisable buttons:
dialog := application.QuestionDialog()dialog.SetTitle("Save Changes")dialog.SetMessage("Do you want to save your changes?")dialog.AddButton("Save").OnClick(func() { // Handle save})saveButton := dialog.AddButton("Don't Save")dialog.SetDefaultButton(saveButton)dialog.Show()
Error Dialog
Display error messages:
dialog := application.ErrorDialog()dialog.SetTitle("Error")dialog.SetMessage("Failed to save file")dialog.Show()
File Dialogs
Open File Dialog
Allow users to select files to open:
dialog := application.OpenFileDialog()dialog.SetTitle("Select Image")dialog.SetFilters([]*application.FileFilter{ { DisplayName: "Images (*.png;*.jpg)", Pattern: "*.png;*.jpg", },})
// Single file selectionif path, err := dialog.PromptForSingleSelection(); err == nil { // Use selected file path}
// Multiple file selectionif paths, err := dialog.PromptForMultipleSelection(); err == nil { // Use selected file paths}
Save File Dialog
Allow users to choose where to save files:
dialog := application.SaveFileDialog()dialog.SetTitle("Save Document")dialog.SetDefaultFilename("document.txt")dialog.SetFilters([]*application.FileFilter{ { DisplayName: "Text Files (*.txt)", Pattern: "*.txt", },})
if path, err := dialog.PromptForSingleSelection(); err == nil { // Save file to selected path}
Dialog Customisation
Setting Icons
Dialogs can use custom icons from the built-in icon set:
dialog := application.InfoDialog()dialog.SetIcon(icons.ApplicationDarkMode256)
Window Attachment
Dialogs can be attached to specific windows:
dialog := application.QuestionDialog()dialog.AttachToWindow(app.CurrentWindow())dialog.Show()
Button Customisation
Create buttons with custom labels and actions:
dialog := application.QuestionDialog()dialog.SetMessage("Choose an action")
// Add buttons with custom handlersdialog.AddButton("Save").OnClick(func() { // Handle save})dialog.AddButton("Don't Save").OnClick(func() { // Handle don't save})cancelButton := dialog.AddButton("Cancel")dialog.SetDefaultButton(cancelButton) // Set default button
Platform Considerations
On macOS, dialogs follow system conventions:
- Use system-standard dialog appearances
- Support keyboard navigation (Tab, Space, Return)
- Support standard keyboard shortcuts (⌘+.)
- Automatically handle dark/light mode
- Support system accessibility features
- Position relative to parent window
On Windows, dialogs integrate with the Windows UI:
- Use Windows system dialog styles
- Support keyboard navigation (Tab, Space, Enter)
- Support Windows accessibility features
- Follow Windows dialog positioning rules
- Adapt to Windows theme settings
- Support high DPI displays
On Linux, dialog behaviour depends on the desktop environment:
- Use native dialog widgets when available
- Follow desktop environment theme
- Support keyboard navigation
- Adapt to desktop environment settings
- Position according to window manager rules
- Support desktop environment accessibility
Directory Selection
Allow users to select directories:
dialog := application.DirectoryDialog()dialog.SetTitle("Select Project Directory")if path, err := dialog.PromptForSingleSelection(); err == nil { // Use selected directory path}
About Dialog
Display application information:
app.ShowAboutDialog()
Best Practices
-
Use appropriate dialog types for different scenarios:
- InfoDialog for general messages
- QuestionDialog for user decisions
- ErrorDialog for error messages
- FileDialog for file operations
-
Provide clear and concise messages:
- Use descriptive titles
- Keep messages brief but informative
- Clearly state any required user action
-
Handle dialog responses appropriately:
- Check for errors in file dialogs
- Provide feedback for user actions
- Handle cancellation gracefully
-
Consider platform conventions:
- Follow platform-specific dialog patterns
- Use appropriate button ordering
- Respect system settings