Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new bindings guide. #3359

Open
wants to merge 3 commits into
base: v3-alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mkdocs-website/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM squidfunk/mkdocs-material
RUN pip install mkdocs-macros-plugin
RUN pip install mkdocs-glightbox
RUN pip install mkdocs-table-reader-plugin
RUN pip install mkdocs-static-i18n
49 changes: 23 additions & 26 deletions mkdocs-website/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,36 @@ This is the documentation for Wails v3. It is currently a work in progress.
If you do not wish to build it locally, it is available online at
[https://wailsapp.github.io/wails/](https://wailsapp.github.io/wails/).

## Recommended Setup Steps
## Setup Steps

Install the wails3 CLI if you haven't already:
1. Install the wails3 CLI if you haven't already:

```shell
go install github.com/wailsapp/wails/v3/cmd/wails3@latest
```
```shell
git clone https://github.com/wailsapp/wails.git
cd wails
git checkout v3-alpha
cd v3/cmd/wails3
go install
```
2. Install [docker](https://www.docker.com)
3. Run the following command to build the docker container:

The documentation uses mkdocs, so you will need to install
[Python](https://www.python.org/). Once installed, you can setup the
documentation by running the following command:
```shell
wails3 task docs:setup
```
4. Serve the documentation locally:

```bash
wails3 task docs:setup
```
```shell
wails3 task docs:serve
```

This will install the required dependencies for you.
5. Open your browser to [http://127.0.0.1:8000](http://127.0.0.1:8000)

If you have installed the wails3 CLI, you can run the following command to build
the documentation and serve it locally:
6. For a complete build, run:

```bash
wails3 task docs:serve
```

### Manual Setup

To install manually, you will need to do the following:

- Install [Python](https://www.python.org/)
- Run `pip install -r requirements.txt` to install the required dependencies
- Run `mkdocs serve` to serve the documentation locally
- Run `mkdocs build` to build the documentation
```shell
wails3 task docs:build
```

## Contributing

Expand Down
54 changes: 45 additions & 9 deletions mkdocs-website/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ tasks:
setup:
summary: Setup the project
preconditions:
- sh: python{{exeExt}} --version
msg: "Looks like Python isn't installed. Python is required to build the documentation: https://www.python.org/downloads/"
- sh: docker version
msg: "Looks like docker isn't installed. Visit https://www.docker.com to download and install."
cmds:
- python -m pip install -r requirements.txt --user
- docker build -t squidfunk/mkdocs-material .

setup:insiders:
summary: Setup the project (insiders)
Expand All @@ -31,18 +31,54 @@ tasks:
build:
summary: Builds the documentation
preconditions:
- sh: mkdocs --version
msg: "Looks like mkdocs isn't installed. Run `wails3 task setup` or `task setup` in the documentation directory to install it."
- sh: docker version
msg: "Looks like docker isn't installed. Visit https://www.docker.com to download and install."
cmds:
- build:windows
- build:unix

build:windows:
summary: Builds the documentation (Windows)
internal: true
platforms:
- windows
cmds:
- docker run --rm -v "%cd%":/docs squidfunk/mkdocs-material build

build:unix:
summary: Builds the documentation (Unix)
internal: true
platforms:
- darwin
- linux
cmds:
- mkdocs build
- docker run --rm -v "$(pwd)":/docs squidfunk/mkdocs-material build

serve:
summary: Builds the documentation and serves it locally
preconditions:
- sh: mkdocs --version
msg: "Looks like mkdocs isn't installed. Run `wails3 task setup` or `task setup` in the documentation directory to install it."
- sh: docker version
msg: "Looks like docker isn't installed. Visit https://www.docker.com to download and install."
cmds:
- task: serve:windows
- task: serve:unix

serve:windows:
summary: Builds the documentation and serves it locally (Windows)
internal: true
platforms:
- windows
cmds:
- docker run --rm -it -p 8000:8000 -v "%cd%":/docs squidfunk/mkdocs-material

serve:unix:
summary: Builds the documentation and serves it locally (Unix)
internal: true
platforms:
- darwin
- linux
cmds:
- mkdocs serve
- docker run --rm -it -p 8000:8000 -v "$(pwd)":/docs squidfunk/mkdocs-material

serve:insiders:
summary: Builds the documentation and serves it locally
Expand Down
149 changes: 149 additions & 0 deletions mkdocs-website/docs/en/guides/bindings/enums.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Enums

In Go, an enum (enumeration) is a user-defined type that consists of a set of named constants called enumerators. Enums
are useful for representing a fixed set of values that a variable can take.

To define an enum in Go, you can use a type declaration with a list of constant values. Here's an example:

```go
type Title string

const (
Mr Title = "Mr"
Mrs Title = "Mrs"
Ms Title = "Ms"
)
```

In this example, we define an enum type called `Title` using the `type` keyword. We then declare a set of constants
(`Mr`, `Mrs`, `Ms`) of type `Title` using the `const` keyword.

## Using Enums in Bound Structs

Let's extend our previous example to use the `Title` enum in the `Person` struct:

```go
package main

import (
"github.com/wailsapp/wails/v3/pkg/application"
"log"
)

// Title is a title
type Title string

const (
Mr Title = "Mr"
Mrs Title = "Mrs"
Ms Title = "Ms"
)

// Person is a person
type Person struct {
// Name of the person
Name string
// Title of the person
Title Title
}

type GreetService struct{}

func (g *GreetService) Greet(person Person) string {
return "Hello " + string(person.Title) + " " + person.Name
}

func main() {
app := application.New(application.Options{
Bind: []any{
&GreetService{},
},
})
// ....
app.NewWebviewWindow()
err := app.Run()
if err != nil {
log.Fatal(err)
}
}
```

In this updated example, we add a `Title` field of type `Title` to the `Person` struct. The `Greet` method now includes the `Title` in the greeting message.

When we run the bindings generator, it will process the enum and generate the corresponding JavaScript or TypeScript code.

## Generating Bindings with Enums

After adding the `Title` enum to our example, let's generate the bindings using the following command:

```bash
% wails3 generate bindings
```

The output should display information about the processed package, struct, method, enum, and model:

```bash
INFO Processed: 1 Package, 1 Struct, 1 Method, 1 Enum, 1 Model in 1.044166ms.
INFO Output directory: /Users/lea/GolandProjects/tempdocs/binding/frontend/bindings
```

If we look in the `frontend/bindings` directory, we should see a generated `models.js` file that includes the enum definition:

```javascript
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

/**
* @readonly
* @enum {string}
*/
export const Title = {
Mr: "Mr",
Mrs: "Mrs",
Ms: "Ms",
};

/**
* Person defines a person
*/
export class Person {
/**
* Creates a new Person instance.
* @param {Partial<Person>} [source = {}] - The source object to create the Person.
*/
constructor(source = {}) {
if (!("Name" in source)) {
/**
* Name of the person
* @member
* @type {string}
*/
this["Name"] = "";
}
if (!("Title" in source)) {
/**
* Title of the person
* @member
* @type {Title}
*/
this["Title"] = null;
}
Object.assign(this, source);
}

/**
* Creates a new Person instance from a string or object.
* @param {string|object} source - The source data to create a Person instance from.
* @returns {Person} A new Person instance.
*/
static createFrom(source) {
let parsedSource = typeof source === 'string' ? JSON.parse(source) : source;
return new Person(parsedSource);
}
}
```

The generated `models.js` file includes the `Title` enum definition, which is exported as a read-only object with the enum values as properties. The `Person` class also includes a `Title` property of type `Title`.

You can now use the generated enum and class in your frontend code when interacting with the bound `GreetService` struct.
14 changes: 14 additions & 0 deletions mkdocs-website/docs/en/guides/bindings/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Bindings Generator Guide

Welcome to the Wails v3 Bindings Generator Guide!

One of the key features of Wails is the ability to seamlessly integrate backend Go code with the frontend, enabling
efficient communication between the two layers. This can be done manually by sending messages between the frontend and
backend, but this can be cumbersome and error-prone, especially when dealing with complex data types.

The bindings generator in Wails v3 simplifies this process by automatically generating JavaScript or TypeScript
functions and models that reflect the methods and data structures defined in your Go code. This means you can write
your backend logic in Go and easily expose it to the frontend without the need for manual binding or complex integration.

This guide is designed to help you understand and utilise this powerful binding tool.

Loading
Loading