5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 21:10:54 +08:00
wails/website/i18n/ja/docusaurus-plugin-content-docs/current/guides/application-development.mdx
Lea Anthony b5e98d5780
New Crowdin updates (#1726)
* New translations minecraftupdater.mdx (Korean)
[ci skip]

* New translations links.mdx (Korean)
[ci skip]

* New translations templates.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Korean)
[ci skip]

* New translations documenting.mdx (Korean)
[ci skip]

* New translations troubleshooting.mdx (Korean)
[ci skip]

* New translations windows-installer.mdx (Korean)
[ci skip]

* New translations faq.mdx (Korean)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Korean)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Korean)
[ci skip]

* New translations authors.yml (Korean)
[ci skip]

* New translations credits.mdx (Korean)
[ci skip]

* New translations markdown-page.md (Korean)
[ci skip]

* New translations stats.mdx (Korean)
[ci skip]

* New translations faq.mdx (Japanese)
[ci skip]

* New translations faq.mdx (Russian)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations faq.mdx (Chinese Simplified)
[ci skip]

* New translations options.json (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.38.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.39.json (Korean)
[ci skip]

* New translations footer.json (Korean)
[ci skip]

* New translations navbar.json (Korean)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Korean)
[ci skip]

* New translations howdoesitwork.mdx (Korean)
[ci skip]

* New translations windows.mdx (Korean)
[ci skip]

* New translations log.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations linux.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations browser.mdx (Korean)
[ci skip]

* New translations dialog.mdx (Korean)
[ci skip]

* New translations events.mdx (Korean)
[ci skip]

* New translations intro.mdx (Korean)
[ci skip]

* New translations menu.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations cli.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations menus.mdx (Korean)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations project-config.mdx (Korean)
[ci skip]

* New translations _category_.json (Korean)
[ci skip]

* New translations helloworld.mdx (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Chinese Simplified)
[ci skip]

* New translations firstproject.mdx (Japanese)
[ci skip]

* New translations firstproject.mdx (Russian)
[ci skip]

* New translations firstproject.mdx (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Japanese)
[ci skip]

* New translations installation.mdx (Russian)
[ci skip]

* New translations installation.mdx (Chinese Simplified)
[ci skip]

* New translations dynamic-assets.mdx (Japanese)
[ci skip]

* New translations dynamic-assets.mdx (Russian)
[ci skip]

* New translations dynamic-assets.mdx (Chinese Simplified)
[ci skip]

* New translations frameless.mdx (Japanese)
[ci skip]

* New translations frameless.mdx (Russian)
[ci skip]

* New translations frameless.mdx (Chinese Simplified)
[ci skip]

* New translations documenting.mdx (Japanese)
[ci skip]

* New translations documenting.mdx (Russian)
[ci skip]

* New translations frontend.mdx (Japanese)
[ci skip]

* New translations frontend.mdx (Russian)
[ci skip]

* New translations windows.mdx (Chinese Simplified)
[ci skip]

* New translations troubleshooting.mdx (Japanese)
[ci skip]

* New translations troubleshooting.mdx (Russian)
[ci skip]

* New translations troubleshooting.mdx (Chinese Simplified)
[ci skip]

* New translations windows-installer.mdx (Japanese)
[ci skip]

* New translations windows-installer.mdx (Russian)
[ci skip]

* New translations windows-installer.mdx (Chinese Simplified)
[ci skip]

* New translations dialog.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Russian)
[ci skip]

* New translations signing.mdx (Chinese Simplified)
[ci skip]

* New translations migrating.mdx (Russian)
[ci skip]

* New translations ides.mdx (Japanese)
[ci skip]

* New translations ides.mdx (Russian)
[ci skip]

* New translations ides.mdx (Chinese Simplified)
[ci skip]

* New translations migrating.mdx (Japanese)
[ci skip]

* New translations migrating.mdx (Chinese Simplified)
[ci skip]

* New translations signing.mdx (Russian)
[ci skip]

* New translations mouse-buttons.mdx (Japanese)
[ci skip]

* New translations mouse-buttons.mdx (Russian)
[ci skip]

* New translations mouse-buttons.mdx (Chinese Simplified)
[ci skip]

* New translations overscroll.mdx (Japanese)
[ci skip]

* New translations overscroll.mdx (Russian)
[ci skip]

* New translations routing.mdx (Japanese)
[ci skip]

* New translations routing.mdx (Russian)
[ci skip]

* New translations routing.mdx (Chinese Simplified)
[ci skip]

* New translations signing.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Chinese Simplified)
[ci skip]

