Skip to content

Commit 2ab53c1

Browse files
committed
Calculate pixel_ratio based on users' display
1 parent 3c03ddd commit 2ab53c1

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

example/simpleDemo/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func main() {
3636
gutter.ApplicationICUDataPath(dir + "/icudtl.dat"),
3737
gutter.ApplicationWindowDimension(initialApplicationWidth, initialApplicationHeight),
3838
gutter.OptionWindowInitializer(setIcon),
39-
gutter.OptionPixelRatio(1.2),
4039
gutter.OptionVMArguments([]string{"--dart-non-checked-mode", "--observatory-port=50300"}),
4140
gutter.OptionAddPluginReceiver(ownPlugin, "plugin_demo"),
4241
// Default keyboard is Qwerty, if you want to change it, you can check keyboard.go in gutter package.

flutter/flutter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ type EngineOpenGL struct {
5959
FPlatfromMessage func(message *PlatformMessage, window unsafe.Pointer) bool
6060

6161
// Engine arguments
62-
PixelRatio float64
6362
AssetsPath string
6463
IcuDataPath string
6564
}

gutter.go

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gutter
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
67
"time"
78
"unsafe"
@@ -10,6 +11,9 @@ import (
1011
"github.com/go-gl/glfw/v3.2/glfw"
1112
)
1213

14+
// dpPerInch defines the amount of display pixels per inch as defined for Flutter.
15+
const dpPerInch = 160.0
16+
1317
// Run executes a flutter application with the provided options.
1418
// given limitations this method must be called by the main function directly.
1519
func Run(options ...Option) (err error) {
@@ -164,17 +168,31 @@ func glfwKey(keyboardLayout KeyboardShortcuts) func(w *glfw.Window, key glfw.Key
164168
}
165169
}
166170

167-
func glfwWindowSizeCallback(window *glfw.Window, width int, height int) {
168-
169-
index := *(*int)(window.GetUserPointer())
170-
flutterOGL := flutter.SelectEngine(index)
171+
func newGLFWFramebufferSizeCallback(pixelRatio float64, monitorScreenCoordinatesPerInch float64) func(*glfw.Window, int, int) {
172+
return func(window *glfw.Window, widthPx int, heightPx int) {
173+
index := *(*int)(window.GetUserPointer())
174+
flutterOGL := flutter.SelectEngine(index)
175+
176+
if pixelRatio == 0 {
177+
width, _ := window.GetSize()
178+
windowPixelsPerScreenCoordinate := float64(widthPx) / float64(width)
179+
dpi := windowPixelsPerScreenCoordinate * monitorScreenCoordinatesPerInch
180+
pixelRatio = dpi / dpPerInch
181+
182+
// Limit the ratio to 1 to avoid rendering a smaller UI in standard resolution monitors.
183+
if pixelRatio < 1.0 {
184+
fmt.Println("calculated pixelRatio limited to a minimum of 1.0")
185+
pixelRatio = 1.0
186+
}
187+
}
171188

172-
event := flutter.WindowMetricsEvent{
173-
Width: width,
174-
Height: height,
175-
PixelRatio: flutterOGL.PixelRatio,
189+
event := flutter.WindowMetricsEvent{
190+
Width: widthPx,
191+
Height: heightPx,
192+
PixelRatio: pixelRatio,
193+
}
194+
flutterOGL.EngineSendWindowMetricsEvent(event)
176195
}
177-
flutterOGL.EngineSendWindowMetricsEvent(event)
178196
}
179197

180198
func glfwCharCallback(w *glfw.Window, char rune) {
@@ -211,7 +229,6 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
211229
flutterOGL.FMakeResourceCurrent = func(v unsafe.Pointer) bool {
212230
return false
213231
}
214-
flutterOGL.PixelRatio = c.PixelRatio
215232

216233
// PlatformMessage
217234
flutterOGL.FPlatfromMessage = func(platMessage *flutter.PlatformMessage, window unsafe.Pointer) bool {
@@ -242,8 +259,9 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
242259
panic("Couldn't launch the FlutterEngine")
243260
}
244261

262+
glfwFramebufferSizeCallback := newGLFWFramebufferSizeCallback(c.PixelRatio, getScreenCoordinatesPerInch())
245263
width, height := window.GetFramebufferSize()
246-
glfwWindowSizeCallback(window, width, height)
264+
glfwFramebufferSizeCallback(window, width, height)
247265
var glfwKeyCallback func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey)
248266

249267
if c.KeyboardLayout != nil {
@@ -253,12 +271,32 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
253271
}
254272

255273
window.SetKeyCallback(glfwKeyCallback)
256-
window.SetFramebufferSizeCallback(glfwWindowSizeCallback)
274+
window.SetFramebufferSizeCallback(glfwFramebufferSizeCallback)
257275
window.SetMouseButtonCallback(glfwMouseButtonCallback)
258276
window.SetCharCallback(glfwCharCallback)
259277
return flutterOGL
260278
}
261279

280+
// getScreenCoordinatesPerInch returns the number of screen coordinates per
281+
// inch for the main monitor. If the information is unavailable it returns
282+
// a default value that assumes that a screen coordinate is one dp.
283+
func getScreenCoordinatesPerInch() float64 {
284+
// TODO: multi-monitor support (#74)
285+
primaryMonitor := glfw.GetPrimaryMonitor()
286+
if primaryMonitor == nil {
287+
return dpPerInch
288+
}
289+
primaryMonitorMode := primaryMonitor.GetVideoMode()
290+
if primaryMonitorMode == nil {
291+
return dpPerInch
292+
}
293+
primaryMonitorWidthMM, _ := primaryMonitor.GetPhysicalSize()
294+
if primaryMonitorWidthMM == 0 {
295+
return dpPerInch
296+
}
297+
return float64(primaryMonitorMode.Width) / (float64(primaryMonitorWidthMM) / 25.4)
298+
}
299+
262300
// Update the TextInput with the current state
263301
func updateEditingState(window *glfw.Window) {
264302

option.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func OptionWindowInitializer(ini func(*glfw.Window) error) Option {
6666
}
6767
}
6868

69-
// OptionPixelRatio specify the scale factor for the physical screen.
69+
// OptionPixelRatio forces the the scale factor for the screen.
70+
// By default, go-flutter will calculate the correct pixel ratio for the user based
71+
// on their DPI. Setting this option is not adviced.
7072
func OptionPixelRatio(ratio float64) Option {
7173
return func(c *config) {
7274
c.PixelRatio = ratio

0 commit comments

Comments
 (0)