Files
Reims-Go/internal/host/display.go
T

177 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package host
import (
"fmt"
"math"
)
// DisplayInfo describes a connected physical monitor on the host.
type DisplayInfo struct {
Name string `json:"name,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
ScalePct int `json:"scale_pct"` // Windows DPI scaling percentage, e.g. 100, 125, 150, 200
RefreshHz int `json:"refresh_hz,omitempty"`
Primary bool `json:"primary"`
}
// DisplayPreset is a suggested macOS VM resolution option tailored to the host monitor.
type DisplayPreset struct {
Resolution string `json:"resolution"`
Label string `json:"label"`
HiDPI bool `json:"hidpi"`
Recommended bool `json:"recommended,omitempty"`
}
// PrimaryDisplayOf picks the primary monitor (or the first detected monitor).
func PrimaryDisplayOf(displays []DisplayInfo) *DisplayInfo {
for i := range displays {
if displays[i].Primary {
cp := displays[i]
return &cp
}
}
if len(displays) > 0 {
cp := displays[0]
return &cp
}
return nil
}
// RecommendDisplayPresets computes crisp 1x / 2x HiDPI resolution presets
// based on the host primary monitor's physical pixel dimensions and Windows DPI scaling.
func RecommendDisplayPresets(primary *DisplayInfo) ([]DisplayPreset, string) {
if primary == nil || primary.Width <= 0 || primary.Height <= 0 {
presets := []DisplayPreset{
{Resolution: "1920x1080", Label: "1920 × 1080", Recommended: true},
{Resolution: "2560x1440", Label: "2560 × 1440"},
{Resolution: "2880x1800", Label: "2880 × 1800", HiDPI: true},
{Resolution: "3840x2160", Label: "3840 × 2160", HiDPI: true},
}
return presets, "1920x1080"
}
w, h := primary.Width, primary.Height
scale := primary.ScalePct
if scale <= 0 {
scale = 100
}
aspect := float64(w) / float64(h)
is16by10 := math.Abs(aspect-1.6) < 0.08
type cand struct {
res string
label string
hidpi bool
}
var list []cand
var rec string
add := func(res string, hidpi bool) {
for _, existing := range list {
if existing.res == res {
return
}
}
var cw, ch int
_, _ = fmt.Sscanf(res, "%dx%d", &cw, &ch)
lbl := res
if cw > 0 && ch > 0 {
lbl = fmt.Sprintf("%d × %d", cw, ch)
}
list = append(list, cand{res: res, label: lbl, hidpi: hidpi})
}
switch {
case w >= 3440:
// 4K / ultrawide 4K host: 3840x2160 gives razor-sharp 2x HiDPI (1920x1080 logical).
rec = "3840x2160"
add("3840x2160", true)
add("2880x1800", true)
add("2560x1440", scale >= 150)
add("1920x1080", false)
case w >= 2880:
// 3K / 2880x1800 laptop or 3200x1800 display.
if is16by10 {
rec = "2880x1800"
add("2880x1800", true)
add("2560x1600", scale >= 150)
add("2560x1440", false)
add("1920x1080", false)
} else {
rec = "2560x1440"
add("2560x1440", scale >= 150)
add("2880x1800", true)
add("3840x2160", true)
add("1920x1080", false)
}
case w >= 2560:
// 2K / QHD (2560x1440 or 2560x1600): match aspect ratio cleanly.
if is16by10 {
rec = "2560x1600"
add("2560x1600", scale >= 150)
add("2880x1800", true)
add("2560x1440", false)
add("1920x1080", false)
} else {
rec = "2560x1440"
add("2560x1440", scale >= 150)
add("2880x1800", true)
add("1920x1080", false)
add("3840x2160", true)
}
case w >= 1920:
// 1080p / 1920x1200 display.
if is16by10 {
rec = "1920x1200"
add("1920x1200", false)
add("1920x1080", false)
add("2560x1600", true)
add("2880x1800", true)
} else {
rec = "1920x1080"
add("1920x1080", false)
add("2560x1440", true)
add("2880x1800", true)
add("1600x900", false)
}
default:
// Smaller laptops (e.g., 1366x768 or 1600x900).
rec = "1600x900"
if w >= 1280 && h >= 720 {
native := fmt.Sprintf("%dx%d", w, h)
rec = native
add(native, false)
}
add("1600x900", false)
add("1920x1080", false)
}
// If host native resolution is standard even dimensions and not yet listed, offer it too.
if w >= 1280 && h >= 720 && w%2 == 0 && h%2 == 0 {
native := fmt.Sprintf("%dx%d", w, h)
hasNative := false
for _, c := range list {
if c.res == native {
hasNative = true
break
}
}
if !hasNative && len(list) < 5 {
list = append(list, cand{res: native, label: fmt.Sprintf("%d × %d", w, h), hidpi: scale >= 150})
}
}
out := make([]DisplayPreset, 0, len(list))
for _, c := range list {
out = append(out, DisplayPreset{
Resolution: c.res,
Label: c.label,
HiDPI: c.hidpi,
Recommended: c.res == rec,
})
}
return out, rec
}