internal/ui: refactoring

This commit is contained in:
Hajime Hoshi 2023-09-28 00:52:12 +09:00
parent a65a45586f
commit 7cb64ccffe
5 changed files with 41 additions and 51 deletions

View File

@ -28,12 +28,13 @@ type Monitor struct {
m *glfw.Monitor
videoMode *glfw.VidMode
id int
name string
x int
y int
contentScale float64
videoModeScale_ float64
id int
name string
x int
y int
widthInDIP float64
heightInDIP float64
contentScale float64
}
// Name returns the monitor's name.
@ -50,13 +51,8 @@ func (m *Monitor) deviceScaleFactor() float64 {
return m.contentScale
}
func (m *Monitor) videoModeScale() float64 {
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
// In this case, return 1 as a tentative scale (#1878).
if m == nil {
return 1
}
return m.videoModeScale_
func (m *Monitor) sizeInDIP() (float64, float64) {
return m.widthInDIP, m.heightInDIP
}
type monitors struct {
@ -142,15 +138,17 @@ func (m *monitors) update() {
break
}
w, h := glfwMonitorSizeInDIP(m, contentScale)
newMonitors = append(newMonitors, &Monitor{
m: m,
videoMode: m.GetVideoMode(),
id: i,
name: m.GetName(),
x: x,
y: y,
contentScale: contentScale,
videoModeScale_: videoModeScale(m),
m: m,
videoMode: m.GetVideoMode(),
id: i,
name: m.GetName(),
x: x,
y: y,
widthInDIP: w,
heightInDIP: h,
contentScale: contentScale,
})
}

View File

@ -279,8 +279,7 @@ func (u *userInterfaceImpl) setWindowMonitor(monitor *Monitor) {
w := dipToGLFWPixel(float64(ww), monitor)
h := dipToGLFWPixel(float64(wh), monitor)
mw := dipFromGLFWMonitorPixel(float64(monitor.videoMode.Width), monitor)
mh := dipFromGLFWMonitorPixel(float64(monitor.videoMode.Height), monitor)
mw, mh := monitor.sizeInDIP()
mw = dipToGLFWPixel(mw, monitor)
mh = dipToGLFWPixel(mh, monitor)
px, py := InitialWindowPosition(int(mw), int(mh), int(w), int(h))
@ -519,8 +518,7 @@ func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
}
if !u.isRunning() {
m := u.getInitMonitor()
w := dipFromGLFWMonitorPixel(float64(m.videoMode.Width), m)
h := dipFromGLFWMonitorPixel(float64(m.videoMode.Height), m)
w, h := m.sizeInDIP()
return int(w), int(h)
}
@ -533,8 +531,9 @@ func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
if m == nil {
return
}
w = int(dipFromGLFWMonitorPixel(float64(m.videoMode.Width), m))
h = int(dipFromGLFWMonitorPixel(float64(m.videoMode.Height), m))
wf, hf := m.sizeInDIP()
w = int(wf)
h = int(hf)
})
return w, h
}
@ -763,12 +762,11 @@ func (u *userInterfaceImpl) createWindow() error {
// The position must be set before the size is set (#1982).
// setWindowSizeInDIP refers the current monitor's device scale.
wx, wy := u.getInitWindowPositionInDIP()
mw := int(dipFromGLFWMonitorPixel(float64(monitor.videoMode.Width), monitor))
mh := int(dipFromGLFWMonitorPixel(float64(monitor.videoMode.Height), monitor))
if max := mw - ww; wx >= max {
mw, mh := monitor.sizeInDIP()
if max := int(mw) - ww; wx >= max {
wx = max
}
if max := mh - wh; wy >= max {
if max := int(mh) - wh; wy >= max {
wy = max
}
if wx < 0 {
@ -1018,10 +1016,7 @@ func (u *userInterfaceImpl) outsideSize() (float64, float64) {
// reflecting the adjustment of the view size (#1745).
var w, h float64
if m := u.currentMonitor(); m != nil {
vm := m.videoMode
ww, wh := vm.Width, vm.Height
w = dipFromGLFWMonitorPixel(float64(ww), m)
h = dipFromGLFWMonitorPixel(float64(wh), m)
w, h = m.sizeInDIP()
}
return w, h
}

View File

@ -184,13 +184,10 @@ func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
return metal.NewGraphics()
}
// videoModeScale must be called from the main thread.
func videoModeScale(monitor *glfw.Monitor) float64 {
return 1
}
func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x
// glfwMonitorSizeInDIP must be called from the main thread.
func glfwMonitorSizeInDIP(monitor *glfw.Monitor, contentScale float64) (float64, float64) {
vm := monitor.GetVideoMode()
return float64(vm.Width), float64(vm.Height)
}
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {

View File

@ -110,8 +110,11 @@ func videoModeScale(m *glfw.Monitor) float64 {
return 1
}
func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x / (monitor.videoModeScale() * monitor.deviceScaleFactor())
// glfwMonitorSizeInDIP must be called from the main thread.
func glfwMonitorSizeInDIP(monitor *glfw.Monitor, contentScale float64) (float64, float64) {
vm := monitor.GetVideoMode()
vs := videoModeScale(monitor)
return float64(vm.Width) / (vs * contentScale), float64(vm.Height) / (vs * contentScale)
}
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {

View File

@ -86,13 +86,10 @@ func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
return nil, nil
}
// videoModeScale must be called from the main thread.
func videoModeScale(monitor *glfw.Monitor) float64 {
return 1
}
func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
return x / monitor.deviceScaleFactor()
// glfwMonitorSizeInDIP must be called from the main thread.
func glfwMonitorSizeInDIP(monitor *glfw.Monitor, contentScale float64) (float64, float64) {
vm := monitor.GetVideoMode()
return float64(vm.Width) / contentScale, float64(vm.Height) / contentScale
}
func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {