use native win32 messagebox to prevent wails dialog crash
All checks were successful
Deploy on Master Change / deploy (push) Successful in 37s

This commit is contained in:
2026-07-01 08:54:01 +07:00
parent 745e93d28d
commit 7a8c397538
3 changed files with 38 additions and 15 deletions

View File

@@ -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

View File

@@ -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)
}()
}

View File

@@ -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()