Skip to content

Commit

Permalink
Merge branch 'v3-alpha' into fixed-build-assets-update
Browse files Browse the repository at this point in the history
  • Loading branch information
ansxuman authored Nov 22, 2024
2 parents a7d6520 + eccfd34 commit 4d45825
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 14 deletions.
2 changes: 2 additions & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New `InitialPosition` option to specify if the window should be centered or positioned at the given X/Y location by [leaanthony](https://github.com/leaanthony) in [#3885](https://github.com/wailsapp/wails/pull/3885)
- Add `Path` & `Paths` methods to `application` package by [ansxuman](https://github.com/ansxuman) and [leaanthony](https://github.com/leaanthony) in [#3823](https://github.com/wailsapp/wails/pull/3823)
- Added `GeneralAutofillEnabled` and `PasswordAutosaveEnabled` Windows options by [leaanthony](https://github.com/leaanthony) in [#3766](https://github.com/wailsapp/wails/pull/3766)
- Added the ability to retrieve the window calling a service method by [leaanthony](https://github.com/leaanthony)
in [#3888](https://github.com/wailsapp/wails/pull/3888)

### Changed
- Asset embed to include `all:frontend/dist` to support frameworks that generate subfolders by @atterpac in [#3887](https://github.com/wailsapp/wails/pull/3887)
Expand Down
47 changes: 47 additions & 0 deletions mkdocs-website/docs/en/learn/bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,50 @@ export class Person {
```
Using TypeScript bindings provides type safety and improved IDE support when working with the generated code in your frontend.
### Using `context.Context`
When defining service methods in Go, you can include `context.Context` as the first parameter. The runtime will automatically provide a context when the method is called from the frontend.
The context provides several powerful features:
1. **Cancellation Support**: Long-running operations can be cancelled from the frontend, which will raise an error through the Promise chain.
2. **Window Information**: You can determine which window made the call using these context keys:
- `application.WindowNameKey` - Returns the name of the calling window
- `application.WindowIDKey` - Returns the ID of the calling window
Here are some examples:
```go
// Basic context usage with cancellation
func (s *MyService) LongRunningTask(ctx context.Context, input string) (string, error) {
select {
// Check if the context has been cancelled from the frontend
case <-ctx.Done():
return "", ctx.Err()
default:
// Process task
return "completed", nil
}
}
// Getting caller window information
func (s *MyService) WindowAwareMethod(ctx context.Context) (string, error) {
windowName := ctx.Value(application.WindowNameKey).(string)
windowID := ctx.Value(application.WindowIDKey).(string)
return fmt.Sprintf("Called from window: %s (ID: %s)", windowName, windowID), nil
}
```
From the frontend, these methods can be called normally. If you need to cancel a long-running operation, the Promise will be rejected with the cancellation error:
```javascript
// Call the method
const promise = MyService.LongRunningTask("input");
// Cancel it later if needed
// This will cause the context to be cancelled in the Go method
promise.cancel();
```
6 changes: 3 additions & 3 deletions v3/examples/build/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ tasks:
platforms:
- windows
cmds:
- wails3 generate syso -arch {{.GOARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails3 generate syso -arch {{.GOARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails_windows.syso
vars:
GOARCH: '{{.GOARCH}}'

Expand All @@ -93,7 +93,7 @@ tasks:
vars:
GOARCH: amd64
- go build -tags production -ldflags="-w -s -H windowsgui" -o bin/{{.APP_NAME}}
- powershell Remove-item wails.syso
- powershell Remove-item *.syso


package:windows:arm64:
Expand All @@ -105,6 +105,6 @@ tasks:
vars:
GOARCH: arm64
- go build -tags production -ldflags="-w -s -H windowsgui" -o bin/buildtest.arm64.exe
- powershell Remove-item wails.syso
- powershell Remove-item *.syso
env:
GOARCH: arm64
4 changes: 2 additions & 2 deletions v3/examples/dev/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ tasks:
dir: build
platform: windows
cmds:
- wails generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json
vars:
ARCH: $GOARCH

Expand All @@ -180,4 +180,4 @@ tasks:
vars:
ARCH: amd64
- go build -tags production -ldflags="-w -s -H windowsgui" -o bin/{{.APP_NAME}}.exe
- powershell Remove-item wails.syso
- powershell Remove-item *.syso
6 changes: 3 additions & 3 deletions v3/examples/dialogs/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ tasks:
platforms:
- windows
cmds:
- wails3 generate syso -arch {{.GOARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails3 generate syso -arch {{.GOARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json
vars:
GOARCH: '{{.GOARCH}}'

Expand All @@ -90,7 +90,7 @@ tasks:
vars:
GOARCH: amd64
- go build -tags production -gcflags=all="-N -l" -o bin/{{.APP_NAME}}
- powershell Remove-item wails.syso
- powershell Remove-item *.syso


package:windows:arm64:
Expand All @@ -102,6 +102,6 @@ tasks:
vars:
GOARCH: arm64
- go build -tags production -ldflags="-w -s -H windowsgui" -o bin/buildtest.arm64.exe
- powershell Remove-item wails.syso
- powershell Remove-item *.syso
env:
GOARCH: arm64
3 changes: 2 additions & 1 deletion v3/examples/file-association/build/Taskfile.windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tasks:
- task: generate:syso
cmds:
- go build {{.BUILD_FLAGS}} -o {{.BIN_DIR}}/{{.APP_NAME}}.exe
- powershell Remove-item *.syso
vars:
BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production -trimpath -ldflags="-w -s -H windowsgui"{{else}}-gcflags=all="-l"{{end}}'
env:
Expand All @@ -30,7 +31,7 @@ tasks:
summary: Generates Windows `.syso` file
dir: build
cmds:
- wails3 generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails3 generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json
vars:
ARCH: '{{.ARCH | default ARCH}}'

Expand Down
3 changes: 2 additions & 1 deletion v3/internal/commands/build_assets/Taskfile.windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ tasks:
- task: generate:syso
cmds:
- go build {{.BUILD_FLAGS}} -o {{.BIN_DIR}}/{{.APP_NAME}}.exe
- powershell Remove-item *.syso
vars:
BUILD_FLAGS: '{{if eq .PRODUCTION "true"}}-tags production -trimpath -ldflags="-w -s -H windowsgui"{{else}}-gcflags=all="-l"{{end}}'
env:
Expand All @@ -30,7 +31,7 @@ tasks:
summary: Generates Windows `.syso` file
dir: build
cmds:
- wails3 generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails3 generate syso -arch {{.ARCH}} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails_windows_{{.ARCH}}.syso
vars:
ARCH: '{{.ARCH | default ARCH}}'

Expand Down
1 change: 1 addition & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"type": "module",
"version": "3.0.0-alpha.29",
"description": "Wails Runtime",
"types": "types/index.d.ts",
"exports": {
".": {
"types": "./types/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions v3/internal/templates/_base/default/Taskfile.tmpl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ tasks:
dir: build
platform: windows
cmds:
- wails generate syso -arch {{ "{{.ARCH}}" }} -icon icon.ico -manifest wails.exe.manifest -info info.json -out ../wails.syso
- wails generate syso -arch {{ "{{.ARCH}}" }} -icon icon.ico -manifest wails.exe.manifest -info info.json
vars:
ARCH: $GOARCH

Expand All @@ -186,4 +186,4 @@ tasks:
vars:
ARCH: amd64
- go build -tags production -ldflags="-w -s -H windowsgui" -o bin/{{ "{{.APP_NAME}}" }}.exe
- powershell Remove-item wails.syso
- powershell Remove-item *.syso
18 changes: 16 additions & 2 deletions v3/pkg/application/messageprocessor_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ import (
"net/http"
)

type contextKey string

const (
CallBinding = 0
CallBinding = 0
WindowNameKey contextKey = "WindowName"
WindowIDKey contextKey = "WindowID"
)

func (m *MessageProcessor) callErrorCallback(window Window, message string, callID *string, err error) {
Expand Down Expand Up @@ -95,6 +99,12 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
return
}

// Set the context values for the window
if window != nil {
ctx = context.WithValue(ctx, WindowNameKey, window.Name())
ctx = context.WithValue(ctx, WindowIDKey, window.ID())
}

go func() {
defer func() {
cancel()
Expand Down Expand Up @@ -123,7 +133,11 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
var jsonArgs struct {
Args json.RawMessage `json:"args"`
}
params.ToStruct(&jsonArgs)
err = params.ToStruct(&jsonArgs)
if err != nil {
m.callErrorCallback(window, "Error parsing arguments: %s", callID, err)
return
}
m.Info("Call Binding:", "method", boundMethod, "args", string(jsonArgs.Args), "result", result)
}()
m.ok(rw)
Expand Down

0 comments on commit 4d45825

Please sign in to comment.