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) {
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)
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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) {

View File

@ -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