Skip to content

Commit e75f9c9

Browse files
committed
Fix window shutdown issue.
Handle error serving capabilities.
1 parent 7aa7534 commit e75f9c9

File tree

2 files changed

+35
-85
lines changed

2 files changed

+35
-85
lines changed

v3/pkg/application/application.go

+24-64
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020

2121
"github.com/wailsapp/wails/v3/internal/operatingsystem"
2222

23-
"github.com/wailsapp/wails/v3/internal/signal"
24-
2523
"github.com/pkg/browser"
2624
"github.com/samber/lo"
25+
"github.com/wailsapp/wails/v3/internal/signal"
26+
2727
"github.com/wailsapp/wails/v3/internal/assetserver"
2828
"github.com/wailsapp/wails/v3/internal/assetserver/webview"
2929
"github.com/wailsapp/wails/v3/internal/capabilities"
@@ -101,7 +101,10 @@ func New(appOptions Options) *App {
101101
case "/wails/runtime":
102102
messageProc.ServeHTTP(rw, req)
103103
case "/wails/capabilities":
104-
assetserver.ServeFile(rw, path, globalApplication.capabilities.AsBytes())
104+
err := assetserver.ServeFile(rw, path, globalApplication.capabilities.AsBytes())
105+
if err != nil {
106+
result.handleFatalError(fmt.Errorf("unable to serve capabilities: %s", err.Error()))
107+
}
105108
case "/wails/flags":
106109
updatedOptions := result.impl.GetFlags(appOptions)
107110
flags, err := json.Marshal(updatedOptions)
@@ -581,73 +584,45 @@ func (a *App) Run() error {
581584
a.impl = newPlatformApp(a)
582585
go func() {
583586
for {
584-
select {
585-
case <-a.ctx.Done():
586-
return
587-
case event := <-applicationEvents:
588-
go a.handleApplicationEvent(event)
589-
}
587+
event := <-applicationEvents
588+
go a.handleApplicationEvent(event)
590589
}
591590
}()
592591
go func() {
593592
for {
594-
select {
595-
case <-a.ctx.Done():
596-
return
597-
case event := <-windowEvents:
598-
go a.handleWindowEvent(event)
599-
}
593+
event := <-windowEvents
594+
go a.handleWindowEvent(event)
600595
}
601596
}()
602597
go func() {
603598
for {
604-
select {
605-
case <-a.ctx.Done():
606-
return
607-
case request := <-webviewRequests:
608-
go a.handleWebViewRequest(request)
609-
}
599+
request := <-webviewRequests
600+
go a.handleWebViewRequest(request)
610601
}
611602
}()
612603
go func() {
613604
for {
614-
select {
615-
case <-a.ctx.Done():
616-
return
617-
case event := <-windowMessageBuffer:
618-
go a.handleWindowMessage(event)
619-
}
605+
event := <-windowMessageBuffer
606+
go a.handleWindowMessage(event)
620607
}
621608
}()
622609
go func() {
623610
for {
624-
select {
625-
case <-a.ctx.Done():
626-
return
627-
case event := <-windowKeyEvents:
628-
go a.handleWindowKeyEvent(event)
629-
}
611+
event := <-windowKeyEvents
612+
go a.handleWindowKeyEvent(event)
630613
}
631614
}()
632615
go func() {
633616
for {
634-
select {
635-
case <-a.ctx.Done():
636-
return
637-
case dragAndDropMessage := <-windowDragAndDropBuffer:
638-
go a.handleDragAndDropMessage(dragAndDropMessage)
639-
}
617+
dragAndDropMessage := <-windowDragAndDropBuffer
618+
go a.handleDragAndDropMessage(dragAndDropMessage)
640619
}
641620
}()
621+
642622
go func() {
643623
for {
644-
select {
645-
case <-a.ctx.Done():
646-
return
647-
case menuItemID := <-menuItemClicked:
648-
649-
go a.handleMenuItemClicked(menuItemID)
650-
}
624+
menuItemID := <-menuItemClicked
625+
go a.handleMenuItemClicked(menuItemID)
651626
}
652627
}()
653628

@@ -717,31 +692,25 @@ func (a *App) handleApplicationEvent(event *ApplicationEvent) {
717692
}
718693

719694
func (a *App) handleDragAndDropMessage(event *dragAndDropMessage) {
720-
if globalApplication.performingShutdown {
721-
return
722-
}
723695
// Get window from window map
724696
a.windowsLock.Lock()
725697
window, ok := a.windows[event.windowId]
726698
a.windowsLock.Unlock()
727699
if !ok {
728-
log.Printf("handleDragAndDropMessage: WebviewWindow #%d not found", event.windowId)
700+
log.Printf("WebviewWindow #%d not found", event.windowId)
729701
return
730702
}
731703
// Get callback from window
732704
window.HandleDragAndDropMessage(event.filenames)
733705
}
734706

735707
func (a *App) handleWindowMessage(event *windowMessage) {
736-
if globalApplication.performingShutdown {
737-
return
738-
}
739708
// Get window from window map
740709
a.windowsLock.RLock()
741710
window, ok := a.windows[event.windowId]
742711
a.windowsLock.RUnlock()
743712
if !ok {
744-
log.Printf("handleWindowMessage: WebviewWindow #%d not found", event.windowId)
713+
log.Printf("WebviewWindow #%d not found", event.windowId)
745714
return
746715
}
747716
// Check if the message starts with "wails:"
@@ -759,27 +728,18 @@ func (a *App) handleWebViewRequest(request *webViewAssetRequest) {
759728
}
760729

761730
func (a *App) handleWindowEvent(event *windowEvent) {
762-
if globalApplication.performingShutdown {
763-
return
764-
}
765731
// Get window from window map
766732
a.windowsLock.RLock()
767733
window, ok := a.windows[event.WindowID]
768734
a.windowsLock.RUnlock()
769-
770735
if !ok {
771-
// Window not found - it's probably been destroyed
736+
log.Printf("Window #%d not found", event.WindowID)
772737
return
773738
}
774-
775-
// Normal event handling for active windows
776739
window.HandleWindowEvent(event.EventID)
777740
}
778741

779742
func (a *App) handleMenuItemClicked(menuItemID uint) {
780-
if globalApplication.performingShutdown {
781-
return
782-
}
783743
menuItem := getMenuItemByID(menuItemID)
784744
if menuItem == nil {
785745
log.Printf("MenuItem #%d not found", menuItemID)

v3/pkg/application/webview_window.go

+11-21
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ type (
110110
type WindowEvent struct {
111111
ctx *WindowEventContext
112112
Cancelled bool
113-
WindowID uint
114113
}
115114

116115
func (w *WindowEvent) Context() *WindowEventContext {
@@ -256,8 +255,9 @@ func NewWindow(options WebviewWindowOptions) *WebviewWindow {
256255
shouldClose = result.options.ShouldClose(result)
257256
}
258257
if shouldClose {
259-
globalApplication.deleteWindowByID(result.id)
258+
result.markAsDestroyed()
260259
InvokeSync(result.impl.close)
260+
globalApplication.deleteWindowByID(result.id)
261261
}
262262
})
263263

@@ -719,7 +719,7 @@ func (w *WebviewWindow) startResize(border string) error {
719719
if w.impl == nil && !w.isDestroyed() {
720720
return nil
721721
}
722-
return InvokeSyncWithError(func() error {
722+
return InvokeSyncWithResult(func() error {
723723
return w.impl.startResize(border)
724724
})
725725
}
@@ -771,7 +771,6 @@ func (w *WebviewWindow) RegisterHook(eventType events.WindowEventType, callback
771771
}
772772

773773
func (w *WebviewWindow) HandleWindowEvent(id uint) {
774-
// Get hooks for this event
775774
w.eventListenersLock.RLock()
776775
defer w.eventListenersLock.RUnlock()
777776

@@ -782,7 +781,6 @@ func (w *WebviewWindow) HandleWindowEvent(id uint) {
782781

783782
// Create new WindowEvent
784783
thisEvent := NewWindowEvent()
785-
thisEvent.WindowID = w.id
786784

787785
for _, thisHook := range hooks {
788786
thisHook.callback(thisEvent)
@@ -908,18 +906,11 @@ func (w *WebviewWindow) Destroy() {
908906
return
909907
}
910908

911-
// Mark as being destroyed - this prevents new events from being processed
912-
w.markAsDestroyed()
913-
914-
// Cancel all callbacks
909+
// Cancel the callbacks
915910
for _, cancelFunc := range w.cancellers {
916911
cancelFunc()
917912
}
918913

919-
// Remove from global application map and destroy the native window
920-
globalApplication.windowsLock.Lock()
921-
delete(globalApplication.windows, w.id)
922-
globalApplication.windowsLock.Unlock()
923914
InvokeSync(w.impl.destroy)
924915
}
925916

@@ -1189,7 +1180,6 @@ func (w *WebviewWindow) HandleDragAndDropMessage(filenames []string) {
11891180
ctx := newWindowEventContext()
11901181
ctx.setDroppedFiles(filenames)
11911182
thisEvent.ctx = ctx
1192-
thisEvent.WindowID = w.id
11931183
for _, listener := range w.eventListeners[uint(events.Common.WindowFilesDropped)] {
11941184
listener.callback(thisEvent)
11951185
}
@@ -1234,6 +1224,13 @@ func (w *WebviewWindow) Focus() {
12341224
w.emit(events.Common.WindowFocus)
12351225
}
12361226

1227+
func (w *WebviewWindow) emit(eventType events.WindowEventType) {
1228+
windowEvents <- &windowEvent{
1229+
WindowID: w.id,
1230+
EventID: uint(eventType),
1231+
}
1232+
}
1233+
12371234
func (w *WebviewWindow) startDrag() error {
12381235
if w.impl == nil && !w.isDestroyed() {
12391236
return nil
@@ -1362,10 +1359,3 @@ func (w *WebviewWindow) delete() {
13621359
w.impl.delete()
13631360
}
13641361
}
1365-
1366-
func (w *WebviewWindow) emit(eventType events.WindowEventType) {
1367-
windowEvents <- &windowEvent{
1368-
WindowID: w.id,
1369-
EventID: uint(eventType),
1370-
}
1371-
}

0 commit comments

Comments
 (0)