internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2024-03-03 23:27:02 +09:00
parent 4d72f97e45
commit 3e4c47eb70
7 changed files with 45 additions and 36 deletions

View File

@ -96,8 +96,8 @@ func (u *UserInterface) updateInputStateImpl() error {
if !math.IsNaN(cx) && !math.IsNaN(cy) { if !math.IsNaN(cx) && !math.IsNaN(cy) {
cx2, cy2 := u.context.logicalPositionToClientPosition(cx, cy, s) cx2, cy2 := u.context.logicalPositionToClientPosition(cx, cy, s)
cx2 = dipToGLFWPixel(cx2, m) cx2 = dipToGLFWPixel(cx2, s)
cy2 = dipToGLFWPixel(cy2, m) cy2 = dipToGLFWPixel(cy2, s)
if err := u.window.SetCursorPos(cx2, cy2); err != nil { if err := u.window.SetCursorPos(cx2, cy2); err != nil {
return err return err
} }
@ -106,8 +106,8 @@ func (u *UserInterface) updateInputStateImpl() error {
if err != nil { if err != nil {
return err return err
} }
cx2 = dipFromGLFWPixel(cx2, m) cx2 = dipFromGLFWPixel(cx2, s)
cy2 = dipFromGLFWPixel(cy2, m) cy2 = dipFromGLFWPixel(cy2, s)
cx, cy = u.context.clientPositionToLogicalPosition(cx2, cy2, s) cx, cy = u.context.clientPositionToLogicalPosition(cx2, cy2, s)
} }

View File

@ -52,7 +52,8 @@ func (m *Monitor) DeviceScaleFactor() float64 {
func (m *Monitor) sizeInDIP() (float64, float64) { func (m *Monitor) sizeInDIP() (float64, float64) {
w, h := m.boundsInGLFWPixels.Dx(), m.boundsInGLFWPixels.Dy() 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 { type monitors struct {

View File

@ -206,7 +206,7 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) {
return vm.Width, vm.Height, nil 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. // NOTE: On macOS, GLFW exposes the device independent coordinate system.
// Thus, the conversion functions are unnecessary, // Thus, the conversion functions are unnecessary,
// however we still need the deviceScaleFactor internally // however we still need the deviceScaleFactor internally
@ -214,7 +214,7 @@ func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
return x return x
} }
func dipToGLFWPixel(x float64, monitor *Monitor) float64 { func dipToGLFWPixel(x float64, scale float64) float64 {
return x return x
} }

View File

@ -321,13 +321,14 @@ func (u *UserInterface) setWindowMonitor(monitor *Monitor) error {
} }
} }
w := dipToGLFWPixel(float64(ww), monitor) s := monitor.DeviceScaleFactor()
h := dipToGLFWPixel(float64(wh), monitor) w := dipToGLFWPixel(float64(ww), s)
h := dipToGLFWPixel(float64(wh), s)
mx := monitor.boundsInGLFWPixels.Min.X mx := monitor.boundsInGLFWPixels.Min.X
my := monitor.boundsInGLFWPixels.Min.Y my := monitor.boundsInGLFWPixels.Min.Y
mw, mh := monitor.sizeInDIP() mw, mh := monitor.sizeInDIP()
mw = dipToGLFWPixel(mw, monitor) mw = dipToGLFWPixel(mw, s)
mh = dipToGLFWPixel(mh, monitor) mh = dipToGLFWPixel(mh, s)
px, py := InitialWindowPosition(int(mw), int(mh), int(w), int(h)) px, py := InitialWindowPosition(int(mw), int(mh), int(w), int(h))
if err := u.window.SetPos(mx+px, my+py); err != nil { if err := u.window.SetPos(mx+px, my+py); err != nil {
return err return err
@ -843,8 +844,9 @@ func (u *UserInterface) createWindow() error {
monitor := u.getInitMonitor() monitor := u.getInitMonitor()
ww, wh := u.getInitWindowSizeInDIP() ww, wh := u.getInitWindowSizeInDIP()
width := int(dipToGLFWPixel(float64(ww), monitor)) s := monitor.DeviceScaleFactor()
height := int(dipToGLFWPixel(float64(wh), monitor)) width := int(dipToGLFWPixel(float64(ww), s))
height := int(dipToGLFWPixel(float64(wh), s))
window, err := glfw.CreateWindow(width, height, "", nil, nil) window, err := glfw.CreateWindow(width, height, "", nil, nil)
if err != nil { if err != nil {
return err return err
@ -1240,8 +1242,9 @@ func (u *UserInterface) outsideSize() (float64, float64, error) {
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
w := dipFromGLFWPixel(float64(ww), m) s := m.DeviceScaleFactor()
h := dipFromGLFWPixel(float64(wh), m) w := dipFromGLFWPixel(float64(ww), s)
h := dipFromGLFWPixel(float64(wh), s)
return w, h, nil return w, h, nil
} }
@ -1529,30 +1532,31 @@ func (u *UserInterface) updateWindowSizeLimits() error {
} }
minw, minh, maxw, maxh := u.getWindowSizeLimitsInDIP() minw, minh, maxw, maxh := u.getWindowSizeLimitsInDIP()
s := m.DeviceScaleFactor()
if minw < 0 { if minw < 0 {
// Always set the minimum window width. // Always set the minimum window width.
mw, err := u.minimumWindowWidth() mw, err := u.minimumWindowWidth()
if err != nil { if err != nil {
return err return err
} }
minw = int(dipToGLFWPixel(float64(mw), m)) minw = int(dipToGLFWPixel(float64(mw), s))
} else { } else {
minw = int(dipToGLFWPixel(float64(minw), m)) minw = int(dipToGLFWPixel(float64(minw), s))
} }
if minh < 0 { if minh < 0 {
minh = glfw.DontCare minh = glfw.DontCare
} else { } else {
minh = int(dipToGLFWPixel(float64(minh), m)) minh = int(dipToGLFWPixel(float64(minh), s))
} }
if maxw < 0 { if maxw < 0 {
maxw = glfw.DontCare maxw = glfw.DontCare
} else { } else {
maxw = int(dipToGLFWPixel(float64(maxw), m)) maxw = int(dipToGLFWPixel(float64(maxw), s))
} }
if maxh < 0 { if maxh < 0 {
maxh = glfw.DontCare maxh = glfw.DontCare
} else { } else {
maxh = int(dipToGLFWPixel(float64(maxh), m)) maxh = int(dipToGLFWPixel(float64(maxh), s))
} }
if err := u.window.SetSizeLimits(minw, minh, maxw, maxh); err != nil { if err := u.window.SetSizeLimits(minw, minh, maxw, maxh); err != nil {
return err return err
@ -1640,8 +1644,9 @@ func (u *UserInterface) setWindowSizeInDIP(width, height int, callSetSize bool)
if err != nil { if err != nil {
return err return err
} }
newW := int(dipToGLFWPixel(float64(width), m)) s := m.DeviceScaleFactor()
newH := int(dipToGLFWPixel(float64(height), m)) newW := int(dipToGLFWPixel(float64(width), s))
newH := int(dipToGLFWPixel(float64(height), s))
if oldW != newW || oldH != newH { if oldW != newW || oldH != newH {
// Just after SetSize, GetSize is not reliable especially on Linux/UNIX. // Just after SetSize, GetSize is not reliable especially on Linux/UNIX.
// Let's wait for FramebufferSize callback in any cases. // Let's wait for FramebufferSize callback in any cases.
@ -1740,8 +1745,9 @@ func (u *UserInterface) setFullscreen(fullscreen bool) error {
if err != nil { if err != nil {
return err return err
} }
ww := int(dipToGLFWPixel(float64(u.origWindowWidthInDIP), m)) s := m.DeviceScaleFactor()
wh := int(dipToGLFWPixel(float64(u.origWindowHeightInDIP), m)) ww := int(dipToGLFWPixel(float64(u.origWindowWidthInDIP), s))
wh := int(dipToGLFWPixel(float64(u.origWindowHeightInDIP), s))
if u.isNativeFullscreenAvailable() { if u.isNativeFullscreenAvailable() {
if err := u.setNativeFullscreen(false); err != nil { if err := u.setNativeFullscreen(false); err != nil {
return err return err
@ -2061,8 +2067,9 @@ func (u *UserInterface) setWindowPositionInDIP(x, y int, monitor *Monitor) error
mx := monitor.boundsInGLFWPixels.Min.X mx := monitor.boundsInGLFWPixels.Min.X
my := monitor.boundsInGLFWPixels.Min.Y my := monitor.boundsInGLFWPixels.Min.Y
xf := dipToGLFWPixel(float64(x), monitor) s := monitor.DeviceScaleFactor()
yf := dipToGLFWPixel(float64(y), monitor) xf := dipToGLFWPixel(float64(x), s)
yf := dipToGLFWPixel(float64(y), s)
if x, y := u.adjustWindowPosition(mx+int(xf), my+int(yf), monitor); f { if x, y := u.adjustWindowPosition(mx+int(xf), my+int(yf), monitor); f {
u.setOrigWindowPos(x, y) u.setOrigWindowPos(x, y)
} else { } else {

View File

@ -122,12 +122,12 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) {
return physWidth, physHeight, nil return physWidth, physHeight, nil
} }
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { func dipFromGLFWPixel(x float64, deviceScaleFactor float64) float64 {
return x / monitor.DeviceScaleFactor() return x / deviceScaleFactor
} }
func dipToGLFWPixel(x float64, monitor *Monitor) float64 { func dipToGLFWPixel(x float64, deviceScaleFactor float64) float64 {
return x * monitor.DeviceScaleFactor() return x * deviceScaleFactor
} }
func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {

View File

@ -101,12 +101,12 @@ func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int, error) {
return vm.Width, vm.Height, nil return vm.Width, vm.Height, nil
} }
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 { func dipFromGLFWPixel(x float64, deviceScaleFactor float64) float64 {
return x / monitor.DeviceScaleFactor() return x / deviceScaleFactor
} }
func dipToGLFWPixel(x float64, monitor *Monitor) float64 { func dipToGLFWPixel(x float64, deviceScaleFactor float64) float64 {
return x * monitor.DeviceScaleFactor() return x * deviceScaleFactor
} }
func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) { func (u *UserInterface) adjustWindowPosition(x, y int, monitor *Monitor) (int, int) {

View File

@ -322,8 +322,9 @@ func (w *glfwWindow) Position() (int, int) {
} }
wx -= m.boundsInGLFWPixels.Min.X wx -= m.boundsInGLFWPixels.Min.X
wy -= m.boundsInGLFWPixels.Min.Y wy -= m.boundsInGLFWPixels.Min.Y
xf := dipFromGLFWPixel(float64(wx), m) s := m.DeviceScaleFactor()
yf := dipFromGLFWPixel(float64(wy), m) xf := dipFromGLFWPixel(float64(wx), s)
yf := dipFromGLFWPixel(float64(wy), s)
x, y = int(xf), int(yf) x, y = int(xf), int(yf)
}) })
return x, y return x, y