mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 18:10:48 +08:00
Window event callbacks now take a WindowEventContext
Improved event example
This commit is contained in:
parent
a2528fd066
commit
23bfeac02a
@ -4,14 +4,12 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Title</title>
|
<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>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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Events Demo</h1>
|
<h1>Events Demo</h1>
|
||||||
<br/>
|
<br/>
|
||||||
<div class="file" id="123abc" data-contextmenu="f" data-contextmenu-data="someid" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
|
The main program emits an event every 10s which will be displayed in the section below.
|
||||||
<div class="file" id="234abc" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
|
To send an event from this window, click here: <button onclick="wails.Events.Emit({name:'myevent', data:'hello!'})">Send Event</button>
|
||||||
<div class="file" id="345abc" draggable="true" ondragstart="dragstart()" ondragend="dragend()"></div>
|
|
||||||
<div id="results"></div>
|
<div id="results"></div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
@ -20,9 +18,6 @@
|
|||||||
let currentHTML = document.getElementById("results").innerHTML;
|
let currentHTML = document.getElementById("results").innerHTML;
|
||||||
document.getElementById("results").innerHTML = currentHTML + "<br/>" + JSON.stringify(data);
|
document.getElementById("results").innerHTML = currentHTML + "<br/>" + JSON.stringify(data);
|
||||||
})
|
})
|
||||||
window.addEventListener("dragstart", (event) =>
|
|
||||||
event.dataTransfer.setData("text/plain", "This text may be dragged")
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</html>
|
</html>
|
14
v3/pkg/application/context_window_event.go
Normal file
14
v3/pkg/application/context_window_event.go
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
@ -75,7 +75,7 @@ type WebviewWindow struct {
|
|||||||
assets *assetserver.AssetServer
|
assets *assetserver.AssetServer
|
||||||
messageProcessor *MessageProcessor
|
messageProcessor *MessageProcessor
|
||||||
|
|
||||||
eventListeners map[uint][]func()
|
eventListeners map[uint][]func(ctx *WindowEventContext)
|
||||||
eventListenersLock sync.RWMutex
|
eventListenersLock sync.RWMutex
|
||||||
|
|
||||||
contextMenus map[string]*Menu
|
contextMenus map[string]*Menu
|
||||||
@ -113,7 +113,7 @@ func NewWindow(options *WebviewWindowOptions) *WebviewWindow {
|
|||||||
result := &WebviewWindow{
|
result := &WebviewWindow{
|
||||||
id: getWindowID(),
|
id: getWindowID(),
|
||||||
options: options,
|
options: options,
|
||||||
eventListeners: make(map[uint][]func()),
|
eventListeners: make(map[uint][]func(ctx *WindowEventContext)),
|
||||||
contextMenus: make(map[string]*Menu),
|
contextMenus: make(map[string]*Menu),
|
||||||
|
|
||||||
assets: srv,
|
assets: srv,
|
||||||
@ -400,7 +400,7 @@ func (w *WebviewWindow) Center() {
|
|||||||
w.impl.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)
|
eventID := uint(eventType)
|
||||||
w.eventListenersLock.Lock()
|
w.eventListenersLock.Lock()
|
||||||
defer w.eventListenersLock.Unlock()
|
defer w.eventListenersLock.Unlock()
|
||||||
@ -413,7 +413,7 @@ func (w *WebviewWindow) On(eventType events.WindowEventType, callback func()) {
|
|||||||
func (w *WebviewWindow) handleWindowEvent(id uint) {
|
func (w *WebviewWindow) handleWindowEvent(id uint) {
|
||||||
w.eventListenersLock.RLock()
|
w.eventListenersLock.RLock()
|
||||||
for _, callback := range w.eventListeners[id] {
|
for _, callback := range w.eventListeners[id] {
|
||||||
go callback()
|
go callback(blankWindowEventContext)
|
||||||
}
|
}
|
||||||
w.eventListenersLock.RUnlock()
|
w.eventListenersLock.RUnlock()
|
||||||
}
|
}
|
||||||
|
@ -1122,7 +1122,7 @@ func (w *macosWebviewWindow) run() {
|
|||||||
w.setURL(w.parent.options.URL)
|
w.setURL(w.parent.options.URL)
|
||||||
}
|
}
|
||||||
// We need to wait for the HTML to load before we can execute the javascript
|
// 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 != "" {
|
if w.parent.options.JS != "" {
|
||||||
w.execJS(w.parent.options.JS)
|
w.execJS(w.parent.options.JS)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user