From 3e4c47eb70e45c02ae96bd204ca07b1eec577f3a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 3 Mar 2024 23:27:02 +0900 Subject: [PATCH] internal/ui: refactoring --- internal/ui/input_glfw.go | 8 +++---- internal/ui/monitor_glfw.go | 3 ++- internal/ui/ui_darwin.go | 4 ++-- internal/ui/ui_glfw.go | 45 +++++++++++++++++++++---------------- internal/ui/ui_linbsd.go | 8 +++---- internal/ui/ui_windows.go | 8 +++---- internal/ui/window_glfw.go | 5 +++-- 7 files changed, 45 insertions(+), 36 deletions(-) diff --git a/internal/ui/input_glfw.go b/internal/ui/input_glfw.go index cff97e880..41ead8e88 100644 --- a/internal/ui/input_glfw.go +++ b/internal/ui/input_glfw.go @@ -96,8 +96,8 @@ func (u *UserInterface) updateInputStateImpl() error { if !math.IsNaN(cx) && !math.IsNaN(cy) { cx2, cy2 := u.context.logicalPositionToClientPosition(cx, cy, s) - cx2 = dipToGLFWPixel(cx2, m) - cy2 = dipToGLFWPixel(cy2, m) + cx2 = dipToGLFWPixel(cx2, s) + cy2 = dipToGLFWPixel(cy2, s) if err := u.window.SetCursorPos(cx2, cy2); err != nil { return err } @@ -106,8 +106,8 @@ func (u *UserInterface) updateInputStateImpl() error { if err != nil { return err } - cx2 = dipFromGLFWPixel(cx2, m) - cy2 = dipFromGLFWPixel(cy2, m) + cx2 = dipFromGLFWPixel(cx2, s) + cy2 = dipFromGLFWPixel(cy2, s) cx, cy = u.context.clientPositionToLogicalPosition(cx2, cy2, s) } diff --git a/internal/ui/monitor_glfw.go b/internal/ui/monitor_glfw.go index fa9496db5..9b207f75e 100644 --- a/internal/ui/monitor_glfw.go +++ b/internal/ui/monitor_glfw.go @@ -52,7 +52,8 @@ func (m *Monitor) DeviceScaleFactor() float64 { func (m *Monitor) sizeInDIP() (float64, float64) { w, h := m.boundsInGLFWPixels.Dx(), m.boundsInGLFWPixels.Dy() - return dipFromGLFWPixel(float64(w), m), dipFromGLFWPixel(float64(h), m) + s := m.DeviceScaleFactor() + return dipFromGLFWPixel(float64(w), s), dipFromGLFWPixel(float64(h), s) } type monitors struct { diff --git a/internal/ui/ui_darwin.go b/internal/ui/ui_darwin.go index cae128b77..7d3a36c09 100644 --- a/internal/ui/ui_darwin.go +++ b/internal/ui/ui_darwin.go @@ -206,7 +206,7 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) { return vm.Width, vm.Height, nil } -func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { +func dipFromGLFWPixel(x float64, scale float64) float64 { // NOTE: On macOS, GLFW exposes the device independent coordinate system. // Thus, the conversion functions are unnecessary, // however we still need the deviceScaleFactor internally @@ -214,7 +214,7 @@ func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { return x } -func dipToGLFWPixel(x float64, monitor *Monitor) float64 { +func dipToGLFWPixel(x float64, scale float64) float64 { return x } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 90e863eb0..1221b4c16 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -321,13 +321,14 @@ func (u *UserInterface) setWindowMonitor(monitor *Monitor) error { } } - w := dipToGLFWPixel(float64(ww), monitor) - h := dipToGLFWPixel(float64(wh), monitor) + s := monitor.DeviceScaleFactor() + w := dipToGLFWPixel(float64(ww), s) + h := dipToGLFWPixel(float64(wh), s) mx := monitor.boundsInGLFWPixels.Min.X my := monitor.boundsInGLFWPixels.Min.Y mw, mh := monitor.sizeInDIP() - mw = dipToGLFWPixel(mw, monitor) - mh = dipToGLFWPixel(mh, monitor) + mw = dipToGLFWPixel(mw, s) + mh = dipToGLFWPixel(mh, s) px, py := InitialWindowPosition(int(mw), int(mh), int(w), int(h)) if err := u.window.SetPos(mx+px, my+py); err != nil { return err @@ -843,8 +844,9 @@ func (u *UserInterface) createWindow() error { monitor := u.getInitMonitor() ww, wh := u.getInitWindowSizeInDIP() - width := int(dipToGLFWPixel(float64(ww), monitor)) - height := int(dipToGLFWPixel(float64(wh), monitor)) + s := monitor.DeviceScaleFactor() + width := int(dipToGLFWPixel(float64(ww), s)) + height := int(dipToGLFWPixel(float64(wh), s)) window, err := glfw.CreateWindow(width, height, "", nil, nil) if err != nil { return err @@ -1240,8 +1242,9 @@ func (u *UserInterface) outsideSize() (float64, float64, error) { if err != nil { return 0, 0, err } - w := dipFromGLFWPixel(float64(ww), m) - h := dipFromGLFWPixel(float64(wh), m) + s := m.DeviceScaleFactor() + w := dipFromGLFWPixel(float64(ww), s) + h := dipFromGLFWPixel(float64(wh), s) return w, h, nil } @@ -1529,30 +1532,31 @@ func (u *UserInterface) updateWindowSizeLimits() error { } minw, minh, maxw, maxh := u.getWindowSizeLimitsInDIP() + s := m.DeviceScaleFactor() if minw < 0 { // Always set the minimum window width. mw, err := u.minimumWindowWidth() if err != nil { return err } - minw = int(dipToGLFWPixel(float64(mw), m)) + minw = int(dipToGLFWPixel(float64(mw), s)) } else { - minw = int(dipToGLFWPixel(float64(minw), m)) + minw = int(dipToGLFWPixel(float64(minw), s)) } if minh < 0 { minh = glfw.DontCare } else { - minh = int(dipToGLFWPixel(float64(minh), m)) + minh = int(dipToGLFWPixel(float64(minh), s)) } if maxw < 0 { maxw = glfw.DontCare } else { - maxw = int(dipToGLFWPixel(float64(maxw), m)) + maxw = int(dipToGLFWPixel(float64(maxw), s)) } if maxh < 0 { maxh = glfw.DontCare } else { - maxh = int(dipToGLFWPixel(float64(maxh), m)) + maxh = int(dipToGLFWPixel(float64(maxh), s)) } if err := u.window.SetSizeLimits(minw, minh, maxw, maxh); err != nil { return err @@ -1640,8 +1644,9 @@ func (u *UserInterface) setWindowSizeInDIP(width, height int, callSetSize bool) if err != nil { return err } - newW := int(dipToGLFWPixel(float64(width), m)) - newH := int(dipToGLFWPixel(float64(height), m)) + s := m.DeviceScaleFactor() + newW := int(dipToGLFWPixel(float64(width), s)) + newH := int(dipToGLFWPixel(float64(height), s)) if oldW != newW || oldH != newH { // Just after SetSize, GetSize is not reliable especially on Linux/UNIX. // Let's wait for FramebufferSize callback in any cases. @@ -1740,8 +1745,9 @@ func (u *UserInterface) setFullscreen(fullscreen bool) error { if err != nil { return err } - ww := int(dipToGLFWPixel(float64(u.origWindowWidthInDIP), m)) - wh := int(dipToGLFWPixel(float64(u.origWindowHeightInDIP), m)) + s := m.DeviceScaleFactor() + ww := int(dipToGLFWPixel(float64(u.origWindowWidthInDIP), s)) + wh := int(dipToGLFWPixel(float64(u.origWindowHeightInDIP), s)) if u.isNativeFullscreenAvailable() { if err := u.setNativeFullscreen(false); err != nil { return err @@ -2061,8 +2067,9 @@ func (u *UserInterface) setWindowPositionInDIP(x, y int, monitor *Monitor) error mx := monitor.boundsInGLFWPixels.Min.X my := monitor.boundsInGLFWPixels.Min.Y - xf := dipToGLFWPixel(float64(x), monitor) - yf := dipToGLFWPixel(float64(y), monitor) + s := monitor.DeviceScaleFactor() + xf := dipToGLFWPixel(float64(x), s) + yf := dipToGLFWPixel(float64(y), s) if x, y := u.adjustWindowPosition(mx+int(xf), my+int(yf), monitor); f { u.setOrigWindowPos(x, y) } else { diff --git a/internal/ui/ui_linbsd.go b/internal/ui/ui_linbsd.go index c94dddac2..50b7f4554 100644 --- a/internal/ui/ui_linbsd.go +++ b/internal/ui/ui_linbsd.go @@ -122,12 +122,12 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) { return physWidth, physHeight, nil } -func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { - return x / monitor.DeviceScaleFactor() +func dipFromGLFWPixel(x float64, deviceScaleFactor float64) float64 { + return x / deviceScaleFactor } -func dipToGLFWPixel(x float64, monitor *Monitor) float64 { - return x * monitor.DeviceScaleFactor() +func dipToGLFWPixel(x float64, deviceScaleFactor float64) float64 { + return x * deviceScaleFactor } func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { diff --git a/internal/ui/ui_windows.go b/internal/ui/ui_windows.go index ffe4830d8..4d5d821ef 100644 --- a/internal/ui/ui_windows.go +++ b/internal/ui/ui_windows.go @@ -101,12 +101,12 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) { return vm.Width, vm.Height, nil } -func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { - return x / monitor.DeviceScaleFactor() +func dipFromGLFWPixel(x float64, deviceScaleFactor float64) float64 { + return x / deviceScaleFactor } -func dipToGLFWPixel(x float64, monitor *Monitor) float64 { - return x * monitor.DeviceScaleFactor() +func dipToGLFWPixel(x float64, deviceScaleFactor float64) float64 { + return x * deviceScaleFactor } func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { diff --git a/internal/ui/window_glfw.go b/internal/ui/window_glfw.go index f3883bc35..4cdc7fec1 100644 --- a/internal/ui/window_glfw.go +++ b/internal/ui/window_glfw.go @@ -322,8 +322,9 @@ func (w *glfwWindow) Position() (int, int) { } wx -= m.boundsInGLFWPixels.Min.X wy -= m.boundsInGLFWPixels.Min.Y - xf := dipFromGLFWPixel(float64(wx), m) - yf := dipFromGLFWPixel(float64(wy), m) + s := m.DeviceScaleFactor() + xf := dipFromGLFWPixel(float64(wx), s) + yf := dipFromGLFWPixel(float64(wy), s) x, y = int(xf), int(yf) }) return x, y