Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Windows MessageBox API for error when GLFW can't create a Window. #2976

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (d *gLDriver) windowList() []fyne.Window {
}

func (d *gLDriver) initFailed(msg string, err error) {
fyne.LogError(msg, err)
logError(msg, err)

run.Lock()
if !run.flag {
Expand Down
10 changes: 10 additions & 0 deletions internal/driver/glfw/driver_notwindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !windows
// +build !windows

package glfw

import "fyne.io/fyne/v2"

func logError(msg string, err error) {
fyne.LogError(msg, err)
}
50 changes: 50 additions & 0 deletions internal/driver/glfw/driver_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package glfw

import (
"fmt"
"runtime"
"syscall"
"unsafe"
)

type MB uint32

const (
MB_OK MB = 0x0000_0000
MB_ICONERROR MB = 0x0000_0010
)

func toNativePtr(s string) *uint16 {
pstr, err := syscall.UTF16PtrFromString(s)
if err != nil {
panic(fmt.Sprintf("toNativePtr() failed \"%s\": %s", s, err))
}
return pstr
}

// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
func messageBoxError(text, caption string) {
user32 := syscall.NewLazyDLL("user32.dll")
MessageBox := user32.NewProc("MessageBoxW")

uType := MB_OK | MB_ICONERROR

syscall.Syscall6(MessageBox.Addr(), 4,
uintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(toNativePtr(text))),
uintptr(unsafe.Pointer(toNativePtr(caption))), uintptr(uType),
0, 0)
}

func logError(msg string, err error) {
text := fmt.Sprintf("Fyne error: %v", msg)
if err != nil {
text = text + fmt.Sprintf("\n Cause:%v", err)
}

_, file, line, ok := runtime.Caller(1)
if ok {
text = text + fmt.Sprintf("\n At: %s:%d", file, line)
}

messageBoxError(text, "Fyne Error")
}