ui: Calculate DPI and set the actual scale based on DPI

This commit is contained in:
Hajime Hoshi 2016-02-26 03:09:23 +09:00
parent 3d79e086a9
commit 4d53cf20da
3 changed files with 23 additions and 90 deletions

View File

@ -1,23 +0,0 @@
// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !linux
package ui
func adjustScaleForGLFW(scale int) int {
// On Windows and OSX, GLFW already considers High-DPI scaling
// and scale doesn't have to be adjusted.
return scale
}

View File

@ -1,49 +0,0 @@
// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
import (
"fmt"
"os/exec"
"regexp"
"strconv"
)
var scalingFactorSyntax = regexp.MustCompile(`\Auint32\s+(\d+)\s*\z`)
var deviceScaleFactor = 0
// adjustScaleForGLFW adjusts the given scale which is passed to the GLFW API.
func adjustScaleForGLFW(scale int) int {
if 0 < deviceScaleFactor {
return scale * deviceScaleFactor
}
// Execute gsettings command instead of calling gtk functions so as not to depend on
// gobject-2.0 library.
c := exec.Command("gsettings", "get", "org.gnome.desktop.interface", "scaling-factor")
o, err := c.Output()
if err != nil {
panic(fmt.Sprintf("ui: executing gsettings error %v", err))
}
m := scalingFactorSyntax.FindStringSubmatch(string(o))
if m == nil {
panic("ui: gsettings result syntax is not expected")
}
s, err := strconv.Atoi(m[1])
if err != nil {
panic(fmt.Sprintf("ui: %v", err))
}
deviceScaleFactor = s
return scale * deviceScaleFactor
}

View File

@ -90,12 +90,12 @@ func SwapBuffers() {
func SetScreenSize(width, height int) (bool, int) { func SetScreenSize(width, height int) (bool, int) {
result := currentUI.setScreenSize(width, height, currentUI.scale) result := currentUI.setScreenSize(width, height, currentUI.scale)
return result, currentUI.actualScale return result, currentUI.actualScale()
} }
func SetScreenScale(scale int) (bool, int) { func SetScreenScale(scale int) (bool, int) {
result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale) result := currentUI.setScreenSize(currentUI.width, currentUI.height, scale)
return result, currentUI.actualScale return result, currentUI.actualScale()
} }
type userInterface struct { type userInterface struct {
@ -103,21 +103,30 @@ type userInterface struct {
width int width int
height int height int
scale int scale int
actualScale int deviceScaleFactor float64
context *opengl.Context context *opengl.Context
} }
func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) { func (u *userInterface) start(width, height, scale int, title string) (actualScale int, err error) {
m := glfw.GetPrimaryMonitor()
mw, _ := m.GetPhysicalSize()
v := m.GetVideoMode()
dpi := float64(v.Width) * 25.4 / float64(mw)
u.deviceScaleFactor = dpi / 96
u.setScreenSize(width, height, scale) u.setScreenSize(width, height, scale)
u.window.SetTitle(title) u.window.SetTitle(title)
u.window.Show() u.window.Show()
videoMode := glfw.GetPrimaryMonitor().GetVideoMode() x := (v.Width - width*u.actualScale()) / 2
x := (videoMode.Width - width*adjustScaleForGLFW(scale)) / 2 y := (v.Height - height*u.actualScale()) / 3
y := (videoMode.Height - height*adjustScaleForGLFW(scale)) / 3
u.window.SetPos(x, y) u.window.SetPos(x, y)
return u.actualScale, nil return u.actualScale(), nil
}
func (u *userInterface) actualScale() int {
return int(float64(u.scale) * u.deviceScaleFactor)
} }
func (u *userInterface) pollEvents() error { func (u *userInterface) pollEvents() error {
@ -159,6 +168,7 @@ func (u *userInterface) setScreenSize(width, height, scale int) bool {
if u.width == width && u.height == height && u.scale == scale { if u.width == width && u.height == height && u.scale == scale {
return false return false
} }
u.scale = scale
// To make sure the current existing framebuffers are rendered, // To make sure the current existing framebuffers are rendered,
// swap buffers here before SetSize is called. // swap buffers here before SetSize is called.
@ -171,7 +181,7 @@ func (u *userInterface) setScreenSize(width, height, scale int) bool {
window.SetFramebufferSizeCallback(nil) window.SetFramebufferSizeCallback(nil)
close(ch) close(ch)
}) })
window.SetSize(width*adjustScaleForGLFW(scale), height*adjustScaleForGLFW(scale)) window.SetSize(width*u.actualScale(), height*u.actualScale())
event: event:
for { for {
@ -184,10 +194,5 @@ event:
} }
u.width = width u.width = width
u.height = height u.height = height
u.scale = scale
// For retina displays, recalculate the scale with the framebuffer size.
windowWidth, _ := window.GetFramebufferSize()
u.actualScale = windowWidth / width
return true return true
} }