From 7a8c397538697412fa820fadb6325de96bee739d Mon Sep 17 00:00:00 2001 From: PhuocNTB Date: Wed, 1 Jul 2026 08:54:01 +0700 Subject: [PATCH] use native win32 messagebox to prevent wails dialog crash --- client/app.go | 19 ++++--------------- client/internal/winapi/window_windows.go | 11 +++++++++++ client/main.go | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/client/app.go b/client/app.go index 7618815..9e1d816 100644 --- a/client/app.go +++ b/client/app.go @@ -222,21 +222,10 @@ func (a *App) startup(ctx context.Context) { lastKillDialogTime = time.Now() killDialogMu.Unlock() - go func() { - defer func() { - if r := recover(); r != nil { - log.Printf("[APP] Recovered from MessageDialog panic: %v", r) - } - }() - if a.ctx == nil { - return - } - _, _ = runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{ - Type: runtime.WarningDialog, - Title: "Ứng dụng bị đóng", - Message: fmt.Sprintf("Hệ thống đã tự động đóng ứng dụng '%s' (%s) do không nằm trong danh sách các ứng dụng được phép sử dụng trong giờ học.", procName, title), - }) - }() + winapi.ShowWarningMessageBox( + "Ứng dụng bị đóng", + fmt.Sprintf("Hệ thống đã tự động đóng ứng dụng '%s' (%s) do không nằm trong danh sách các ứng dụng được phép sử dụng trong giờ học.", procName, title), + ) } // Giám sát môi trường Windows: đa màn hình, desktop ảo, đổi user diff --git a/client/internal/winapi/window_windows.go b/client/internal/winapi/window_windows.go index 7afca2e..0f9d48c 100644 --- a/client/internal/winapi/window_windows.go +++ b/client/internal/winapi/window_windows.go @@ -19,6 +19,7 @@ var ( procSetForegroundWindow = user32Window.NewProc("SetForegroundWindow") procFlashWindowEx = user32Window.NewProc("FlashWindowEx") procMessageBeep = user32Window.NewProc("MessageBeep") + procMessageBoxW = user32Window.NewProc("MessageBoxW") ) const ( @@ -89,3 +90,13 @@ func findWindowByTitleContains(hint string) uintptr { _, _, _ = procEnumWindows.Call(enumWindowsCallback, 0) return enumFoundHwnd } + +// ShowWarningMessageBox hiển thị hộp thoại cảnh báo Win32 bất đồng bộ +func ShowWarningMessageBox(title, message string) { + titlePtr, _ := syscall.UTF16PtrFromString(title) + messagePtr, _ := syscall.UTF16PtrFromString(message) + go func() { + // MB_ICONWARNING = 0x00000030, MB_TOPMOST = 0x00040000 + _, _, _ = procMessageBoxW.Call(0, uintptr(unsafe.Pointer(messagePtr)), uintptr(unsafe.Pointer(titlePtr)), 0x00040030) + }() +} diff --git a/client/main.go b/client/main.go index 601be59..3009dbe 100644 --- a/client/main.go +++ b/client/main.go @@ -2,6 +2,9 @@ package main import ( "embed" + "log" + "os" + "path/filepath" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/menu" @@ -14,7 +17,27 @@ import ( //go:embed all:frontend/dist var assets embed.FS +func initLogging() *os.File { + configDir, err := os.UserConfigDir() + if err != nil { + return nil + } + appDir := filepath.Join(configDir, "SimpleCare") + _ = os.MkdirAll(appDir, 0755) + logFile, err := os.OpenFile(filepath.Join(appDir, "app.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err == nil { + log.SetOutput(logFile) + log.Println("--- Client App Started ---") + } + return logFile +} + func main() { + lf := initLogging() + if lf != nil { + defer lf.Close() + } + // Create an instance of the app structure app := NewApp()