mirror of
https://github.com/wavetermdev/wails.git
synced 2026-04-22 15:26:15 -07:00
Initial signal handling
This commit is contained in:
@@ -3,14 +3,17 @@ package application
|
||||
import (
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/pkg/browser"
|
||||
"github.com/samber/lo"
|
||||
@@ -66,6 +69,10 @@ func New(appOptions Options) *App {
|
||||
result.logStartup()
|
||||
result.logPlatformInfo()
|
||||
|
||||
if !appOptions.DisableDefaultSignalHandler {
|
||||
result.setupDefaultSignalHandler()
|
||||
}
|
||||
|
||||
result.Events = NewWailsEventProcessor(result.dispatchEventToWindows)
|
||||
|
||||
opts := &assetserver.Options{
|
||||
@@ -876,3 +883,27 @@ func (a *App) shouldQuit() bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (a *App) setupDefaultSignalHandler() {
|
||||
const maxSignal = 3
|
||||
|
||||
ctrlC := make(chan os.Signal, maxSignal)
|
||||
signal.Notify(ctrlC, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
for i := 1; i <= maxSignal; i++ {
|
||||
sig := <-ctrlC
|
||||
|
||||
if i == 1 {
|
||||
fmt.Printf("Received signal: %v. Quitting...\n", sig)
|
||||
go a.Quit()
|
||||
} else if i < maxSignal {
|
||||
fmt.Printf("Received signal: %v. Press CTRL+C %d more times to force quit...\n", sig, maxSignal-i)
|
||||
continue
|
||||
} else {
|
||||
fmt.Printf("Received signal: %v. Force quitting...\n", sig)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -46,9 +46,12 @@ type Options struct {
|
||||
// This is also used by Wails to provide information to the frontend.
|
||||
Flags map[string]any
|
||||
|
||||
// PanicHandler is a way to register a custom panic handler
|
||||
// PanicHandler is called when a panic occurs
|
||||
PanicHandler func(any)
|
||||
|
||||
// DisableDefaultSignalHandler disables the default signal handler
|
||||
DisableDefaultSignalHandler bool
|
||||
|
||||
// KeyBindings is a map of key bindings to functions
|
||||
KeyBindings map[string]func(window *WebviewWindow)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user