* New translations project-config.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations howdoesitwork.mdx (Japanese)
[ci skip]

* New translations howdoesitwork.mdx (Russian)
[ci skip]

* New translations howdoesitwork.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Japanese)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Japanese)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Russian)
[ci skip]

* New translations menu.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations cli.mdx (Chinese Simplified)
[ci skip]

* New translations menus.mdx (Japanese)
[ci skip]

* New translations menus.mdx (Russian)
[ci skip]

* New translations menus.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Russian)
[ci skip]

* New translations 2021-11-08-v2-beta2-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations emailit.mdx (Chinese Simplified)
[ci skip]

* New translations modalfilemanager.mdx (Chinese Simplified)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Japanese)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Russian)
[ci skip]

* New translations 2022-02-22-v2-beta3-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations credits.mdx (Japanese)
[ci skip]

* New translations credits.mdx (Russian)
[ci skip]

* New translations credits.mdx (Chinese Simplified)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations developing-new-features.mdx (Japanese)
[ci skip]

* New translations developing-new-features.mdx (Russian)
[ci skip]

* New translations ways-of-contributing.mdx (Japanese)
[ci skip]

* New translations documenting.mdx (Japanese)
[ci skip]

* New translations testing.mdx (Japanese)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Japanese)
[ci skip]

* New translations fixing-bugs.mdx (Japanese)
[ci skip]

* New translations helping-others.mdx (Japanese)
[ci skip]

* New translations ways-of-contributing.mdx (Russian)
[ci skip]

* New translations fixing-bugs.mdx (Chinese Simplified)
[ci skip]

* New translations documenting.mdx (Russian)
[ci skip]

* New translations documenting.mdx (Chinese Simplified)
[ci skip]

* New translations testing.mdx (Chinese Simplified)
[ci skip]

* New translations fixing-bugs.mdx (Russian)
[ci skip]

* New translations ways-of-contributing.mdx (Chinese Simplified)
[ci skip]

* New translations testing.mdx (Russian)
[ci skip]

* New translations helping-others.mdx (Russian)
[ci skip]

* New translations helping-others.mdx (Chinese Simplified)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Chinese Simplified)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Russian)
[ci skip]

* New translations developing-new-features.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Japanese)
[ci skip]

* New translations current.json (Russian)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Japanese)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Russian)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Japanese)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Russian)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Chinese Simplified)
[ci skip]

* New translations installation.mdx (Korean)
[ci skip]

* New translations ways-of-contributing.mdx (Korean)
[ci skip]

* New translations testing.mdx (Korean)
[ci skip]

* New translations developing-new-features.mdx (Korean)
[ci skip]

* New translations documenting.mdx (Korean)
[ci skip]

* New translations fixing-bugs.mdx (Korean)
[ci skip]

* New translations helping-others.mdx (Korean)
[ci skip]

* New translations setting-up-a-dev-environment.mdx (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations options.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Korean)
[ci skip]

* New translations version-v2.0.0-beta.43.json (Korean)
[ci skip]

* New translations footer.json (Korean)
[ci skip]

* New translations navbar.json (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations community-guide.mdx (Japanese)
[ci skip]

* New translations community-guide.mdx (Korean)
[ci skip]

* New translations community-guide.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Japanese)
[ci skip]

* New translations current.json (Russian)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations code.json (Japanese)
[ci skip]

* New translations code.json (Russian)
[ci skip]

* New translations code.json (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations current.json (Korean)
[ci skip]

* New translations code.json (Korean)
[ci skip]

* New translations community-guide.mdx (Japanese)
[ci skip]

* New translations community-guide.mdx (Korean)
[ci skip]

* New translations community-guide.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations vscode.mdx (Japanese)
[ci skip]

* New translations vscode.mdx (Korean)
[ci skip]

* New translations vscode.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Chinese Simplified)
[ci skip]

* New translations current.json (Chinese Simplified)
[ci skip]

* New translations riftshare.mdx (Chinese Simplified)
[ci skip]

* New translations surge.mdx (Chinese Simplified)
[ci skip]

* New translations wally.mdx (Chinese Simplified)
[ci skip]

* New translations encrypteasy.mdx (Chinese Simplified)
[ci skip]

* New translations filehound.mdx (Chinese Simplified)
[ci skip]

* New translations mollywallet.mdx (Chinese Simplified)
[ci skip]

* New translations optimus.mdx (Chinese Simplified)
[ci skip]

* New translations _category_.json (Chinese Simplified)
[ci skip]

* New translations emailit.mdx (Chinese Simplified)
[ci skip]

