This commit is contained in:
Lea Anthony
2023-12-09 17:29:56 +11:00
parent 07fc8e3707
commit 6a8322cdb5
526 changed files with 42791 additions and 249 deletions
@@ -0,0 +1,4 @@
{
"label": "Runtime",
"position": 1
}
@@ -0,0 +1,14 @@
---
sidebar_position: 7
---
# Browser
These methods are related to the system browser.
### BrowserOpenURL
Opens the given URL in the system browser.
Go: `BrowserOpenURL(ctx context.Context, url string)`<br/>
JS: `BrowserOpenURL(url string)`
@@ -0,0 +1,28 @@
---
sidebar_position: 8
---
# Clipboard
This part of the runtime provides access to the operating system's clipboard.<br/>
The current implementation only handles text.
### ClipboardGetText
This method reads the currently stored text from the clipboard.
Go: `ClipboardGetText(ctx context.Context) (string, error)`<br/>
Returns: a string (if the clipboard is empty an empty string will be returned) or an error.
JS: `ClipboardGetText(): Promise<string>`<br/>
Returns: a promise with a string result (if the clipboard is empty an empty string will be returned).
### ClipboardSetText
This method writes a text to the clipboard.
Go: `ClipboardSetText(ctx context.Context, text string) error`<br/>
Returns: an error if there is any.
JS: `ClipboardSetText(text: string): Promise<boolean>`<br/>
Returns: a promise with true result if the text was successfully set on the clipboard, false otherwise.
@@ -0,0 +1,309 @@
---
sidebar_position: 5
---
# Dialog
This part of the runtime provides access to native dialogs, such as File Selectors and Message boxes.
:::info JavaScript
Dialog is currently unsupported in the JS runtime.
:::
### OpenDirectoryDialog
Opens a dialog that prompts the user to select a directory. Can be customised using [OpenDialogOptions](#opendialogoptions).
Go: `OpenDirectoryDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)`
Returns: Selected directory (blank if the user cancelled) or an error
### OpenFileDialog
Opens a dialog that prompts the user to select a file. Can be customised using [OpenDialogOptions](#opendialogoptions).
Go: `OpenFileDialog(ctx context.Context, dialogOptions OpenDialogOptions) (string, error)`
Returns: Selected file (blank if the user cancelled) or an error
### OpenMultipleFilesDialog
Opens a dialog that prompts the user to select multiple files. Can be customised using [OpenDialogOptions](#opendialogoptions).
Go: `OpenMultipleFilesDialog(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error)`
Returns: Selected files (nil if the user cancelled) or an error
### SaveFileDialog
Opens a dialog that prompts the user to select a filename for the purposes of saving. Can be customised using [SaveDialogOptions](#savedialogoptions).
Go: `SaveFileDialog(ctx context.Context, dialogOptions SaveDialogOptions) (string, error)`
Returns: The selected file (blank if the user cancelled) or an error
### MessageDialog
Displays a message using a message dialog. Can be customised using [MessageDialogOptions](#messagedialogoptions).
Go: `MessageDialog(ctx context.Context, dialogOptions MessageDialogOptions) (string, error)`
Returns: The text of the selected button or an error
## Options
### OpenDialogOptions
```go
type OpenDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
ResolvesAliases bool
TreatPackagesAsDirectories bool
}
```
| Field | Description | Win | Mac | Lin |
| -------------------------- | ---------------------------------------------- | --- | --- | --- |
| DefaultDirectory | The directory the dialog will show when opened | ✅ | ✅ | ✅ |
| DefaultFilename | The default filename | ✅ | ✅ | ✅ |
| Title | Title for the dialog | ✅ | ✅ | ✅ |
| [Filters](#filefilter) | A list of file filters | ✅ | ✅ | ✅ |
| ShowHiddenFiles | Show files hidden by the system | | ✅ | ✅ |
| CanCreateDirectories | Allow user to create directories | | ✅ | |
| ResolvesAliases | If true, returns the file not the alias | | ✅ | |
| TreatPackagesAsDirectories | Allow navigating into packages | | ✅ | |
### SaveDialogOptions
```go
type SaveDialogOptions struct {
DefaultDirectory string
DefaultFilename string
Title string
Filters []FileFilter
ShowHiddenFiles bool
CanCreateDirectories bool
TreatPackagesAsDirectories bool
}
```
| Field | Description | Win | Mac | Lin |
| -------------------------- | ---------------------------------------------- | --- | --- | --- |
| DefaultDirectory | The directory the dialog will show when opened | ✅ | ✅ | ✅ |
| DefaultFilename | The default filename | ✅ | ✅ | ✅ |
| Title | Title for the dialog | ✅ | ✅ | ✅ |
| [Filters](#filefilter) | A list of file filters | ✅ | ✅ | ✅ |
| ShowHiddenFiles | Show files hidden by the system | | ✅ | ✅ |
| CanCreateDirectories | Allow user to create directories | | ✅ | |
| TreatPackagesAsDirectories | Allow navigating into packages | | ✅ | |
### MessageDialogOptions
```go
type MessageDialogOptions struct {
Type DialogType
Title string
Message string
Buttons []string
DefaultButton string
CancelButton string
}
```
| Field | Description | Win | Mac | Lin |
|---------------|----------------------------------------------------------------------------|----------------|-----|-----|
| Type | The type of message dialog, eg question, info... | ✅ | ✅ | ✅ |
| Title | Title for the dialog | ✅ | ✅ | ✅ |
| Message | The message to show the user | ✅ | ✅ | ✅ |
| Buttons | A list of button titles | | ✅ | |
| DefaultButton | The button with this text should be treated as default. Bound to `return`. | ✅[*](#windows) | ✅ | |
| CancelButton | The button with this text should be treated as cancel. Bound to `escape` | | ✅ | |
#### Windows
Windows has standard dialog types in which the buttons are not customisable.
The value returned will be one of: "Ok", "Cancel", "Abort", "Retry", "Ignore", "Yes", "No", "Try Again" or "Continue".
For Question dialogs, the default button is "Yes" and the cancel button is "No".
This can be changed by setting the `DefaultButton` value to `"No"`.
Example:
```go
result, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Question",
Message: "Do you want to continue?",
DefaultButton: "No",
})
```
#### Linux
Linux has standard dialog types in which the buttons are not customisable.
The value returned will be one of: "Ok", "Cancel", "Yes", "No"
#### Mac
A message dialog on Mac may specify up to 4 buttons. If no `DefaultButton` or `CancelButton` is given, the first button
is considered default and is bound to the `return` key.
For the following code:
```go
selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
})
```
the first button is shown as default:
```mdx-code-block
<div class="text--center">
<img
src={require("@site/static/img/runtime/dialog_no_defaults.png").default}
width="30%"
class="screenshot"
/>
</div>
<br />
```
And if we specify `DefaultButton` to be "two":
```go
selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
DefaultButton: "two",
})
```
the second button is shown as default. When `return` is pressed, the value "two" is returned.
```mdx-code-block
<div class="text--center">
<img
src={require("@site/static/img/runtime/dialog_default_button.png").default}
width="30%"
class="screenshot"
/>
</div>
<br />
```
If we now specify `CancelButton` to be "three":
```go
selection, err := runtime.MessageDialog(b.ctx, runtime.MessageDialogOptions{
Title: "It's your turn!",
Message: "Select a number",
Buttons: []string{"one", "two", "three", "four"},
DefaultButton: "two",
CancelButton: "three",
})
```
the button with "three" is shown at the bottom of the dialog. When `escape` is pressed, the value "three" is returned:
```mdx-code-block
<div class="text--center">
<img
src={require("@site/static/img/runtime/dialog_default_cancel.png").default}
width="30%"
class="screenshot"
/>
</div>
<br />
<br />
<br />
```
#### DialogType
```go
const (
InfoDialog DialogType = "info"
WarningDialog DialogType = "warning"
ErrorDialog DialogType = "error"
QuestionDialog DialogType = "question"
)
```
### FileFilter
```go
type FileFilter struct {
DisplayName string // Filter information EG: "Image Files (*.jpg, *.png)"
Pattern string // semi-colon separated list of extensions, EG: "*.jpg;*.png"
}
```
#### Windows
Windows allows you to use multiple file filters in dialog boxes. Each FileFilter will show up as a separate entry in the
dialog:
```mdx-code-block
<div class="text--center">
<img
src={require("@site/static/img/runtime/dialog_win_filters.png").default}
width="50%"
class="screenshot"
/>
</div>
<br />
<br />
<br />
```
#### Linux
Linux allows you to use multiple file filters in dialog boxes. Each FileFilter will show up as a separate entry in the
dialog:
```mdx-code-block
<div class="text--center">
<img
src={require("@site/static/img/runtime/dialog_lin_filters.png").default}
width="50%"
class="screenshot"
/>
</div>
<br />
<br />
<br />
```
#### Mac
Mac dialogs only have the concept of a single set of patterns to filter files. If multiple FileFilters are provided,
Wails will use all the Patterns defined.
Example:
```go
selection, err := runtime.OpenFileDialog(b.ctx, runtime.OpenDialogOptions{
Title: "Select File",
Filters: []runtime.FileFilter{
{
DisplayName: "Images (*.png;*.jpg)",
Pattern: "*.png;*.jpg",
}, {
DisplayName: "Videos (*.mov;*.mp4)",
Pattern: "*.mov;*.mp4",
},
},
})
```
This will result in the Open File dialog using `*.png,*.jpg,*.mov,*.mp4` as a filter.
@@ -0,0 +1,47 @@
---
sidebar_position: 2
---
# Events
The Wails runtime provides a unified events system, where events can be emitted or received by either Go or JavaScript.
Optionally, data may be passed with the events. Listeners will receive the data in the local data types.
### EventsOn
This method sets up a listener for the given event name. When an event of type `eventName` is [emitted](#EventsEmit),
the callback is triggered. Any additional data sent with the emitted event will be passed to the callback. It returns
a function to cancel the listener.
Go: `EventsOn(ctx context.Context, eventName string, callback func(optionalData ...interface{})) func()`<br/>
JS: `EventsOn(eventName string, callback function(optionalData?: any)): () => void`
### EventsOff
This method unregisters the listener for the given event name, optionally multiple listeneres can be unregistered via `additionalEventNames`.
Go: `EventsOff(ctx context.Context, eventName string, additionalEventNames ...string)`<br/>
JS: `EventsOff(eventName string, ...additionalEventNames)`
### EventsOnce
This method sets up a listener for the given event name, but will only trigger once. It returns a function to cancel
the listener.
Go: `EventsOnce(ctx context.Context, eventName string, callback func(optionalData ...interface{})) func()`<br/>
JS: `EventsOnce(eventName string, callback function(optionalData?: any)): () => void`
### EventsOnMultiple
This method sets up a listener for the given event name, but will only trigger a maximum of `counter` times. It returns
a function to cancel the listener.
Go: `EventsOnMultiple(ctx context.Context, eventName string, callback func(optionalData ...interface{}), counter int) func()`<br/>
JS: `EventsOnMultiple(eventName string, callback function(optionalData?: any), counter int): () => void`
### EventsEmit
This method emits the given event. Optional data may be passed with the event. This will trigger any event listeners.
Go: `EventsEmit(ctx context.Context, eventName string, optionalData ...interface{})`<br/>
JS: `EventsEmit(eventName: string, ...optionalData: any)`
@@ -0,0 +1,100 @@
---
sidebar_position: 1
---
# Introduction
The runtime is a library that provides utility methods for your application. There is both a Go and JavaScript runtime
and the aim is to try and keep them at parity where possible.
It has utility methods for:
- [Window](window.mdx)
- [Menu](menu.mdx)
- [Dialog](dialog.mdx)
- [Events](events.mdx)
- [Browser](browser.mdx)
- [Log](log.mdx)
- [Clipboard](clipboard.mdx)
The Go Runtime is available through importing `github.com/wailsapp/wails/v2/pkg/runtime`. All methods in this package
take a context as the first parameter. This context should be obtained from the [OnStartup](../options.mdx#onstartup)
or [OnDomReady](../options.mdx#ondomready) hooks.
:::info Note
Whilst the context will be provided to the
[OnStartup](../options.mdx#onstartup) method, there's no guarantee the runtime will work in this method as
the window is initialising in a different thread. If
you wish to call runtime methods at startup, use [OnDomReady](../options.mdx#ondomready).
:::
The JavaScript library is available to the frontend via the `window.runtime` map. There is a runtime package generated when using `dev`
mode that provides TypeScript declarations for the runtime. This should be located in the `wailsjs` directory in your
frontend directory.
### Hide
Go: `Hide(ctx context.Context)`<br/>
JS: `Hide()`
Hides the application.
:::info Note
On Mac, this will hide the application in the same way as the `Hide` menu item in standard Mac applications.
This is different to hiding the window, but the application still being in the foreground.
For Windows and Linux, this is currently the same as `WindowHide`.
:::
### Show
Shows the application.
:::info Note
On Mac, this will bring the application back into the foreground.
For Windows and Linux, this is currently the same as `WindowShow`.
:::
Go: `Show(ctx context.Context)`<br/>
JS: `Show()`
### Quit
Quits the application.
Go: `Quit(ctx context.Context)`<br/>
JS: `Quit()`
### Environment
Returns details of the current environment.
Go: `Environment(ctx context.Context) EnvironmentInfo`<br/>
JS: `Environment(): Promise<EnvironmentInfo>`
#### EnvironmentInfo
Go:
```go
type EnvironmentInfo struct {
BuildType string
Platform string
Arch string
}
```
JS:
```ts
interface EnvironmentInfo {
buildType: string;
platform: string;
arch: string;
}
```
@@ -0,0 +1,142 @@
---
sidebar_position: 3
---
# Log
The Wails runtime provides a logging mechanism that may be called from Go or JavaScript. Like most
loggers, there are a number of log levels:
- Trace
- Debug
- Info
- Warning
- Error
- Fatal
The logger will output any log message at the current, or higher, log level. Example: The `Debug` log
level will output all messages except `Trace` messages.
### LogPrint
Logs the given message as a raw message.
Go: `LogPrint(ctx context.Context, message string)`<br/>
JS: `LogPrint(message: string)`
### LogPrintf
Logs the given message as a raw message.
Go: `LogPrintf(ctx context.Context, format string, args ...interface{})`<br/>
### LogTrace
Logs the given message at the `Trace` log level.
Go: `LogTrace(ctx context.Context, message string)`<br/>
JS: `LogTrace(message: string)`
### LogTracef
Logs the given message at the `Trace` log level.
Go: `LogTracef(ctx context.Context, format string, args ...interface{})`<br/>
### LogDebug
Logs the given message at the `Debug` log level.
Go: `LogDebug(ctx context.Context, message string)`<br/>
JS: `LogDebug(message: string)`
### LogDebugf
Logs the given message at the `Debug` log level.
Go: `LogDebugf(ctx context.Context, format string, args ...interface{})`<br/>
### LogInfo
Logs the given message at the `Info` log level.
Go: `LogInfo(ctx context.Context, message string)`<br/>
JS: `LogInfo(message: string)`
### LogInfof
Logs the given message at the `Info` log level.
Go: `LogInfof(ctx context.Context, format string, args ...interface{})`<br/>
### LogWarning
Logs the given message at the `Warning` log level.
Go: `LogWarning(ctx context.Context, message string)`<br/>
JS: `LogWarning(message: string)`
### LogWarningf
Logs the given message at the `Warning` log level.
Go: `LogWarningf(ctx context.Context, format string, args ...interface{})`<br/>
### LogError
Logs the given message at the `Error` log level.
Go: `LogError(ctx context.Context, message string)`<br/>
JS: `LogError(message: string)`
### LogErrorf
Logs the given message at the `Error` log level.
Go: `LogErrorf(ctx context.Context, format string, args ...interface{})`<br/>
### LogFatal
Logs the given message at the `Fatal` log level.
Go: `LogFatal(ctx context.Context, message string)`<br/>
JS: `LogFatal(message: string)`
### LogFatalf
Logs the given message at the `Fatal` log level.
Go: `LogFatalf(ctx context.Context, format string, args ...interface{})`<br/>
### LogSetLogLevel
Sets the log level. In JavaScript, the number relates to the following log levels:
| Value | Log Level |
| ----- | --------- |
| 1 | Trace |
| 2 | Debug |
| 3 | Info |
| 4 | Warning |
| 5 | Error |
Go: `LogSetLogLevel(ctx context.Context, level logger.LogLevel)`<br/>
JS: `LogSetLogLevel(level: number)`
## Using a Custom Logger
A custom logger may be used by providing it using the [Logger](../options.mdx#logger)
application option. The only requirement is that the logger implements the `logger.Logger` interface
defined in `github.com/wailsapp/wails/v2/pkg/logger`:
```go title="logger.go"
type Logger interface {
Print(message string)
Trace(message string)
Debug(message string)
Info(message string)
Warning(message string)
Error(message string)
Fatal(message string)
}
```
@@ -0,0 +1,25 @@
---
sidebar_position: 6
---
# Menu
These methods are related to the application menu.
:::info JavaScript
Menu is currently unsupported in the JS runtime.
:::
### MenuSetApplicationMenu
Sets the application menu to the given [menu](../menus.mdx).
Go: `MenuSetApplicationMenu(ctx context.Context, menu *menu.Menu)`
### MenuUpdateApplicationMenu
Updates the application menu, picking up any changes to the menu passed to `MenuSetApplicationMenu`.
Go: `MenuUpdateApplicationMenu(ctx context.Context)`
@@ -0,0 +1,37 @@
---
sidebar_position: 9
---
# Screen
These methods provide information about the currently connected screens.
### ScreenGetAll
Returns a list of currently connected screens.
Go: `ScreenGetAll(ctx context.Context) []screen`<br/>
JS: `ScreenGetAll()`
#### Screen
Go struct:
```go
type Screen struct {
IsCurrent bool
IsPrimary bool
Width int
Height int
}
```
Typescript interface:
```ts
interface Screen {
isCurrent: boolean;
isPrimary: boolean;
width : number
height : number
}
```
@@ -0,0 +1,261 @@
---
sidebar_position: 4
---
# Window
These methods give control of the application window.
### WindowSetTitle
Sets the text in the window title bar.
Go: `WindowSetTitle(ctx context.Context, title string)`<br/>
JS: `WindowSetTitle(title: string)`
### WindowFullscreen
Makes the window full screen.
Go: `WindowFullscreen(ctx context.Context)`<br/>
JS: `WindowFullscreen()`
### WindowUnfullscreen
Restores the previous window dimensions and position prior to full screen.
Go: `WindowUnfullscreen(ctx context.Context)`<br/>
JS: `WindowUnfullscreen()`
### WindowIsFullscreen
Returns true if the window is full screen.
Go: `WindowIsFullscreen(ctx context.Context) bool`<br/>
JS: `WindowIsFullscreen() bool`
### WindowCenter
Centers the window on the monitor the window is currently on.
Go: `WindowCenter(ctx context.Context)`<br/>
JS: `WindowCenter()`
### WindowExecJS
Executes arbitrary JS code in the window.
This method runs the code in the browser asynchronously and returns immediately.
If the script causes any errors, they will only be available in the browser console.
Go: `WindowExecJS(ctx context.Context, js string)`
### WindowReload
Performs a "reload" (Reloads current page).
Go: `WindowReload(ctx context.Context)`<br/>
JS: `WindowReload()`
### WindowReloadApp
Reloads the application frontend.
Go: `WindowReloadApp(ctx context.Context)`<br/>
JS: `WindowReloadApp()`
### WindowSetSystemDefaultTheme
Windows only.
Go: `WindowSetSystemDefaultTheme(ctx context.Context)`<br/>
JS: `WindowSetSystemDefaultTheme()`
Sets window theme to system default (dark/light).
### WindowSetLightTheme
Windows only.
Go: `WindowSetLightTheme(ctx context.Context)`<br/>
JS: `WindowSetLightTheme()`
Sets window theme to light.
### WindowSetDarkTheme
Windows only.
Go: `WindowSetDarkTheme(ctx context.Context)`<br/>
JS: `WindowSetDarkTheme()`
Sets window theme to dark.
### WindowShow
Shows the window, if it is currently hidden.
Go: `WindowShow(ctx context.Context)`<br/>
JS: `WindowShow()`
### WindowHide
Hides the window, if it is currently visible.
Go: `WindowHide(ctx context.Context)`<br/>
JS: `WindowHide()`
### WindowIsNormal
Returns true if the window not minimised, maximised or fullscreen.
Go: `WindowIsNormal(ctx context.Context) bool`<br/>
JS: `WindowIsNormal() bool`
### WindowSetSize
Sets the width and height of the window.
Go: `WindowSetSize(ctx context.Context, width int, height int)`<br/>
JS: `WindowSetSize(width: number, height: number)`
### WindowGetSize
Gets the width and height of the window.
Go: `WindowGetSize(ctx context.Context) (width int, height int)`<br/>
JS: `WindowGetSize() : Size`
### WindowSetMinSize
Sets the minimum window size.
Will resize the window if the window is currently smaller than the given dimensions.
Setting a size of `0,0` will disable this constraint.
Go: `WindowSetMinSize(ctx context.Context, width int, height int)`<br/>
JS: `WindowSetMinSize(width: number, height: number)`
### WindowSetMaxSize
Sets the maximum window size.
Will resize the window if the window is currently larger than the given dimensions.
Setting a size of `0,0` will disable this constraint.
Go: `WindowSetMaxSize(ctx context.Context, width int, height int)`<br/>
JS: `WindowSetMaxSize(width: number, height: number)`
### WindowSetAlwaysOnTop
Sets the window AlwaysOnTop or not on top.
Go: `WindowSetAlwaysOnTop(ctx context.Context, b bool)`<br/>
JS: `WindowSetAlwaysOnTop(b: Boolen)`
### WindowSetPosition
Sets the window position relative to the monitor the window is currently on.
Go: `WindowSetPosition(ctx context.Context, x int, y int)`<br/>
JS: `WindowSetPosition(x: number, y: number)`
### WindowGetPosition
Gets the window position relative to the monitor the window is currently on.
Go: `WindowGetPosition(ctx context.Context) (x int, y int)`<br/>
JS: `WindowGetPosition() : Position`
### WindowMaximise
Maximises the window to fill the screen.
Go: `WindowMaximise(ctx context.Context)`<br/>
JS: `WindowMaximise()`
### WindowUnmaximise
Restores the window to the dimensions and position prior to maximising.
Go: `WindowUnmaximise(ctx context.Context)`<br/>
JS: `WindowUnmaximise()`
### WindowIsMaximised
Returns true if the window is maximised.
Go: `WindowIsMaximised(ctx context.Context) bool`<br/>
JS: `WindowIsMaximised() bool`
### WindowToggleMaximise
Toggles between Maximised and UnMaximised.
Go: `WindowToggleMaximise(ctx context.Context)`<br/>
JS: `WindowToggleMaximise()`
### WindowMinimise
Minimises the window.
Go: `WindowMinimise(ctx context.Context)`<br/>
JS: `WindowMinimise()`
### WindowUnminimise
Restores the window to the dimensions and position prior to minimising.
Go: `WindowUnminimise(ctx context.Context)`<br/>
JS: `WindowUnminimise()`
### WindowIsMinimised
Returns true if the window is minimised.
Go: `WindowIsMinimised(ctx context.Context) bool`<br/>
JS: `WindowIsMinimised() bool`
### WindowSetBackgroundColour
Sets the background colour of the window to the given RGBA colour definition.
This colour will show through for all transparent pixels.
Valid values for R, G, B and A are 0-255.
:::info Windows
On Windows, only alpha values of 0 or 255 are supported.
Any value that is not 0 will be considered 255.
:::
Go: `WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8)`<br/>
JS: `WindowSetBackgroundColour(R, G, B, A)`
### WindowPrint
Opens tha native print dialog.
Go: `WindowPrint(ctx context.Context)`<br/>
JS: `WindowPrint()`
## TypeScript Object Definitions
### Position
```ts
interface Position {
x: number;
y: number;
}
```
### Size
```ts
interface Size {
w: number;
h: number;
}
```