5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-02 18:29:53 +08:00

Window event callbacks now take a WindowEventContext

Improved event example
This commit is contained in:
Lea Anthony 2023-02-12 07:58:14 +11:00
parent a2528fd066
commit 23bfeac02a
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
4 changed files with 21 additions and 12 deletions

View File

@ -4,14 +4,12 @@
<meta charset="UTF-8">
<title>Title</title>
<style>body{ text-align: center; color: white; background-color: rgba(0,0,0,0); user-select: none; -ms-user-select: none; -webkit-user-select: none; }</style>
<style>.file{ width: 100px; height: 100px; border: 3px solid black; }</style>
</head>
<body>
<h1>Events Demo</h1>
<br/>
<div class="file" id="123abc" data-contextmenu="f" data-contextmenu-data="someid" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
<div class="file" id="234abc" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
<div class="file" id="345abc" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
The main program emits an event every 10s which will be displayed in the section below.
To send an event from this window, click here: <button onclick="wails.Events.Emit({name:'myevent', data:'hello!'})">Send Event</button>
<div id="results"></div>
</body>
@ -20,9 +18,6 @@
let currentHTML = document.getElementById("results").innerHTML;
document.getElementById("results").innerHTML = currentHTML + "<br/>" + JSON.stringify(data);
})
window.addEventListener("dragstart", (event) =>
event.dataTransfer.setData("text/plain", "This text may be dragged")
);
</script>
</html>

View File

@ -0,0 +1,14 @@
package application
var blankWindowEventContext = &WindowEventContext{}
type WindowEventContext struct {
// contains filtered or unexported fields
data map[string]any
}
func newWindowEventContext() *Context {
return &Context{
data: make(map[string]any),
}
}

View File

@ -75,7 +75,7 @@ type WebviewWindow struct {
assets *assetserver.AssetServer
messageProcessor *MessageProcessor
eventListeners map[uint][]func()
eventListeners map[uint][]func(ctx *WindowEventContext)
eventListenersLock sync.RWMutex
contextMenus map[string]*Menu
@ -113,7 +113,7 @@ func NewWindow(options *WebviewWindowOptions) *WebviewWindow {
result := &WebviewWindow{
id: getWindowID(),
options: options,
eventListeners: make(map[uint][]func()),
eventListeners: make(map[uint][]func(ctx *WindowEventContext)),
contextMenus: make(map[string]*Menu),
assets: srv,
@ -400,7 +400,7 @@ func (w *WebviewWindow) Center() {
w.impl.center()
}
func (w *WebviewWindow) On(eventType events.WindowEventType, callback func()) {
func (w *WebviewWindow) On(eventType events.WindowEventType, callback func(ctx *WindowEventContext)) {
eventID := uint(eventType)
w.eventListenersLock.Lock()
defer w.eventListenersLock.Unlock()
@ -413,7 +413,7 @@ func (w *WebviewWindow) On(eventType events.WindowEventType, callback func()) {
func (w *WebviewWindow) handleWindowEvent(id uint) {
w.eventListenersLock.RLock()
for _, callback := range w.eventListeners[id] {
go callback()
go callback(blankWindowEventContext)
}
w.eventListenersLock.RUnlock()
}

View File

@ -1122,7 +1122,7 @@ func (w *macosWebviewWindow) run() {
w.setURL(w.parent.options.URL)
}
// We need to wait for the HTML to load before we can execute the javascript
w.parent.On(events.Mac.WebViewDidFinishNavigation, func() {
w.parent.On(events.Mac.WebViewDidFinishNavigation, func(_ *WindowEventContext) {
if w.parent.options.JS != "" {
w.execJS(w.parent.options.JS)
}