From 00c6f0dfdbd7f34b5a6bcaeae21fa1c37cd1da6b Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Mon, 1 May 2023 21:11:40 +1000 Subject: [PATCH] [v3 windows] Implement `getScreen` --- v3/pkg/application/webview_window_windows.go | 60 +++++++++++++++++++- v3/pkg/w32/user32.go | 8 +++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index b820abe9a..aacc1e993 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -7,6 +7,8 @@ import ( "fmt" "github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/w32" + "strconv" + "unicode/utf16" "unsafe" ) @@ -392,9 +394,63 @@ func (w *windowsWebviewWindow) hide() { w32.ShowWindow(w.hwnd, w32.SW_HIDE) } +// Get the screen for the current window func (w *windowsWebviewWindow) getScreen() (*Screen, error) { - //TODO implement me - panic("implement me") + hMonitor := w32.MonitorFromWindow(w.hwnd, w32.MONITOR_DEFAULTTONEAREST) + + var mi w32.MONITORINFOEX + mi.CbSize = uint32(unsafe.Sizeof(mi)) + w32.GetMonitorInfoEx(hMonitor, &mi) + var thisScreen Screen + thisScreen.X = int(mi.RcMonitor.Left) + thisScreen.Y = int(mi.RcMonitor.Top) + thisScreen.Size = Size{ + Width: int(mi.RcMonitor.Right - mi.RcMonitor.Left), + Height: int(mi.RcMonitor.Bottom - mi.RcMonitor.Top), + } + thisScreen.Bounds = Rect{ + X: int(mi.RcMonitor.Left), + Y: int(mi.RcMonitor.Top), + Width: int(mi.RcMonitor.Right - mi.RcMonitor.Left), + Height: int(mi.RcMonitor.Bottom - mi.RcMonitor.Top), + } + thisScreen.WorkArea = Rect{ + X: int(mi.RcWork.Left), + Y: int(mi.RcWork.Top), + Width: int(mi.RcWork.Right - mi.RcWork.Left), + Height: int(mi.RcWork.Bottom - mi.RcWork.Top), + } + thisScreen.ID = strconv.Itoa(int(hMonitor)) + thisScreen.Name = string(utf16.Decode(mi.SzDevice[:])) + var xdpi, ydpi w32.UINT + w32.GetDPIForMonitor(hMonitor, w32.MDT_EFFECTIVE_DPI, &xdpi, &ydpi) + thisScreen.Scale = float32(xdpi) / 96.0 + thisScreen.IsPrimary = mi.DwFlags&w32.MONITORINFOF_PRIMARY != 0 + + // TODO: Get screen rotation + // https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-devmodea + + //// get display settings for monitor + //var dm w32.DEVMODE + //dm.DmSize = uint16(unsafe.Sizeof(dm)) + //dm.DmDriverExtra = 0 + //w32.EnumDisplaySettingsEx(&mi.SzDevice[0], w32.ENUM_CURRENT_SETTINGS, &dm, 0) + // + //// check display settings for rotation + //rotationAngle := dm.DmDi + //if rotationAngle == DMDO_0 { + // printf("Monitor is not rotated\n") + //} else if rotationAngle == DMDO_90 { + // printf("Monitor is rotated 90 degrees\n") + //} else if rotationAngle == DMDO_180 { + // printf("Monitor is rotated 180 degrees\n") + //} else if rotationAngle == DMDO_270 { + // printf("Monitor is rotated 270 degrees\n") + //} else { + // printf("Monitor is rotated at an unknown angle\n") + //} + + return &thisScreen, nil } func (w *windowsWebviewWindow) setFrameless(b bool) { diff --git a/v3/pkg/w32/user32.go b/v3/pkg/w32/user32.go index 9357d2586..caac6fa39 100644 --- a/v3/pkg/w32/user32.go +++ b/v3/pkg/w32/user32.go @@ -1151,6 +1151,14 @@ func GetMonitorInfo(hMonitor HMONITOR, lmpi *MONITORINFO) bool { return ret != 0 } +func GetMonitorInfoEx(hMonitor HMONITOR, lmpi *MONITORINFOEX) bool { + ret, _, _ := procGetMonitorInfo.Call( + uintptr(hMonitor), + uintptr(unsafe.Pointer(lmpi)), + ) + return ret != 0 +} + func EnumDisplayMonitors(hdc HDC, clip *RECT, fnEnum uintptr, dwData unsafe.Pointer) bool { ret, _, _ := procEnumDisplayMonitors.Call( hdc,