* New translations modalfilemanager.mdx (Chinese Simplified)
[ci skip]

* New translations browser.mdx (Chinese Simplified)
[ci skip]

* New translations vscode.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Chinese Simplified)
[ci skip]

* New translations browser.mdx (Japanese)
[ci skip]

* New translations browser.mdx (Korean)
[ci skip]

* New translations browser.mdx (Russian)
[ci skip]

* New translations dialog.mdx (Japanese)
[ci skip]

* New translations vscode.mdx (Japanese)
[ci skip]

* New translations dialog.mdx (Korean)
[ci skip]

* New translations dialog.mdx (Russian)
[ci skip]

* New translations dialog.mdx (Chinese Simplified)
[ci skip]

* New translations events.mdx (Japanese)
[ci skip]

* New translations events.mdx (Korean)
[ci skip]

* New translations events.mdx (Russian)
[ci skip]

* New translations events.mdx (Chinese Simplified)
[ci skip]

* New translations intro.mdx (Japanese)
[ci skip]

* New translations intro.mdx (Korean)
[ci skip]

* New translations intro.mdx (Russian)
[ci skip]

* New translations vscode.mdx (Korean)
[ci skip]

* New translations intro.mdx (Chinese Simplified)
[ci skip]

* New translations log.mdx (Japanese)
[ci skip]

* New translations options.mdx (Russian)
[ci skip]

* New translations introduction.mdx (Japanese)
[ci skip]

* New translations options.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Korean)
[ci skip]

* New translations log.mdx (Korean)
[ci skip]

* New translations log.mdx (Russian)
[ci skip]

* New translations log.mdx (Chinese Simplified)
[ci skip]

* New translations menu.mdx (Japanese)
[ci skip]

* New translations menu.mdx (Korean)
[ci skip]

* New translations menu.mdx (Russian)
[ci skip]

* New translations menu.mdx (Chinese Simplified)
[ci skip]

* New translations window.mdx (Japanese)
[ci skip]

* New translations window.mdx (Korean)
[ci skip]

* New translations window.mdx (Russian)
[ci skip]

* New translations window.mdx (Chinese Simplified)
[ci skip]

* New translations options.mdx (Japanese)
[ci skip]

* New translations introduction.mdx (Korean)
[ci skip]

* New translations introduction.mdx (Russian)
[ci skip]

* New translations community-guide.mdx (Chinese Simplified)
[ci skip]

* New translations stats.mdx (Chinese Simplified)
[ci skip]

* New translations faq.mdx (Chinese Simplified)
[ci skip]

* New translations markdown-page.md (Chinese Simplified)
[ci skip]

* New translations introduction.mdx (Chinese Simplified)
[ci skip]

* New translations 2021-09-27-v2-beta1-release-notes.mdx (Chinese Simplified)
[ci skip]

* New translations version-v2.0.0-beta.44.json (Chinese Simplified)
[ci skip]

* Empty-Commit

* Fix crowdin screwups again
2022-08-27 20:17:15 +10:00

195 lines
7.7 KiB
Plaintext

