@@ -2,6 +2,7 @@ package gutter
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
5
6
"log"
6
7
"time"
7
8
"unsafe"
@@ -10,6 +11,9 @@ import (
10
11
"github.com/go-gl/glfw/v3.2/glfw"
11
12
)
12
13
14
+ // dpPerInch defines the amount of display pixels per inch as defined for Flutter.
15
+ const dpPerInch = 160.0
16
+
13
17
// Run executes a flutter application with the provided options.
14
18
// given limitations this method must be called by the main function directly.
15
19
func Run (options ... Option ) (err error ) {
@@ -164,17 +168,31 @@ func glfwKey(keyboardLayout KeyboardShortcuts) func(w *glfw.Window, key glfw.Key
164
168
}
165
169
}
166
170
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
+ }
171
188
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 )
176
195
}
177
- flutterOGL .EngineSendWindowMetricsEvent (event )
178
196
}
179
197
180
198
func glfwCharCallback (w * glfw.Window , char rune ) {
@@ -211,7 +229,6 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
211
229
flutterOGL .FMakeResourceCurrent = func (v unsafe.Pointer ) bool {
212
230
return false
213
231
}
214
- flutterOGL .PixelRatio = c .PixelRatio
215
232
216
233
// PlatformMessage
217
234
flutterOGL .FPlatfromMessage = func (platMessage * flutter.PlatformMessage , window unsafe.Pointer ) bool {
@@ -242,8 +259,9 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
242
259
panic ("Couldn't launch the FlutterEngine" )
243
260
}
244
261
262
+ glfwFramebufferSizeCallback := newGLFWFramebufferSizeCallback (c .PixelRatio , getScreenCoordinatesPerInch ())
245
263
width , height := window .GetFramebufferSize ()
246
- glfwWindowSizeCallback (window , width , height )
264
+ glfwFramebufferSizeCallback (window , width , height )
247
265
var glfwKeyCallback func (w * glfw.Window , key glfw.Key , scancode int , action glfw.Action , mods glfw.ModifierKey )
248
266
249
267
if c .KeyboardLayout != nil {
@@ -253,12 +271,32 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
253
271
}
254
272
255
273
window .SetKeyCallback (glfwKeyCallback )
256
- window .SetFramebufferSizeCallback (glfwWindowSizeCallback )
274
+ window .SetFramebufferSizeCallback (glfwFramebufferSizeCallback )
257
275
window .SetMouseButtonCallback (glfwMouseButtonCallback )
258
276
window .SetCharCallback (glfwCharCallback )
259
277
return flutterOGL
260
278
}
261
279
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
+
262
300
// Update the TextInput with the current state
263
301
func updateEditingState (window * glfw.Window ) {
264
302
0 commit comments