mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
Potential solution for getting the calling window of a bound method.
This commit is contained in:
@@ -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 {
|
||||
win := ctx.Value("window").(application.Window)
|
||||
return "[ctx] Hello " + name + " on " + win.Name()
|
||||
}
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
// @ts-check
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||
// This file is automatically generated. DO NOT EDIT
|
||||
|
||||
// Person holds someone's most important attributes
|
||||
export const Person = class {
|
||||
/**
|
||||
* Creates a new Person instance.
|
||||
* @constructor
|
||||
* @param {Object} source - The source object to create the Person.
|
||||
* @param {string} source.Name
|
||||
*/
|
||||
constructor(source = {}) {
|
||||
const { name = "" } = source;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -26,6 +26,8 @@ func main() {
|
||||
|
||||
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
|
||||
URL: "/",
|
||||
Name: "Window 1",
|
||||
Title: "Window 1",
|
||||
DevToolsEnabled: true,
|
||||
})
|
||||
|
||||
|
||||
@@ -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()
|
||||
@@ -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" {
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user