# Application Development
There are no hard and fast rules for developing applications with Wails, but there are some basic guidelines.
## Application Setup
The pattern used by the default templates are that `main.go` is used for configuring and running the application, whilst `app.go` is used for defining the application logic.
The `app.go` file will define a struct that has 2 methods which act as hooks into the main application:
```go title="app.go"
type App struct {
ctx context.Context
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) shutdown(ctx context.Context) {
}
```
- The startup method is called as soon as Wails allocates the resources it needs and is a good place for creating resources, setting up event listeners and anything else the application needs at startup. It is given a `context.Context` which is usually saved in a struct field. This context is needed for calling the [runtime](../reference/runtime/intro.mdx). If this method returns an error, the application will terminate. In dev mode, the error will be output to the console.
- The shutdown method will be called by Wails right at the end of the shutdown process. This is a good place to deallocate memory and perform any shutdown tasks.
The `main.go` file generally consists of a single call to `wails.Run()`, which accepts the application configuration. The pattern used by the templates is that before the call to `wails.Run()`, an instance of the struct we defined in `app.go` is created and saved in a variable called `app`. This configuration is where we add our callbacks:
```go {3,9,10} title="main.go"
func main() {
app := NewApp()
err := wails.Run(&options.App{
Title: "My App",
Width: 800,
Height: 600,
OnStartup: app.startup,
OnShutdown: app.shutdown,
})
if err != nil {
log.Fatal(err)
}
}
```
More information on application lifecycle hooks can be found [here](../howdoesitwork.mdx#application-lifecycle-callbacks).
## Binding Methods
It is likely that you will want to call Go methods from the frontend. This is normally done by adding public methods to the already defined struct in `app.go`:
```go {16-18} title="app.go"
type App struct {
ctx context.Context
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) shutdown(ctx context.Context) {
}
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
```
In the main application configuration, the `Bind` key is where we can tell Wails what we want to bind:
```go {11-13} title="main.go"
func main() {
app := NewApp()
err := wails.Run(&options.App{
Title: "My App",
Width: 800,
Height: 600,
OnStartup: app.startup,
OnShutdown: app.shutdown,
Bind: []interface{}{
app,
},
})
if err != nil {
log.Fatal(err)
}
}
```
This will bind all public methods in our `App` struct (it will never bind the startup and shutdown methods).
### Dealing with context when binding multiple structs
If you want to bind methods for multiple structs but want each struct to keep a reference to the context so that you can use the runtime functions, a good pattern is to pass the context from the `OnStartup` method to your struct instances :
```go
func main() {
app := NewApp()
otherStruct := NewOtherStruct()
err := wails.Run(&options.App{
Title: "My App",
Width: 800,
Height: 600,
OnStartup: func(ctx context.Context){
app.SetContext(ctx)
otherStruct.SetContext(ctx)
},
OnShutdown: app.shutdown,
Bind: []interface{}{
app,
otherStruct
},
})
if err != nil {
log.Fatal(err)
}
}
```
More information on Binding can be found [here](../howdoesitwork.mdx#method-binding).
## Application Menu
Wails supports adding a menu to your application. This is done by passing a [Menu](../reference/menus.mdx#menu) struct to application config. It's common to use a method that returns a Menu, and even more common for that to be a method on the `App` struct used for the lifecycle hooks.
```go {11} title="main.go"
func main() {
app := NewApp()
err := wails.Run(&options.App{
Title: "My App",
Width: 800,
Height: 600,
OnStartup: app.startup,
OnShutdown: app.shutdown,
Menu: app.menu(),
Bind: []interface{}{
app,
},
})
if err != nil {
log.Fatal(err)
}
}
```
## Assets
The great thing about the way Wails v2 handles assets is that it doesn't! The only thing you need to give Wails is an `embed.FS`. How you get to that is entirely up to you. You can use vanilla html/css/js files like the vanilla template. You could have some complicated build system, it doesn't matter.
When `wails build` is run, it will check the `wails.json` project file at the project root. There are 2 keys in the project file that are read:
- "frontend:install"
- "frontend:build"
The first, if given, will be executed in the `frontend` directory to install the node modules. The second, if given, will be executed in the `frontend` directory to build the frontend project.
If these 2 keys aren't given, then Wails does absolutely nothing with the frontend. It is only expecting that `embed.FS`.
### AssetsHandler
A Wails v2 app can optionally define a `http.Handler` in the `options.App`, which allows hooking into the AssetServer to create files on the fly or process POST/PUT requests. GET requests are always first handled by the `assets` FS. If the FS doesn't find the requested file the request will be forwarded to the `http.Handler` for serving. Any requests other than GET will be directly processed by the `AssetsHandler` if specified. It's also possible to only use the `AssetsHandler` by specifiy `nil` as the `Assets` option.
## Built in Dev Server
Running `wails dev` will start the built in dev server which will start a file watcher in your project directory. By default, if any file changes, wails checks if it was an application file (default: `.go`, configurable with `-e` flag). If it was, then it will rebuild your application and relaunch it. If the changed file was in the assets, it will issue a reload after a short amount of time.
The dev server uses a technique called "debouncing" which means it doesn't reload straight away, as there may be multiple files changed in a short amount of time. When a trigger occurs, it waits for a set amount of time before issuing a reload. If another trigger happens, it resets to the wait time again. By default this value is `100ms`. If this value doesn't work for your project, it can be configured using the `-debounce` flag. If used, this value will be saved to your project config and become the default.
## External Dev Server
Some frameworks come with their own live-reloading server, however they will not be able to take advantage of the Wails Go bindings. In this scenario, it is best to run a watcher script that rebuilds the project into the build directory, which Wails will be watching. For an example, see the default svelte template that uses [rollup](https://rollupjs.org/guide/en/). For [create-react-app](https://create-react-app.dev/), it's possible to use [this script](https://gist.github.com/int128/e0cdec598c5b3db728ff35758abdbafd) to achieve a similar result.
## Go Module
The default Wails templates generate a `go.mod` file that contains the module name "changeme". You should change this to something more appropriate after project generation.