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

View File

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

View File

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

View File

@ -110,8 +110,11 @@ func videoModeScale(m *glfw.Monitor) float64 {
return 1 return 1
} }
func dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 { // glfwMonitorSizeInDIP must be called from the main thread.
return x / (monitor.videoModeScale() * monitor.deviceScaleFactor()) 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 { func dipFromGLFWPixel(x float64, monitor *Monitor) float64 {

View File

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