internal/ui: refactoring

Updates #2781
This commit is contained in:
Hajime Hoshi 2023-09-30 00:11:06 +09:00
parent 03d6811a65
commit 2fbef2106d

View File

@ -18,7 +18,6 @@ package ui
import ( import (
"fmt" "fmt"
"math"
"runtime" "runtime"
"github.com/jezek/xgb" "github.com/jezek/xgb"
@ -51,8 +50,11 @@ func (*graphicsDriverCreatorImpl) newMetal() (graphicsdriver.Graphics, error) {
return nil, nil return nil, nil
} }
// videoModeScale must be called from the main thread. // glfwMonitorSizeInGLFWPixels must be called from the main thread.
func videoModeScale(m *glfw.Monitor) float64 { func glfwMonitorSizeInGLFWPixels(m *glfw.Monitor) (int, int) {
vm := m.GetVideoMode()
physWidth, physHeight := vm.Width, vm.Height
// TODO: if glfw/glfw#1961 gets fixed, this function may need revising. // TODO: if glfw/glfw#1961 gets fixed, this function may need revising.
// In case GLFW decides to switch to returning logical pixels, we can just return 1. // In case GLFW decides to switch to returning logical pixels, we can just return 1.
@ -66,20 +68,20 @@ func videoModeScale(m *glfw.Monitor) float64 {
// No X11 connection? // No X11 connection?
// Assume we're on pure Wayland then. // Assume we're on pure Wayland then.
// GLFW/Wayland shouldn't be having this issue. // GLFW/Wayland shouldn't be having this issue.
return 1 return physWidth, physHeight
} }
defer xconn.Close() defer xconn.Close()
if err := randr.Init(xconn); err != nil { if err := randr.Init(xconn); err != nil {
// No RANDR extension? No problem. // No RANDR extension? No problem.
return 1 return physWidth, physHeight
} }
root := xproto.Setup(xconn).DefaultScreen(xconn).Root root := xproto.Setup(xconn).DefaultScreen(xconn).Root
res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply() res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply()
if err != nil { if err != nil {
// Likely means RANDR is not working. No problem. // Likely means RANDR is not working. No problem.
return 1 return physWidth, physHeight
} }
monitorX, monitorY := m.GetPos() monitorX, monitorY := m.GetPos()
@ -96,25 +98,18 @@ func videoModeScale(m *glfw.Monitor) float64 {
continue continue
} }
if int(info.X) == monitorX && int(info.Y) == monitorY { if int(info.X) == monitorX && int(info.Y) == monitorY {
xWidth, xHeight := info.Width, info.Height return int(info.Width), int(info.Height)
vm := m.GetVideoMode()
physWidth, physHeight := vm.Width, vm.Height
// Return one scale, even though there may be separate X and Y scales.
// Return the _larger_ scale, as this would yield a letterboxed display on mismatch, rather than a cut-off one.
scale := math.Max(float64(physWidth)/float64(xWidth), float64(physHeight)/float64(xHeight))
return scale
} }
} }
// Monitor not known to XRandR. Weird. // Monitor not known to XRandR. Weird.
return 1 return physWidth, physHeight
} }
// glfwMonitorSizeInDIP must be called from the main thread. // glfwMonitorSizeInDIP must be called from the main thread.
func glfwMonitorSizeInDIP(monitor *glfw.Monitor, contentScale float64) (float64, float64) { func glfwMonitorSizeInDIP(monitor *glfw.Monitor, contentScale float64) (float64, float64) {
vm := monitor.GetVideoMode() w, h := glfwMonitorSizeInGLFWPixels(monitor)
vs := videoModeScale(monitor) return float64(w) / contentScale, float64(h) / contentScale
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 {