mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
internal/ui: use the cursor position to choose the initial monitor for Windows
Updates #1918 Updates #1982
This commit is contained in:
parent
cafb71719c
commit
ed04021151
@ -170,8 +170,12 @@ func initialize() error {
|
||||
glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI)
|
||||
|
||||
var m *glfw.Monitor
|
||||
if runtime.GOOS == "darwin" {
|
||||
m = initialMonitorByOS()
|
||||
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
|
||||
var err error
|
||||
m, err = initialMonitorByOS()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m == nil {
|
||||
m = glfw.GetPrimaryMonitor()
|
||||
}
|
||||
@ -189,7 +193,10 @@ func initialize() error {
|
||||
defer w.Destroy()
|
||||
initializeWindowAfterCreation(w)
|
||||
theUI.waitForFramebufferSizeCallback(w, nil)
|
||||
m = initialMonitorByOS()
|
||||
m, err = initialMonitorByOS()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if m == nil {
|
||||
m = currentMonitorImpl(w)
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
||||
return x, y
|
||||
}
|
||||
|
||||
func initialMonitorByOS() *glfw.Monitor {
|
||||
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||
var cx, cy C.int
|
||||
C.currentMouseLocation(&cx, &cy)
|
||||
x, y := int(cx), int(cy)
|
||||
@ -176,11 +176,11 @@ func initialMonitorByOS() *glfw.Monitor {
|
||||
for _, m := range ensureMonitors() {
|
||||
w, h := m.vm.Width, m.vm.Height
|
||||
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
||||
return m.m
|
||||
return m.m, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
||||
|
@ -133,8 +133,8 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
||||
return x, y
|
||||
}
|
||||
|
||||
func initialMonitorByOS() *glfw.Monitor {
|
||||
return nil
|
||||
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor {
|
||||
|
@ -46,12 +46,17 @@ type monitorInfo struct {
|
||||
dwFlags uint32
|
||||
}
|
||||
|
||||
type point struct {
|
||||
x int32
|
||||
y int32
|
||||
}
|
||||
|
||||
var (
|
||||
// user32 is defined at hideconsole_windows.go
|
||||
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
|
||||
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
|
||||
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
|
||||
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
|
||||
procGetCursorPos = user32.NewProc("GetCursorPos")
|
||||
)
|
||||
|
||||
func getSystemMetrics(nIndex int) (int32, error) {
|
||||
@ -64,11 +69,6 @@ func getSystemMetrics(nIndex int) (int32, error) {
|
||||
return int32(r), nil
|
||||
}
|
||||
|
||||
func getForegroundWindow() windows.HWND {
|
||||
r, _, _ := procGetForegroundWindow.Call()
|
||||
return windows.HWND(r)
|
||||
}
|
||||
|
||||
func monitorFromWindow(hwnd windows.HWND, dwFlags uint32) uintptr {
|
||||
r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags))
|
||||
return r
|
||||
@ -85,6 +85,18 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getCursorPos() (int32, int32, error) {
|
||||
var pt point
|
||||
r, _, e := procGetCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
|
||||
if r == 0 {
|
||||
if e != nil && e != windows.ERROR_SUCCESS {
|
||||
return 0, 0, fmt.Errorf("ui: GetCursorPos failed: error code: %w", e)
|
||||
}
|
||||
return 0, 0, fmt.Errorf("ui: GetCursorPos failed: returned 0")
|
||||
}
|
||||
return pt.x, pt.y, nil
|
||||
}
|
||||
|
||||
// clearVideoModeScaleCache must be called from the main thread.
|
||||
func clearVideoModeScaleCache() {}
|
||||
|
||||
@ -120,14 +132,22 @@ func (u *UserInterface) adjustWindowPosition(x, y int) (int, int) {
|
||||
return x, y
|
||||
}
|
||||
|
||||
func initialMonitorByOS() *glfw.Monitor {
|
||||
// Get the foreground window, that is common among multiple processes.
|
||||
w := getForegroundWindow()
|
||||
if w == 0 {
|
||||
// GetForegroundWindow can return null according to the document.
|
||||
return nil
|
||||
func initialMonitorByOS() (*glfw.Monitor, error) {
|
||||
px, py, err := getCursorPos()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return monitorFromWin32Window(w)
|
||||
x, y := int(px), int(py)
|
||||
|
||||
// Find the monitor including the cursor.
|
||||
for _, m := range ensureMonitors() {
|
||||
w, h := m.vm.Width, m.vm.Height
|
||||
if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h {
|
||||
return m.m, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
|
||||
|
Loading…
Reference in New Issue
Block a user