5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 07:40:17 +08:00
wails/website/versioned_docs/version-v2.7.0/reference/runtime/intro.mdx
Dana Woodman f9aa4d3b60
Give explicity example of importing JS runtime (#3178)
* Give explicity example of importing JS runtime

No where in the docs, google, github issues or discussions could I find an example of importing the runtime JS but after some experimentation I figured it out.

I think it would help future users if a simple example was shown like this so they have a clear reference of how to import the runtime.

* make generic

* Update changelog.mdx

---------

Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
2024-01-17 22:40:12 +11:00

108 lines
2.5 KiB
Plaintext

---
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.
e.g. if you wanted to import the [EventsOn](./events/#eventson) function in your frontend, you can do the following:
```ts
// src/index.js (for example)
import { EventsOn } from '../wailsjs/runtime'
```
### 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;
}
```