Initial signal handling

This commit is contained in:
Lea Anthony
2024-01-18 08:41:32 +11:00
parent 23c1a7d1d8
commit da6311f7d6
2 changed files with 35 additions and 1 deletions
+31
View File
@@ -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)
}
}
}()
}
+4 -1
View File
@@ -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)