@@ -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 ) {
@@ -76,18 +80,28 @@ func glfwCursorPositionCallbackAtPhase(
76
80
77
81
func glfwMouseButtonCallback (window * glfw.Window , key glfw.MouseButton , action glfw.Action , mods glfw.ModifierKey ) {
78
82
79
- if key == glfw .MouseButton1 && action == glfw . Press {
83
+ if key == glfw .MouseButton1 {
80
84
x , y := window .GetCursorPos ()
81
- glfwCursorPositionCallbackAtPhase (window , flutter .KDown , x , y )
82
- window .SetCursorPosCallback (func (window * glfw.Window , x float64 , y float64 ) {
83
- glfwCursorPositionCallbackAtPhase (window , flutter .KMove , x , y )
84
- })
85
- }
86
85
87
- if key == glfw .MouseButton1 && action == glfw .Release {
88
- x , y := window .GetCursorPos ()
89
- glfwCursorPositionCallbackAtPhase (window , flutter .KUp , x , y )
90
- window .SetCursorPosCallback (nil )
86
+ // recalculate x and y from screen cordinates to pixels
87
+ widthPx , _ := window .GetFramebufferSize ()
88
+ width , _ := window .GetSize ()
89
+ pixelsPerScreenCoordinate := float64 (widthPx ) / float64 (width )
90
+ x = x * pixelsPerScreenCoordinate
91
+ y = y * pixelsPerScreenCoordinate
92
+
93
+ if action == glfw .Press {
94
+ glfwCursorPositionCallbackAtPhase (window , flutter .KDown , x , y )
95
+ window .SetCursorPosCallback (func (window * glfw.Window , x float64 , y float64 ) {
96
+ glfwCursorPositionCallbackAtPhase (window , flutter .KMove , x , y )
97
+ })
98
+ }
99
+
100
+ if action == glfw .Release {
101
+ x , y := window .GetCursorPos ()
102
+ glfwCursorPositionCallbackAtPhase (window , flutter .KUp , x , y )
103
+ window .SetCursorPosCallback (nil )
104
+ }
91
105
}
92
106
93
107
}
@@ -164,17 +178,31 @@ func glfwKey(keyboardLayout KeyboardShortcuts) func(w *glfw.Window, key glfw.Key
164
178
}
165
179
}
166
180
167
- func glfwWindowSizeCallback (window * glfw.Window , width int , height int ) {
168
-
169
- index := * (* int )(window .GetUserPointer ())
170
- flutterOGL := flutter .SelectEngine (index )
181
+ func newGLFWFramebufferSizeCallback (pixelRatio float64 , monitorScreenCoordinatesPerInch float64 ) func (* glfw.Window , int , int ) {
182
+ return func (window * glfw.Window , widthPx int , heightPx int ) {
183
+ index := * (* int )(window .GetUserPointer ())
184
+ flutterOGL := flutter .SelectEngine (index )
185
+
186
+ if pixelRatio == 0 {
187
+ width , _ := window .GetSize ()
188
+ pixelsPerScreenCoordinate := float64 (widthPx ) / float64 (width )
189
+ dpi := pixelsPerScreenCoordinate * monitorScreenCoordinatesPerInch
190
+ pixelRatio = dpi / dpPerInch
191
+
192
+ // Limit the ratio to 1 to avoid rendering a smaller UI in standard resolution monitors.
193
+ if pixelRatio < 1.0 {
194
+ fmt .Println ("calculated pixelRatio limited to a minimum of 1.0" )
195
+ pixelRatio = 1.0
196
+ }
197
+ }
171
198
172
- event := flutter.WindowMetricsEvent {
173
- Width : width ,
174
- Height : height ,
175
- PixelRatio : flutterOGL .PixelRatio ,
199
+ event := flutter.WindowMetricsEvent {
200
+ Width : widthPx ,
201
+ Height : heightPx ,
202
+ PixelRatio : pixelRatio ,
203
+ }
204
+ flutterOGL .EngineSendWindowMetricsEvent (event )
176
205
}
177
- flutterOGL .EngineSendWindowMetricsEvent (event )
178
206
}
179
207
180
208
func glfwCharCallback (w * glfw.Window , char rune ) {
@@ -211,7 +239,6 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
211
239
flutterOGL .FMakeResourceCurrent = func (v unsafe.Pointer ) bool {
212
240
return false
213
241
}
214
- flutterOGL .PixelRatio = c .PixelRatio
215
242
216
243
// PlatformMessage
217
244
flutterOGL .FPlatfromMessage = func (platMessage * flutter.PlatformMessage , window unsafe.Pointer ) bool {
@@ -242,8 +269,9 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
242
269
panic ("Couldn't launch the FlutterEngine" )
243
270
}
244
271
272
+ glfwFramebufferSizeCallback := newGLFWFramebufferSizeCallback (c .PixelRatio , getScreenCoordinatesPerInch ())
245
273
width , height := window .GetFramebufferSize ()
246
- glfwWindowSizeCallback (window , width , height )
274
+ glfwFramebufferSizeCallback (window , width , height )
247
275
var glfwKeyCallback func (w * glfw.Window , key glfw.Key , scancode int , action glfw.Action , mods glfw.ModifierKey )
248
276
249
277
if c .KeyboardLayout != nil {
@@ -253,12 +281,32 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
253
281
}
254
282
255
283
window .SetKeyCallback (glfwKeyCallback )
256
- window .SetFramebufferSizeCallback (glfwWindowSizeCallback )
284
+ window .SetFramebufferSizeCallback (glfwFramebufferSizeCallback )
257
285
window .SetMouseButtonCallback (glfwMouseButtonCallback )
258
286
window .SetCharCallback (glfwCharCallback )
259
287
return flutterOGL
260
288
}
261
289
290
+ // getScreenCoordinatesPerInch returns the number of screen coordinates per
291
+ // inch for the main monitor. If the information is unavailable it returns
292
+ // a default value that assumes that a screen coordinate is one dp.
293
+ func getScreenCoordinatesPerInch () float64 {
294
+ // TODO: multi-monitor support (#74)
295
+ primaryMonitor := glfw .GetPrimaryMonitor ()
296
+ if primaryMonitor == nil {
297
+ return dpPerInch
298
+ }
299
+ primaryMonitorMode := primaryMonitor .GetVideoMode ()
300
+ if primaryMonitorMode == nil {
301
+ return dpPerInch
302
+ }
303
+ primaryMonitorWidthMM , _ := primaryMonitor .GetPhysicalSize ()
304
+ if primaryMonitorWidthMM == 0 {
305
+ return dpPerInch
306
+ }
307
+ return float64 (primaryMonitorMode .Width ) / (float64 (primaryMonitorWidthMM ) / 25.4 )
308
+ }
309
+
262
310
// Update the TextInput with the current state
263
311
func updateEditingState (window * glfw.Window ) {
264
312
0 commit comments