5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:51:26 +08:00

Remove usage of unsafe.Pointer in winc (#1556)

* Remove usage of unsafe.Pointer

* [windows] Remove MakeIntResource and add overloads for Load functions

* Fix `EnumProc` race condition.

* Refactor `EnumDisplayMonitors` to use `unsafe.Pointer` instead of `uintptr`

Co-authored-by: stffabi <stffabi@users.noreply.github.com>
This commit is contained in:
Lea Anthony 2022-07-18 18:22:46 +10:00 committed by GitHub
parent 463f5b3cb7
commit af1c530442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 30 additions and 21 deletions

View File

@ -39,7 +39,7 @@ func GetMonitorInfo(hMonitor w32.HMONITOR) (*w32.MONITORINFO, error) {
return &info, nil return &info, nil
} }
func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, dwData w32.LPARAM) uintptr { func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT, screenContainer *ScreenContainer) uintptr {
// adapted from https://stackoverflow.com/a/23492886/4188138 // adapted from https://stackoverflow.com/a/23492886/4188138
// see docs for the following pages to better understand this function // see docs for the following pages to better understand this function
@ -49,8 +49,6 @@ func EnumProc(hMonitor w32.HMONITOR, hdcMonitor w32.HDC, lprcMonitor *w32.RECT,
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow
ourMonitorData := Screen{} ourMonitorData := Screen{}
screenContainer := (*ScreenContainer)(unsafe.Pointer(dwData))
currentMonHndl := w32.MonitorFromWindow(screenContainer.mainWinHandle, w32.MONITOR_DEFAULTTONEAREST) currentMonHndl := w32.MonitorFromWindow(screenContainer.mainWinHandle, w32.MONITOR_DEFAULTTONEAREST)
currentMonInfo, currErr := GetMonitorInfo(currentMonHndl) currentMonInfo, currErr := GetMonitorInfo(currentMonHndl)
@ -99,7 +97,7 @@ func GetAllScreens(mainWinHandle w32.HWND) ([]Screen, error) {
dc := w32.GetDC(0) dc := w32.GetDC(0)
defer w32.ReleaseDC(0, dc) defer w32.ReleaseDC(0, dc)
succeeded := w32.EnumDisplayMonitors(dc, nil, syscall.NewCallback(EnumProc), uintptr(unsafe.Pointer(&monitorContainer))) succeeded := w32.EnumDisplayMonitors(dc, nil, syscall.NewCallback(EnumProc), unsafe.Pointer(&monitorContainer))
if !succeeded { if !succeeded {
return monitorContainer.monitors, errors.New("Windows call to EnumDisplayMonitors failed") return monitorContainer.monitors, errors.New("Windows call to EnumDisplayMonitors failed")
} }

View File

@ -29,7 +29,7 @@ func NewIconFromFile(path string) (*Icon, error) {
func NewIconFromResource(instance w32.HINSTANCE, resId uint16) (*Icon, error) { func NewIconFromResource(instance w32.HINSTANCE, resId uint16) (*Icon, error) {
ico := new(Icon) ico := new(Icon)
var err error var err error
if ico.handle = w32.LoadIcon(instance, w32.MakeIntResource(resId)); ico.handle == 0 { if ico.handle = w32.LoadIconWithResourceID(instance, resId); ico.handle == 0 {
err = errors.New(fmt.Sprintf("Cannot load icon from resource with id %v", resId)) err = errors.New(fmt.Sprintf("Cannot load icon from resource with id %v", resId))
} }
return ico, err return ico, err

View File

@ -292,7 +292,7 @@ func (iv *ImageViewBox) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
} else { } else {
if !iv.add { if !iv.add {
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(iv.getCursor(x, y)))) w32.SetCursor(w32.LoadCursorWithResourceID(0, iv.getCursor(x, y)))
} }
// do not call repaint if underMouse item did not change. // do not call repaint if underMouse item did not change.
if iv.updateHighlight(x, y) { if iv.updateHighlight(x, y) {

View File

@ -50,7 +50,7 @@ func (sp *VResizer) SetControl(control1, control2 Dockable, dir Direction, minSi
if sp.drag { if sp.drag {
x := e.Data.(*MouseEventData).X x := e.Data.(*MouseEventData).X
sp.update(x) sp.update(x)
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_SIZEWE))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_SIZEWE))
} }
fmt.Println("control1.OnMouseMove") fmt.Println("control1.OnMouseMove")
@ -60,7 +60,7 @@ func (sp *VResizer) SetControl(control1, control2 Dockable, dir Direction, minSi
if sp.drag { if sp.drag {
x := e.Data.(*MouseEventData).X x := e.Data.(*MouseEventData).X
sp.update(x) sp.update(x)
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_SIZEWE))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_SIZEWE))
} }
fmt.Println("control2.OnMouseMove") fmt.Println("control2.OnMouseMove")
@ -95,7 +95,7 @@ func (sp *VResizer) update(x int) {
fm := sp.parent.(*Form) fm := sp.parent.(*Form)
fm.UpdateLayout() fm.UpdateLayout()
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_ARROW))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_ARROW))
} }
func (sp *VResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr { func (sp *VResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
@ -108,7 +108,7 @@ func (sp *VResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
x, _ := genPoint(lparam) x, _ := genPoint(lparam)
sp.update(x) sp.update(x)
} else { } else {
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_SIZEWE))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_SIZEWE))
} }
if sp.mouseLeft { if sp.mouseLeft {
@ -179,7 +179,7 @@ func (sp *HResizer) update(y int) {
fm := sp.parent.(*Form) fm := sp.parent.(*Form)
fm.UpdateLayout() fm.UpdateLayout()
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_ARROW))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_ARROW))
} }
func (sp *HResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr { func (sp *HResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
@ -192,7 +192,7 @@ func (sp *HResizer) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
_, y := genPoint(lparam) _, y := genPoint(lparam)
sp.update(y) sp.update(y)
} else { } else {
w32.SetCursor(w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_SIZENS))) w32.SetCursor(w32.LoadCursorWithResourceID(0, w32.IDC_SIZENS))
} }
if sp.mouseLeft { if sp.mouseLeft {

View File

@ -78,7 +78,7 @@ func CreateWindow(className string, parent Controller, exStyle, style uint) w32.
func RegisterClass(className string, wndproc uintptr) { func RegisterClass(className string, wndproc uintptr) {
instance := GetAppInstance() instance := GetAppInstance()
icon := w32.LoadIcon(instance, w32.MakeIntResource(w32.IDI_APPLICATION)) icon := w32.LoadIconWithResourceID(instance, w32.IDI_APPLICATION)
var wc w32.WNDCLASSEX var wc w32.WNDCLASSEX
wc.Size = uint32(unsafe.Sizeof(wc)) wc.Size = uint32(unsafe.Sizeof(wc))
@ -87,7 +87,7 @@ func RegisterClass(className string, wndproc uintptr) {
wc.Instance = instance wc.Instance = instance
wc.Background = w32.COLOR_BTNFACE + 1 wc.Background = w32.COLOR_BTNFACE + 1
wc.Icon = icon wc.Icon = icon
wc.Cursor = w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_ARROW)) wc.Cursor = w32.LoadCursorWithResourceID(0, w32.IDC_ARROW)
wc.ClassName = syscall.StringToUTF16Ptr(className) wc.ClassName = syscall.StringToUTF16Ptr(className)
wc.MenuName = nil wc.MenuName = nil
wc.IconSm = icon wc.IconSm = icon

View File

@ -179,13 +179,28 @@ func LoadIcon(instance HINSTANCE, iconName *uint16) HICON {
return HICON(ret) return HICON(ret)
} }
func LoadIconWithResourceID(instance HINSTANCE, res uint16) HICON {
ret, _, _ := procLoadIcon.Call(
uintptr(instance),
uintptr(res))
return HICON(ret)
}
func LoadCursor(instance HINSTANCE, cursorName *uint16) HCURSOR { func LoadCursor(instance HINSTANCE, cursorName *uint16) HCURSOR {
ret, _, _ := procLoadCursor.Call( ret, _, _ := procLoadCursor.Call(
uintptr(instance), uintptr(instance),
uintptr(unsafe.Pointer(cursorName))) uintptr(unsafe.Pointer(cursorName)))
return HCURSOR(ret) return HCURSOR(ret)
}
func LoadCursorWithResourceID(instance HINSTANCE, res uint16) HCURSOR {
ret, _, _ := procLoadCursor.Call(
uintptr(instance),
uintptr(res))
return HCURSOR(ret)
} }
func ShowWindow(hwnd HWND, cmdshow int) bool { func ShowWindow(hwnd HWND, cmdshow int) bool {
@ -1115,12 +1130,12 @@ func GetMonitorInfo(hMonitor HMONITOR, lmpi *MONITORINFO) bool {
return ret != 0 return ret != 0
} }
func EnumDisplayMonitors(hdc HDC, clip *RECT, fnEnum, dwData uintptr) bool { func EnumDisplayMonitors(hdc HDC, clip *RECT, fnEnum uintptr, dwData unsafe.Pointer) bool {
ret, _, _ := procEnumDisplayMonitors.Call( ret, _, _ := procEnumDisplayMonitors.Call(
uintptr(hdc), hdc,
uintptr(unsafe.Pointer(clip)), uintptr(unsafe.Pointer(clip)),
fnEnum, fnEnum,
dwData, uintptr(dwData),
) )
return ret != 0 return ret != 0
} }

View File

@ -37,10 +37,6 @@ func FAILED(hr HRESULT) bool {
return hr < 0 return hr < 0
} }
func MakeIntResource(id uint16) *uint16 {
return (*uint16)(unsafe.Pointer(uintptr(id)))
}
func LOWORD(dw uint32) uint16 { func LOWORD(dw uint32) uint16 {
return uint16(dw) return uint16(dw)
} }