Skip to content

Commit

Permalink
Fix window shutdown issue.
Browse files Browse the repository at this point in the history
Handle error serving capabilities.
  • Loading branch information
leaanthony committed Dec 14, 2024
1 parent 7aa7534 commit e75f9c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 85 deletions.
88 changes: 24 additions & 64 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import (

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

"github.com/wailsapp/wails/v3/internal/signal"

"github.com/pkg/browser"
"github.com/samber/lo"
"github.com/wailsapp/wails/v3/internal/signal"

"github.com/wailsapp/wails/v3/internal/assetserver"
"github.com/wailsapp/wails/v3/internal/assetserver/webview"
"github.com/wailsapp/wails/v3/internal/capabilities"
Expand Down Expand Up @@ -101,7 +101,10 @@ func New(appOptions Options) *App {
case "/wails/runtime":
messageProc.ServeHTTP(rw, req)
case "/wails/capabilities":
assetserver.ServeFile(rw, path, globalApplication.capabilities.AsBytes())
err := assetserver.ServeFile(rw, path, globalApplication.capabilities.AsBytes())
if err != nil {
result.handleFatalError(fmt.Errorf("unable to serve capabilities: %s", err.Error()))
}
case "/wails/flags":
updatedOptions := result.impl.GetFlags(appOptions)
flags, err := json.Marshal(updatedOptions)
Expand Down Expand Up @@ -581,73 +584,45 @@ func (a *App) Run() error {
a.impl = newPlatformApp(a)
go func() {
for {
select {
case <-a.ctx.Done():
return
case event := <-applicationEvents:
go a.handleApplicationEvent(event)
}
event := <-applicationEvents
go a.handleApplicationEvent(event)
}
}()
go func() {
for {
select {
case <-a.ctx.Done():
return
case event := <-windowEvents:
go a.handleWindowEvent(event)
}
event := <-windowEvents
go a.handleWindowEvent(event)
}
}()
go func() {
for {
select {
case <-a.ctx.Done():
return
case request := <-webviewRequests:
go a.handleWebViewRequest(request)
}
request := <-webviewRequests
go a.handleWebViewRequest(request)
}
}()
go func() {
for {
select {
case <-a.ctx.Done():
return
case event := <-windowMessageBuffer:
go a.handleWindowMessage(event)
}
event := <-windowMessageBuffer
go a.handleWindowMessage(event)
}
}()
go func() {
for {
select {
case <-a.ctx.Done():
return
case event := <-windowKeyEvents:
go a.handleWindowKeyEvent(event)
}
event := <-windowKeyEvents
go a.handleWindowKeyEvent(event)
}
}()
go func() {
for {
select {
case <-a.ctx.Done():
return
case dragAndDropMessage := <-windowDragAndDropBuffer:
go a.handleDragAndDropMessage(dragAndDropMessage)
}
dragAndDropMessage := <-windowDragAndDropBuffer
go a.handleDragAndDropMessage(dragAndDropMessage)
}
}()

go func() {
for {
select {
case <-a.ctx.Done():
return
case menuItemID := <-menuItemClicked:

go a.handleMenuItemClicked(menuItemID)
}
menuItemID := <-menuItemClicked
go a.handleMenuItemClicked(menuItemID)
}
}()

Expand Down Expand Up @@ -717,31 +692,25 @@ func (a *App) handleApplicationEvent(event *ApplicationEvent) {
}

func (a *App) handleDragAndDropMessage(event *dragAndDropMessage) {
if globalApplication.performingShutdown {
return
}
// Get window from window map
a.windowsLock.Lock()
window, ok := a.windows[event.windowId]
a.windowsLock.Unlock()
if !ok {
log.Printf("handleDragAndDropMessage: WebviewWindow #%d not found", event.windowId)
log.Printf("WebviewWindow #%d not found", event.windowId)
return
}
// Get callback from window
window.HandleDragAndDropMessage(event.filenames)
}

func (a *App) handleWindowMessage(event *windowMessage) {
if globalApplication.performingShutdown {
return
}
// Get window from window map
a.windowsLock.RLock()
window, ok := a.windows[event.windowId]
a.windowsLock.RUnlock()
if !ok {
log.Printf("handleWindowMessage: WebviewWindow #%d not found", event.windowId)
log.Printf("WebviewWindow #%d not found", event.windowId)
return
}
// Check if the message starts with "wails:"
Expand All @@ -759,27 +728,18 @@ func (a *App) handleWebViewRequest(request *webViewAssetRequest) {
}

func (a *App) handleWindowEvent(event *windowEvent) {
if globalApplication.performingShutdown {
return
}
// Get window from window map
a.windowsLock.RLock()
window, ok := a.windows[event.WindowID]
a.windowsLock.RUnlock()

if !ok {
// Window not found - it's probably been destroyed
log.Printf("Window #%d not found", event.WindowID)
return
}

// Normal event handling for active windows
window.HandleWindowEvent(event.EventID)
}

func (a *App) handleMenuItemClicked(menuItemID uint) {
if globalApplication.performingShutdown {
return
}
menuItem := getMenuItemByID(menuItemID)
if menuItem == nil {
log.Printf("MenuItem #%d not found", menuItemID)
Expand Down
32 changes: 11 additions & 21 deletions v3/pkg/application/webview_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ type (
type WindowEvent struct {
ctx *WindowEventContext
Cancelled bool
WindowID uint
}

func (w *WindowEvent) Context() *WindowEventContext {
Expand Down Expand Up @@ -256,8 +255,9 @@ func NewWindow(options WebviewWindowOptions) *WebviewWindow {
shouldClose = result.options.ShouldClose(result)
}
if shouldClose {
globalApplication.deleteWindowByID(result.id)
result.markAsDestroyed()
InvokeSync(result.impl.close)
globalApplication.deleteWindowByID(result.id)
}
})

Expand Down Expand Up @@ -719,7 +719,7 @@ func (w *WebviewWindow) startResize(border string) error {
if w.impl == nil && !w.isDestroyed() {
return nil
}
return InvokeSyncWithError(func() error {
return InvokeSyncWithResult(func() error {
return w.impl.startResize(border)
})
}
Expand Down Expand Up @@ -771,7 +771,6 @@ func (w *WebviewWindow) RegisterHook(eventType events.WindowEventType, callback
}

func (w *WebviewWindow) HandleWindowEvent(id uint) {
// Get hooks for this event
w.eventListenersLock.RLock()
defer w.eventListenersLock.RUnlock()

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

// Create new WindowEvent
thisEvent := NewWindowEvent()
thisEvent.WindowID = w.id

for _, thisHook := range hooks {
thisHook.callback(thisEvent)
Expand Down Expand Up @@ -908,18 +906,11 @@ func (w *WebviewWindow) Destroy() {
return
}

// Mark as being destroyed - this prevents new events from being processed
w.markAsDestroyed()

// Cancel all callbacks
// Cancel the callbacks
for _, cancelFunc := range w.cancellers {
cancelFunc()
}

// Remove from global application map and destroy the native window
globalApplication.windowsLock.Lock()
delete(globalApplication.windows, w.id)
globalApplication.windowsLock.Unlock()
InvokeSync(w.impl.destroy)
}

Expand Down Expand Up @@ -1189,7 +1180,6 @@ func (w *WebviewWindow) HandleDragAndDropMessage(filenames []string) {
ctx := newWindowEventContext()
ctx.setDroppedFiles(filenames)
thisEvent.ctx = ctx
thisEvent.WindowID = w.id
for _, listener := range w.eventListeners[uint(events.Common.WindowFilesDropped)] {
listener.callback(thisEvent)
}
Expand Down Expand Up @@ -1234,6 +1224,13 @@ func (w *WebviewWindow) Focus() {
w.emit(events.Common.WindowFocus)
}

func (w *WebviewWindow) emit(eventType events.WindowEventType) {
windowEvents <- &windowEvent{
WindowID: w.id,
EventID: uint(eventType),
}
}

func (w *WebviewWindow) startDrag() error {
if w.impl == nil && !w.isDestroyed() {
return nil
Expand Down Expand Up @@ -1362,10 +1359,3 @@ func (w *WebviewWindow) delete() {
w.impl.delete()
}
}

func (w *WebviewWindow) emit(eventType events.WindowEventType) {
windowEvents <- &windowEvent{
WindowID: w.id,
EventID: uint(eventType),
}
}

0 comments on commit e75f9c9

Please sign in to comment.