Skip to content

Commit 3178ec3

Browse files
committed
fixes pixel ration on macos
1 parent b27cd2e commit 3178ec3

File tree

2 files changed

+5
-67
lines changed

2 files changed

+5
-67
lines changed

application.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ func (a *Application) Run() error {
320320
})
321321
})
322322
})
323+
a.window.SetContentScaleCallback(func(window *glfw.Window, x float32, y float32) {
324+
windowManager.glfwRefreshCallback(window)
325+
})
323326

324327
// Attach glfw window callbacks for text input
325328
a.window.SetKeyCallback(

glfw.go

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package flutter
22

33
import (
4-
"encoding/json"
54
"fmt"
6-
"sync"
75
"unsafe"
86

97
"github.com/go-flutter-desktop/go-flutter/embedder"
@@ -21,9 +19,6 @@ type windowManager struct {
2119
// forcedPixelRatio forces the pixelRatio to given value, when value is not zero.
2220
forcedPixelRatio float64
2321

24-
// sync.Once to limit pixelRatio warning messages.
25-
oncePrintPixelRatioLimit sync.Once
26-
2722
// current pointer state
2823
pointerPhase embedder.PointerPhase
2924
pointerButton embedder.PointerButtonMouse
@@ -207,70 +202,10 @@ func (m *windowManager) glfwRefreshCallback(window *glfw.Window) {
207202
if m.forcedPixelRatio != 0 {
208203
pixelRatio = m.forcedPixelRatio
209204
} else {
210-
var selectedMonitor *glfw.Monitor
211-
winX, winY := window.GetPos()
212-
winCenterX, winCenterY := winX+widthPx/2, winY+heightPx/2
213-
214-
monitors := glfw.GetMonitors()
215-
for _, monitor := range monitors {
216-
monX1, monY1 := monitor.GetPos()
217-
monMode := monitor.GetVideoMode()
218-
if monMode == nil {
219-
continue
220-
}
221-
monX2, monY2 := monX1+monMode.Width, monY1+monMode.Height
222-
if (monX1 <= winCenterX && winCenterX <= monX2) &&
223-
(monY1 <= winCenterY && winCenterY <= monY2) {
224-
selectedMonitor = monitor
225-
break
226-
}
227-
}
228-
229-
if selectedMonitor == nil {
230-
// when no monitor was selected, try fallback to primary monitor
231-
// TODO: ? perhaps select monitor that is "closest" to the window ?
232-
selectedMonitor = glfw.GetPrimaryMonitor()
233-
}
234-
if selectedMonitor == nil {
235-
pixelRatio = 1.0
236-
goto SendWindowMetricsEvent
237-
}
238-
selectedMonitorMode := selectedMonitor.GetVideoMode()
239-
if selectedMonitorMode == nil {
240-
pixelRatio = 1.0
241-
goto SendWindowMetricsEvent
242-
}
243-
selectedMonitorWidthMM, _ := selectedMonitor.GetPhysicalSize()
244-
if selectedMonitorWidthMM == 0 {
245-
pixelRatio = 1.0
246-
goto SendWindowMetricsEvent
247-
}
248-
monitorScreenCoordinatesPerInch := float64(selectedMonitorMode.Width) / (float64(selectedMonitorWidthMM) / 25.4)
249-
250-
dpi := m.pixelsPerScreenCoordinate * monitorScreenCoordinatesPerInch
251-
pixelRatio = dpi / dpPerInch
252-
253-
// Limit the ratio to 1 to avoid rendering a smaller UI in standard resolution monitors.
254-
if pixelRatio < 1.0 {
255-
m.oncePrintPixelRatioLimit.Do(func() {
256-
metrics := map[string]interface{}{
257-
"ppsc": m.pixelsPerScreenCoordinate,
258-
"windowWidthPx": widthPx,
259-
"windowWidthSc": width,
260-
"mscpi": monitorScreenCoordinatesPerInch,
261-
"dpi": dpi,
262-
"pixelRatio": pixelRatio,
263-
"monitorWidthMm": selectedMonitorWidthMM,
264-
"monitorWidthSc": selectedMonitorMode.Width,
265-
}
266-
metricsBytes, _ := json.Marshal(metrics)
267-
fmt.Println("go-flutter: calculated pixelRatio limited to a minimum of 1.0. metrics: " + string(metricsBytes))
268-
})
269-
pixelRatio = 1.0
270-
}
205+
xscale, _ := window.GetContentScale()
206+
pixelRatio = float64(xscale)
271207
}
272208

273-
SendWindowMetricsEvent:
274209
event := embedder.WindowMetricsEvent{
275210
Width: widthPx,
276211
Height: heightPx,

0 commit comments

Comments
 (0)