docs: Comprehensive API Reference documentation with event tables and examples

- Restructured API Reference to use flat file structure instead of subdirectories
- Added complete API documentation for all major Wails v3 APIs
- Created comprehensive event tables documenting all built-in system events
- Fixed incorrect window lifecycle methods (OnClose/OnFocus/OnBlur don't exist)
- Replaced with correct OnWindowEvent() and RegisterHook() implementations
- Added detailed explanations of Common Events vs Platform-Native Events
- Expanded Frontend Runtime API with complete method signatures and examples
- Added comprehensive Dialogs, Menu, and Events API documentation
- Included practical code examples for Go and JavaScript throughout
- Documented event patterns with use case descriptions
- Removed placeholder content and "Best Practices" sections
- Made Go API Conventions collapsible with beginner-friendly intro text

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Lea Anthony 2025-11-22 22:28:26 +11:00
commit 3f1e16dcfb
13 changed files with 3834 additions and 709 deletions

View file

@ -266,41 +266,13 @@ export default defineConfig({
collapsed: true,
items: [
{ label: "Overview", link: "/reference/overview" },
{
label: "Application",
collapsed: true,
autogenerate: { directory: "reference/application" },
},
{
label: "Window",
collapsed: true,
autogenerate: { directory: "reference/window" },
},
{
label: "Menu",
collapsed: true,
autogenerate: { directory: "reference/menu" },
},
{
label: "Events",
collapsed: true,
autogenerate: { directory: "reference/events" },
},
{
label: "Dialogs",
collapsed: true,
autogenerate: { directory: "reference/dialogs" },
},
{
label: "Runtime",
collapsed: true,
autogenerate: { directory: "reference/runtime" },
},
{
label: "CLI",
collapsed: true,
autogenerate: { directory: "reference/cli" },
},
{ label: "Application", link: "/reference/application" },
{ label: "Window", link: "/reference/window" },
{ label: "Menu", link: "/reference/menu" },
{ label: "Events", link: "/reference/events" },
{ label: "Dialogs", link: "/reference/dialogs" },
{ label: "Frontend Runtime", link: "/reference/frontend-runtime" },
{ label: "CLI", link: "/reference/cli" },
],
},

View file

@ -277,7 +277,7 @@ cd myapp && wails3 dev
## Next Steps
Next: [Build a complete application](/tutorials/03-notes-vanilla), browse [examples](https://github.com/wailsapp/wails/tree/v3-alpha/v3/examples), or check the [API reference](/reference/application-api). Migrating from v2? See the [upgrade guide](/migration/v2-to-v3).
Next: [Build a complete application](/tutorials/03-notes-vanilla), browse [examples](https://github.com/wailsapp/wails/tree/v3-alpha/v3/examples), or check the [API reference](/reference/application). Migrating from v2? See the [upgrade guide](/migration/v2-to-v3).
:::note[Production Ready]

View file

@ -389,103 +389,30 @@ import (
"github.com/wailsapp/wails/v3/pkg/application"
)
type App struct {
app *application.Application
}
func NewApp(app *application.Application) *App {
return &App{app: app}
}
func (a *App) Startup() {
// Listen for events
a.app.OnEvent("data-updated", func(e *application.CustomEvent) {
a.app.Logger.Info("Data updated", "data", e.Data)
})
// Listen for shutdown
a.app.OnApplicationEvent(application.EventApplicationShutdown, func(e *application.ApplicationEvent) {
a.app.Logger.Info("Shutting down")
// Cleanup
})
}
func (a *App) ShowMessage(message string) {
a.app.InfoDialog().
SetTitle("Message").
SetMessage(message).
Show()
}
func (a *App) GetData() string {
// Get clipboard
text, _ := a.app.Clipboard.Text()
return text
}
func main() {
app := application.New(application.Options{
Name: "My App",
Services: []application.Service{
application.NewService(NewApp(app)),
Name: "My Application",
Description: "A demo application",
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "My App",
Width: 800,
Height: 600,
// Create main window
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "My App",
Width: 1024,
Height: 768,
MinWidth: 800,
MinHeight: 600,
BackgroundColour: application.NewRGB(255, 255, 255),
URL: "http://wails.localhost/",
})
window.Centre()
window.Show()
app.Run()
}
```
## Best Practices
### ✅ Do
- **Store app reference** - Keep in service structs
- **Use managers** - Access features through managers
- **Handle errors** - Check dialog results
- **Use logger** - Structured logging
- **Clean up** - Use shutdown hooks
### ❌ Don't
- **Don't block Run()** - It's the event loop
- **Don't ignore errors** - Handle all errors
- **Don't create multiple apps** - One per process
- **Don't call Quit() in startup** - Let app initialize
## Next Steps
<CardGrid>
<Card title="Window API" icon="laptop">
Complete window management reference.
[Learn More →](/reference/window-api)
</Card>
<Card title="Services" icon="puzzle">
Learn about service architecture.
[Learn More →](/features/bindings/services)
</Card>
<Card title="Events" icon="star">
Master the event system.
[Learn More →](/features/events/system)
</Card>
<Card title="dialogs" icon="information">
Use native dialogs.
[Learn More →](/features/dialogs/overview)
</Card>
</CardGrid>
---
**Questions?** Ask in [Discord](https://discord.gg/JDdSxwjhGf) or check the [examples](https://github.com/wailsapp/wails/tree/v3-alpha/v3/examples).

View file

@ -0,0 +1,28 @@
---
title: CLI Reference
description: Complete reference for the Wails CLI commands
sidebar:
order: 1
---
## Overview
The Wails CLI (`wails3`) provides commands for creating, developing, building, and managing Wails applications.
## Coming Soon
This section is under construction. For now, use `wails3 --help` or `wails3 [command] --help` for command documentation.
**Common commands:**
```bash
wails3 init # Create a new project
wails3 dev # Run in development mode
wails3 build # Build for production
wails3 generate # Generate bindings or runtime
wails3 doctor # Check development environment
```
---
**Questions?** Ask in [Discord](https://discord.gg/JDdSxwjhGf) or check the [examples](https://github.com/wailsapp/wails/tree/v3-alpha/v3/examples).

View file

@ -0,0 +1,791 @@
---
title: Dialogs API
description: Complete reference for native dialog APIs
sidebar:
order: 5
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Dialogs API provides methods to show native file dialogs and message dialogs.
**Dialog Types:**
- **File Dialogs** - Open, Save, and Select Folder dialogs
- **Message Dialogs** - Info, Error, Warning, and Question dialogs
All dialogs are **native OS dialogs** that match the platform's look and feel.
## File Dialogs
### OpenFileDialog()
Creates a file open dialog.
```go
func (a *App) OpenFileDialog() *OpenFileDialog
```
**Example:**
```go
dialog := app.OpenFileDialog()
```
### OpenFileDialog Methods
#### SetTitle()
Sets the dialog title.
```go
func (d *OpenFileDialog) SetTitle(title string) *OpenFileDialog
```
**Example:**
```go
dialog.SetTitle("Select Image")
```
#### SetFilters()
Sets file type filters.
```go
func (d *OpenFileDialog) SetFilters(filters []FileFilter) *OpenFileDialog
```
**FileFilter structure:**
```go
type FileFilter struct {
DisplayName string // "Images", "Documents", etc.
Pattern string // "*.png;*.jpg", "*.pdf;*.doc", etc.
}
```
**Example:**
```go
dialog.SetFilters([]application.FileFilter{
{DisplayName: "Images", Pattern: "*.png;*.jpg;*.gif"},
{DisplayName: "Documents", Pattern: "*.pdf;*.docx"},
{DisplayName: "All Files", Pattern: "*.*"},
})
```
#### SetDefaultDirectory()
Sets the initial directory.
```go
func (d *OpenFileDialog) SetDefaultDirectory(path string) *OpenFileDialog
```
**Example:**
```go
homeDir, _ := os.UserHomeDir()
dialog.SetDefaultDirectory(homeDir)
```
#### PromptForSingleSelection()
Shows the dialog and returns the selected file.
```go
func (d *OpenFileDialog) PromptForSingleSelection() (string, error)
```
**Returns:**
- `string` - Selected file path
- `error` - Error if cancelled or failed
**Example:**
```go
path, err := app.OpenFileDialog().
SetTitle("Select Image").
SetFilters([]application.FileFilter{
{DisplayName: "Images", Pattern: "*.png;*.jpg;*.gif"},
}).
PromptForSingleSelection()
if err != nil {
// User cancelled or error occurred
return
}
// Use the selected file
processFile(path)
```
#### PromptForMultipleSelection()
Shows the dialog and returns multiple selected files.
```go
func (d *OpenFileDialog) PromptForMultipleSelection() ([]string, error)
```
**Returns:**
- `[]string` - Array of selected file paths
- `error` - Error if cancelled or failed
**Example:**
```go
paths, err := app.OpenFileDialog().
SetTitle("Select Images").
SetFilters([]application.FileFilter{
{DisplayName: "Images", Pattern: "*.png;*.jpg"},
}).
PromptForMultipleSelection()
if err != nil {
return
}
for _, path := range paths {
processFile(path)
}
```
### SaveFileDialog()
Creates a file save dialog.
```go
func (a *App) SaveFileDialog() *SaveFileDialog
```
**Example:**
```go
dialog := app.SaveFileDialog()
```
### SaveFileDialog Methods
#### SetTitle()
Sets the dialog title.
```go
func (d *SaveFileDialog) SetTitle(title string) *SaveFileDialog
```
#### SetDefaultFilename()
Sets the default filename.
```go
func (d *SaveFileDialog) SetDefaultFilename(filename string) *SaveFileDialog
```
**Example:**
```go
dialog.SetDefaultFilename("document.pdf")
```
#### SetFilters()
Sets file type filters (same as OpenFileDialog).
```go
func (d *SaveFileDialog) SetFilters(filters []FileFilter) *SaveFileDialog
```
#### SetDefaultDirectory()
Sets the initial directory (same as OpenFileDialog).
```go
func (d *SaveFileDialog) SetDefaultDirectory(path string) *SaveFileDialog
```
#### PromptForSingleSelection()
Shows the dialog and returns the save path.
```go
func (d *SaveFileDialog) PromptForSingleSelection() (string, error)
```
**Example:**
```go
path, err := app.SaveFileDialog().
SetTitle("Save Document").
SetDefaultFilename("untitled.pdf").
SetFilters([]application.FileFilter{
{DisplayName: "PDF Document", Pattern: "*.pdf"},
}).
PromptForSingleSelection()
if err != nil {
// User cancelled
return
}
// Save to the selected path
saveDocument(path)
```
### SelectFolderDialog()
Creates a folder selection dialog.
```go
func (a *App) SelectFolderDialog() *SelectFolderDialog
```
**Example:**
```go
dialog := app.SelectFolderDialog()
```
### SelectFolderDialog Methods
#### SetTitle()
Sets the dialog title.
```go
func (d *SelectFolderDialog) SetTitle(title string) *SelectFolderDialog
```
#### SetDefaultDirectory()
Sets the initial directory.
```go
func (d *SelectFolderDialog) SetDefaultDirectory(path string) *SelectFolderDialog
```
#### PromptForSingleSelection()
Shows the dialog and returns the selected folder.
```go
func (d *SelectFolderDialog) PromptForSingleSelection() (string, error)
```
**Example:**
```go
path, err := app.SelectFolderDialog().
SetTitle("Select Output Folder").
PromptForSingleSelection()
if err != nil {
// User cancelled
return
}
// Use the selected folder
outputDir = path
```
## Message Dialogs
### InfoDialog()
Creates an information dialog.
```go
func (a *App) InfoDialog() *InfoDialog
```
**Example:**
```go
dialog := app.InfoDialog()
```
### InfoDialog Methods
#### SetTitle()
Sets the dialog title.
```go
func (d *InfoDialog) SetTitle(title string) *InfoDialog
```
#### SetMessage()
Sets the dialog message.
```go
func (d *InfoDialog) SetMessage(message string) *InfoDialog
```
#### Show()
Shows the dialog.
```go
func (d *InfoDialog) Show()
```
**Example:**
```go
app.InfoDialog().
SetTitle("Success").
SetMessage("File saved successfully!").
Show()
```
### ErrorDialog()
Creates an error dialog.
```go
func (a *App) ErrorDialog() *ErrorDialog
```
**Methods:** Same as InfoDialog (SetTitle, SetMessage, Show)
**Example:**
```go
app.ErrorDialog().
SetTitle("Error").
SetMessage("Failed to save file: " + err.Error()).
Show()
```
### WarningDialog()
Creates a warning dialog.
```go
func (a *App) WarningDialog() *WarningDialog
```
**Methods:** Same as InfoDialog (SetTitle, SetMessage, Show)
**Example:**
```go
app.WarningDialog().
SetTitle("Warning").
SetMessage("This action cannot be undone.").
Show()
```
### QuestionDialog()
Creates a question dialog with custom buttons.
```go
func (a *App) QuestionDialog() *QuestionDialog
```
### QuestionDialog Methods
#### SetTitle()
Sets the dialog title.
```go
func (d *QuestionDialog) SetTitle(title string) *QuestionDialog
```
#### SetMessage()
Sets the dialog message.
```go
func (d *QuestionDialog) SetMessage(message string) *QuestionDialog
```
#### SetButtons()
Sets the button labels.
```go
func (d *QuestionDialog) SetButtons(buttons ...string) *QuestionDialog
```
**Parameters:**
- `buttons` - Variable number of button labels
**Example:**
```go
dialog.SetButtons("Yes", "No")
dialog.SetButtons("Save", "Don't Save", "Cancel")
```
#### SetDefaultButton()
Sets which button is the default (activated by pressing Enter).
```go
func (d *QuestionDialog) SetDefaultButton(index int) *QuestionDialog
```
**Example:**
```go
// Make "Yes" the default button
dialog.SetButtons("Yes", "No").SetDefaultButton(0)
```
#### SetCancelButton()
Sets which button is the cancel button (activated by pressing Escape).
```go
func (d *QuestionDialog) SetCancelButton(index int) *QuestionDialog
```
**Example:**
```go
// Make "Cancel" the cancel button
dialog.SetButtons("Save", "Don't Save", "Cancel").SetCancelButton(2)
```
#### Show()
Shows the dialog and returns the selected button.
```go
func (d *QuestionDialog) Show() (string, error)
```
**Returns:**
- `string` - The label of the button that was clicked
- `error` - Error if dialog failed to show
**Example:**
```go
result, err := app.QuestionDialog().
SetTitle("Confirm").
SetMessage("Do you want to save changes?").
SetButtons("Save", "Don't Save", "Cancel").
SetDefaultButton(0).
SetCancelButton(2).
Show()
if err != nil {
return
}
switch result {
case "Save":
saveDocument()
case "Don't Save":
// Continue without saving
case "Cancel":
return
}
```
## Complete Examples
### File Selection Example
```go
type FileService struct {
app *application.Application
}
func (s *FileService) OpenImage() (string, error) {
path, err := s.app.OpenFileDialog().
SetTitle("Select Image").
SetFilters([]application.FileFilter{
{DisplayName: "Images", Pattern: "*.png;*.jpg;*.jpeg;*.gif"},
{DisplayName: "All Files", Pattern: "*.*"},
}).
PromptForSingleSelection()
if err != nil {
return "", err
}
return path, nil
}
func (s *FileService) SaveDocument(defaultName string) (string, error) {
path, err := s.app.SaveFileDialog().
SetTitle("Save Document").
SetDefaultFilename(defaultName).
SetFilters([]application.FileFilter{
{DisplayName: "PDF Document", Pattern: "*.pdf"},
{DisplayName: "Text Document", Pattern: "*.txt"},
}).
PromptForSingleSelection()
if err != nil {
return "", err
}
return path, nil
}
func (s *FileService) SelectOutputFolder() (string, error) {
path, err := s.app.SelectFolderDialog().
SetTitle("Select Output Folder").
PromptForSingleSelection()
if err != nil {
return "", err
}
return path, nil
}
```
### Confirmation Dialog Example
```go
func (s *Service) DeleteItem(id string) error {
// Confirm before deleting
result, err := s.app.QuestionDialog().
SetTitle("Confirm Delete").
SetMessage("Are you sure you want to delete this item?").
SetButtons("Delete", "Cancel").
SetDefaultButton(1). // Default to Cancel
SetCancelButton(1).
Show()
if err != nil || result == "Cancel" {
return nil // User cancelled
}
// Perform deletion
return deleteFromDatabase(id)
}
```
### Save Changes Dialog
```go
func (s *Editor) PromptSaveChanges() (bool, error) {
result, err := s.app.QuestionDialog().
SetTitle("Unsaved Changes").
SetMessage("Do you want to save your changes before closing?").
SetButtons("Save", "Don't Save", "Cancel").
SetDefaultButton(0).
SetCancelButton(2).
Show()
if err != nil {
return false, err
}
switch result {
case "Save":
return s.Save()
case "Don't Save":
return true, nil
case "Cancel":
return false, nil
}
return false, nil
}
```
### Multi-File Processing
```go
func (s *Service) ProcessMultipleFiles() error {
// Select multiple files
paths, err := s.app.OpenFileDialog().
SetTitle("Select Files to Process").
SetFilters([]application.FileFilter{
{DisplayName: "Images", Pattern: "*.png;*.jpg"},
}).
PromptForMultipleSelection()
if err != nil {
return err
}
if len(paths) == 0 {
s.app.InfoDialog().
SetTitle("No Files Selected").
SetMessage("Please select at least one file.").
Show()
return nil
}
// Process files
for i, path := range paths {
err := processFile(path)
if err != nil {
s.app.ErrorDialog().
SetTitle("Processing Error").
SetMessage(fmt.Sprintf("Failed to process %s: %v", path, err)).
Show()
continue
}
// Show progress
s.app.EmitEvent("progress", map[string]interface{}{
"current": i + 1,
"total": len(paths),
})
}
// Show completion
s.app.InfoDialog().
SetTitle("Complete").
SetMessage(fmt.Sprintf("Successfully processed %d files", len(paths))).
Show()
return nil
}
```
### Error Handling with Dialogs
```go
func (s *Service) SaveFile(data []byte) error {
// Select save location
path, err := s.app.SaveFileDialog().
SetTitle("Save File").
SetDefaultFilename("data.json").
SetFilters([]application.FileFilter{
{DisplayName: "JSON File", Pattern: "*.json"},
}).
PromptForSingleSelection()
if err != nil {
// User cancelled - not an error
return nil
}
// Attempt to save
err = os.WriteFile(path, data, 0644)
if err != nil {
// Show error dialog
s.app.ErrorDialog().
SetTitle("Save Failed").
SetMessage(fmt.Sprintf("Could not save file: %v", err)).
Show()
return err
}
// Show success
s.app.InfoDialog().
SetTitle("Success").
SetMessage("File saved successfully!").
Show()
return nil
}
```
### Platform-Specific Defaults
```go
import (
"os"
"path/filepath"
"runtime"
)
func (s *Service) GetDefaultDirectory() string {
homeDir, _ := os.UserHomeDir()
switch runtime.GOOS {
case "windows":
return filepath.Join(homeDir, "Documents")
case "darwin":
return filepath.Join(homeDir, "Documents")
case "linux":
return filepath.Join(homeDir, "Documents")
default:
return homeDir
}
}
func (s *Service) OpenWithDefaults() (string, error) {
return s.app.OpenFileDialog().
SetTitle("Open File").
SetDefaultDirectory(s.GetDefaultDirectory()).
SetFilters([]application.FileFilter{
{DisplayName: "All Files", Pattern: "*.*"},
}).
PromptForSingleSelection()
}
```
## Best Practices
### ✅ Do
- **Use native dialogs** - They match the platform's look and feel
- **Provide clear titles** - Help users understand the purpose
- **Set appropriate filters** - Guide users to correct file types
- **Handle cancellation** - Check for errors (user may cancel)
- **Show confirmation for destructive actions** - Use QuestionDialog
- **Provide feedback** - Use InfoDialog for success messages
- **Set sensible defaults** - Default directory, filename, etc.
### ❌ Don't
- **Don't ignore errors** - User cancellation returns an error
- **Don't use ambiguous button labels** - "OK" vs "Save"/"Cancel"
- **Don't overuse dialogs** - They interrupt workflow
- **Don't show errors for cancellation** - It's a normal action
- **Don't forget file filters** - Help users find the right files
- **Don't hardcode paths** - Use os.UserHomeDir() or similar
## Dialog Types by Platform
### macOS
- Dialogs slide down from title bar
- "Sheet" style attached to parent window
- Native macOS appearance
### Windows
- Standard Windows dialogs
- Follows Windows design guidelines
- Modern Windows 10/11 appearance
### Linux
- GTK dialogs on GTK-based systems
- Qt dialogs on Qt-based systems
- Matches desktop environment
## Common Patterns
### "Save As" Pattern
```go
func (s *Service) SaveAs(currentPath string) (string, error) {
// Extract filename from current path
filename := filepath.Base(currentPath)
// Show save dialog
path, err := s.app.SaveFileDialog().
SetTitle("Save As").
SetDefaultFilename(filename).
PromptForSingleSelection()
if err != nil {
return "", err
}
return path, nil
}
```
### "Open Recent" Pattern
```go
func (s *Service) OpenRecent(recentPath string) error {
// Check if file still exists
if _, err := os.Stat(recentPath); os.IsNotExist(err) {
result, _ := s.app.QuestionDialog().
SetTitle("File Not Found").
SetMessage("The file no longer exists. Remove from recent files?").
SetButtons("Remove", "Cancel").
Show()
if result == "Remove" {
s.removeFromRecent(recentPath)
}
return err
}
return s.openFile(recentPath)
}
```

View file

@ -0,0 +1,801 @@
---
title: Events API
description: Complete reference for the Events API
sidebar:
order: 4
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Events API provides methods to emit and listen to events, enabling communication between different parts of your application.
**Event Types:**
- **Application Events** - App lifecycle events (startup, shutdown)
- **Window Events** - Window state changes (focus, blur, resize)
- **Custom Events** - User-defined events for app-specific communication
**Communication Patterns:**
- **Go to Frontend** - Emit events from Go, listen in JavaScript
- **Frontend to Go** - Not directly (use service bindings instead)
- **Frontend to Frontend** - Via Go or local runtime events
- **Window to Window** - Target specific windows or broadcast to all
## Event Methods (Go)
### EmitEvent()
Emits a custom event to all windows.
```go
func (a *App) EmitEvent(name string, data ...interface{})
```
**Parameters:**
- `name` - Event name
- `data` - Optional data to send with the event
**Example:**
```go
// Emit simple event
app.EmitEvent("user-logged-in")
// Emit with data
app.EmitEvent("data-updated", map[string]interface{}{
"count": 42,
"status": "success",
})
// Emit multiple values
app.EmitEvent("progress", 75, "Processing files...")
```
### OnEvent()
Listens for custom events in Go.
```go
func (a *App) OnEvent(name string, callback func(*CustomEvent)) func()
```
**Parameters:**
- `name` - Event name to listen for
- `callback` - Function called when event is emitted
**Returns:** Cleanup function to remove the event listener
**Example:**
```go
// Listen for events
cleanup := app.OnEvent("user-action", func(e *application.CustomEvent) {
data := e.Data.(map[string]interface{})
action := data["action"].(string)
app.Logger.Info("User action", "action", action)
})
// Later, remove listener
cleanup()
```
### Window-Specific Events
Emit events to a specific window:
```go
// Emit to specific window
window.EmitEvent("notification", "Hello from Go!")
// Emit to all windows
app.EmitEvent("global-update", data)
```
## Event Methods (Frontend)
### On()
Listens for events from Go.
```javascript
import { On } from '@wailsio/runtime'
On(eventName, callback)
```
**Parameters:**
- `eventName` - Name of the event to listen for
- `callback` - Function called when event is received
**Returns:** Cleanup function
**Example:**
```javascript
import { On } from '@wailsio/runtime'
// Listen for events
const cleanup = On('data-updated', (data) => {
console.log('Count:', data.count)
console.log('Status:', data.status)
updateUI(data)
})
// Later, remove listener
cleanup()
```
### Once()
Listens for a single event occurrence.
```javascript
import { Once } from '@wailsio/runtime'
Once(eventName, callback)
```
**Example:**
```javascript
import { Once } from '@wailsio/runtime'
// Listen for first occurrence only
Once('initialization-complete', (data) => {
console.log('App initialized!', data)
// This will only fire once
})
```
### Off()
Removes an event listener.
```javascript
import { Off } from '@wailsio/runtime'
Off(eventName, callback)
```
**Example:**
```javascript
import { On, Off } from '@wailsio/runtime'
const handler = (data) => {
console.log('Event received:', data)
}
// Start listening
On('my-event', handler)
// Stop listening
Off('my-event', handler)
```
### OffAll()
Removes all listeners for an event.
```javascript
import { OffAll } from '@wailsio/runtime'
OffAll(eventName)
```
**Example:**
```javascript
// Remove all listeners for this event
OffAll('data-updated')
```
## Application Events
### OnApplicationEvent()
Listens for application lifecycle events.
```go
func (a *App) OnApplicationEvent(eventType ApplicationEventType, callback func(*ApplicationEvent)) func()
```
**Event Types:**
- `EventApplicationStarted` - Application has started
- `EventApplicationShutdown` - Application is shutting down
- `EventApplicationDebug` - Debug event (dev mode only)
**Example:**
```go
// Handle application startup
app.OnApplicationEvent(application.EventApplicationStarted, func(e *application.ApplicationEvent) {
app.Logger.Info("Application started")
// Initialize resources
})
// Handle application shutdown
app.OnApplicationEvent(application.EventApplicationShutdown, func(e *application.ApplicationEvent) {
app.Logger.Info("Application shutting down")
// Cleanup resources, save state
database.Close()
saveSettings()
})
```
## Window Events
### OnWindowEvent()
Listens for window-specific events.
```go
func (w *Window) OnWindowEvent(eventType WindowEventType, callback func(*WindowEvent)) func()
```
**Event Types:**
- `EventWindowFocus` - Window gained focus
- `EventWindowBlur` - Window lost focus
- `EventWindowClose` - Window is closing
- `EventWindowResize` - Window was resized
- `EventWindowMove` - Window was moved
**Example:**
```go
// Handle window focus
window.OnWindowEvent(application.EventWindowFocus, func(e *application.WindowEvent) {
app.Logger.Info("Window focused")
})
// Handle window resize
window.OnWindowEvent(application.EventWindowResize, func(e *application.WindowEvent) {
width, height := window.Size()
app.Logger.Info("Window resized", "width", width, "height", height)
})
```
## Common Patterns
These patterns demonstrate proven approaches for using events in real-world applications. Each pattern solves a specific communication challenge between your Go backend and frontend, helping you build responsive, well-structured applications.
### Request/Response Pattern
Use this when you want to notify the frontend about the completion of backend operations, such as after data fetching, file processing, or background tasks. The service binding returns data directly, while events provide additional notifications for UI updates like showing toast messages or refreshing lists.
**Go:**
```go
// Service method
type DataService struct {
app *application.Application
}
func (s *DataService) FetchData(query string) ([]Item, error) {
items := fetchFromDatabase(query)
// Emit event when done
s.app.EmitEvent("data-fetched", map[string]interface{}{
"query": query,
"count": len(items),
})
return items, nil
}
```
**JavaScript:**
```javascript
import { FetchData } from './bindings/DataService'
import { On } from '@wailsio/runtime'
// Listen for completion event
On('data-fetched', (data) => {
console.log(`Fetched ${data.count} items for query: ${data.query}`)
showNotification(`Found ${data.count} results`)
})
// Call service method
const items = await FetchData("search term")
displayItems(items)
```
### Progress Updates
Ideal for long-running operations like file uploads, batch processing, large data imports, or video encoding. Emit progress events during the operation to update progress bars, status text, or step indicators in the UI, providing users with real-time feedback.
**Go:**
```go
func (s *Service) ProcessFiles(files []string) error {
total := len(files)
for i, file := range files {
// Process file
processFile(file)
// Emit progress event
s.app.EmitEvent("progress", map[string]interface{}{
"current": i + 1,
"total": total,
"percent": float64(i+1) / float64(total) * 100,
"file": file,
})
}
s.app.EmitEvent("processing-complete")
return nil
}
```
**JavaScript:**
```javascript
import { On, Once } from '@wailsio/runtime'
// Update progress bar
On('progress', (data) => {
progressBar.style.width = `${data.percent}%`
statusText.textContent = `Processing ${data.file}... (${data.current}/${data.total})`
})
// Handle completion
Once('processing-complete', () => {
progressBar.style.width = '100%'
statusText.textContent = 'Complete!'
setTimeout(() => hideProgressBar(), 2000)
})
```
### Multi-Window Communication
Perfect for applications with multiple windows like settings panels, dashboards, or document viewers. Broadcast events to synchronize state across all windows (theme changes, user preferences) or send targeted events to specific windows for window-specific updates.
**Go:**
```go
// Broadcast to all windows
app.EmitEvent("theme-changed", "dark")
// Send to specific window
preferencesWindow.EmitEvent("settings-updated", settings)
// Window-specific listener
window1.OnEvent("request-data", func(e *application.CustomEvent) {
// Only this window will receive this event
window1.EmitEvent("data-response", data)
})
```
**JavaScript:**
```javascript
import { On } from '@wailsio/runtime'
// Listen in any window
On('theme-changed', (theme) => {
document.body.className = theme
})
```
### State Synchronization
Use when you need to keep frontend and backend state in sync, such as user sessions, application configuration, or collaborative features. When state changes on the backend, emit events to update all connected frontends, ensuring consistency across your application.
**Go:**
```go
type StateService struct {
app *application.Application
state map[string]interface{}
mu sync.RWMutex
}
func (s *StateService) UpdateState(key string, value interface{}) {
s.mu.Lock()
s.state[key] = value
s.mu.Unlock()
// Notify all windows
s.app.EmitEvent("state-updated", map[string]interface{}{
"key": key,
"value": value,
})
}
func (s *StateService) GetState(key string) interface{} {
s.mu.RLock()
defer s.mu.RUnlock()
return s.state[key]
}
```
**JavaScript:**
```javascript
import { On } from '@wailsio/runtime'
import { GetState } from './bindings/StateService'
// Keep local state in sync
let localState = {}
On('state-updated', async (data) => {
localState[data.key] = data.value
updateUI(data.key, data.value)
})
// Initialize state
const initialState = await GetState("all")
localState = initialState
```
### Event-Driven Notifications
Best for displaying user feedback like success confirmations, error alerts, or info messages. Instead of calling UI code directly from services, emit notification events that the frontend handles consistently, making it easy to change notification styles or add features like notification history.
**Go:**
```go
type NotificationService struct {
app *application.Application
}
func (s *NotificationService) Success(message string) {
s.app.EmitEvent("notification", map[string]interface{}{
"type": "success",
"message": message,
})
}
func (s *NotificationService) Error(message string) {
s.app.EmitEvent("notification", map[string]interface{}{
"type": "error",
"message": message,
})
}
func (s *NotificationService) Info(message string) {
s.app.EmitEvent("notification", map[string]interface{}{
"type": "info",
"message": message,
})
}
```
**JavaScript:**
```javascript
import { On } from '@wailsio/runtime'
// Unified notification handler
On('notification', (data) => {
const toast = document.createElement('div')
toast.className = `toast toast-${data.type}`
toast.textContent = data.message
document.body.appendChild(toast)
setTimeout(() => {
toast.classList.add('fade-out')
setTimeout(() => toast.remove(), 300)
}, 3000)
})
```
## Complete Example
**Go:**
```go
package main
import (
"sync"
"time"
"github.com/wailsapp/wails/v3/pkg/application"
)
type EventDemoService struct {
app *application.Application
mu sync.Mutex
}
func NewEventDemoService(app *application.Application) *EventDemoService {
service := &EventDemoService{app: app}
// Listen for custom events
app.OnEvent("user-action", func(e *application.CustomEvent) {
data := e.Data.(map[string]interface{})
app.Logger.Info("User action received", "data", data)
})
return service
}
func (s *EventDemoService) StartLongTask() {
go func() {
s.app.EmitEvent("task-started")
for i := 1; i <= 10; i++ {
time.Sleep(500 * time.Millisecond)
s.app.EmitEvent("task-progress", map[string]interface{}{
"step": i,
"total": 10,
"percent": i * 10,
})
}
s.app.EmitEvent("task-completed", map[string]interface{}{
"message": "Task finished successfully!",
})
}()
}
func (s *EventDemoService) BroadcastMessage(message string) {
s.app.EmitEvent("broadcast", message)
}
func main() {
app := application.New(application.Options{
Name: "Event Demo",
})
// Handle application lifecycle
app.OnApplicationEvent(application.EventApplicationStarted, func(e *application.ApplicationEvent) {
app.Logger.Info("Application started!")
})
app.OnApplicationEvent(application.EventApplicationShutdown, func(e *application.ApplicationEvent) {
app.Logger.Info("Application shutting down...")
})
// Register service
service := NewEventDemoService(app)
app.RegisterService(application.NewService(service))
// Create window
window := app.NewWebviewWindow()
// Handle window events
window.OnWindowEvent(events.Common.WindowFocus, func(e *application.WindowEvent) {
window.EmitEvent("window-state", "focused")
})
window.OnWindowEvent(events.Common.WindowLostFocus, func(e *application.WindowEvent) {
window.EmitEvent("window-state", "blurred")
})
window.Show()
app.Run()
}
```
**JavaScript:**
```javascript
import { On, Once } from '@wailsio/runtime'
import { StartLongTask, BroadcastMessage } from './bindings/EventDemoService'
// Task events
On('task-started', () => {
console.log('Task started...')
document.getElementById('status').textContent = 'Running...'
})
On('task-progress', (data) => {
const progressBar = document.getElementById('progress')
progressBar.style.width = `${data.percent}%`
console.log(`Step ${data.step} of ${data.total}`)
})
Once('task-completed', (data) => {
console.log('Task completed!', data.message)
document.getElementById('status').textContent = data.message
})
// Broadcast events
On('broadcast', (message) => {
console.log('Broadcast:', message)
alert(message)
})
// Window state events
On('window-state', (state) => {
console.log('Window is now:', state)
document.body.dataset.windowState = state
})
// Trigger long task
document.getElementById('startTask').addEventListener('click', async () => {
await StartLongTask()
})
// Send broadcast
document.getElementById('broadcast').addEventListener('click', async () => {
const message = document.getElementById('message').value
await BroadcastMessage(message)
})
```
## Built-in Events
Wails provides built-in system events for application and window lifecycle. These events are emitted automatically by the framework.
### Common Events vs Platform-Native Events
Wails provides two types of system events:
**Common Events** (`events.Common.*`) are cross-platform abstractions that work consistently across macOS, Windows, and Linux. These are the events you should use in your application for maximum portability.
**Platform-Native Events** (`events.Mac.*`, `events.Windows.*`, `events.Linux.*`) are the underlying OS-specific events that Common Events are mapped from. These provide access to platform-specific behaviors and edge cases.
**How They Work:**
```go
import "github.com/wailsapp/wails/v3/pkg/events"
// ✅ RECOMMENDED: Use Common Events for cross-platform code
window.OnWindowEvent(events.Common.WindowClosing, func(e *application.WindowEvent) {
// This works on all platforms
})
// Platform-specific events for advanced use cases
window.OnWindowEvent(events.Mac.WindowWillClose, func(e *application.WindowEvent) {
// macOS-specific "will close" event (before WindowClosing)
})
window.OnWindowEvent(events.Windows.WindowClosing, func(e *application.WindowEvent) {
// Windows-specific close event
})
```
**Event Mapping:**
Platform-native events are automatically mapped to Common Events:
- macOS: `events.Mac.WindowShouldClose` → `events.Common.WindowClosing`
- Windows: `events.Windows.WindowClosing` → `events.Common.WindowClosing`
- Linux: `events.Linux.WindowDeleteEvent` → `events.Common.WindowClosing`
This mapping happens automatically in the background, so when you listen for `events.Common.WindowClosing`, you'll receive it regardless of the platform.
**When to Use Each:**
- **Use Common Events** for 99% of your application code - they provide consistent behavior across platforms
- **Use Platform-Native Events** only when you need platform-specific functionality that isn't available in Common Events (e.g., macOS-specific window lifecycle events, Windows power management events)
### Application Events
| Event | Description | When Emitted | Cancellable |
|-------|-------------|--------------|-------------|
| `ApplicationOpenedWithFile` | Application opened with a file | When app is launched with a file (e.g., from file association) | No |
| `ApplicationStarted` | Application has finished launching | After app initialization is complete and app is ready | No |
| `ApplicationLaunchedWithUrl` | Application launched with a URL | When app is launched via URL scheme | No |
| `ThemeChanged` | System theme changed | When OS theme switches between light/dark mode | No |
**Usage:**
```go
import "github.com/wailsapp/wails/v3/pkg/events"
app.OnApplicationEvent(events.Common.ApplicationStarted, func(e *application.ApplicationEvent) {
app.Logger.Info("Application ready!")
})
app.OnApplicationEvent(events.Common.ThemeChanged, func(e *application.ApplicationEvent) {
// Update app theme
})
```
### Window Events
| Event | Description | When Emitted | Cancellable |
|-------|-------------|--------------|-------------|
| `WindowClosing` | Window is about to close | Before window closes (user clicked X, Close() called) | Yes |
| `WindowDidMove` | Window moved to new position | After window position changes (debounced) | No |
| `WindowDidResize` | Window was resized | After window size changes | No |
| `WindowDPIChanged` | Window DPI scaling changed | When moving between monitors with different DPI (Windows) | No |
| `WindowFilesDropped` | Files dropped via native OS drag-drop | After files are dropped from OS onto window | No |
| `WindowFocus` | Window gained focus | When window becomes active | No |
| `WindowFullscreen` | Window entered fullscreen | After Fullscreen() or user enters fullscreen | No |
| `WindowHide` | Window was hidden | After Hide() or window becomes occluded | No |
| `WindowLostFocus` | Window lost focus | When window becomes inactive | No |
| `WindowMaximise` | Window was maximized | After Maximise() or user maximizes | Yes (macOS) |
| `WindowMinimise` | Window was minimized | After Minimise() or user minimizes | Yes (macOS) |
| `WindowRestore` | Window restored from min/max state | After Restore() (Windows primarily) | No |
| `WindowRuntimeReady` | Wails runtime loaded and ready | When JavaScript runtime initialization completes | No |
| `WindowShow` | Window became visible | After Show() or window becomes visible | No |
| `WindowUnFullscreen` | Window exited fullscreen | After UnFullscreen() or user exits fullscreen | No |
| `WindowUnMaximise` | Window exited maximized state | After UnMaximise() or user unmaximizes | Yes (macOS) |
| `WindowUnMinimise` | Window exited minimized state | After UnMinimise()/Restore() or user restores | Yes (macOS) |
| `WindowZoomIn` | Window content zoom increased | After ZoomIn() called (macOS primarily) | Yes (macOS) |
| `WindowZoomOut` | Window content zoom decreased | After ZoomOut() called (macOS primarily) | Yes (macOS) |
| `WindowZoomReset` | Window content zoom reset to 100% | After ZoomReset() called (macOS primarily) | Yes (macOS) |
| `WindowDropZoneFilesDropped` | Files dropped on JS-defined drop zone | When files dropped onto element with drop zone | No |
**Usage:**
```go
import "github.com/wailsapp/wails/v3/pkg/events"
// Listen for window events
window.OnWindowEvent(events.Common.WindowFocus, func(e *application.WindowEvent) {
app.Logger.Info("Window focused")
})
// Cancel window close
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
result, _ := app.QuestionDialog().
SetMessage("Close window?").
SetButtons("Yes", "No").
Show()
if result == "No" {
e.Cancel() // Prevent close
}
})
// Wait for runtime ready
window.OnWindowEvent(events.Common.WindowRuntimeReady, func(e *application.WindowEvent) {
app.Logger.Info("Runtime ready, safe to emit events to frontend")
window.EmitEvent("app-initialized", data)
})
```
**Important Notes:**
- **WindowRuntimeReady** is critical - wait for this event before emitting events to the frontend
- **WindowDidMove** and **WindowDidResize** are debounced (50ms default) to prevent event flooding
- **Cancellable events** can be prevented by calling `event.Cancel()` in a `RegisterHook()` handler
- **WindowFilesDropped** is for native OS file drops; **WindowDropZoneFilesDropped** is for web-based drop zones
- Some events are platform-specific (e.g., WindowDPIChanged on Windows, zoom events primarily on macOS)
## Event Naming Conventions
```go
// Good - descriptive and specific
app.EmitEvent("user:logged-in", user)
app.EmitEvent("data:fetch:complete", results)
app.EmitEvent("ui:theme:changed", theme)
// Bad - vague and unclear
app.EmitEvent("event1", data)
app.EmitEvent("update", stuff)
app.EmitEvent("e", value)
```
## Performance Considerations
### Debouncing High-Frequency Events
```go
type Service struct {
app *application.Application
lastEmit time.Time
debounceWindow time.Duration
}
func (s *Service) EmitWithDebounce(event string, data interface{}) {
now := time.Now()
if now.Sub(s.lastEmit) < s.debounceWindow {
return // Skip this emission
}
s.app.EmitEvent(event, data)
s.lastEmit = now
}
```
### Throttling Events
```javascript
import { On } from '@wailsio/runtime'
let lastUpdate = 0
const throttleMs = 100
On('high-frequency-event', (data) => {
const now = Date.now()
if (now - lastUpdate < throttleMs) {
return // Skip this update
}
processUpdate(data)
lastUpdate = now
})
```

View file

@ -0,0 +1,809 @@
---
title: Frontend Runtime
description: The Wails JavaScript runtime package for frontend integration
sidebar:
order: 1
---
The Wails frontend runtime is the standard library for Wails applications. It provides a
number of features that may be used in your applications, including:
- Window management
- Dialogs
- Browser integration
- Clipboard
- Menus
- System information
- Events
- Context Menus
- Screens
- WML (Wails Markup Language)
The runtime is required for integration between Go and the frontend. There are 2
ways to integrate the runtime:
- Using the `@wailsio/runtime` package
- Using a pre-built bundle
## Using the npm package
The `@wailsio/runtime` package is a JavaScript package that provides access to
the Wails runtime from the frontend. It is used by all standard templates
and is the recommended way to integrate the runtime into your application.
By using the `@wailsio/runtime` package, you will only include the parts of the runtime that you use.
The package is available on npm and can be installed using:
```shell
npm install --save @wailsio/runtime
```
## Using a pre-built bundle
Some projects will not use a Javascript bundler and may prefer to use a
pre-built bundled version of the runtime. This version can be generated locally
using the following command:
```shell
wails3 generate runtime
```
The command will output a `runtime.js` (and `runtime.debug.js`) file in the current
directory. This file is an ES module that can be imported by your application scripts
just like the npm package, but the API is also exported to the global window object,
so for simpler applications you can use it as follows:
```html
<html>
<head>
<script type="module" src="./runtime.js"></script>
<script>
window.onload = function () {
wails.Window.SetTitle("A new window title");
}
</script>
</head>
<!--- ... -->
</html>
```
:::caution
It is important to include the `type="module"` attribute on the `<script>` tag that loads the runtime
and to wait for the page to be fully loaded before calling the API,
because scripts with the `type="module"` attribute run asynchronously.
:::
## Initialisation
Apart from the API functions, the runtime provides support for context menus and window dragging.
These features will only work as expected after the runtime has been initialised.
Even if you don't use the API, make sure to include a side-effect import statement
somewhere in your frontend code:
```javascript
import "@wailsio/runtime";
```
Your bundler should detect the presence of side-effects and include
all required initialisation code in the build.
:::note
If you prefer the pre-built bundle, adding a script tag as shown above suffices.
:::
## API Reference
The runtime is organized into modules, each providing specific functionality. Import only what you need:
```javascript
import { On, Emit } from '@wailsio/runtime/events'
import Window from '@wailsio/runtime/window'
import { SetText, Text } from '@wailsio/runtime/clipboard'
```
### Events
Event system for communication between Go and JavaScript.
#### On()
Register a callback for an event.
```typescript
function On(eventName: string, callback: (event: WailsEvent) => void): () => void
```
**Returns:** Unsubscribe function
**Example:**
```javascript
import { On } from '@wailsio/runtime/events'
const unsubscribe = On('user-logged-in', (event) => {
console.log('User:', event.data.username)
})
// Later: unsubscribe()
```
#### Once()
Register a callback that runs only once.
```typescript
function Once(eventName: string, callback: (event: WailsEvent) => void): () => void
```
**Example:**
```javascript
import { Once } from '@wailsio/runtime/events'
Once('app-ready', () => {
console.log('App initialized')
})
```
#### Emit()
Emit an event to Go backend.
```typescript
function Emit(name: string, data?: any): Promise<void>
```
**Example:**
```javascript
import { Emit } from '@wailsio/runtime/events'
await Emit('button-clicked', { buttonId: 'submit' })
```
#### Off()
Remove event listeners.
```typescript
function Off(...eventNames: string[]): void
```
**Example:**
```javascript
import { Off } from '@wailsio/runtime/events'
Off('user-logged-in', 'user-logged-out')
```
#### OffAll()
Remove all event listeners.
```typescript
function OffAll(): void
```
### Window
Window management methods. The default export is the current window.
```javascript
import Window from '@wailsio/runtime/window'
// Current window
await Window.SetTitle('New Title')
await Window.Center()
// Get another window
const otherWindow = Window.Get('secondary')
await otherWindow.Show()
```
#### Visibility
**Show()** - Shows the window
```typescript
function Show(): Promise<void>
```
**Hide()** - Hides the window
```typescript
function Hide(): Promise<void>
```
**Close()** - Closes the window
```typescript
function Close(): Promise<void>
```
#### Size and Position
**SetSize(width, height)** - Sets window size
```typescript
function SetSize(width: number, height: number): Promise<void>
```
**Size()** - Gets window size
```typescript
function Size(): Promise<{ width: number, height: number }>
```
**SetPosition(x, y)** - Sets absolute position
```typescript
function SetPosition(x: number, y: number): Promise<void>
```
**Position()** - Gets absolute position
```typescript
function Position(): Promise<{ x: number, y: number }>
```
**Center()** - Centers the window
```typescript
function Center(): Promise<void>
```
**Example:**
```javascript
import Window from '@wailsio/runtime/window'
// Resize and center
await Window.SetSize(800, 600)
await Window.Center()
// Get current size
const { width, height } = await Window.Size()
```
#### Window State
**Minimise()** - Minimizes the window
```typescript
function Minimise(): Promise<void>
```
**Maximise()** - Maximizes the window
```typescript
function Maximise(): Promise<void>
```
**Fullscreen()** - Enters fullscreen
```typescript
function Fullscreen(): Promise<void>
```
**Restore()** - Restores from minimized/maximized/fullscreen
```typescript
function Restore(): Promise<void>
```
**IsMinimised()** - Checks if minimized
```typescript
function IsMinimised(): Promise<boolean>
```
**IsMaximised()** - Checks if maximized
```typescript
function IsMaximised(): Promise<boolean>
```
**IsFullscreen()** - Checks if fullscreen
```typescript
function IsFullscreen(): Promise<boolean>
```
#### Window Properties
**SetTitle(title)** - Sets window title
```typescript
function SetTitle(title: string): Promise<void>
```
**Name()** - Gets window name
```typescript
function Name(): Promise<string>
```
**SetBackgroundColour(r, g, b, a)** - Sets background color
```typescript
function SetBackgroundColour(r: number, g: number, b: number, a: number): Promise<void>
```
**SetAlwaysOnTop(alwaysOnTop)** - Keeps window on top
```typescript
function SetAlwaysOnTop(alwaysOnTop: boolean): Promise<void>
```
**SetResizable(resizable)** - Makes window resizable
```typescript
function SetResizable(resizable: boolean): Promise<void>
```
#### Focus and Screen
**Focus()** - Focuses the window
```typescript
function Focus(): Promise<void>
```
**IsFocused()** - Checks if focused
```typescript
function IsFocused(): Promise<boolean>
```
**GetScreen()** - Gets the screen the window is on
```typescript
function GetScreen(): Promise<Screen>
```
#### Content
**Reload()** - Reloads the page
```typescript
function Reload(): Promise<void>
```
**ForceReload()** - Forces page reload (clears cache)
```typescript
function ForceReload(): Promise<void>
```
#### Zoom
**SetZoom(level)** - Sets zoom level
```typescript
function SetZoom(level: number): Promise<void>
```
**GetZoom()** - Gets zoom level
```typescript
function GetZoom(): Promise<number>
```
**ZoomIn()** - Increases zoom
```typescript
function ZoomIn(): Promise<void>
```
**ZoomOut()** - Decreases zoom
```typescript
function ZoomOut(): Promise<void>
```
**ZoomReset()** - Resets zoom to 100%
```typescript
function ZoomReset(): Promise<void>
```
### Clipboard
Clipboard operations.
#### SetText()
Set clipboard text.
```typescript
function SetText(text: string): Promise<void>
```
**Example:**
```javascript
import { SetText } from '@wailsio/runtime/clipboard'
await SetText('Hello from Wails!')
```
#### Text()
Get clipboard text.
```typescript
function Text(): Promise<string>
```
**Example:**
```javascript
import { Text } from '@wailsio/runtime/clipboard'
const clipboardText = await Text()
console.log('Clipboard:', clipboardText)
```
### Application
Application-level methods.
#### Show()
Shows all application windows.
```typescript
function Show(): Promise<void>
```
#### Hide()
Hides all application windows.
```typescript
function Hide(): Promise<void>
```
#### Quit()
Quits the application.
```typescript
function Quit(): Promise<void>
```
**Example:**
```javascript
import { Quit } from '@wailsio/runtime/application'
// Add quit button
document.getElementById('quit-btn').addEventListener('click', async () => {
await Quit()
})
```
### Browser
Open URLs in the default browser.
#### OpenURL()
Opens a URL in the system browser.
```typescript
function OpenURL(url: string | URL): Promise<void>
```
**Example:**
```javascript
import { OpenURL } from '@wailsio/runtime/browser'
await OpenURL('https://wails.io')
```
### Screens
Screen information and management.
#### GetAll()
Gets all screens.
```typescript
function GetAll(): Promise<Screen[]>
```
#### GetPrimary()
Gets the primary screen.
```typescript
function GetPrimary(): Promise<Screen>
```
#### GetCurrent()
Gets the current active screen.
```typescript
function GetCurrent(): Promise<Screen>
```
**Screen Interface:**
```typescript
interface Screen {
ID: string
Name: string
ScaleFactor: number
X: number
Y: number
Size: { Width: number, Height: number }
Bounds: { X: number, Y: number, Width: number, Height: number }
WorkArea: { X: number, Y: number, Width: number, Height: number }
IsPrimary: boolean
Rotation: number
}
```
**Example:**
```javascript
import { GetAll, GetPrimary } from '@wailsio/runtime/screens'
// List all screens
const screens = await GetAll()
screens.forEach(screen => {
console.log(`${screen.Name}: ${screen.Size.Width}x${screen.Size.Height}`)
})
// Get primary screen
const primary = await GetPrimary()
console.log('Primary screen:', primary.Name)
```
### Dialogs
Native OS dialogs from JavaScript.
#### Info()
Shows an information dialog.
```typescript
function Info(options: MessageDialogOptions): Promise<string>
```
**Example:**
```javascript
import { Info } from '@wailsio/runtime/dialogs'
await Info({
Title: 'Success',
Message: 'Operation completed successfully!'
})
```
#### Error()
Shows an error dialog.
```typescript
function Error(options: MessageDialogOptions): Promise<string>
```
#### Warning()
Shows a warning dialog.
```typescript
function Warning(options: MessageDialogOptions): Promise<string>
```
#### Question()
Shows a question dialog with custom buttons.
```typescript
function Question(options: QuestionDialogOptions): Promise<string>
```
**Example:**
```javascript
import { Question } from '@wailsio/runtime/dialogs'
const result = await Question({
Title: 'Confirm Delete',
Message: 'Are you sure you want to delete this file?',
Buttons: [
{ Label: 'Delete', IsDefault: false },
{ Label: 'Cancel', IsDefault: true }
]
})
if (result === 'Delete') {
// Delete the file
}
```
#### OpenFile()
Shows a file open dialog.
```typescript
function OpenFile(options: OpenFileDialogOptions): Promise<string | string[]>
```
**Example:**
```javascript
import { OpenFile } from '@wailsio/runtime/dialogs'
const file = await OpenFile({
Title: 'Select Image',
Filters: [
{ DisplayName: 'Images', Pattern: '*.png;*.jpg;*.jpeg' },
{ DisplayName: 'All Files', Pattern: '*.*' }
]
})
if (file) {
console.log('Selected:', file)
}
```
#### SaveFile()
Shows a file save dialog.
```typescript
function SaveFile(options: SaveFileDialogOptions): Promise<string>
```
### WML (Wails Markup Language)
WML provides declarative attributes for common actions. Add attributes to HTML elements:
#### Attributes
**wml-event** - Emits an event when clicked
```html
<button wml-event="save-clicked">Save</button>
```
**wml-window** - Calls a window method
```html
<button wml-window="Close">Close Window</button>
<button wml-window="Minimise">Minimize</button>
```
**wml-target-window** - Specifies target window for wml-window
```html
<button wml-window="Show" wml-target-window="settings">
Show Settings
</button>
```
**wml-openurl** - Opens a URL in the browser
```html
<a href="#" wml-openurl="https://wails.io">Visit Wails</a>
```
**wml-confirm** - Shows confirmation dialog before action
```html
<button wml-window="Close" wml-confirm="Are you sure you want to close?">
Close
</button>
```
**Example:**
```html
<div>
<button wml-event="save-clicked">Save</button>
<button wml-window="Minimise">Minimize</button>
<button wml-window="Close" wml-confirm="Close window?">Close</button>
<a href="#" wml-openurl="https://github.com/wailsapp/wails">GitHub</a>
</div>
```
## Complete Example
```javascript
import { On, Emit } from '@wailsio/runtime/events'
import Window from '@wailsio/runtime/window'
import { SetText, Text } from '@wailsio/runtime/clipboard'
import { Question } from '@wailsio/runtime/dialogs'
import { GetAll } from '@wailsio/runtime/screens'
// Listen for events from Go
On('data-updated', (event) => {
console.log('Data:', event.data)
updateUI(event.data)
})
// Window management
document.getElementById('center-btn').addEventListener('click', async () => {
await Window.Center()
})
document.getElementById('fullscreen-btn').addEventListener('click', async () => {
const isFullscreen = await Window.IsFullscreen()
if (isFullscreen) {
await Window.UnFullscreen()
} else {
await Window.Fullscreen()
}
})
// Clipboard operations
document.getElementById('copy-btn').addEventListener('click', async () => {
await SetText('Copied from Wails!')
})
// Dialog with confirmation
document.getElementById('delete-btn').addEventListener('click', async () => {
const result = await Question({
Title: 'Confirm',
Message: 'Delete this item?',
Buttons: [
{ Label: 'Delete' },
{ Label: 'Cancel', IsDefault: true }
]
})
if (result === 'Delete') {
await Emit('delete-item', { id: currentItemId })
}
})
// Screen information
const screens = await GetAll()
console.log(`Detected ${screens.length} screen(s)`)
screens.forEach(screen => {
console.log(`- ${screen.Name}: ${screen.Size.Width}x${screen.Size.Height}`)
})
```
## Best Practices
### ✅ Do
- **Import selectively** - Only import what you need
- **Handle promises** - All methods return promises
- **Use WML for simple actions** - Cleaner than JavaScript
- **Check return values** - Especially for dialogs
- **Unsubscribe events** - Clean up when done
### ❌ Don't
- **Don't forget await** - Most methods are async
- **Don't block UI** - Use async/await properly
- **Don't ignore errors** - Always handle rejections
## TypeScript Support
The runtime includes full TypeScript definitions:
```typescript
import { On, WailsEvent } from '@wailsio/runtime/events'
import Window from '@wailsio/runtime/window'
On('custom-event', (event: WailsEvent) => {
// TypeScript knows event.data, event.name, event.sender
console.log(event.data)
})
// All methods are fully typed
const size: { width: number, height: number } = await Window.Size()
```

View file

@ -1,178 +0,0 @@
---
title: Menu API
description: Complete reference for the Menu API
sidebar:
order: 3
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Menu API provides methods to create and manage application menus, context menus, and system tray menus.
## Creating Menus
### NewMenu()
```go
menu := app.NewMenu()
```
### Add()
```go
item := menu.Add("Menu Item")
```
### AddSubmenu()
```go
submenu := menu.AddSubmenu("File")
```
### AddSeparator()
```go
menu.AddSeparator()
```
## Menu Item Methods
### OnClick()
```go
item.OnClick(func(ctx *application.Context) {
fmt.Println("Clicked!")
})
```
### SetLabel()
```go
item.SetLabel("New Label")
```
### SetEnabled()
```go
item.SetEnabled(false)
menu.Update() // Important!
```
### SetChecked()
```go
item.SetChecked(true)
```
### SetAccelerator()
```go
item.SetAccelerator("Ctrl+S")
```
## Application Menu
### SetApplicationMenu()
```go
menu := app.NewMenu()
fileMenu := menu.AddSubmenu("File")
fileMenu.Add("Open").OnClick(openFile)
fileMenu.Add("Quit").OnClick(func(ctx *application.Context) {
app.Quit()
})
app.SetApplicationMenu(menu)
```
## Context Menu
### RegisterContextMenu()
```go
menu := app.NewMenu()
menu.Add("Copy").OnClick(copyHandler)
menu.Add("Paste").OnClick(pasteHandler)
app.RegisterContextMenu("myContext", menu)
```
## System Tray Menu
```go
tray := app.NewSystemTray()
menu := app.NewMenu()
menu.Add("Show").OnClick(showWindow)
menu.Add("Quit").OnClick(app.Quit)
tray.SetMenu(menu)
```
## Complete Example
```go
// Create application menu
menu := app.NewMenu()
// File menu
fileMenu := menu.AddSubmenu("File")
fileMenu.Add("New").SetAccelerator("Ctrl+N").OnClick(newFile)
fileMenu.Add("Open").SetAccelerator("Ctrl+O").OnClick(openFile)
fileMenu.AddSeparator()
fileMenu.Add("Quit").SetAccelerator("Ctrl+Q").OnClick(func(ctx *application.Context) {
app.Quit()
})
// Edit menu
editMenu := menu.AddSubmenu("Edit")
editMenu.Add("Cut").SetAccelerator("Ctrl+X").OnClick(cut)
editMenu.Add("Copy").SetAccelerator("Ctrl+C").OnClick(copy)
editMenu.Add("Paste").SetAccelerator("Ctrl+V").OnClick(paste)
// Set as application menu
app.SetApplicationMenu(menu)
```
## Best Practices
### ✅ Do
- Call `menu.Update()` after changing item state
- Use standard accelerators
- Provide clear labels
- Group related items
### ❌ Don't
- Don't forget to call Update()
- Don't use ambiguous labels
- Don't overcomplicate menus
## Next Steps
<CardGrid>
<Card title="Application Menus" icon="list">
Learn about application menus.
[Learn More →](/learn/application-menu)
</Card>
<Card title="Context Menus" icon="puzzle">
Create context menus.
[Learn More →](/learn/context-menu)
</Card>
<Card title="System Tray" icon="star">
System tray menus.
[Learn More →](/learn/system-tray-menu)
</Card>
<Card title="Menu Patterns" icon="open-book">
Common menu patterns.
[Learn More →](/learn/menu-patterns)
</Card>
</CardGrid>

View file

@ -0,0 +1,724 @@
---
title: Menu API
description: Complete reference for the Menu API
sidebar:
order: 3
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Menu API provides methods to create and manage application menus, context menus, and system tray menus.
**Menu Types:**
- **Application Menus** - Top menu bar (File, Edit, etc.)
- **Context Menus** - Right-click menus
- **System Tray Menus** - Menus in the system tray/notification area
## Creating Menus
### NewMenu()
Creates a new menu.
```go
func (a *App) NewMenu() *Menu
```
**Example:**
```go
menu := app.NewMenu()
```
## Menu Methods
### Add()
Adds a menu item to the menu.
```go
func (m *Menu) Add(label string) *MenuItem
```
**Parameters:**
- `label` - The text displayed for the menu item
**Returns:** The created menu item
**Example:**
```go
item := menu.Add("Open File")
item.OnClick(func(ctx *application.Context) {
// Handle click
})
```
### AddSubmenu()
Adds a submenu to the menu.
```go
func (m *Menu) AddSubmenu(label string) *Menu
```
**Parameters:**
- `label` - The submenu label
**Returns:** The created submenu
**Example:**
```go
fileMenu := menu.AddSubmenu("File")
fileMenu.Add("New")
fileMenu.Add("Open")
fileMenu.Add("Save")
```
### AddSeparator()
Adds a visual separator line between menu items.
```go
func (m *Menu) AddSeparator()
```
**Example:**
```go
menu.Add("Copy")
menu.Add("Paste")
menu.AddSeparator()
menu.Add("Select All")
```
**Best practice:** Use separators to group related menu items.
### AddCheckbox()
Adds a checkable menu item.
```go
func (m *Menu) AddCheckbox(label string, checked bool) *MenuItem
```
**Parameters:**
- `label` - The checkbox label
- `checked` - Initial checked state
**Example:**
```go
darkMode := menu.AddCheckbox("Dark Mode", false)
darkMode.OnClick(func(ctx *application.Context) {
isChecked := darkMode.Checked()
// Toggle dark mode
})
```
### AddRadio()
Adds a radio menu item (mutually exclusive group).
```go
func (m *Menu) AddRadio(label string, checked bool) *MenuItem
```
**Parameters:**
- `label` - The radio button label
- `checked` - Initial checked state
**Example:**
```go
// Create radio group for view modes
viewMenu := menu.AddSubmenu("View")
listView := viewMenu.AddRadio("List View", true)
gridView := viewMenu.AddRadio("Grid View", false)
treeView := viewMenu.AddRadio("Tree View", false)
listView.OnClick(func(ctx *application.Context) {
setViewMode("list")
})
gridView.OnClick(func(ctx *application.Context) {
setViewMode("grid")
})
```
### Update()
Updates the menu to reflect any changes made to menu items.
```go
func (m *Menu) Update()
```
**Example:**
```go
item.SetEnabled(false)
menu.Update() // Must call to apply changes
```
**Important:** Always call `Update()` after modifying menu item properties.
## Menu Item Methods
### OnClick()
Registers a click handler for the menu item.
```go
func (mi *MenuItem) OnClick(callback func(ctx *application.Context)) *MenuItem
```
**Parameters:**
- `callback` - Function called when item is clicked
**Returns:** The menu item (for chaining)
**Example:**
```go
item.OnClick(func(ctx *application.Context) {
fmt.Println("Menu item clicked")
app.Logger.Info("User clicked menu item")
})
```
### SetLabel()
Changes the menu item's label.
```go
func (mi *MenuItem) SetLabel(label string) *MenuItem
```
**Example:**
```go
item.SetLabel("Save As...")
menu.Update()
```
### SetEnabled()
Enables or disables the menu item.
```go
func (mi *MenuItem) SetEnabled(enabled bool) *MenuItem
```
**Example:**
```go
// Disable save when no document is open
saveItem.SetEnabled(hasOpenDocument)
menu.Update()
```
**Common pattern:**
```go
// Update menu state based on application state
func updateMenuState() {
saveItem.SetEnabled(hasUnsavedChanges)
undoItem.SetEnabled(canUndo)
redoItem.SetEnabled(canRedo)
menu.Update()
}
```
### SetChecked()
Sets the checked state for checkbox/radio menu items.
```go
func (mi *MenuItem) SetChecked(checked bool) *MenuItem
```
**Example:**
```go
darkModeItem.SetChecked(isDarkModeEnabled)
menu.Update()
```
### Checked()
Returns the current checked state.
```go
func (mi *MenuItem) Checked() bool
```
**Example:**
```go
if darkModeItem.Checked() {
// Dark mode is enabled
}
```
### SetAccelerator()
Sets a keyboard shortcut for the menu item.
```go
func (mi *MenuItem) SetAccelerator(accelerator string) *MenuItem
```
**Parameters:**
- `accelerator` - Keyboard shortcut (e.g., "Ctrl+S", "Cmd+Q")
**Accelerator format:**
- **Modifiers:** `Ctrl`, `Cmd`, `Alt`, `Shift`
- **Keys:** `A-Z`, `0-9`, `F1-F12`, `Enter`, `Backspace`, etc.
- **Platform:** Use `Cmd` on macOS, `Ctrl` on Windows/Linux
**Example:**
```go
saveItem.SetAccelerator("Ctrl+S")
quitItem.SetAccelerator("Ctrl+Q")
newItem.SetAccelerator("Ctrl+N")
```
**Platform-aware example:**
```go
import "runtime"
var quitShortcut string
if runtime.GOOS == "darwin" {
quitShortcut = "Cmd+Q"
} else {
quitShortcut = "Ctrl+Q"
}
quitItem.SetAccelerator(quitShortcut)
```
### SetTooltip()
Sets a tooltip that appears when hovering over the menu item.
```go
func (mi *MenuItem) SetTooltip(tooltip string) *MenuItem
```
**Example:**
```go
item.SetTooltip("Opens a file from disk")
```
### SetHidden()
Shows or hides the menu item.
```go
func (mi *MenuItem) SetHidden(hidden bool) *MenuItem
```
**Example:**
```go
// Hide debug menu in production
debugItem.SetHidden(!isDevelopment)
menu.Update()
```
## Application Menu
### SetApplicationMenu()
Sets the application's main menu bar.
```go
func (a *App) SetApplicationMenu(menu *Menu)
```
**Example:**
```go
menu := app.NewMenu()
// File menu
fileMenu := menu.AddSubmenu("File")
fileMenu.Add("New").SetAccelerator("Ctrl+N").OnClick(newFile)
fileMenu.Add("Open").SetAccelerator("Ctrl+O").OnClick(openFile)
fileMenu.Add("Save").SetAccelerator("Ctrl+S").OnClick(saveFile)
fileMenu.AddSeparator()
fileMenu.Add("Exit").SetAccelerator("Ctrl+Q").OnClick(func(ctx *application.Context) {
app.Quit()
})
// Edit menu
editMenu := menu.AddSubmenu("Edit")
editMenu.Add("Undo").SetAccelerator("Ctrl+Z").OnClick(undo)
editMenu.Add("Redo").SetAccelerator("Ctrl+Y").OnClick(redo)
editMenu.AddSeparator()
editMenu.Add("Cut").SetAccelerator("Ctrl+X").OnClick(cut)
editMenu.Add("Copy").SetAccelerator("Ctrl+C").OnClick(copy)
editMenu.Add("Paste").SetAccelerator("Ctrl+V").OnClick(paste)
app.SetApplicationMenu(menu)
```
**Platform notes:**
- **macOS:** Menu appears in the top menu bar
- **Windows/Linux:** Menu appears in the window title bar
- **macOS:** Automatically adds application menu with app name
## Context Menus
### RegisterContextMenu()
Registers a context menu (right-click menu) with a specific name.
```go
func (a *App) RegisterContextMenu(name string, menu *Menu)
```
**Parameters:**
- `name` - Unique identifier for the context menu
- `menu` - The menu to show
**Go:**
```go
// Create context menu
contextMenu := app.NewMenu()
contextMenu.Add("Cut").OnClick(cut)
contextMenu.Add("Copy").OnClick(copy)
contextMenu.Add("Paste").OnClick(paste)
contextMenu.AddSeparator()
contextMenu.Add("Select All").OnClick(selectAll)
// Register it
app.RegisterContextMenu("editor", contextMenu)
```
**HTML:**
```html
<!-- Trigger context menu on right-click -->
<div data-wails-context-menu="editor">
Right-click here for context menu
</div>
```
**Dynamic context menus:**
```go
// Update context menu based on selection
func updateContextMenu() {
contextMenu := app.NewMenu()
if hasSelection {
contextMenu.Add("Cut").OnClick(cut)
contextMenu.Add("Copy").OnClick(copy)
}
contextMenu.Add("Paste").SetEnabled(hasClipboardContent).OnClick(paste)
app.RegisterContextMenu("editor", contextMenu)
}
```
## System Tray Menu
### NewSystemTray()
Creates a new system tray icon.
```go
func (a *App) NewSystemTray() *SystemTray
```
**Example:**
```go
tray := app.NewSystemTray()
```
### SetIcon()
Sets the system tray icon.
```go
func (st *SystemTray) SetIcon(icon []byte) *SystemTray
```
**Example:**
```go
iconData, _ := os.ReadFile("icon.png")
tray.SetIcon(iconData)
```
### SetMenu()
Sets the menu for the system tray.
```go
func (st *SystemTray) SetMenu(menu *Menu) *SystemTray
```
**Example:**
```go
trayMenu := app.NewMenu()
trayMenu.Add("Show Window").OnClick(func(ctx *application.Context) {
window.Show()
window.SetFocus()
})
trayMenu.Add("Settings").OnClick(openSettings)
trayMenu.AddSeparator()
trayMenu.Add("Quit").OnClick(func(ctx *application.Context) {
app.Quit()
})
tray.SetMenu(trayMenu)
```
### SetTooltip()
Sets the tooltip shown when hovering over the tray icon.
```go
func (st *SystemTray) SetTooltip(tooltip string) *SystemTray
```
**Example:**
```go
tray.SetTooltip("My Application - Running")
```
### OnClick()
Handles left-click on the tray icon.
```go
func (st *SystemTray) OnClick(callback func()) *SystemTray
```
**Example:**
```go
tray.OnClick(func() {
if window.IsVisible() {
window.Hide()
} else {
window.Show()
window.SetFocus()
}
})
```
## Complete Examples
### Standard Application Menu
```go
package main
import (
"github.com/wailsapp/wails/v3/pkg/application"
)
func createMenu(app *application.Application) *application.Menu {
menu := app.NewMenu()
// File menu
fileMenu := menu.AddSubmenu("File")
fileMenu.Add("New").
SetAccelerator("Ctrl+N").
OnClick(func(ctx *application.Context) {
// Create new document
})
fileMenu.Add("Open").
SetAccelerator("Ctrl+O").
OnClick(func(ctx *application.Context) {
// Open file dialog
})
fileMenu.Add("Save").
SetAccelerator("Ctrl+S").
OnClick(func(ctx *application.Context) {
// Save document
})
fileMenu.AddSeparator()
fileMenu.Add("Exit").OnClick(func(ctx *application.Context) {
app.Quit()
})
// Edit menu
editMenu := menu.AddSubmenu("Edit")
editMenu.Add("Undo").SetAccelerator("Ctrl+Z")
editMenu.Add("Redo").SetAccelerator("Ctrl+Y")
editMenu.AddSeparator()
editMenu.Add("Cut").SetAccelerator("Ctrl+X")
editMenu.Add("Copy").SetAccelerator("Ctrl+C")
editMenu.Add("Paste").SetAccelerator("Ctrl+V")
// View menu
viewMenu := menu.AddSubmenu("View")
darkMode := viewMenu.AddCheckbox("Dark Mode", false)
darkMode.OnClick(func(ctx *application.Context) {
// Toggle dark mode
isChecked := darkMode.Checked()
app.Logger.Info("Dark mode", "enabled", isChecked)
})
viewMenu.AddSeparator()
viewMenu.AddRadio("List View", true)
viewMenu.AddRadio("Grid View", false)
viewMenu.AddRadio("Detail View", false)
// Help menu
helpMenu := menu.AddSubmenu("Help")
helpMenu.Add("Documentation").OnClick(func(ctx *application.Context) {
// Open docs
})
helpMenu.Add("About").OnClick(func(ctx *application.Context) {
// Show about dialog
})
return menu
}
func main() {
app := application.New(application.Options{
Name: "Menu Demo",
})
menu := createMenu(app)
app.SetApplicationMenu(menu)
window := app.NewWebviewWindow()
window.Show()
app.Run()
}
```
### System Tray Application
```go
func setupSystemTray(app *application.Application, window *application.Window) {
// Create system tray
tray := app.NewSystemTray()
// Set icon
iconData, _ := os.ReadFile("icon.png")
tray.SetIcon(iconData)
tray.SetTooltip("My App - Running")
// Handle left-click on tray icon
tray.OnClick(func() {
if window.IsVisible() {
window.Hide()
} else {
window.Show()
window.SetFocus()
}
})
// Create tray menu
trayMenu := app.NewMenu()
showItem := trayMenu.Add("Show Window")
showItem.OnClick(func(ctx *application.Context) {
window.Show()
window.SetFocus()
})
trayMenu.AddSeparator()
trayMenu.Add("Settings").OnClick(func(ctx *application.Context) {
// Open settings window
})
trayMenu.Add("About").OnClick(func(ctx *application.Context) {
// Show about dialog
})
trayMenu.AddSeparator()
trayMenu.Add("Quit").OnClick(func(ctx *application.Context) {
app.Quit()
})
tray.SetMenu(trayMenu)
}
```
### Dynamic Menu Updates
```go
type Editor struct {
app *application.Application
menu *application.Menu
undoItem *application.MenuItem
redoItem *application.MenuItem
saveItem *application.MenuItem
undoStack []string
redoStack []string
hasChanges bool
}
func (e *Editor) createMenu() {
e.menu = e.app.NewMenu()
fileMenu := e.menu.AddSubmenu("File")
e.saveItem = fileMenu.Add("Save").SetAccelerator("Ctrl+S")
e.saveItem.OnClick(func(ctx *application.Context) {
e.save()
})
editMenu := e.menu.AddSubmenu("Edit")
e.undoItem = editMenu.Add("Undo").SetAccelerator("Ctrl+Z")
e.undoItem.OnClick(func(ctx *application.Context) {
e.undo()
})
e.redoItem = editMenu.Add("Redo").SetAccelerator("Ctrl+Y")
e.redoItem.OnClick(func(ctx *application.Context) {
e.redo()
})
e.updateMenuState()
e.app.SetApplicationMenu(e.menu)
}
func (e *Editor) updateMenuState() {
// Update menu items based on current state
e.saveItem.SetEnabled(e.hasChanges)
e.undoItem.SetEnabled(len(e.undoStack) > 0)
e.redoItem.SetEnabled(len(e.redoStack) > 0)
e.menu.Update()
}
func (e *Editor) onChange() {
e.hasChanges = true
e.updateMenuState()
}
func (e *Editor) save() {
// Save logic
e.hasChanges = false
e.updateMenuState()
}
```
## Best Practices
### ✅ Do
- **Use standard accelerators** - Follow platform conventions (Ctrl+C for copy, etc.)
- **Call Update() after changes** - Menu won't reflect changes otherwise
- **Group related items** - Use separators to organize menu items
- **Disable unavailable actions** - Don't hide, disable with SetEnabled(false)
- **Use clear labels** - Be concise and descriptive
- **Follow platform conventions** - macOS vs Windows/Linux menu patterns
### ❌ Don't
- **Don't forget Update()** - Most common mistake
- **Don't nest too deeply** - Keep menus 2-3 levels maximum
- **Don't use ambiguous labels** - "Process" vs "Process Document"
- **Don't overcomplicate** - Keep menus simple and focused
- **Don't mix metaphors** - Consistent naming and organization
## Platform-Specific Notes
### macOS
- Application menu automatically added with app name
- Use `Cmd` instead of `Ctrl` for accelerators
- "About", "Preferences", and "Quit" in application menu by default
### Windows/Linux
- No automatic application menu
- Use `Ctrl` for accelerators
- "Exit" typically in File menu

View file

@ -12,98 +12,18 @@ import { Card, CardGrid, Tabs, TabItem } from "@astrojs/starlight/components";
This is the complete API reference for Wails v3. It documents every public type, method, and option available in the framework.
**Organisation:**
- **Application** - Core application APIs
- **Window** - Window creation and management
- **Menu** - Application, context, and system tray menus
- **Events** - Event system and built-in events
- **Dialogs** - File and message dialogs
- **Runtime** - Frontend runtime APIs
- **CLI** - Command-line interface
## Quick Navigation
<CardGrid>
<Card title="Application API" icon="seti:go">
Core application creation and lifecycle.
- `application.New()`
- `application.Options`
- `app.Run()`
- Lifecycle hooks
[View API →](/reference/application/core)
</Card>
<Card title="Window API" icon="laptop">
Window creation and management.
- `app.NewWebviewWindow()`
- `WebviewWindow` methods
- Window options
- Window events
[View API →](/reference/window/webview-window)
</Card>
<Card title="Menu API" icon="list-format">
Menu creation and management.
- Application menus
- Context menus
- System tray menus
- Menu items
[View API →](/reference/menu/application-menu)
</Card>
<Card title="Events API" icon="rocket">
Event system and built-in events.
- `app.EmitEvent()`
- `app.OnEvent()`
- Built-in events
- Custom events
[View API →](/reference/events/system)
</Card>
<Card title="Dialogs API" icon="approve-check">
File and message dialogs.
- File open/save
- Message dialogs
- dialog options
- Platform behaviour
[View API →](/reference/dialogs/file)
</Card>
<Card title="Runtime API" icon="seti:javascript">
Frontend runtime APIs (JavaScript).
- Window management
- Events
- Clipboard
- Browser
[View API →](/reference/runtime/window)
</Card>
<Card title="CLI Reference" icon="seti:shell">
Command-line interface.
- `wails3 init`
- `wails3 dev`
- `wails3 build`
- All commands
[View API →](/reference/cli/init)
</Card>
</CardGrid>
- [Application](/reference/application) - Core application APIs
- [Window](/reference/window) - Window creation and management
- [Menu](/reference/menu) - Application, context, and system tray menus
- [Events](/reference/events) - Event system and built-in events
- [Dialogs](/reference/dialogs) - File and message dialogs
- [Frontend Runtime](/reference/frontend-runtime) - Frontend runtime APIs
- [CLI](/reference/cli) - Command-line interface
## API Conventions
### Go API Conventions
<details>
<summary><strong>Go API Conventions</strong> - For developers new to Go</summary>
#### Naming
@ -145,6 +65,8 @@ app := application.New(application.Options{
})
```
</details>
### JavaScript API Conventions
#### Naming

View file

@ -1,92 +0,0 @@
---
title: Runtime Overview
description: The Wails JavaScript runtime package for frontend integration
sidebar:
order: 1
---
The Wails runtime is the standard library for Wails applications. It provides a
number of features that may be used in your applications, including:
- Window management
- Dialogs
- Browser integration
- Clipboard
- Menus
- System information
- Events
- Context Menus
- Screens
- WML (Wails Markup Language)
The runtime is required for integration between Go and the frontend. There are 2
ways to integrate the runtime:
- Using the `@wailsio/runtime` package
- Using a pre-built bundle
## Using the npm package
The `@wailsio/runtime` package is a JavaScript package that provides access to
the Wails runtime from the frontend. It is used by all standard templates
and is the recommended way to integrate the runtime into your application.
By using the `@wailsio/runtime` package, you will only include the parts of the runtime that you use.
The package is available on npm and can be installed using:
```shell
npm install --save @wailsio/runtime
```
## Using a pre-built bundle
Some projects will not use a Javascript bundler and may prefer to use a
pre-built bundled version of the runtime. This version can be generated locally
using the following command:
```shell
wails3 generate runtime
```
The command will output a `runtime.js` (and `runtime.debug.js`) file in the current
directory. This file is an ES module that can be imported by your application scripts
just like the npm package, but the API is also exported to the global window object,
so for simpler applications you can use it as follows:
```html
<html>
<head>
<script type="module" src="./runtime.js"></script>
<script>
window.onload = function () {
wails.Window.SetTitle("A new window title");
}
</script>
</head>
<!--- ... -->
</html>
```
:::caution
It is important to include the `type="module"` attribute on the `<script>` tag that loads the runtime
and to wait for the page to be fully loaded before calling the API,
because scripts with the `type="module"` attribute run asynchronously.
:::
## Initialisation
Apart from the API functions, the runtime provides support for context menus and window dragging.
These features will only work as expected after the runtime has been initialised.
Even if you don't use the API, make sure to include a side-effect import statement
somewhere in your frontend code:
```javascript
import "@wailsio/runtime";
```
Your bundler should detect the presence of side-effects and include
all required initialisation code in the build.
:::note
If you prefer the pre-built bundle, adding a script tag as shown above suffices.
:::

View file

@ -1,223 +0,0 @@
---
title: Window API
description: Complete reference for the Window API
sidebar:
order: 2
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Window API provides methods to control window appearance, behaviour, and lifecycle. Access through window instances or the Window manager.
## Core Methods
### Show() / Hide()
```go
window.Show() // Show window
window.Hide() // Hide window
```
### Close()
```go
window.Close() // Close window
```
### SetTitle()
```go
window.SetTitle("New Title")
```
### SetSize()
```go
window.SetSize(800, 600)
```
### Size()
```go
width, height := window.Size()
```
### SetPosition()
```go
window.SetPosition(100, 100)
```
### Position()
```go
x, y := window.Position()
```
### Centre()
```go
window.Centre() // Centre on screen
```
### SetFocus()
```go
window.SetFocus() // Bring to front
```
## State Methods
### Minimise() / UnMinimise()
```go
window.Minimise()
window.UnMinimise()
```
### Maximise() / UnMaximise()
```go
window.Maximise()
window.UnMaximise()
```
### Fullscreen() / UnFullscreen()
```go
window.Fullscreen()
window.UnFullscreen()
```
### IsMinimised() / IsMaximised() / IsFullscreen()
```go
if window.IsMinimised() {
window.UnMinimise()
}
```
## Content Methods
### SetURL()
```go
window.SetURL("http://wails.localhost/page.html")
```
### SetHTML()
```go
window.SetHTML("<h1>Hello</h1>")
```
### Reload()
```go
window.Reload() // Reload content
```
## Event Methods
### OnClose()
```go
window.OnClose(func() bool {
// Return false to prevent close
return true
})
```
### OnFocus() / OnBlur()
```go
window.OnFocus(func() {
fmt.Println("Window focused")
})
window.OnBlur(func() {
fmt.Println("Window lost focus")
})
```
### EmitEvent()
```go
window.EmitEvent("custom-event", data)
```
## Properties
### Name()
```go
name := window.Name()
```
### SetEnabled()
```go
window.SetEnabled(false) // Disable window
```
## Complete Example
```go
// Create and configure window
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "My Window",
Width: 800,
Height: 600,
})
// Set up event handlers
window.OnClose(func() bool {
result, _ := app.QuestionDialog().
SetMessage("Close window?").
SetButtons("Yes", "No").
Show()
return result == "Yes"
})
window.OnFocus(func() {
window.SetTitle("My Window (Active)")
})
window.OnBlur(func() {
window.SetTitle("My Window")
})
// Show window
window.Centre()
window.Show()
```
## Next Steps
<CardGrid>
<Card title="Application API" icon="rocket">
Application methods and managers.
[Learn More →](/reference/application-api)
</Card>
<Card title="Window Basics" icon="laptop">
Learn window management.
[Learn More →](/features/windows/basics)
</Card>
<Card title="Window Options" icon="seti:config">
All window configuration options.
[Learn More →](/features/windows/options)
</Card>
<Card title="Events" icon="star">
Window event handling.
[Learn More →](/features/windows/events)
</Card>
</CardGrid>

View file

@ -0,0 +1,644 @@
---
title: Window API
description: Complete reference for the Window API
sidebar:
order: 2
---
import { Card, CardGrid } from "@astrojs/starlight/components";
## Overview
The Window API provides methods to control window appearance, behaviour, and lifecycle. Access through window instances or the Window manager.
**Common operations:**
- Create and show windows
- Control size, position, and state
- Handle window events
- Manage window content
- Configure appearance and behaviour
## Visibility
### Show()
Shows the window. If the window was hidden, it becomes visible.
```go
func (w *Window) Show()
```
**Example:**
```go
window := app.NewWebviewWindow()
window.Show()
```
### Hide()
Hides the window without closing it. The window remains in memory and can be shown again.
```go
func (w *Window) Hide()
```
**Example:**
```go
// Hide window temporarily
window.Hide()
// Show it again later
window.Show()
```
**Use cases:**
- System tray applications that hide to tray
- Wizard flows where windows are reused
- Temporary hiding during operations
### Close()
Closes the window. This triggers the `WindowClosing` event.
```go
func (w *Window) Close()
```
**Example:**
```go
window.Close()
```
**Note:** If a registered hook calls `event.Cancel()`, the close will be prevented.
## Window Properties
### SetTitle()
Sets the window title bar text.
```go
func (w *Window) SetTitle(title string)
```
**Parameters:**
- `title` - The new window title
**Example:**
```go
window.SetTitle("My Application - Document.txt")
```
### Name()
Returns the window's unique name identifier.
```go
func (w *Window) Name() string
```
**Example:**
```go
name := window.Name()
fmt.Println("Window name:", name)
// Retrieve window by name later
window := app.GetWindowByName(name)
```
## Size and Position
### SetSize()
Sets the window dimensions in pixels.
```go
func (w *Window) SetSize(width, height int)
```
**Parameters:**
- `width` - Window width in pixels
- `height` - Window height in pixels
**Example:**
```go
window.SetSize(1024, 768)
```
### Size()
Returns the current window dimensions.
```go
func (w *Window) Size() (width, height int)
```
**Example:**
```go
width, height := window.Size()
fmt.Printf("Window is %dx%d\n", width, height)
```
### SetMinSize() / SetMaxSize()
Sets minimum and maximum window dimensions.
```go
func (w *Window) SetMinSize(width, height int)
func (w *Window) SetMaxSize(width, height int)
```
**Example:**
```go
// Prevent window from being too small
window.SetMinSize(800, 600)
// Prevent window from being too large
window.SetMaxSize(1920, 1080)
```
### SetPosition()
Sets the window position relative to the top-left corner of the screen.
```go
func (w *Window) SetPosition(x, y int)
```
**Parameters:**
- `x` - Horizontal position in pixels
- `y` - Vertical position in pixels
**Example:**
```go
// Position window at top-left
window.SetPosition(0, 0)
// Position window 100px from top-left
window.SetPosition(100, 100)
```
### Position()
Returns the current window position.
```go
func (w *Window) Position() (x, y int)
```
**Example:**
```go
x, y := window.Position()
fmt.Printf("Window is at (%d, %d)\n", x, y)
```
### Centre()
Centers the window on the screen.
```go
func (w *Window) Centre()
```
**Example:**
```go
window := app.NewWebviewWindow()
window.Centre()
window.Show()
```
**Note:** Centre on the primary monitor. For multi-monitor setups, see screen APIs.
### SetFocus()
Brings the window to the front and gives it keyboard focus.
```go
func (w *Window) SetFocus()
```
**Example:**
```go
// Bring window to front
window.SetFocus()
```
## Window State
### Minimise() / UnMinimise()
Minimizes the window to the taskbar/dock or restores it.
```go
func (w *Window) Minimise()
func (w *Window) UnMinimise()
```
**Example:**
```go
// Minimize window
window.Minimise()
// Restore from minimized state
window.UnMinimise()
```
### Maximise() / UnMaximise()
Maximizes the window to fill the screen or restores it to its previous size.
```go
func (w *Window) Maximise()
func (w *Window) UnMaximise()
```
**Example:**
```go
// Maximize window
window.Maximise()
// Restore to previous size
window.UnMaximise()
```
### Fullscreen() / UnFullscreen()
Enters or exits fullscreen mode.
```go
func (w *Window) Fullscreen()
func (w *Window) UnFullscreen()
```
**Example:**
```go
// Enter fullscreen
window.Fullscreen()
// Exit fullscreen
window.UnFullscreen()
```
### IsMinimised() / IsMaximised() / IsFullscreen()
Checks the current window state.
```go
func (w *Window) IsMinimised() bool
func (w *Window) IsMaximised() bool
func (w *Window) IsFullscreen() bool
```
**Example:**
```go
if window.IsMinimised() {
window.UnMinimise()
}
if window.IsMaximised() {
fmt.Println("Window is maximized")
}
if window.IsFullscreen() {
window.UnFullscreen()
}
```
## Window Content
### SetURL()
Navigates to a specific URL within the window.
```go
func (w *Window) SetURL(url string)
```
**Parameters:**
- `url` - URL to navigate to (can be `http://wails.localhost/` for embedded assets)
**Example:**
```go
// Navigate to embedded page
window.SetURL("http://wails.localhost/settings.html")
// Navigate to external URL (if allowed)
window.SetURL("https://wails.io")
```
### SetHTML()
Sets the window content directly from an HTML string.
```go
func (w *Window) SetHTML(html string)
```
**Parameters:**
- `html` - HTML content to display
**Example:**
```go
html := `
<!DOCTYPE html>
<html>
<head><title>Dynamic Content</title></head>
<body>
<h1>Hello from Go!</h1>
<p>This content was generated dynamically.</p>
</body>
</html>
`
window.SetHTML(html)
```
**Use cases:**
- Dynamic content generation
- Simple windows without frontend build process
- Error pages or splash screens
### Reload()
Reloads the current window content.
```go
func (w *Window) Reload()
```
**Example:**
```go
// Reload current page
window.Reload()
```
**Note:** Useful during development or when content needs refreshing.
## Window Events
Wails provides two methods for handling window events:
- **OnWindowEvent()** - Listen to window events (cannot prevent them)
- **RegisterHook()** - Hook into window events (can prevent them by calling `event.Cancel()`)
### OnWindowEvent()
Registers a callback for window events.
```go
func (w *Window) OnWindowEvent(
eventType events.WindowEventType,
callback func(event *WindowEvent),
) func()
```
**Returns:** Cleanup function to remove the event listener
**Example:**
```go
import "github.com/wailsapp/wails/v3/pkg/events"
// Listen for window focus
window.OnWindowEvent(events.Common.WindowFocus, func(e *application.WindowEvent) {
app.Logger.Info("Window gained focus")
})
// Listen for window lost focus
window.OnWindowEvent(events.Common.WindowLostFocus, func(e *application.WindowEvent) {
app.Logger.Info("Window lost focus")
})
// Listen for window resize
window.OnWindowEvent(events.Common.WindowDidResize, func(e *application.WindowEvent) {
app.Logger.Info("Window resized")
})
```
**Common Window Events:**
- `events.Common.WindowClosing` - Window is about to close
- `events.Common.WindowFocus` - Window gained focus
- `events.Common.WindowLostFocus` - Window lost focus
- `events.Common.WindowDidMove` - Window moved
- `events.Common.WindowDidResize` - Window resized
- `events.Common.WindowMinimise` - Window minimized
- `events.Common.WindowMaximise` - Window maximized
- `events.Common.WindowFullscreen` - Window entered fullscreen
### RegisterHook()
Registers a hook for window events. Hooks run before listeners and can prevent the event by calling `event.Cancel()`.
```go
func (w *Window) RegisterHook(
eventType events.WindowEventType,
callback func(event *WindowEvent),
) func()
```
**Returns:** Cleanup function to remove the hook
**Example - Prevent window close:**
```go
import "github.com/wailsapp/wails/v3/pkg/events"
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
result, _ := app.QuestionDialog().
SetTitle("Confirm Close").
SetMessage("Are you sure you want to close this window?").
SetButtons("Yes", "No").
Show()
if result == "No" {
e.Cancel() // Prevent window from closing
}
})
```
**Example - Save before closing:**
```go
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
if hasUnsavedChanges {
result, _ := app.QuestionDialog().
SetMessage("Save changes before closing?").
SetButtons("Save", "Don't Save", "Cancel").
Show()
switch result {
case "Save":
saveData()
// Allow close
case "Don't Save":
// Allow close
case "Cancel":
e.Cancel() // Prevent close
}
}
})
```
### EmitEvent()
Emits a custom event to the window's frontend.
```go
func (w *Window) EmitEvent(name string, data ...interface{})
```
**Parameters:**
- `name` - Event name
- `data` - Optional data to send with the event
**Example:**
```go
// Send data to specific window
window.EmitEvent("data-updated", map[string]interface{}{
"count": 42,
"status": "success",
})
```
**Frontend (JavaScript):**
```javascript
import { On } from '@wailsio/runtime'
On('data-updated', (data) => {
console.log('Count:', data.count)
console.log('Status:', data.status)
})
```
## Other Methods
### SetEnabled()
Enables or disables user interaction with the window.
```go
func (w *Window) SetEnabled(enabled bool)
```
**Example:**
```go
// Disable window during long operation
window.SetEnabled(false)
// Perform operation
performLongOperation()
// Re-enable window
window.SetEnabled(true)
```
### SetBackgroundColour()
Sets the window background color (shown before content loads).
```go
func (w *Window) SetBackgroundColour(r, g, b, a uint8)
```
**Parameters:**
- `r` - Red (0-255)
- `g` - Green (0-255)
- `b` - Blue (0-255)
- `a` - Alpha/opacity (0-255)
**Example:**
```go
// Set white background
window.SetBackgroundColour(255, 255, 255, 255)
// Set dark background
window.SetBackgroundColour(30, 30, 30, 255)
```
### SetResizable()
Controls whether the window can be resized by the user.
```go
func (w *Window) SetResizable(resizable bool)
```
**Example:**
```go
// Make window fixed size
window.SetResizable(false)
```
### SetAlwaysOnTop()
Sets whether the window stays above other windows.
```go
func (w *Window) SetAlwaysOnTop(alwaysOnTop bool)
```
**Example:**
```go
// Keep window on top
window.SetAlwaysOnTop(true)
```
## Complete Example
```go
package main
import (
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
func main() {
app := application.New(application.Options{
Name: "Window API Demo",
})
// Create window with options
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "My Application",
Width: 1024,
Height: 768,
MinWidth: 800,
MinHeight: 600,
BackgroundColour: application.NewRGB(255, 255, 255),
URL: "http://wails.localhost/",
})
// Configure window behavior
window.SetResizable(true)
window.SetMinSize(800, 600)
window.SetMaxSize(1920, 1080)
// Set up event hooks
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
// Confirm before closing
result, _ := app.QuestionDialog().
SetTitle("Confirm Close").
SetMessage("Are you sure you want to close this window?").
SetButtons("Yes", "No").
Show()
if result == "No" {
e.Cancel()
}
})
// Listen for window events
window.OnWindowEvent(events.Common.WindowFocus, func(e *application.WindowEvent) {
window.SetTitle("My Application (Active)")
app.Logger.Info("Window gained focus")
})
window.OnWindowEvent(events.Common.WindowLostFocus, func(e *application.WindowEvent) {
window.SetTitle("My Application")
app.Logger.Info("Window lost focus")
})
// Position and show window
window.Centre()
window.Show()
app.Run()
}
```