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

Potential solution for getting the calling window of a bound method. #3424

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c76b88b
Potential solution for getting the calling window of a bound method.
leaanthony Apr 25, 2024
8ea58be
Support window as second parameter
leaanthony Apr 27, 2024
29d4aac
Support window as second parameter
leaanthony Apr 27, 2024
2405133
Remove window from context
leaanthony Apr 28, 2024
6639076
Fix pipeline tests
leaanthony Apr 27, 2024
fee490f
Fix pipeline tests
leaanthony Apr 27, 2024
ccd7561
Fix example building
leaanthony Apr 27, 2024
2866a6b
set target branch to v3-alpha
leaanthony Apr 28, 2024
156497b
Fix nanoid dep for npm package
leaanthony Apr 28, 2024
91f4d2b
More optimised production build in project taskfile
leaanthony Apr 29, 2024
6f03d9e
Add -trimpath for production build in project taskfile
leaanthony Apr 29, 2024
b0bce07
[v3] Binding runtime fixes (#3431)
fbbdev May 2, 2024
0316c70
[v3] Add `port` flag to dev command (#3429)
abichinger May 2, 2024
c65e130
add missing map init from `application.init()` (#3426)
hfoxy May 2, 2024
5ee1140
remove Register/UnregisterWindow functionality
tmclane Apr 30, 2024
628b276
remove Println
tmclane May 2, 2024
910b693
noop: remove Println for events.Linux.ApplicationStartup
tmclane May 2, 2024
8b4b14a
example: environment - change css
tmclane Apr 23, 2024
d3d54e0
update devmode to use types and yaml (#3454)
atterpac May 5, 2024
bf9c1f0
Potential solution for getting the calling window of a bound method.
leaanthony Apr 25, 2024
ddec528
Remove window from context
leaanthony Apr 28, 2024
a788b3a
Improved window parameter handling
leaanthony May 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions v3/examples/binding/GreetService.go
@@ -1,17 +1,21 @@
package main

import "github.com/wailsapp/wails/v3/examples/binding/data"
import (
"context"
"github.com/wailsapp/wails/v3/pkg/application"
)

// GreetService is a service that greets people
type GreetService struct {
}

// Greet greets a person
func (*GreetService) Greet(name string) string {
return "Hello " + name
func (*GreetService) Greet(win application.Window, name string) string {
return "Hello " + name + " on " + win.Name()
}

// GreetPerson greets a person
func (*GreetService) GreetPerson(person data.Person) string {
return "Hello " + person.Name
// GreetWithCtx greets a person
func (*GreetService) GreetWithCtx(ctx context.Context, name string) string {
Copy link
Member

@tmclane tmclane Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like making this a specific type of Context aka a CallContext such that you can add porcelain to ask for the things you want to pull out. Would allow the user to not have to typecast for example.

type CallIdentifier string

type CallContext interface {
    Application() *application.App
    Attrs() map[string]any
    CallID() CallIdentifier 
    Get(string) any
    Window() application.Window
}

win := ctx.Value("window").(application.Window)
return "[ctx] Hello " + name + " on " + win.Name()
}
28 changes: 0 additions & 28 deletions v3/examples/binding/assets/bindings/data/models.js

This file was deleted.

8 changes: 4 additions & 4 deletions v3/examples/binding/assets/bindings/main/GreetService.js
Expand Up @@ -19,10 +19,10 @@ export async function Greet(name) {

/**
* GreetPerson greets a person
* @function GreetPerson
* @param person {dataPerson}
* @function GreetWithCtx
* @param name {string}
* @returns {Promise<string>}
**/
export async function GreetPerson(person) {
return Call.ByName("main.GreetService.GreetPerson", ...Array.prototype.slice.call(arguments, 0));
export async function GreetWithCtx(name) {
return Call.ByName("main.GreetService.GreetWithCtx", ...Array.prototype.slice.call(arguments, 0));
}
12 changes: 10 additions & 2 deletions v3/examples/binding/assets/index.html
Expand Up @@ -83,9 +83,17 @@
<script type="module">
import * as GreetService from "./bindings/main/GreetService.js";

let useCtx = false;
async function greet() {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value);
if( useCtx ) {
document.getElementById("result").innerHTML =
await GreetService.GreetWithCtx(document.getElementById("name").value);
useCtx = false;
} else {
document.getElementById("result").innerHTML =
await GreetService.Greet(document.getElementById("name").value);
useCtx = true;
}
}

document.getElementById("greet-btn").onclick = greet;
Expand Down
2 changes: 2 additions & 0 deletions v3/examples/binding/main.go
Expand Up @@ -26,6 +26,8 @@ func main() {

app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
URL: "/",
Name: "Window 1",
Title: "Window 1",
DevToolsEnabled: true,
})

Expand Down
8 changes: 8 additions & 0 deletions v3/pkg/application/messageprocessor_call.go
Expand Up @@ -79,6 +79,7 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
}

ctx, cancel := context.WithCancel(context.WithoutCancel(r.Context()))
ctx = context.WithValue(ctx, "window", window)

ambigiousID := false
m.l.Lock()
Expand All @@ -104,6 +105,13 @@ func (m *MessageProcessor) processCallMethod(method int, rw http.ResponseWriter,
m.l.Unlock()
}()

// Check if the first bound method parameter is a Window interface
if len(boundMethod.Inputs) > 0 {
if boundMethod.Inputs[0].ReflectType.String() == "application.Window" {
leaanthony marked this conversation as resolved.
Show resolved Hide resolved
// Prepend the options.Args with the current window
options.Args = append([]interface{}{window}, options.Args...)
}
}
result, err := boundMethod.Call(ctx, options.Args)
if err != nil {
m.callErrorCallback(window, "Error calling method: %s", callID, err)
Expand Down