5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 02:39:30 +08:00

Support raw messages from JS

This commit is contained in:
Lea Anthony 2024-06-08 20:43:07 +10:00
parent 9d24c039fe
commit 9d488c97e0
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
9 changed files with 104 additions and 11 deletions

View File

@ -0,0 +1,11 @@
# Raw Message Example
This example is a demonstration of sending raw messages from JS to Go.
## Running the example
To run the example, simply run the following command:
```bash
go run main.go
```

View File

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>body{ text-align: center; color: white; background-color: #191919; user-select: none; -ms-user-select: none; -webkit-user-select: none; }</style>
<script type="module">
import {System} from "/wails/runtime.js";
document.getElementById('sendrawmessage').addEventListener('click', function (event) {
let rawMessage = document.getElementById("message").value;
System.invoke(rawMessage);
});
</script>
</head>
<body>
<h1>Raw Message Demo</h1>
<br/>
To send a raw message from this window, enter some text and click the button:
<br/>
<br/>
<label for="message"></label><input type="text" id="message" placeholder="Enter message here"/>
<button id="sendrawmessage">Send Raw Message</button>
</body>
</html>

View File

@ -0,0 +1,44 @@
package main
import (
"embed"
_ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"log"
)
//go:embed assets
var assets embed.FS
func main() {
app := application.New(application.Options{
Name: "Raw Message Demo",
Description: "A demo of sending raw messages from the frontend",
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
RawMessageHandler: func(window application.Window, message string) {
println("Raw message received from Window '" + window.Name() + "' with message: " + message)
},
})
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Title: "Window 1",
Name: "Window 1",
Mac: application.MacWindow{
Backdrop: application.MacBackdropTranslucent,
TitleBar: application.MacTitleBarHiddenInsetUnified,
InvisibleTitleBarHeight: 50,
},
})
err := app.Run()
if err != nil {
log.Fatal(err.Error())
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -46,7 +46,7 @@ function onMouseDown(e) {
// Check for resizing
if (resizeEdge) {
invoke("resize:" + resizeEdge);
invoke("wails:resize:" + resizeEdge);
e.preventDefault();
return;
}
@ -76,7 +76,7 @@ function onMouseMove(e) {
shouldDrag = false;
let mousePressed = e.buttons !== undefined ? e.buttons : e.which;
if (mousePressed > 0) {
invoke("drag");
invoke("wails:drag");
return;
}
}

View File

@ -10,6 +10,7 @@ import (
"os"
"runtime"
"strconv"
"strings"
"sync"
"github.com/wailsapp/wails/v3/internal/operatingsystem"
@ -612,8 +613,14 @@ func (a *App) handleWindowMessage(event *windowMessage) {
log.Printf("WebviewWindow #%d not found", event.windowId)
return
}
// Get callback from window
window.HandleMessage(event.message)
// Check if the message starts with "wails:"
if strings.HasPrefix(event.message, "wails:") {
window.HandleMessage(event.message)
} else {
if a.options.RawMessageHandler != nil {
a.options.RawMessageHandler(window, event.message)
}
}
}
func (a *App) handleWebViewRequest(request *webViewAssetRequest) {

View File

@ -88,6 +88,10 @@ type Options struct {
// If the function returns false, the application will not quit.
ShouldQuit func() bool
// RawMessageHandler is called when the frontend sends a raw message.
// This is useful for implementing custom frontend-to-backend communication.
RawMessageHandler func(window Window, message string)
// This blank field ensures types from other packages
// are never convertible to Options.
// This property, in turn, improves the accuracy of the binding generator.

View File

@ -647,7 +647,7 @@ func (w *WebviewWindow) SetBackgroundColour(colour RGBA) Window {
func (w *WebviewWindow) HandleMessage(message string) {
// Check for special messages
switch true {
case message == "drag":
case message == "wails:drag":
if !w.IsFullscreen() {
InvokeSync(func() {
err := w.startDrag()
@ -656,7 +656,7 @@ func (w *WebviewWindow) HandleMessage(message string) {
}
})
}
case strings.HasPrefix(message, "resize:"):
case strings.HasPrefix(message, "wails:resize:"):
if !w.IsFullscreen() {
sl := strings.Split(message, ":")
if len(sl) != 2 {
@ -675,6 +675,8 @@ func (w *WebviewWindow) HandleMessage(message string) {
for _, js := range w.pendingJS {
w.ExecJS(js)
}
default:
w.Error("Unknown message sent via 'invoke' on frontend: %v", message)
}
}