scale.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package glfw
  2. import (
  3. "math"
  4. "os"
  5. "strconv"
  6. "fyne.io/fyne/v2"
  7. )
  8. const (
  9. baselineDPI = 120.0
  10. scaleEnvKey = "FYNE_SCALE"
  11. scaleAuto = float32(-1.0) // some platforms allow setting auto-scale (linux/BSD)
  12. )
  13. func calculateDetectedScale(widthMm, widthPx int) float32 {
  14. dpi := float32(widthPx) / (float32(widthMm) / 25.4)
  15. if dpi > 1000 || dpi < 10 {
  16. dpi = baselineDPI
  17. }
  18. scale := float32(float64(dpi) / baselineDPI)
  19. if scale < 1.0 {
  20. return 1.0
  21. }
  22. return scale
  23. }
  24. func calculateScale(user, system, detected float32) float32 {
  25. if user < 0 {
  26. user = 1.0
  27. }
  28. if system == scaleAuto {
  29. system = detected
  30. }
  31. raw := system * user
  32. return float32(math.Round(float64(raw*10.0))) / 10.0
  33. }
  34. func userScale() float32 {
  35. env := os.Getenv(scaleEnvKey)
  36. if env != "" && env != "auto" {
  37. scale, err := strconv.ParseFloat(env, 32)
  38. if err == nil && scale != 0 {
  39. return float32(scale)
  40. }
  41. fyne.LogError("Error reading scale", err)
  42. }
  43. if env != "auto" {
  44. if setting := fyne.CurrentApp().Settings().Scale(); setting > 0 {
  45. return setting
  46. }
  47. }
  48. return 1.0 // user preference for auto is now passed as 1 so the system auto is picked up
  49. }