diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 95a2c5b56..cd87b5ade 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -29,15 +29,13 @@ import ( ) type userInterface struct { - window *glfw.Window - width int - height int - scale float64 - deviceScale float64 - framebufferScale float64 - context *opengl.Context - funcs chan func() - sizeChanged bool + window *glfw.Window + width int + height int + scale float64 + context *opengl.Context + funcs chan func() + sizeChanged bool } var currentUI *userInterface @@ -143,8 +141,6 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er u.runOnMainThread(func() { m := glfw.GetPrimaryMonitor() v := m.GetVideoMode() - u.deviceScale = deviceScale() - u.framebufferScale = 1 if !u.setScreenSize(width, height, scale) { err = errors.New("ui: Fail to set the screen size") return @@ -152,24 +148,25 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er u.window.SetTitle(title) u.window.Show() - x := (v.Width - int(float64(width)*u.windowScale())) / 2 - y := (v.Height - int(float64(height)*u.windowScale())) / 3 + w, h := u.glfwSize() + x := (v.Width - w) / 2 + y := (v.Height - h) / 3 u.window.SetPos(x, y) }) return err } -func (u *userInterface) windowScale() float64 { - return u.scale * u.deviceScale +func (u *userInterface) glfwSize() (int, int) { + return int(float64(u.width) * u.scale * glfwScale()), int(float64(u.height) * u.scale * glfwScale()) } func (u *userInterface) actualScreenScale() float64 { - return u.windowScale() * u.framebufferScale + return u.scale * deviceScale() } func (u *userInterface) pollEvents() error { glfw.PollEvents() - return currentInput.update(u.window, u.windowScale()) + return currentInput.update(u.window, u.scale*glfwScale()) } func (u *userInterface) Update() (interface{}, error) { @@ -261,7 +258,6 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool { return false } - // u.scale should be set first since this affects windowScale(). origScale := u.scale u.scale = scale @@ -286,7 +282,8 @@ func (u *userInterface) setScreenSize(width, height int, scale float64) bool { window.SetFramebufferSizeCallback(nil) close(ch) }) - window.SetSize(int(float64(width)*u.windowScale()), int(float64(height)*u.windowScale())) + w, h := u.glfwSize() + window.SetSize(w, h) event: for { @@ -297,9 +294,6 @@ event: default: } } - // This is usually 1, but sometimes more than 1 (e.g. Retina Mac) - fw, _ := window.GetFramebufferSize() - u.framebufferScale = float64(fw) / float64(width) / u.windowScale() u.sizeChanged = true return true } diff --git a/internal/ui/ui_linux.go b/internal/ui/ui_linux.go index ab602eb32..036b10485 100644 --- a/internal/ui/ui_linux.go +++ b/internal/ui/ui_linux.go @@ -21,3 +21,7 @@ func deviceScale() float64 { // TODO: Implement this return 1 } + +func glfwScale() float64 { + return deviceScale() +} diff --git a/internal/ui/ui_mac.go b/internal/ui/ui_mac.go index 7079e70f8..10b4862ae 100644 --- a/internal/ui/ui_mac.go +++ b/internal/ui/ui_mac.go @@ -18,8 +18,21 @@ package ui +// #cgo CFLAGS: -x objective-c +// #cgo LDFLAGS: -framework AppKit +// +// #import +// +// static float scale() { +// NSScreen* primary = [[NSScreen screens] firstObject]; +// return [primary backingScaleFactor]; +// } +import "C" + func deviceScale() float64 { - // The device scale is always 1 on Mac OS. - // The frame buffer size is different from the window size. + return float64(C.scale()) +} + +func glfwScale() float64 { return 1 } diff --git a/internal/ui/ui_windows.go b/internal/ui/ui_windows.go index 86b540d92..9e0940c56 100644 --- a/internal/ui/ui_windows.go +++ b/internal/ui/ui_windows.go @@ -30,3 +30,7 @@ func deviceScale() float64 { dpi := int(C.getDPI()) return float64(dpi) / 96 } + +func glfwScale() float64 { + return deviceScale() +}